|
| 1 | +// All material is licensed under the Apache License Version 2.0, January 2004 |
| 2 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 3 | + |
| 4 | +// This example demonstrates how to train a regression model in Go. The example |
| 5 | +// also prints out formatted results and saves two plot: (i) a plot of the raw input |
| 6 | +// data, and (ii) a plot of the trained function overlaid on the raw input data. |
| 7 | +// The input data is data about Go github repositories gathered via getrepos.go. |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "encoding/csv" |
| 13 | + "log" |
| 14 | + "sort" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/gonum/plot" |
| 18 | + "github.com/gonum/plot/plotter" |
| 19 | + "github.com/gonum/plot/plotutil" |
| 20 | + "github.com/gonum/plot/vg" |
| 21 | + "github.com/pachyderm/pachyderm/src/client" |
| 22 | + "github.com/pkg/errors" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + |
| 27 | + // Aggregate the counts of created repos per day over all days. |
| 28 | + counts, err := prepareCountData("repodata") |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + // Create and save the plot showing the time series of |
| 34 | + // observed daily created repo counts. |
| 35 | + xys := preparePlotData(counts) |
| 36 | + if err = makePlots(xys); err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// prepareCountData transforms the dataset into a series of |
| 42 | +// daily count values for plotting. |
| 43 | +func prepareCountData(dataSet string) ([][]int, error) { |
| 44 | + |
| 45 | + // Create a map to store the daily counts of created repos. |
| 46 | + countMap := make(map[int]int) |
| 47 | + |
| 48 | + // Get the data set we stored in pachyderm. |
| 49 | + data, err := getDataSet(dataSet, "master", "godata") |
| 50 | + if err != nil { |
| 51 | + return [][]int{}, errors.Wrap(err, "Could not get data from pachyderm") |
| 52 | + } |
| 53 | + |
| 54 | + // Extract the records from the data. |
| 55 | + reader := csv.NewReader(bytes.NewReader(data.Bytes())) |
| 56 | + reader.FieldsPerRecord = -1 |
| 57 | + records, err := reader.ReadAll() |
| 58 | + if err != nil { |
| 59 | + return [][]int{}, errors.Wrap(err, "Could not read in data records.") |
| 60 | + } |
| 61 | + |
| 62 | + // Create a map of daily created repos where the keys are the days and |
| 63 | + // the values are the counts of created repos on that day. |
| 64 | + startTime := time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 65 | + layout := "2006-01-02 15:04:05" |
| 66 | + for _, each := range records { |
| 67 | + t, err := time.Parse(layout, each[2][0:19]) |
| 68 | + if err != nil { |
| 69 | + return [][]int{}, errors.Wrap(err, "Could not parse timestamps") |
| 70 | + } |
| 71 | + interval := int(t.Sub(startTime).Hours() / 24.0) |
| 72 | + countMap[interval]++ |
| 73 | + } |
| 74 | + |
| 75 | + // Sort the day values which is required for plotting. |
| 76 | + var keys []int |
| 77 | + for k := range countMap { |
| 78 | + keys = append(keys, k) |
| 79 | + } |
| 80 | + sort.Ints(keys) |
| 81 | + var sortedCounts [][]int |
| 82 | + for _, k := range keys { |
| 83 | + sortedCounts = append(sortedCounts, []int{k, countMap[k]}) |
| 84 | + } |
| 85 | + |
| 86 | + return sortedCounts, nil |
| 87 | +} |
| 88 | + |
| 89 | +// getDataSet gets a previously stored dataset from pachyderm data versioning. |
| 90 | +func getDataSet(dataSet, branch, repoName string) (bytes.Buffer, error) { |
| 91 | + |
| 92 | + // Open a connection to pachyderm running on localhost. |
| 93 | + c, err := client.NewFromAddress("localhost:30650") |
| 94 | + if err != nil { |
| 95 | + return bytes.Buffer{}, errors.Wrap(err, "Could not connect to Pachyderm") |
| 96 | + } |
| 97 | + |
| 98 | + // Read the latest commit of filename to the given repoName. |
| 99 | + var buffer bytes.Buffer |
| 100 | + if err := c.GetFile(repoName, branch, dataSet, 0, 0, "", nil, &buffer); err != nil { |
| 101 | + return buffer, errors.Wrap(err, "Could not retrieve pachyderm file") |
| 102 | + } |
| 103 | + |
| 104 | + return buffer, nil |
| 105 | +} |
| 106 | + |
| 107 | +// preparePlotData prepares the raw input data for plotting. |
| 108 | +func preparePlotData(counts [][]int) plotter.XYs { |
| 109 | + pts := make(plotter.XYs, len(counts)) |
| 110 | + var i int |
| 111 | + |
| 112 | + for _, count := range counts { |
| 113 | + pts[i].X = float64(count[0]) |
| 114 | + pts[i].Y = float64(count[1]) |
| 115 | + i++ |
| 116 | + } |
| 117 | + |
| 118 | + return pts |
| 119 | +} |
| 120 | + |
| 121 | +// makePlots creates and saves the first of our plots showing the raw input data. |
| 122 | +func makePlots(xys plotter.XYs) error { |
| 123 | + |
| 124 | + // Create a new plot. |
| 125 | + p, err := plot.New() |
| 126 | + if err != nil { |
| 127 | + return errors.Wrap(err, "Could not create plot object") |
| 128 | + } |
| 129 | + |
| 130 | + // Label the new plot. |
| 131 | + p.Title.Text = "Daily Counts of Go Repos Created" |
| 132 | + p.X.Label.Text = "Days from Jan. 1, 2013" |
| 133 | + p.Y.Label.Text = "Count" |
| 134 | + |
| 135 | + // Add the prepared points to the plot. |
| 136 | + if err = plotutil.AddLinePoints(p, "Counts", xys); err != nil { |
| 137 | + return errors.Wrap(err, "Could not add lines to plot") |
| 138 | + } |
| 139 | + |
| 140 | + // Save the plot to a PNG file. |
| 141 | + if err := p.Save(7*vg.Inch, 4*vg.Inch, "countseries.png"); err != nil { |
| 142 | + return errors.Wrap(err, "Could not output plot") |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
0 commit comments