Esempio n. 1
0
File: cmd.go Progetto: timtadh/sfp
func CPUProfile(cpuProfile string) func() {
	errors.Logf("DEBUG", "starting cpu profile: %v", cpuProfile)
	f, err := os.Create(cpuProfile)
	if err != nil {
		log.Fatal(err)
	}
	err = pprof.StartCPUProfile(f)
	if err != nil {
		log.Fatal(err)
	}
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		sig := <-sigs
		errors.Logf("DEBUG", "closing cpu profile")
		pprof.StopCPUProfile()
		err := f.Close()
		errors.Logf("DEBUG", "closed cpu profile, err: %v", err)
		panic(errors.Errorf("caught signal: %v", sig))
	}()
	return func() {
		errors.Logf("DEBUG", "closing cpu profile")
		pprof.StopCPUProfile()
		err := f.Close()
		errors.Logf("DEBUG", "closed cpu profile, err: %v", err)
	}
}
Esempio n. 2
0
File: mine.go Progetto: timtadh/sfp
func (w *Walker) probabilities(lat *lattice.Lattice) ([]int, error) {
	P := make([]int, len(lat.V))
	for i, node := range lat.V {
		count, err := node.ChildCount()
		if err != nil {
			return nil, err
		}
		if count < len(lat.Children(i)) {
			errors.Logf("WARNING", "count < lat.Children count %v < %v", count, len(lat.Children(i)))
			count = len(lat.Children(i))
		}
		if count > 0 && len(lat.Children(i)) == 0 {
			errors.Logf("WARNING", "count > 0 && lat.Children == 0 : %v > 0 lat.Children == %v", count, len(lat.Children(i)))
		}
		if i+1 == len(lat.V) {
			P[i] = -1
		} else if count == 0 {
			P[i] = 1
			errors.Logf("WARNING", "0 count for %v, using %v", node, P[i])
		} else {
			P[i] = count
		}
	}
	return P, nil
}
Esempio n. 3
0
File: mine.go Progetto: timtadh/sfp
func (w *Walker) Mine(dt lattice.DataType, rptr miners.Reporter, fmtr lattice.Formatter) error {
	errors.Logf("INFO", "Customize creation")

	pConf := w.Config.Copy()
	pConf.Samples = 1000
	pConf.Unique = false
	premine := walker.NewWalker(pConf, ospace.MakeUniformWalk(.02, false))
	premine.Reject = false
	collector := &reporters.Collector{make([]lattice.Node, 0, 10)}
	uniq, err := reporters.NewUnique(pConf, fmtr, collector, "")
	if err != nil {
		return err
	}
	pRptr := &reporters.Skip{
		Skip:     10,
		Reporter: &reporters.Chain{[]miners.Reporter{reporters.NewLog(fmtr, false, "DEBUG", "premining"), uniq}},
	}

	err = premine.Mine(dt, pRptr, fmtr)
	if err != nil {
		return err
	}

	w.Teleports = collector.Nodes
	errors.Logf("INFO", "teleports %v", len(w.Teleports))

	return (w.Walker).Mine(dt, rptr, fmtr)
}
Esempio n. 4
0
File: file.go Progetto: timtadh/sfp
func (r *File) Report(n lattice.Node) error {
	err := r.fmtr.FormatPattern(r.patterns, n)
	if err != nil {
		return err
	}
	err = r.fmtr.FormatEmbeddings(r.embeddings, n)
	if err != nil {
		return err
	}
	_, err = fmt.Fprintf(r.names, "%v\n", r.fmtr.PatternName(n))
	if err != nil {
		return err
	}
	if r.prfmtr != nil {
		matrices, err := r.prfmtr.Matrices(n)
		if err == nil {
			r.prfmtr.FormatMatrices(r.matrices, r.fmtr, n, matrices)
		}
		if err != nil {
			fmt.Fprintf(r.matrices, "ERR: %v\n", err)
			errors.Logf("ERROR", "Pr Matrices Computation Error: vs", err)
		} else if r.prfmtr.CanComputeSelPr(n, matrices) {
			pr, err := r.prfmtr.SelectionProbability(n, matrices)
			if err != nil {
				fmt.Fprintf(r.prs, "ERR: %v\n", err)
				errors.Logf("ERROR", "PrComputation Error: %v", err)
			} else {
				fmt.Fprintf(r.prs, "%g, %v\n", pr, r.fmtr.PatternName(n))
			}
		} else {
			fmt.Fprintf(r.prs, "SKIPPED\n")
		}
	}
	return nil
}
Esempio n. 5
0
File: cmd.go Progetto: timtadh/sfp
func countReporter(rptrs map[string]Reporter, argv []string, fmtr lattice.Formatter, conf *config.Config) (miners.Reporter, []string) {
	args, optargs, err := getopt.GetOpt(
		argv,
		"hf:",
		[]string{
			"help",
			"filename=",
		},
	)
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		Usage(ErrorCodes["opts"])
	}
	filename := "count"
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		case "-f", "--filename":
			filename = oa.Arg()
		default:
			errors.Logf("ERROR", "Unknown flag '%v'\n", oa.Opt())
			Usage(ErrorCodes["opts"])
		}
	}
	r, err := reporters.NewCount(conf, filename)
	if err != nil {
		errors.Logf("ERROR", "There was error creating output files\n")
		errors.Logf("ERROR", "%v", err)
		os.Exit(1)
	}
	return r, args
}
Esempio n. 6
0
func (w *Walker) RejectingWalk(samples chan lattice.Node, terminate chan bool) chan lattice.Node {
	accepted := make(chan lattice.Node)
	go func() {
		i := 0
		seen := set.NewSortedSet(w.Config.Samples)
		for sampled := range samples {
			accept := false
			if !w.Reject || w.Dt.Acceptable(sampled) {
				label := types.ByteSlice(sampled.Pattern().Label())
				if !w.Config.Unique || !seen.Has(label) {
					if w.Config.Unique {
						seen.Add(label)
					}
					accept = true
					i++
				} else {
					errors.Logf("DEBUG", "duplicate %v", sampled)
				}
			} else {
				errors.Logf("DEBUG", "rejected %v", sampled)
			}
			if i >= w.Config.Samples {
				terminate <- true
			} else {
				terminate <- false
			}
			if accept {
				accepted <- sampled
			}
		}
		close(accepted)
		close(terminate)
	}()
	return accepted
}
Esempio n. 7
0
func (l *IntLoader) items(input lattice.Input) func(do func(tx, item int32) error) error {
	return func(do func(tx, item int32) error) error {
		in, closer := input()
		defer closer()
		scanner := bufio.NewScanner(in)
		tx := int32(0)
		for scanner.Scan() {
			if tx%1000 == 0 {
				errors.Logf("INFO", "line %d", tx)
			}
			line := scanner.Text()
			for _, col := range strings.Split(line, " ") {
				if col == "" {
					continue
				}
				item, err := strconv.Atoi(col)
				if err != nil {
					errors.Logf("WARN", "input line %d contained non int '%s'", tx, col)
					continue
				}
				err = do(tx, int32(item))
				if err != nil {
					errors.Logf("ERROR", "%v", err)
					return err
				}
			}
			tx += 1
		}
		if err := scanner.Err(); err != nil {
			return err
		}
		return nil
	}
}
Esempio n. 8
0
func (w *Walker) Mine(dt lattice.DataType, rptr miners.Reporter, fmtr lattice.Formatter) error {
	err := w.Init(dt, rptr)
	if err != nil {
		return err
	}
	errors.Logf("INFO", "finished initialization, starting walk")
	samples, terminate, errs := w.Walk(w)
	samples = w.RejectingWalk(samples, terminate)
loop:
	for {
		select {
		case sampled, open := <-samples:
			if sampled != nil {
				if err := w.Rptr.Report(sampled); err != nil {
					return err
				}
			}
			if !open {
				break loop
			}
		case err := <-errs:
			if err != nil {
				return err
			}
		}
	}
	errors.Logf("INFO", "exiting walker Mine")
	return nil
}
Esempio n. 9
0
File: cmd.go Progetto: timtadh/sfp
func fileReporter(rptrs map[string]Reporter, argv []string, fmtr lattice.Formatter, conf *config.Config) (miners.Reporter, []string) {
	args, optargs, err := getopt.GetOpt(
		argv,
		"hp:e:n:",
		[]string{
			"help",
			"patterns=",
			"embeddings=",
			"names=",
			"matrices=",
			"probabilities=",
			"show-pr",
		},
	)
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		Usage(ErrorCodes["opts"])
	}
	patterns := "patterns"
	embeddings := "embeddings"
	names := "names.txt"
	matrices := "matrices.json"
	probabilities := "probabilities.prs"
	showPr := false
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		case "-p", "--patterns":
			patterns = oa.Arg()
		case "-e", "--embeddings":
			embeddings = oa.Arg()
		case "-n", "--names":
			names = oa.Arg()
		case "--matrices":
			matrices = oa.Arg()
		case "--probabilites":
			probabilities = oa.Arg()
		case "--show-pr":
			showPr = true
		default:
			errors.Logf("ERROR", "Unknown flag '%v'\n", oa.Opt())
			Usage(ErrorCodes["opts"])
		}
	}
	fr, err := reporters.NewFile(conf, fmtr, showPr, patterns, embeddings, names, matrices, probabilities)
	if err != nil {
		errors.Logf("ERROR", "There was error creating output files\n")
		errors.Logf("ERROR", "%v\n", err)
		os.Exit(1)
	}
	return fr, args
}
Esempio n. 10
0
File: cmd.go Progetto: timtadh/sfp
func dbscanReporter(rptrs map[string]Reporter, argv []string, fmtr lattice.Formatter, conf *config.Config) (miners.Reporter, []string) {
	args, optargs, err := getopt.GetOpt(
		argv,
		"hf:e:g:a:",
		[]string{
			"help",
			"filename=",
			"epsilon=",
			"gamma=",
			"attr=",
		},
	)
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		Usage(ErrorCodes["opts"])
	}
	clusters := "clusters"
	metrics := "metrics"
	attr := ""
	epsilon := 0.2
	gamma := 0.2
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		case "-c", "--clusters-name":
			clusters = oa.Arg()
		case "-m", "--metrics-name":
			metrics = oa.Arg()
		case "-a", "--attr":
			attr = oa.Arg()
		case "-e", "--epsilon":
			epsilon = ParseFloat(oa.Arg())
		case "-g", "--gamma":
			gamma = ParseFloat(oa.Arg())
		default:
			errors.Logf("ERROR", "Unknown flag '%v'\n", oa.Opt())
			Usage(ErrorCodes["opts"])
		}
	}
	if attr == "" {
		errors.Logf("ERROR", "You must supply --attr=<attr> to dbscan")
		Usage(ErrorCodes["opts"])
	}
	r, err := reporters.NewDbScan(conf, fmtr, clusters, metrics, attr, epsilon, gamma)
	if err != nil {
		errors.Logf("ERROR", "There was error creating output files\n")
		errors.Logf("ERROR", "%v", err)
		os.Exit(1)
	}
	return r, args
}
Esempio n. 11
0
File: mine.go Progetto: timtadh/sfp
func (m *Miner) Mine(dt lattice.DataType, rptr miners.Reporter, fmtr lattice.Formatter) error {
	err := m.Init(dt, rptr)
	if err != nil {
		return err
	}
	errors.Logf("INFO", "finished initialization, starting walk")
	err = m.mine()
	if err != nil {
		return err
	}
	errors.Logf("INFO", "exiting Mine")
	return nil
}
Esempio n. 12
0
File: mine.go Progetto: timtadh/sfp
func (w *Walker) walkFrom(v lattice.Node) (path []lattice.Node, err error) {
	weight := func(_, a lattice.Node) (float64, error) {
		// kids, err := a.CanonKids()
		// if err != nil {
		// 	return 0, err
		// }
		// return float64(len(kids)), nil
		return 1, nil
	}
	transition := func(c lattice.Node) (lattice.Node, error) {
		errors.Logf("EST-WALK-DEBUG", "walk cur %v", c)
		kids, err := c.CanonKids()
		if err != nil {
			return nil, err
		}
		// errors.Logf("EST-WALK-DEBUG", "cur %v len(kids) %v", c, len(kids))
		_, next, err := walker.Transition(c, kids, weight, false)
		return next, err
	}
	c := v
	n, err := transition(c)
	if err != nil {
		return nil, err
	}
	path = append(path, c)
	for n != nil {
		c = n
		path = append(path, c)
		n, err = transition(c)
		if err != nil {
			return nil, err
		}
	}
	return path, nil
}
Esempio n. 13
0
File: mine.go Progetto: timtadh/sfp
func Next(ctx interface{}, cur lattice.Node) (lattice.Node, error) {
	w := ctx.(*Walker)
	if ismax, err := cur.Maximal(); err != nil {
		return nil, err
	} else if ismax && w.Dt.Acceptable(cur) {
		w.teleportAllowed = true
		errors.Logf("INFO", "ALLOWING TELEPORTS")
	}
	if w.teleportAllowed && rand.Float64() < w.TeleportProbability {
		w.teleportAllowed = false
		next := w.Teleports[rand.Intn(len(w.Teleports))]
		errors.Logf("INFO", "TELEPORT\n    from %v\n      to %v", cur, next)
		return next, nil
	}
	return musk.Next(ctx, cur)
}
Esempio n. 14
0
func extendNode(dt *Digraph, n Node, debug bool) (*hashtable.LinearHash, error) {
	if debug {
		errors.Logf("DEBUG", "n.SubGraph %v", n.SubGraph())
	}
	sg := n.SubGraph()
	b := subgraph.Build(len(sg.V), len(sg.E)).From(sg)
	extPoints, err := n.Extensions()
	if err != nil {
		return nil, err
	}
	patterns := hashtable.NewLinearHash()
	for _, ep := range extPoints {
		bc := b.Copy()
		bc.Extend(ep)
		if len(bc.V) > dt.MaxVertices {
			continue
		}
		vord, eord := bc.CanonicalPermutation()
		ext := bc.BuildFromPermutation(vord, eord)
		if !patterns.Has(ext) {
			patterns.Put(ext, &extInfo{ep, vord})
		}
	}

	return patterns, nil
}
Esempio n. 15
0
func (l *IntLoader) indices(items itemsIter) (idx, inv index, err error) {
	max_tx, max_item, err := l.max(items)
	if err != nil {
		return nil, nil, err
	}
	counts := make([]int, max_item+1)
	err = items(func(tx, item int32) error {
		counts[item]++
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	errors.Logf("DEBUG", "max tx : %v, max item : %v", max_tx, max_item)
	idx = make(index, max_tx+1)
	inv = make(index, max_item+1)
	err = items(func(tx, item int32) error {
		if counts[item] > l.sets.Support() {
			idx = idx.add(tx, item)
			inv = inv.add(item, tx)
		}
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	return idx, inv, nil
}
Esempio n. 16
0
File: cmd.go Progetto: timtadh/sfp
func uniqueReporter(reports map[string]Reporter, argv []string, fmtr lattice.Formatter, conf *config.Config) (miners.Reporter, []string) {
	args, optargs, err := getopt.GetOpt(
		argv,
		"h",
		[]string{
			"help",
			"histogram=",
		},
	)
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		Usage(ErrorCodes["opts"])
	}
	histogram := ""
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		case "--histogram":
			histogram = oa.Arg()
		default:
			errors.Logf("ERROR", "Unknown flag '%v'", oa.Opt())
			Usage(ErrorCodes["opts"])
		}
	}
	var rptr miners.Reporter
	if len(args) == 0 {
		errors.Logf("ERROR", "You must supply an inner reporter to unique")
		fmt.Fprintln(os.Stderr, "try: unique file")
		Usage(ErrorCodes["opts"])
	} else if _, has := reports[args[0]]; !has {
		errors.Logf("ERROR", "Unknown reporter '%v'", args[0])
		fmt.Fprintln(os.Stderr, "Reporters:")
		for k := range reports {
			fmt.Fprintln(os.Stderr, "  ", k)
		}
		Usage(ErrorCodes["opts"])
	} else {
		rptr, args = reports[args[0]](reports, args[1:], fmtr, conf)
	}
	uniq, err := reporters.NewUnique(conf, fmtr, rptr, histogram)
	if err != nil {
		errors.Logf("ERROR", "Error creating unique reporter '%v'\n", err)
		Usage(ErrorCodes["opts"])
	}
	return uniq, args
}
Esempio n. 17
0
func NewIndices(b *Builder, minSupport int) *Indices {
	errors.Logf("DEBUG", "About to build indices %v %v", len(b.V), len(b.E))
	i := &Indices{
		ColorIndex:     make(map[int][]int, len(b.VertexColors)),
		SrcIndex:       make(map[IdColorColor][]int, len(b.V)),
		TargIndex:      make(map[IdColorColor][]int, len(b.V)),
		EdgeIndex:      make(map[Edge]*Edge, len(b.E)),
		EdgeCounts:     make(map[Colors]int, len(b.EdgeColors)),
		FreqEdges:      make([]Colors, 0, len(b.EdgeColors)),
		EdgesFromColor: make(map[int][]Colors, len(b.VertexColors)),
		EdgesToColor:   make(map[int][]Colors, len(b.VertexColors)),
		VertexColors:   b.VertexColors,
		EdgeColors:     b.EdgeColors,
	}
	i.G = b.Build(
		func(u *Vertex) {
			if b.VertexColors[u.Color] < minSupport {
				return
			}
			if i.ColorIndex[u.Color] == nil {
				i.ColorIndex[u.Color] = make([]int, 0, b.VertexColors[u.Color])
			}
			i.ColorIndex[u.Color] = append(i.ColorIndex[u.Color], u.Idx)
		},
		func(e *Edge) {
			edge := Edge{Src: e.Src, Targ: e.Targ, Color: e.Color}
			srcKey := IdColorColor{e.Src, e.Color, b.V[e.Targ].Color}
			targKey := IdColorColor{e.Targ, e.Color, b.V[e.Src].Color}
			colorKey := Colors{b.V[e.Src].Color, b.V[e.Targ].Color, e.Color}
			if i.SrcIndex[srcKey] == nil {
				i.SrcIndex[srcKey] = make([]int, 0, len(b.Adj[e.Src]))
			}
			if i.TargIndex[targKey] == nil {
				i.TargIndex[targKey] = make([]int, 0, len(b.Adj[e.Targ]))
			}
			i.EdgeIndex[edge] = e
			i.SrcIndex[srcKey] = append(i.SrcIndex[srcKey], e.Targ)
			i.TargIndex[targKey] = append(i.TargIndex[targKey], e.Src)
			i.EdgeCounts[colorKey] += 1
			// only add to frequent edges exactly when this colorKey has
			// surpassed min_support.
			if i.EdgeCounts[colorKey] == minSupport {
				if i.EdgesFromColor[e.Color] == nil {
					i.EdgesFromColor[e.Color] = make([]Colors, 0, 10)
				}
				if i.EdgesToColor[e.Color] == nil {
					i.EdgesToColor[e.Color] = make([]Colors, 0, 10)
				}
				i.FreqEdges = append(i.FreqEdges, colorKey)
				i.EdgesFromColor[colorKey.SrcColor] = append(
					i.EdgesFromColor[colorKey.SrcColor],
					colorKey)
				i.EdgesToColor[colorKey.TargColor] = append(
					i.EdgesToColor[colorKey.TargColor],
					colorKey)
			}
		})
	return i
}
Esempio n. 18
0
File: cmd.go Progetto: timtadh/sfp
func chainReporter(reports map[string]Reporter, argv []string, fmtr lattice.Formatter, conf *config.Config) (miners.Reporter, []string) {
	args, optargs, err := getopt.GetOpt(
		argv,
		"h",
		[]string{
			"help",
		},
	)
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		Usage(ErrorCodes["opts"])
	}
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		default:
			errors.Logf("ERROR", "Unknown flag '%v'", oa.Opt())
			Usage(ErrorCodes["opts"])
		}
	}
	rptrs := make([]miners.Reporter, 0, 10)
	for len(args) >= 1 {
		if args[0] == "endchain" {
			args = args[1:]
			break
		}
		if _, has := reports[args[0]]; !has {
			errors.Logf("ERROR", "Unknown reporter '%v'\n", args[0])
			fmt.Fprintln(os.Stderr, "Reporters:")
			for k := range reports {
				fmt.Fprintln(os.Stderr, "  ", k)
			}
			Usage(ErrorCodes["opts"])
		}
		var rptr miners.Reporter
		rptr, args = reports[args[0]](reports, args[1:], fmtr, conf)
		rptrs = append(rptrs, rptr)
	}
	if len(rptrs) == 0 {
		errors.Logf("ERROR", "Empty chain")
		fmt.Fprintln(os.Stderr, "try: chain log file")
		Usage(ErrorCodes["opts"])
	}
	return &reporters.Chain{rptrs}, args
}
Esempio n. 19
0
File: mine.go Progetto: timtadh/sfp
func NewWalker(conf *config.Config, teleportProbability float64) *Walker {
	errors.Logf("INFO", "teleport probability %v", teleportProbability)
	miner := &Walker{
		TeleportProbability: teleportProbability,
	}
	miner.Walker = *walker.NewWalker(conf, musk.MakeMaxUniformWalk(Next, miner))
	return miner
}
Esempio n. 20
0
File: cmd.go Progetto: timtadh/sfp
func Run(dt lattice.DataType, fmtr lattice.Formatter, mode miners.Miner, rptr miners.Reporter) int {
	errors.Logf("INFO", "loaded data, about to start mining")
	mineErr := mode.Mine(dt, rptr, fmtr)

	code := 0
	if e := mode.Close(); e != nil {
		errors.Logf("ERROR", "error closing %v", e)
		code++
	}
	if mineErr != nil {
		fmt.Fprintf(os.Stderr, "There was error during the mining process\n")
		fmt.Fprintf(os.Stderr, "%v\n", mineErr)
		code++
	} else {
		errors.Logf("INFO", "Done!")
	}
	return code
}
Esempio n. 21
0
func cachedAdj(n Node, dt *Digraph, count bytes_int.MultiMap, cache bytes_bytes.MultiMap) (nodes []lattice.Node, has bool, err error) {
	if dt.Mode&Caching == 0 {
		return nil, false, nil
	}
	dt.lock.RLock()
	defer dt.lock.RUnlock()
	key := n.Label()
	if has, err := count.Has(key); err != nil {
		return nil, false, err
	} else if !has {
		return nil, false, nil
	}
	// errors.Logf("DEBUG", "loading %v", n)
	err = cache.DoFind(key, func(_, adj []byte) (err error) {
		// WHY DO WE NEED TO UNLOCK?
		// We will aquire this lock in the course of LoadEmbList in READ MODE
		// This is fine to re-aquire in READ
		// However, if another thread tries to aquire in WRITE before we re-aquire
		// There will be a waiting WRITE when we try to aquire READ
		// A waiting WRITE will prevent all READ aquisitions
		// DEADLOCK.
		dt.lock.RUnlock()
		defer dt.lock.RLock()
		node, err := LoadEmbListNode(dt, adj)
		if err != nil {
			return err
		}
		if node == nil {
			errors.Logf("ERROR", "node is nil")
			panic("node was nil")
		}
		nodes = append(nodes, node)
		return nil
	})
	if err != nil {
		return nil, false, err
	}
	if false {
		pat, _ := LoadSubgraphPattern(dt, key)
		errors.Logf("LOAD-DEBUG", "Loaded Cached Adj %v adj %v", pat.Pat, len(nodes))
	}
	return nodes, true, nil
}
Esempio n. 22
0
func randomGraph(t testing.TB, V, E int, vlabels, elabels []string) (*Digraph, *goiso.Graph, *goiso.SubGraph, *subgraph.SubGraph, *EmbListNode) {
	Graph := goiso.NewGraph(10, 10)
	G := &Graph

	vidxs := make([]int, 0, V)
	vertices := make([]*goiso.Vertex, 0, V)
	for i := 0; i < V; i++ {
		v := G.AddVertex(i, vlabels[rand.Intn(len(vlabels))])
		vertices = append(vertices, v)
		vidxs = append(vidxs, v.Idx)
	}
	for i := 0; i < E; i++ {
		src := rand.Intn(len(vertices))
		targ := rand.Intn(len(vertices))
		elabel := rand.Intn(len(elabels))
		if !G.HasEdge(vertices[src], vertices[targ], elabels[elabel]) {
			G.AddEdge(vertices[src], vertices[targ], elabels[elabel])
		}
	}

	sg, _ := G.SubGraph(vidxs, nil)

	// make config
	conf := &config.Config{
		Support:     2,
		Parallelism: -1,
	}

	// make the *Digraph
	dt, err := NewDigraph(conf, &Config{
		MinEdges:            0,
		MaxEdges:            len(G.E),
		MinVertices:         0,
		MaxVertices:         len(G.V),
		Mode:                Automorphs | Caching | ExtensionPruning | EmbeddingPruning | ExtFromFreqEdges,
		EmbSearchStartPoint: subgraph.RandomStart,
	})
	if err != nil {
		errors.Logf("ERROR", "%v", err)
		t.Fatal(err)
	}

	err = dt.Init(G)
	if err != nil {
		t.Fatal(err)
	}

	f, err := os.Create("/tmp/random-graph.dot")
	if err == nil {
		fmt.Fprintf(f, "%v", G)
		f.Close()
	}

	return dt, G, sg, subgraph.FromEmbedding(sg), RootEmbListNode(dt)
}
Esempio n. 23
0
func (sg *SubGraph) Metric(o *SubGraph) float64 {
	labels := make(map[int]int, len(sg.V)+len(o.V))
	rlabels := make([]int, 0, len(sg.V)+len(o.V))
	addLabel := func(label int) {
		if _, has := labels[label]; !has {
			labels[label] = len(rlabels)
			rlabels = append(rlabels, label)
		}
	}
	for i := range sg.V {
		addLabel(sg.V[i].Color)
	}
	for i := range o.V {
		addLabel(o.V[i].Color)
	}
	for i := range sg.E {
		addLabel(sg.E[i].Color)
	}
	for i := range o.E {
		addLabel(o.E[i].Color)
	}
	W := sg.Walks(labels)
	err := W.Subtract(o.Walks(labels))
	if err != nil {
		log.Fatal(err)
	}
	W2, err := W.DenseMatrix().ElementMult(W)
	if err != nil {
		log.Fatal(err)
	}
	norm := W2.DenseMatrix().TwoNorm()
	size := float64(len(rlabels) * len(rlabels))
	mean := norm / size
	metric := math.Sqrt(mean)
	if false {
		errors.Logf("SIM", "sg    %v", sg)
		errors.Logf("SIM", "o     %v", o)
		errors.Logf("SIM", "score %v", metric)
		errors.Logf("SIM", "W2 \n%v", W2)
	}
	return metric
}
Esempio n. 24
0
File: mine.go Progetto: timtadh/sfp
func (w *Walker) Next(cur lattice.Node) (lattice.Node, error) {
	kids, err := cur.Children()
	if err != nil {
		return nil, err
	}
	if false {
		errors.Logf("DEBUG", "cur %v kids %v", cur, len(kids))
	}
	_, next, err := walker.Transition(cur, kids, w.weight, false)
	return next, err
}
Esempio n. 25
0
File: main.go Progetto: timtadh/sfp
func profileWriter(w io.Writer) {
	for {
		data := runtime.CPUProfile()
		if data == nil {
			break
		}
		errors.Logf("DEBUG", "profileWriter got data %v", len(data))
		w.Write(data)
	}
	profileDone <- true
}
Esempio n. 26
0
func (r *Unique) Close() error {
	if r.histogram != nil {
		err := bytes_int.Do(r.Seen.Iterate, func(k []byte, c int32) error {
			name := string(k)
			fmt.Fprintf(r.histogram, "%d, %.5g, %v\n", c, float64(c)/float64(r.count), name)
			return nil
		})
		if err != nil {
			errors.Logf("ERROR", "%v", err)
		}
		err = r.histogram.Close()
		if err != nil {
			errors.Logf("ERROR", "%v", err)
		}
	}
	err := r.Seen.Delete()
	if err != nil {
		errors.Logf("ERROR", "%v", err)
	}
	return r.Reporter.Close()
}
Esempio n. 27
0
func (lr *Log) Report(n lattice.Node) error {
	lr.count++
	prfmtr := lr.fmtr.PrFormatter()
	pr := -1.0
	if lr.prs && prfmtr != nil {
		matrices, err := prfmtr.Matrices(n)
		if err != nil {
			errors.Logf("ERROR", "Pr Matrices Computation Error: %v", err)
		} else if prfmtr.CanComputeSelPr(n, matrices) {
			pr, err = prfmtr.SelectionProbability(n, matrices)
			if err != nil {
				errors.Logf("ERROR", "PrComputation Error: %v", err)
			}
		}
	}
	if lr.prefix != "" && pr > -1.0 {
		errors.Logf(lr.level, "%s %v (pr = %5.3g) %v", lr.prefix, lr.count, pr, n)
	} else if lr.prefix != "" {
		errors.Logf(lr.level, "%s %v %v", lr.prefix, lr.count, n)
	} else if pr > -1.0 {
		errors.Logf(lr.level, "%v (pr = %5.3g) %v", lr.count, pr, n)
	} else {
		errors.Logf(lr.level, "%v %v", lr.count, n)
	}
	return nil
}
Esempio n. 28
0
func (i index) grow(size int32) index {
	newcap := len(i) + 1
	for newcap-1 <= int(size) {
		if len(i) < 10000 {
			newcap *= 2
		} else {
			newcap += 100
		}
		errors.Logf("DEBUG", "expanding. len(i) %v newcap %v size %v", len(i), newcap, size)
	}
	n := make(index, newcap)
	copy(n, i)
	return n
}
Esempio n. 29
0
File: mine.go Progetto: timtadh/sfp
func Next(ctx interface{}, cur lattice.Node) (lattice.Node, error) {
	kids, err := cur.Children()
	if err != nil {
		return nil, err
	}
	parents, err := cur.Parents()
	if err != nil {
		return nil, err
	}
	adjs := append(kids, parents...)
	errors.Logf("DEBUG", "cur %v parents %v kids %v adjs %v", cur, len(parents), len(kids), len(adjs))
	_, next, err := walker.Transition(cur, adjs, weight, false)
	return next, err
}
Esempio n. 30
0
func (b *Builder) Build(indexVertex func(*Vertex), indexEdge func(*Edge)) *Digraph {
	g := &Digraph{
		V:       make(Vertices, len(b.V)),
		E:       make(Edges, len(b.E)),
		Adj:     make([][]int, len(b.V)),
		Kids:    make([][]int, len(b.V)),
		Parents: make([][]int, len(b.V)),
	}
	for i := range b.V {
		g.V[i].Idx = b.V[i].Idx
		g.V[i].Color = b.V[i].Color
		g.Adj[i] = make([]int, len(b.Adj[i]))
		kids := 0
		parents := 0
		for j, e := range b.Adj[i] {
			g.Adj[i][j] = e
			if b.E[e].Src == i {
				kids++
			} else if b.E[e].Targ == i {
				parents++
			} else {
				panic("edge on neither source or target")
			}
		}
		g.Kids[i] = make([]int, 0, kids)
		g.Parents[i] = make([]int, 0, parents)
		for _, e := range b.Adj[i] {
			if b.E[e].Src == i {
				g.Kids[i] = append(g.Kids[i], e)
			} else if b.E[e].Targ == i {
				g.Parents[i] = append(g.Parents[i], e)
			} else {
				panic("edge on neither source or target")
			}
		}
		if indexVertex != nil {
			indexVertex(&g.V[i])
		}
	}
	errors.Logf("DEBUG", "Built vertex indices about to build edge indices")
	for i := range b.E {
		g.E[i].Src = b.E[i].Src
		g.E[i].Targ = b.E[i].Targ
		g.E[i].Color = b.E[i].Color
		if indexEdge != nil {
			indexEdge(&g.E[i])
		}
	}
	return g
}