/
app.py
executable file
·566 lines (431 loc) · 18.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env python3
import os, sys
from collections import defaultdict
from datetime import timedelta
import PyQt5.QtCore
from PyQt5.QtCore import (
pyqtSignal,
)
from PyQt5.QtWidgets import (
QApplication, QWidget, QTabWidget,
QVBoxLayout, QHBoxLayout,
QLabel, QMessageBox, QGroupBox,
QLineEdit, QPushButton, QFileDialog, QComboBox, QCheckBox, QSpinBox,
)
from datasource import Journal, get_value
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.pyplot import subplots
from matplotlib.figure import Figure
from matplotlib.cm import get_cmap
def debug():
from PyQt5.QtCore import pyqtRemoveInputHook; pyqtRemoveInputHook()
import ipdb; ipdb.set_trace()
class Options(QWidget):
reset = pyqtSignal()
redraw = pyqtSignal()
def __init__(self, window):
super(Options, self).__init__()
self.window = window
layout = QVBoxLayout(self)
self.button = QPushButton("Click to select a file to open", self)
self.button.clicked.connect(self.select_file)
layout.addWidget(self.button)
currencyLayout = QHBoxLayout()
label = QLabel("Show in terms of")
self.show_currency = QComboBox()
currencyLayout.addWidget(label)
currencyLayout.addWidget(self.show_currency)
viewLayout = QHBoxLayout()
self.merge = QCheckBox("Merge")
self.merge.stateChanged.connect(self.reset)
self.effective_date = QCheckBox("Use Effective Dates")
self.effective_date.stateChanged.connect(self.reset)
depthLayout = QHBoxLayout()
label = QLabel("Account depth to show")
self.depth_limit = QSpinBox()
self.depth_limit.setSpecialValueText("Unlimited")
self.depth_limit.setMinimum(0)
self.depth_limit.setValue(0)
self.depth_limit.valueChanged.connect(self.redraw)
depthLayout.addWidget(label)
depthLayout.addWidget(self.depth_limit)
viewLayout.addLayout(currencyLayout)
viewLayout.addWidget(self.merge)
viewLayout.addWidget(self.effective_date)
viewLayout.addLayout(depthLayout)
filterLayout = QHBoxLayout()
label = QLabel("Filter:")
self.filter = QLineEdit()
filterLayout.addWidget(label)
filterLayout.addWidget(self.filter)
layout.addLayout(viewLayout)
layout.addLayout(filterLayout)
def select_file(self):
selected_file, ignored = QFileDialog(self, "Ledger file to open").getOpenFileName()
if selected_file:
try:
journal = Journal(selected_file, effective_date=self.effective_date)
self.journal = journal
self.effective_date.stateChanged.connect(journal.set_effective_date)
self.button.setText(selected_file)
self.filename = selected_file
self.window.setWindowTitle("Ledger visualizer - " + selected_file)
self.filter.editingFinished.connect(self.reset)
self.show_currency.addItems(journal.commodities.keys())
self.show_currency.currentTextChanged.connect(self.reset)
self.reset.emit()
except RuntimeError:
message = QMessageBox(self)
message.setText("Ledger could not parse the selected file")
message.exec()
class CommodityBox(QGroupBox):
changed = pyqtSignal()
def __init__(self, options, title="Commodities"):
super(CommodityBox, self).__init__(title)
self.options = options
self.options.reset.connect(self.reset)
self.setToolTip("Show only selected commodities")
self.setCheckable(True)
self.setChecked(False)
self.toggled.connect(self.changed)
self.layout = QVBoxLayout(self)
self.checkboxes = {}
def __contains__(self, commodity):
if not self.isChecked():
return True
checkbox = self.checkboxes.get(commodity)
return checkbox and checkbox.isChecked()
def reset(self):
for checkbox in self.checkboxes.values():
self.layout.removeWidget(checkbox)
self.checkboxes = {}
commodities = self.options.journal.commodities
for commodity in commodities.keys():
checkbox = QCheckBox(commodity, self)
self.checkboxes[commodity] = checkbox
self.layout.addWidget(checkbox)
checkbox.stateChanged.connect(self.changed)
class GraphTab(QWidget):
def __init__(self, options):
super(GraphTab, self).__init__()
self.options = options
self.options.reset.connect(self.reset)
self.options.redraw.connect(self.redraw)
self.commodities = CommodityBox(options)
self.commodities.changed.connect(self.redraw)
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.cmap = get_cmap('gist_ncar')
graphLayout = QVBoxLayout()
graphLayout.addWidget(self.canvas)
graphLayout.addWidget(self.mpl_toolbar)
layout = QHBoxLayout(self)
layout.addWidget(self.commodities)
layout.addLayout(graphLayout)
self.running_total = None
def reset(self):
options = self.options
self.commodity = options.show_currency.currentText()
self.merge = bool(self.commodity and options.merge.isChecked())
filter = options.filter.text()
self.running_total, self.total = options.journal.time_series(filter, self.commodity, self.merge)
self.redraw()
def redraw(self):
self.ax.clear()
if not self.running_total:
return
lines = len(self.total)
colors = map(self.cmap, ((x+0.5)/lines for x in range(lines)))
for color, (commodity, amount) in zip(colors, sorted(self.total.items(), key=lambda x: x[1].number(), reverse=True)):
if commodity not in self.commodities:
continue
series = self.running_total[commodity]
x = sorted(series.keys())
y = [series[i].number() for i in x]
label = ("%s (%." + str(amount.commodity.precision) + "f %s)") % (commodity, amount.number(), amount.commodity.symbol)
self.ax.plot_date(x, y, fmt='o-', color=color, label=label)
if self.commodity:
self.ax.set_ylabel(self.commodity)
self.ax.legend(loc='upper left')
self.fig.canvas.draw()
class AccountTab(QWidget):
def __init__(self, options):
super(AccountTab, self).__init__()
self.options = options
self.options.reset.connect(self.reset)
self.options.redraw.connect(self.redraw)
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.cmap = get_cmap('gist_ncar')
graphLayout = QVBoxLayout()
graphLayout.addWidget(self.canvas)
graphLayout.addWidget(self.mpl_toolbar)
layout = QHBoxLayout(self)
layout.addLayout(graphLayout)
self.series = None
self.commodity = None
def reset(self):
options = self.options
self.commodity = options.show_currency.currentText()
self.merge = bool(self.commodity and options.merge.isChecked())
filter = options.filter.text()
self.series = options.journal.account_series(filter)
self.redraw()
def redraw(self):
self.ax.clear()
if not self.series or not self.commodity:
return
def useable_accounts(limit):
aggregate = {}
for name in self.series.running_total:
account = self.series.accounts[name]
if limit and account.depth >= limit:
while account.depth > limit:
account = account.parent
name = account.fullname()
aggregate[name] = (get_value(self.series.aggregated_total[name], commodity),
self.series.aggregated_running[name])
elif name not in aggregate:
aggregate[name] = (get_value(self.series.total[name], commodity),
self.series.running_total[name])
accounts = len(aggregate)
colors = map(self.cmap, ((x+0.5)/accounts for x in range(accounts)))
return ((name, next(colors), aggregate[name]) for name in
sorted(aggregate, key=lambda x: aggregate[x][0], reverse=True))
commodity = self.options.journal.commodities[self.commodity]
limit = self.options.depth_limit.value()
for name, color, (total, running_total) in useable_accounts(limit):
label = ("%s (%s)") % (name, total)
running_total = {date: get_value(amount, commodity, date) for (date, amount) in running_total.items()}
x, y = zip(*sorted(running_total.items()))
self.ax.plot_date(x, y, fmt='o-', label=label, color=color)
self.ax.set_ylabel(self.commodity)
self.ax.legend(loc='upper left')
self.fig.canvas.draw()
class BarTab(QWidget):
def __init__(self, options):
super(BarTab, self).__init__()
self.options = options
self.options.reset.connect(self.reset)
self.options.redraw.connect(self.redraw)
self.classifiers = self.monthly
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.cmap = get_cmap('gist_ncar')
graphLayout = QVBoxLayout()
graphLayout.addWidget(self.canvas)
graphLayout.addWidget(self.mpl_toolbar)
#optionLayout = QVBoxLayout(self)
#optionLayout.addWidget(self.classifiers)
layout = QHBoxLayout(self)
layout.addLayout(graphLayout)
self.series = None
self.commodity = None
def monthly(self, name, date, amount, to_commodity):
return (date.replace(day=1), name, get_value(amount, to_commodity, date))
def reset(self):
options = self.options
self.commodity = options.show_currency.currentText()
self.merge = bool(self.commodity and options.merge.isChecked())
filter = options.filter.text()
self.series = options.journal.account_series(filter)
self.redraw()
def redraw(self):
self.ax.clear()
if not self.series or not self.commodity:
return
accounts = {}
accounts_sorted = []
data = defaultdict(dict)
commodity = self.options.journal.commodities[self.commodity]
classifier = self.classifiers
limit = self.options.depth_limit.value()
width = timedelta(10)
Balance = self.options.journal.ledger.Balance
Amount = self.options.journal.ledger.Amount
def useable_accounts(series, commodity, limit):
aggregate = {}
for name in series.running_total:
account = series.accounts[name]
if limit and account.depth >= limit:
while account.depth > limit:
account = account.parent
name = account.fullname()
aggregate[name] = (get_value(series.aggregated_total[name], commodity),
series.aggregated_postings[name])
elif name not in aggregate:
aggregate[name] = (get_value(series.total[name], commodity),
series.postings[name])
accounts = len(aggregate)
return ((name, aggregate[name]) for name in
sorted(aggregate, key=lambda x: aggregate[x][0], reverse=True))
for name, (total, postings) in useable_accounts(self.series, commodity, limit):
label = ("%s (%s)") % (name, total)
accounts_sorted.append(label)
number = accounts[name] = (len(accounts), label)
for group, bucket, value in (
classifier(accounts[name], date, amount, commodity)
for (date, amount) in postings.items()):
data[group][bucket] = data[group].get(bucket, Balance()) + value
postings = {date: get_value(amount, commodity, date)
for (date, amount) in postings.items()}
view = defaultdict(dict)
for group, buckets in data.items():
offsets = [Balance(), Balance()]
sum_neg = Balance()
for account in sorted(accounts.values()):
if account not in buckets:
continue
value = buckets[account]
value = value and value.to_amount() or Amount(0)
negative = int(value < 0)
offset = offsets[negative]
view[account][group] = [negative, offset, value]
offsets[negative] = offset + value
if negative:
sum_neg += value
for account, l in view.items():
if group not in l:
continue
value = l[group]
if value[0]:
new = value[1] + value[2] - sum_neg
value[2].in_place_negate()
else:
new = value[1]
value[1] = new and float(new.to_amount().number()) or 0
value[2] = float(value[2].number())
horizontal_offsets = [ -width/2, width/2 ]
for key, data in view.items():
index, label = key
label = accounts_sorted[index]
color = self.cmap((index+0.5)/len(accounts_sorted))
bars = [defaultdict(list), defaultdict(list)]
for group in sorted(data.keys()):
negative, offset, height = data[group]
bars[negative]['group'].append(group+horizontal_offsets[negative])
bars[negative]['offset'].append(offset)
bars[negative]['height'].append(height)
self.ax.bar(bars[0]['group'], bars[0]['height'], width.days,
bottom=bars[0]['offset'], color=color, label=label)
self.ax.bar(bars[1]['group'], bars[1]['height'], width.days,
bottom=bars[1]['offset'], color=color)
self.ax.set_ylabel(self.commodity)
#self.ax.legend(loc='upper left')
self.fig.canvas.draw()
class PieTab(QWidget):
def __init__(self, options):
super(PieTab, self).__init__()
self.options = options
self.options.reset.connect(self.reset)
self.options.redraw.connect(self.redraw)
self.fig, self.ax = subplots()
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.cmap = get_cmap('gist_ncar')
self.account = QLineEdit(self)
self.account.editingFinished.connect(self.redraw)
graphLayout = QVBoxLayout(self)
graphLayout.addWidget(self.account)
graphLayout.addWidget(self.canvas)
graphLayout.addWidget(self.mpl_toolbar)
self.series = None
self.commodity = None
def reset(self):
options = self.options
self.commodity = options.show_currency.currentText()
if not self.commodity:
return
filter = options.filter.text()
self.series = options.journal.account_series(filter)
self.redraw()
def wedges(self, values, threshold=0.01):
"""Generates wedges as long as the new one would be
at least (threshold * current total), then one more
for the rest of the data"""
out = []
total = 0
while values:
current = values[0]
value, name = current
if total and float(value / total) < threshold and len(values) > 1:
remainder = sum((x[0] for x in values))
current = (remainder, 'long tail of {} below {:.2%}'.format(len(values), threshold))
out.append(current)
total += remainder
break
else:
out.append(current)
total += value
values.pop(0)
if total:
out = [(value.abs(), "{} ({:.2%})".format(name, float(value / total))) for (value, name) in out]
# the graph gets drawn counter-clockwise, reverse to get it clockwise
return zip(*reversed(out))
def redraw(self):
self.ax.clear()
if not self.series or not self.commodity:
return
commodity = self.options.journal.commodities[self.commodity]
data = []
processed = set()
for name, account in self.series.accounts.items():
limit = self.options.depth_limit.value()
aggregate = limit and account.depth >= limit
if aggregate:
while account.depth > limit:
account = account.parent
name = account.fullname()
value = self.series.aggregated_total[name]
else:
value = self.series.total[name]
if name in processed:
continue
data.append((get_value(value, commodity), name))
processed.add(name)
if not data:
return
data = sorted(data, reverse=True, key=lambda x: x[0].abs())
sizes, labels = self.wedges(data)
colors = map(self.cmap, (1 - float(x)/len(sizes) for x in range(len(sizes))))
self.ax.pie(sizes, labels=labels, colors=list(colors), startangle=90)
self.fig.canvas.draw()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle("Ledger visualizer")
# Create a layout Object, attached to the window.
layout = QVBoxLayout(self)
self.options = Options(self)
layout.addWidget(self.options)
tabs = QTabWidget()
layout.addWidget(tabs)
graph = GraphTab(self.options)
tabs.addTab(graph, "Time series")
graph = AccountTab(self.options)
tabs.addTab(graph, "Account breakdown")
graph = BarTab(self.options)
tabs.addTab(graph, "Timed breakdown")
text = PieTab(self.options)
tabs.addTab(text, "Pie charts")
button = QPushButton("Quit", self)
layout.addWidget(button)
button.clicked.connect(app.quit)
if __name__=='__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())