Пример #1
0
func evTreeRun(c *cmdapp.Command, args []string) {
	d, err := loadData()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	r := raster.Rasterize(d, numCols, numFill)
	ts, err := loadTrees()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	f := os.Stdin
	if len(inFile) > 0 {
		f, err = os.Open(inFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		defer f.Close()
	}
	recs, err := events.Read(f, r, ts, szExtra)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	err = treesvg.SVG(ts, recs, stepX, stepY)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
}
Пример #2
0
func txLsRun(c *cmdapp.Command, args []string) {
	d, err := loadData()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	for _, tx := range d.Ls {
		fmt.Printf("%s\n", tx.Name)
	}
}
Пример #3
0
func trLsRun(c *cmdapp.Command, args []string) {
	ls, err := loadTrees()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	for _, t := range ls {
		fmt.Printf("%s\n", t.ID)
	}
}
Пример #4
0
func rBayRun(c *cmdapp.Command, args []string) {
	if len(args) == 0 {
		fmt.Fprintf(os.Stderr, "%s: expecting output file\n", c.Name())
		os.Exit(1)
	}
	d, err := loadData()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	ras := raster.Rasterize(d, numCols, 0)

	geo, err := os.Create(args[0] + ".geo.txt")
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	defer geo.Close()
	fmt.Fprintf(geo, "# 0.0\n")
	var pxls []int
	for y := 0; y < ((numCols / 2) + 1); y++ {
		lat := 90 - ((float64(y) * ras.Resol) + (ras.Resol / 2))
		for x := 0; x < numCols; x++ {
			px := (y * numCols) + x
			if _, ok := ras.Pixel[px]; ok {
				pxls = append(pxls, px)
				lon := ((float64(x) * ras.Resol) + (ras.Resol / 2)) - 180
				fmt.Fprintf(geo, "%.4f %.4f\n", lat, lon)
			}
		}
	}

	areas, err := os.Create(args[0] + ".areas.txt")
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	defer areas.Close()
	fmt.Fprintf(areas, "%d %d\n", len(ras.Names), len(pxls))
	for _, tx := range ras.Names {
		fmt.Fprintf(areas, "%s\t", tx.Name)
		for _, v := range pxls {
			px := ras.Pixel[v]
			if tx.Obs.IsOn(px) {
				fmt.Fprintf(areas, "1")
			} else {
				fmt.Fprintf(areas, "0")
			}
		}
		fmt.Fprintf(areas, "\n")
	}
}
Пример #5
0
func evFlipRun(c *cmdapp.Command, args []string) {
	if noVic && noSymp && noPoint && noFound {
		fmt.Fprintf(os.Stderr, "%s: at least one event must be allowed\n", c.Name())
		os.Exit(1)
	}
	o := os.Stdout
	if len(outFile) > 0 {
		var err error
		o, err = os.Create(outFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		defer o.Close()
	}
	d, err := loadData()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	r := raster.Rasterize(d, numCols, numFill)
	ts, err := loadTrees()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	best := make(chan []*events.Recons)
	if numReps <= 0 {
		numReps = 100
	}
	if numProc <= 0 {
		numProc = runtime.NumCPU() * 2
	}
	for _, t := range ts {
		out := make(chan []*events.Recons)
		or := events.OR(r, t, szExtra)

		// modify or to take into account the prohibited events
		if (noVic) && (noSymp) {
			for i := range or.Rec {
				if (or.Rec[i].Flag == events.Vic) || (or.Rec[i].Flag == events.SympU) {
					if noFound {
						or.Rec[i].Flag = events.PointR
					} else {
						or.Rec[i].Flag = events.FoundR
					}
					or.DownPass(i)
				}
			}
		} else if noVic {
			for i := range or.Rec {
				if or.Rec[i].Flag == events.Vic {
					or.Rec[i].Flag = events.SympU
					or.DownPass(i)
				}
			}
		} else if noSymp {
			for i := range or.Rec {
				if or.Rec[i].Flag == events.SympU {
					or.Rec[i].Flag = events.Vic
					or.DownPass(i)
				}
			}
		}
		go doFlip(or, out)
		go getBestFlip(or, out, best)
	}
	head := true
	for _ = range ts {
		recs := <-best
		if verbose {
			fmt.Printf("Tree %s best: %.3f recs found: %d\n", recs[0].Tree.ID, recs[0].Cost(), len(recs))
		}
		for _, b := range recs {
			err := b.Write(o, head)
			if err != nil {
				fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
				os.Exit(1)
			}
			head = false
		}
	}
}
Пример #6
0
func evMapRun(c *cmdapp.Command, args []string) {
	d, err := loadData()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	r := raster.Rasterize(d, numCols, numFill)
	ts, err := loadTrees()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	f := os.Stdin
	if len(inFile) > 0 {
		f, err = os.Open(inFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		defer f.Close()
	}
	recs, err := events.Read(f, r, ts, szExtra)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}

	err = treesvg.SVG(ts, nil, 0, 0)

	// determines the boudaries of the geography
	minLat := float64(biogeo.MaxLat)
	maxLat := float64(biogeo.MinLat)
	minLon := float64(biogeo.MaxLon)
	maxLon := float64(biogeo.MinLon)
	for _, tx := range d.Ls {
		for _, g := range tx.Recs {
			if g.Lat < minLat {
				minLat = g.Lat
			}
			if g.Lat > maxLat {
				maxLat = g.Lat
			}
			if g.Lon < minLon {
				minLon = g.Lon
			}
			if g.Lon > maxLon {
				maxLon = g.Lon
			}
		}
	}
	maxLat += 10
	if maxLat > biogeo.MaxLat {
		maxLat = biogeo.MaxLat
	}
	minLat -= 10
	if minLat < biogeo.MinLat {
		minLat = biogeo.MinLat
	}
	maxLon += 10
	if maxLon > biogeo.MaxLon {
		maxLon = biogeo.MaxLon
	}
	minLon -= 10
	if minLon < biogeo.MinLon {
		minLon = biogeo.MinLon
	}

	// loads the map
	var imgmap image.Image
	if len(args) == 0 {
		imgmap = image.NewRGBA(image.Rect(0, 0, 360, 180))
	} else {
		im, err := os.Open(args[0])
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		imgmap, _, err = image.Decode(im)
		im.Close()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
	}
	sizeX := imgmap.Bounds().Max.X
	sizeY := imgmap.Bounds().Max.Y
	scaleX := float64(sizeX) / 360
	scaleY := float64(sizeY) / 180

	szX := (maxLon - minLon) * scaleX
	szY := (maxLat - minLat) * scaleY
	originX := int((180 + minLon) * scaleX)
	originY := int((90 - maxLat) * scaleY)

	imgPump := make(chan *image.RGBA64, 10)
	go func() {
		ln := 0
		for _, rc := range recs {
			for i := range rc.Rec {
				if rc.Rec[i].Node.First == nil {
					continue
				}
				ln++
			}
		}
		for i := 0; i < ln; i++ {
			dest := image.NewRGBA64(image.Rect(0, 0, int(szX), int(szY)))
			for x := 0; x < int(szX); x++ {
				for y := 0; y < int(szY); y++ {
					dest.Set(x, y, imgmap.At(x+originX, y+originY))
				}
			}
			imgPump <- dest
		}
	}()

	black := color.RGBA64{0, 0, 0, 0xFFFF}

	var done sync.WaitGroup
	var errVal error
	for _, rv := range recs {
		done.Add(1)
		go func(rc *events.Recons) {
			defer done.Done()
			for i := range rc.Rec {
				if rc.Rec[i].Node.First == nil {
					continue
				}
				dest := <-imgPump
				e := "noev"
				switch rc.Rec[i].Flag {
				case events.Vic:
					e = "vic"
				case events.SympU, events.SympL, events.SympR:
					e = "symp"
				case events.PointL, events.PointR:
					e = "point"
				case events.FoundL, events.FoundR:
					e = "found"
				}
				for j := range rc.Rec {
					if rc.Rec[j].Node.First != nil {
						continue
					}
					if len(rc.Rec[j].Node.Term) == 0 {
						continue
					}
					tx := d.Taxon(rc.Rec[j].Node.Term)
					if tx == nil {
						continue
					}
					cr, ok := eventColor(rc, i, j)
					if !ok {
						continue
					}
					for _, g := range tx.Recs {
						c := int((180+g.Lon)*scaleX) - originX
						r := int((90-g.Lat)*scaleY) - originY
						for x := c - 3; x <= c+3; x++ {
							for y := r - 3; y <= r+3; y++ {
								dest.Set(x, y, black)
							}
						}
						for x := c - 2; x <= c+2; x++ {
							for y := r - 2; y <= r+2; y++ {
								dest.Set(x, y, cr)
							}
						}
					}
				}
				im, err := os.Create(rc.Tree.ID + "-n" + rc.Rec[i].Node.ID + "-r" + rc.ID + "-" + e + ".png")
				if err != nil {
					errVal = err
					return
				}
				err = png.Encode(im, dest)
				im.Close()
				if err != nil {
					errVal = err
					return
				}
			}
		}(rv)
	}
	done.Wait()
	if errVal != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), errVal)
		os.Exit(1)
	}
}
Пример #7
0
func trInRun(c *cmdapp.Command, args []string) {
	if len(args) == 0 {
		fmt.Fprintf(os.Stderr, "%s: expecting tree ID\n", c.Name())
		os.Exit(1)
	}
	var ts []*tree.Tree
	if _, err := os.Stat(treeFileName); err == nil {
		ts, err = loadTrees()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
	}
	f := os.Stdin
	if len(inFile) != 0 {
		var err error
		f, err = os.Open(inFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		defer f.Close()
	}
	for _, t := range ts {
		if t.ID == args[0] {
			fmt.Fprintf(os.Stderr, "%s: tree ID already used\n", c.Name())
			os.Exit(1)
		}
	}

	// reads the tree
	in := os.Stdin
	if len(inFile) != 0 {
		var err error
		in, err = os.Open(inFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		defer in.Close()
	}
	t, err := tree.ReadParenthetic(in, args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	ts = append(ts, t)

	// writes the trees into the database
	out, err := os.Create(treeFileName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
		os.Exit(1)
	}
	defer out.Close()
	head := true
	for _, v := range ts {
		err = v.Write(out, head)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
			os.Exit(1)
		}
		head = false
	}
}