|
| 1 | +// Copyright © 2016 Wei Shen <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "regexp" |
| 26 | + "runtime" |
| 27 | + "strconv" |
| 28 | + "sort" |
| 29 | + |
| 30 | + "github.com/brentp/xopen" |
| 31 | + "github.com/spf13/cobra" |
| 32 | + "github.com/gonum/floats" |
| 33 | + "github.com/tatsushid/go-prettytable" |
| 34 | + "github.com/dustin/go-humanize" |
| 35 | + "github.com/gonum/stat" |
| 36 | + "github.com/shenwei356/util/math" |
| 37 | +) |
| 38 | + |
| 39 | +// stat2Cmd represents the seq command |
| 40 | +var stat2Cmd = &cobra.Command{ |
| 41 | + Use: "stat2", |
| 42 | + Short: "summary of selected number fields", |
| 43 | + Long: `summary of selected number fields: num, sum, min, max, mean, stdev |
| 44 | +
|
| 45 | +`, |
| 46 | + Run: func(cmd *cobra.Command, args []string) { |
| 47 | + config := getConfigs(cmd) |
| 48 | + files := getFileList(args) |
| 49 | + if len(files) > 1 { |
| 50 | + checkError(fmt.Errorf("no more than one file should be given")) |
| 51 | + } |
| 52 | + runtime.GOMAXPROCS(config.NumCPUs) |
| 53 | + |
| 54 | + fieldStr := getFlagString(cmd, "fields") |
| 55 | + if fieldStr == "" { |
| 56 | + checkError(fmt.Errorf("flag -f (--field) needed")) |
| 57 | + } |
| 58 | + |
| 59 | + fuzzyFields := getFlagBool(cmd, "fuzzy-fields") |
| 60 | + fields, colnames, negativeFields, needParseHeaderRow := parseFields(cmd, fieldStr, config.NoHeaderRow) |
| 61 | + var fieldsMap map[int]struct{} |
| 62 | + if len(fields) > 0 { |
| 63 | + fields2 := make([]int, len(fields)) |
| 64 | + fieldsMap = make(map[int]struct{}, len(fields)) |
| 65 | + for i, f := range fields { |
| 66 | + if negativeFields { |
| 67 | + fieldsMap[f*-1] = struct{}{} |
| 68 | + fields2[i] = f * -1 |
| 69 | + } else { |
| 70 | + fieldsMap[f] = struct{}{} |
| 71 | + fields2[i] = f |
| 72 | + } |
| 73 | + } |
| 74 | + fields = fields2 |
| 75 | + } |
| 76 | + |
| 77 | + outfh, err := xopen.Wopen(config.OutFile) |
| 78 | + checkError(err) |
| 79 | + defer outfh.Close() |
| 80 | + |
| 81 | + |
| 82 | + file := files[0] |
| 83 | + csvReader, err := newCSVReaderByConfig(config, file) |
| 84 | + checkError(err) |
| 85 | + csvReader.Run() |
| 86 | + |
| 87 | + parseHeaderRow := needParseHeaderRow // parsing header row |
| 88 | + var colnames2fileds map[string]int // column name -> field |
| 89 | + var colnamesMap map[string]*regexp.Regexp |
| 90 | + var HeaderRow []string |
| 91 | + var isHeaderRow bool |
| 92 | + |
| 93 | + checkFields := true |
| 94 | + |
| 95 | + data := make(map[int][]float64) |
| 96 | + |
| 97 | + for chunk := range csvReader.Ch { |
| 98 | + checkError(chunk.Err) |
| 99 | + |
| 100 | + for _, record := range chunk.Data { |
| 101 | + if parseHeaderRow { // parsing header row |
| 102 | + colnames2fileds = make(map[string]int, len(record)) |
| 103 | + for i, col := range record { |
| 104 | + colnames2fileds[col] = i + 1 |
| 105 | + } |
| 106 | + colnamesMap = make(map[string]*regexp.Regexp, len(colnames)) |
| 107 | + for _, col := range colnames { |
| 108 | + if negativeFields { |
| 109 | + colnamesMap[col[1:]] = fuzzyField2Regexp(col) |
| 110 | + } else { |
| 111 | + colnamesMap[col] = fuzzyField2Regexp(col) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if len(fields) == 0 { // user gives the colnames |
| 116 | + fields = []int{} |
| 117 | + for _, col := range record { |
| 118 | + var ok bool |
| 119 | + if fuzzyFields { |
| 120 | + for _, re := range colnamesMap { |
| 121 | + if re.MatchString(col) { |
| 122 | + ok = true |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + _, ok = colnamesMap[col] |
| 128 | + } |
| 129 | + if (negativeFields && !ok) || (!negativeFields && ok) { |
| 130 | + fields = append(fields, colnames2fileds[col]) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + fieldsMap = make(map[int]struct{}, len(fields)) |
| 136 | + for _, f := range fields { |
| 137 | + fieldsMap[f] = struct{}{} |
| 138 | + } |
| 139 | + |
| 140 | + HeaderRow = record |
| 141 | + parseHeaderRow = false |
| 142 | + isHeaderRow = true |
| 143 | + } |
| 144 | + if checkFields { |
| 145 | + fields2 := []int{} |
| 146 | + for f := range record { |
| 147 | + _, ok := fieldsMap[f+1] |
| 148 | + if negativeFields { |
| 149 | + if !ok { |
| 150 | + fields2 = append(fields2, f+1) |
| 151 | + } |
| 152 | + } else { |
| 153 | + if ok { |
| 154 | + fields2 = append(fields2, f+1) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + fields = fields2 |
| 159 | + if len(fields) == 0 { |
| 160 | + checkError(fmt.Errorf("no fields matched in file: %s", file)) |
| 161 | + } |
| 162 | + |
| 163 | + checkFields = false |
| 164 | + } |
| 165 | + |
| 166 | + if isHeaderRow { |
| 167 | + isHeaderRow = false |
| 168 | + continue |
| 169 | + } |
| 170 | + for _, f := range fields { |
| 171 | + if !reDigitals.MatchString(record[f-1]) { |
| 172 | + checkError(fmt.Errorf("column %d has non-number data: %s", f, record[f-1])) |
| 173 | + } |
| 174 | + v, e := strconv.ParseFloat(removeComma(record[f-1]), 64) |
| 175 | + checkError(e) |
| 176 | + if _, ok := data[f]; !ok { |
| 177 | + data[f] = []float64{} |
| 178 | + } |
| 179 | + data[f] = append(data[f], v) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + tbl, err := prettytable.NewTable([]prettytable.Column{ |
| 184 | + {Header: "field"}, |
| 185 | + {Header: "num", AlignRight: true}, |
| 186 | + {Header: "sum", AlignRight: true}, |
| 187 | + {Header: "min", AlignRight: true}, |
| 188 | + {Header: "max", AlignRight: true}, |
| 189 | + {Header: "mean", AlignRight: true}, |
| 190 | + {Header: "stdev", AlignRight: true}}...) |
| 191 | + checkError(err) |
| 192 | + tbl.Separator = " " |
| 193 | + |
| 194 | + fields = []int{} |
| 195 | + for f := range data { |
| 196 | + fields = append(fields, f) |
| 197 | + } |
| 198 | + sort.Ints(fields) |
| 199 | + |
| 200 | + var fieldS string |
| 201 | + for _, f := range fields { |
| 202 | + if needParseHeaderRow { |
| 203 | + fieldS = HeaderRow[f-1] |
| 204 | + } else { |
| 205 | + fieldS = fmt.Sprintf("%d", f) |
| 206 | + } |
| 207 | + mean, stdev := stat.MeanStdDev(data[f], nil) |
| 208 | + tbl.AddRow( |
| 209 | + fieldS, |
| 210 | + humanize.Comma(int64(len(data[f]))), |
| 211 | + humanize.Commaf(math.Round(floats.Sum(data[f]),2)), |
| 212 | + humanize.Commaf(math.Round(floats.Min(data[f]),2)), |
| 213 | + humanize.Commaf(math.Round(floats.Max(data[f]),2)), |
| 214 | + humanize.Commaf(math.Round(mean,2)), |
| 215 | + humanize.Commaf(math.Round(stdev,2))) |
| 216 | + } |
| 217 | + outfh.Write(tbl.Bytes()) |
| 218 | + }, |
| 219 | +} |
| 220 | + |
| 221 | +func init() { |
| 222 | + RootCmd.AddCommand(stat2Cmd) |
| 223 | + stat2Cmd.Flags().StringP("fields", "f", "", `select only these fields. e.g -f 1,2 or -f columnA,columnB`) |
| 224 | + stat2Cmd.Flags().BoolP("fuzzy-fields", "F", false, `using fuzzy fields, e.g. *name or id123*`) |
| 225 | +} |
0 commit comments