mirror of https://github.com/curusarn/resh
parent
6f7f505420
commit
039573e240
@ -0,0 +1,41 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import sys |
||||
import json |
||||
from collections import defaultdict |
||||
import matplotlib.pyplot as plt |
||||
import matplotlib.path as mpath |
||||
import numpy as np |
||||
|
||||
|
||||
def addRank(data): |
||||
return list(enumerate(data, start=1)) |
||||
|
||||
|
||||
data = json.load(sys.stdin) |
||||
# for strategy in data["Strategies"]: |
||||
# print(json.dumps(strategy)) |
||||
|
||||
cmd_count = defaultdict(int) |
||||
cmdLine_count = defaultdict(int) |
||||
|
||||
for record in data["Records"]: |
||||
cmd_count[record["firstWord"]] += 1 |
||||
cmdLine_count[record["cmdLine"]] += 1 |
||||
|
||||
|
||||
cmdFrq = list(map(lambda x: x[1] / len(data["Records"]), sorted(cmd_count.items(), key=lambda x: x[1], reverse=True))) |
||||
cmdLineFrq = list(map(lambda x: x[1] / len(data["Records"]), sorted(cmdLine_count.items(), key=lambda x: x[1], reverse=True))) |
||||
|
||||
print(cmdFrq) |
||||
print("#################") |
||||
#print(cmdLineFrq_rank) |
||||
|
||||
plt.plot(range(1, len(cmdFrq)+1), cmdFrq) |
||||
plt.title("Command frequency") |
||||
#plt.xticks(range(1, len(cmdFrq)+1)) |
||||
plt.show() |
||||
|
||||
plt.plot(range(1, len(cmdLineFrq)+1), cmdLineFrq) |
||||
plt.title("Commandline frequency") |
||||
plt.show() |
||||
@ -1,99 +0,0 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"bufio" |
||||
"fmt" |
||||
"log" |
||||
"math" |
||||
"strconv" |
||||
) |
||||
|
||||
type results struct { |
||||
writer *bufio.Writer |
||||
size int |
||||
matches []int // matches[N] -> # of matches at distance N
|
||||
matchesTotal int |
||||
charactersRecalled []int |
||||
charactersRecalledTotal int |
||||
dataPointCount int |
||||
} |
||||
|
||||
func (r *results) init() { |
||||
r.matches = make([]int, r.size) |
||||
r.charactersRecalled = make([]int, r.size) |
||||
} |
||||
|
||||
func (r *results) addMatch(distance int, cmdLength int) { |
||||
if distance >= r.size { |
||||
// --calculate-total
|
||||
// log.Fatal("Match distance is greater than size of statistics")
|
||||
r.matchesTotal++ |
||||
r.charactersRecalledTotal += cmdLength |
||||
return |
||||
} |
||||
r.matches[distance]++ |
||||
r.matchesTotal++ |
||||
r.charactersRecalled[distance] += cmdLength |
||||
r.charactersRecalledTotal += cmdLength |
||||
r.dataPointCount++ |
||||
} |
||||
|
||||
func (r *results) addMiss() { |
||||
r.dataPointCount++ |
||||
} |
||||
|
||||
func (r *results) printCumulative() { |
||||
matchesPercent := 0.0 |
||||
out := "### Matches ###\n" |
||||
for i := 0; i < r.size; i++ { |
||||
matchesPercent += 100 * float64(r.matches[i]) / float64(r.dataPointCount) |
||||
out += strconv.Itoa(i) + " ->" |
||||
out += fmt.Sprintf(" (%.1f %%)\n", matchesPercent) |
||||
for j := 0; j < int(math.Round(matchesPercent)); j++ { |
||||
out += "#" |
||||
} |
||||
out += "\n" |
||||
} |
||||
matchesPercent = 100 * float64(r.matchesTotal) / float64(r.dataPointCount) |
||||
out += "TOTAL ->" |
||||
out += fmt.Sprintf(" (%.1f %%)\n", matchesPercent) |
||||
for j := 0; j < int(math.Round(matchesPercent)); j++ { |
||||
out += "#" |
||||
} |
||||
out += "\n" |
||||
|
||||
n, err := r.writer.WriteString(string(out) + "\n\n") |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
if n == 0 { |
||||
log.Fatal("Nothing was written", n) |
||||
} |
||||
|
||||
charsRecall := 0.0 |
||||
out = "### Characters recalled per submission ###\n" |
||||
for i := 0; i < r.size; i++ { |
||||
charsRecall += float64(r.charactersRecalled[i]) / float64(r.dataPointCount) |
||||
out += strconv.Itoa(i) + " ->" |
||||
out += fmt.Sprintf(" (%.2f)\n", charsRecall) |
||||
for j := 0; j < int(math.Round(charsRecall)); j++ { |
||||
out += "#" |
||||
} |
||||
out += "\n" |
||||
} |
||||
charsRecall = float64(r.charactersRecalledTotal) / float64(r.dataPointCount) |
||||
out += "TOTAL ->" |
||||
out += fmt.Sprintf(" (%.2f)\n", charsRecall) |
||||
for j := 0; j < int(math.Round(charsRecall)); j++ { |
||||
out += "#" |
||||
} |
||||
out += "\n" |
||||
|
||||
n, err = r.writer.WriteString(string(out) + "\n\n") |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
if n == 0 { |
||||
log.Fatal("Nothing was written", n) |
||||
} |
||||
} |
||||
@ -1,117 +0,0 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"bytes" |
||||
"io/ioutil" |
||||
"log" |
||||
"sort" |
||||
|
||||
"github.com/wcharczuk/go-chart" |
||||
) |
||||
|
||||
type statistics struct { |
||||
//size int
|
||||
dataPointCount int |
||||
cmdLineCount map[string]int |
||||
} |
||||
|
||||
func (s *statistics) init() { |
||||
s.cmdLineCount = make(map[string]int) |
||||
} |
||||
|
||||
func (s *statistics) addCmdLine(cmdLine string, cmdLength int) { |
||||
s.cmdLineCount[cmdLine]++ |
||||
s.dataPointCount++ |
||||
} |
||||
|
||||
func (s *statistics) graphCmdFrequencyAsFuncOfRank() { |
||||
|
||||
var xValues []float64 |
||||
var yValues []float64 |
||||
|
||||
sortedValues := sortMapByvalue(s.cmdLineCount) |
||||
sortedValues = sortedValues[:100] // cut off at rank 100
|
||||
|
||||
normalizeCoeficient := float64(s.dataPointCount) / float64(sortedValues[0].Value) |
||||
for i, pair := range sortedValues { |
||||
rank := i + 1 |
||||
frequency := float64(pair.Value) / float64(s.dataPointCount) |
||||
normalizeFrequency := frequency * normalizeCoeficient |
||||
|
||||
xValues = append(xValues, float64(rank)) |
||||
yValues = append(yValues, normalizeFrequency) |
||||
} |
||||
|
||||
graphName := "cmdFrqAsFuncOfRank" |
||||
graph := chart.Chart{ |
||||
XAxis: chart.XAxis{ |
||||
Style: chart.StyleShow(), //enables / displays the x-axis
|
||||
Ticks: []chart.Tick{ |
||||
{0.0, "0"}, |
||||
{1.0, "1"}, |
||||
{2.0, "2"}, |
||||
{3.0, "3"}, |
||||
{4.0, "4"}, |
||||
{5.0, "5"}, |
||||
{10.0, "10"}, |
||||
{15.0, "15"}, |
||||
{20.0, "20"}, |
||||
{25.0, "25"}, |
||||
{30.0, "30"}, |
||||
{35.0, "35"}, |
||||
{40.0, "40"}, |
||||
{45.0, "45"}, |
||||
{50.0, "50"}, |
||||
}, |
||||
}, |
||||
YAxis: chart.YAxis{ |
||||
AxisType: chart.YAxisSecondary, |
||||
Style: chart.StyleShow(), //enables / displays the y-axis
|
||||
}, |
||||
Series: []chart.Series{ |
||||
chart.ContinuousSeries{ |
||||
Style: chart.Style{ |
||||
Show: true, |
||||
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64), |
||||
FillColor: chart.GetDefaultColor(0).WithAlpha(64), |
||||
DotColor: chart.GetDefaultColor(0), |
||||
DotWidth: 3.0, |
||||
}, |
||||
XValues: xValues, |
||||
YValues: yValues, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
buffer := bytes.NewBuffer([]byte{}) |
||||
err := graph.Render(chart.PNG, buffer) |
||||
if err != nil { |
||||
log.Fatal("chart.Render error:", err) |
||||
} |
||||
ioutil.WriteFile("/tmp/resh-graph_"+graphName+".png", buffer.Bytes(), 0644) |
||||
} |
||||
|
||||
func sortMapByvalue(input map[string]int) []Pair { |
||||
p := make(PairList, len(input)) |
||||
|
||||
i := 0 |
||||
for k, v := range input { |
||||
p[i] = Pair{k, v} |
||||
i++ |
||||
} |
||||
sort.Sort(sort.Reverse(p)) |
||||
return p |
||||
} |
||||
|
||||
// Pair - A data structure to hold key/value pairs
|
||||
type Pair struct { |
||||
Key string |
||||
Value int |
||||
} |
||||
|
||||
// PairList - A slice of pairs that implements sort.Interface to sort by values
|
||||
type PairList []Pair |
||||
|
||||
func (p PairList) Len() int { return len(p) } |
||||
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
||||
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value } |
||||
Loading…
Reference in new issue