better-git-of-theseus 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,162 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2016 Erik Bernhardsson
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import matplotlib
18
+
19
+ matplotlib.use("Agg")
20
+
21
+ import argparse
22
+ import collections
23
+ import json
24
+ import math
25
+ import os
26
+ import sys
27
+
28
+ import numpy
29
+ from matplotlib import pyplot
30
+
31
+
32
+ def survival_plot(
33
+ input_fns, exp_fit=False, display=False, outfile=None, years=5, title=None
34
+ ):
35
+ all_deltas = []
36
+ YEAR = 365.25 * 24 * 60 * 60
37
+ fig = pyplot.figure(figsize=(13, 8))
38
+ pyplot.style.use("ggplot")
39
+
40
+ for fn_or_data in input_fns:
41
+ if isinstance(fn_or_data, str):
42
+ print("reading %s" % fn_or_data)
43
+ commit_history = json.load(open(fn_or_data))
44
+ fn = fn_or_data
45
+ else:
46
+ commit_history = fn_or_data
47
+ fn = "data"
48
+
49
+ print("counting %d commits" % len(commit_history))
50
+ deltas = collections.defaultdict(lambda: numpy.zeros(2))
51
+ total_n = 0
52
+ for commit, history in commit_history.items():
53
+ t0, orig_count = history[0]
54
+ total_n += orig_count
55
+ last_count = orig_count
56
+ for t, count in history[1:]:
57
+ deltas[t - t0] += (count - last_count, 0)
58
+ last_count = count
59
+ deltas[history[-1][0] - t0] += (-last_count, -orig_count)
60
+
61
+ all_deltas.append((total_n, deltas))
62
+ print("adding %d deltas..." % len(deltas))
63
+ total_k = total_n
64
+ P = 1.0
65
+ xs = []
66
+ ys = []
67
+ for t in sorted(deltas.keys()):
68
+ delta_k, delta_n = deltas[t]
69
+ xs.append(t / YEAR)
70
+ ys.append(100.0 * P)
71
+ P *= 1 + delta_k / total_n
72
+ total_k += delta_k
73
+ total_n += delta_n
74
+ if P < 0.05:
75
+ break
76
+
77
+ print("plotting...")
78
+ if exp_fit:
79
+ pyplot.plot(xs, ys, color="darkgray")
80
+ else:
81
+ parts = os.path.split(fn)
82
+ pyplot.plot(xs, ys, label=(len(parts) > 1 and parts[-2] or None))
83
+
84
+ def fit(k):
85
+ loss = 0.0
86
+ for total_n, deltas in all_deltas:
87
+ total_k = total_n
88
+ P = 1.0
89
+ for t in sorted(deltas.keys()):
90
+ delta_k, delta_n = deltas[t]
91
+ pred = total_n * math.exp(-k * t / YEAR)
92
+ loss += (total_n * P - pred) ** 2
93
+ P *= 1 + delta_k / total_n
94
+ total_k += delta_k
95
+ total_n += delta_n
96
+ print(k, loss)
97
+ return loss
98
+
99
+ if exp_fit:
100
+ try:
101
+ import scipy.optimize
102
+ except ImportError:
103
+ sys.exit("Scipy is a required dependency when using the --exp-fit flag")
104
+
105
+ print("fitting exponential function")
106
+ k = scipy.optimize.fmin(fit, 0.5, maxiter=50)[0]
107
+ ts = numpy.linspace(0, years, 1000)
108
+ ys = [100.0 * math.exp(-k * t) for t in ts]
109
+ pyplot.plot(
110
+ ts,
111
+ ys,
112
+ color="red",
113
+ label="Exponential fit, half-life = %.2f years" % (math.log(2) / k),
114
+ )
115
+
116
+ pyplot.xlabel("Years")
117
+ pyplot.ylabel("%")
118
+ pyplot.xlim([0, years])
119
+ pyplot.ylim([0, 100])
120
+ pyplot.title("% of lines still present in code after n years")
121
+ pyplot.legend()
122
+ pyplot.tight_layout()
123
+
124
+ if title:
125
+ pyplot.text(0.5, 0.5, title, transform=pyplot.gca().transAxes,
126
+ fontsize=40, color='gray', alpha=0.3,
127
+ ha='center', va='center', rotation=30)
128
+
129
+ if outfile:
130
+ print("Writing output to %s" % outfile)
131
+ pyplot.savefig(outfile)
132
+
133
+ if display:
134
+ pyplot.show()
135
+
136
+ return fig
137
+
138
+
139
+ def survival_plot_cmdline():
140
+ parser = argparse.ArgumentParser(description="Plot survival plot")
141
+ parser.add_argument("--exp-fit", action="store_true", help="Plot exponential fit")
142
+ parser.add_argument("--display", action="store_true", help="Display plot")
143
+ parser.add_argument(
144
+ "--outfile",
145
+ default="survival_plot.png",
146
+ type=str,
147
+ help="Output file to store results (default: %(default)s)",
148
+ )
149
+ parser.add_argument(
150
+ "--years",
151
+ type=float,
152
+ default=5,
153
+ help="Number of years on x axis (default: %(default)s)",
154
+ )
155
+ parser.add_argument("input_fns", nargs="*")
156
+ kwargs = vars(parser.parse_args())
157
+
158
+ survival_plot(**kwargs)
159
+
160
+
161
+ if __name__ == "__main__":
162
+ survival_plot_cmdline()
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2016 Erik Bernhardsson
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import itertools, numpy
18
+
19
+
20
+ def generate_n_colors(n):
21
+ vs = numpy.linspace(0.4, 0.9, 6)
22
+ colors = [(0.9, 0.4, 0.4)]
23
+
24
+ def euclidean(a, b):
25
+ return sum((x - y) ** 2 for x, y in zip(a, b))
26
+
27
+ while len(colors) < n:
28
+ new_color = max(
29
+ itertools.product(vs, vs, vs),
30
+ key=lambda a: min(euclidean(a, b) for b in colors),
31
+ )
32
+ colors.append(new_color)
33
+ return colors