/
report.py
783 lines (676 loc) · 35.7 KB
/
report.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import json
from datetime import timedelta, date, datetime
from itertools import chain, repeat, groupby
from operator import itemgetter
import requests
from lxml import etree
from lxml.builder import ElementMaker
from lxml.etree import Comment
from more_itertools import pairwise
from faust_logging import logging
from graph import MacrogenesisInfo, EARLIEST, LATEST, DAY
import csv
from collections.__init__ import defaultdict, Counter
from html import escape
from pathlib import Path
from typing import Iterable, List, Dict, Mapping, Tuple, Sequence, Union
import networkx as nx
import faust
from datings import BiblSource
from uris import Reference, Witness, Inscription, UnknownRef, AmbiguousRef
from visualize import write_dot, simplify_graph
logger = logging.getLogger(__name__)
target = Path(faust.config.get('macrogenesis', 'output-dir'))
RELATION_LABELS = {'not_before': 'nicht vor',
'not_after': 'nicht nach',
'from_': 'von',
'to': 'bis',
'when': 'am',
'temp-syn': 'ca. gleichzeitig',
'temp-pre': 'zeitlich vor',
None: '???'
}
class HtmlTable:
"""
Helper class to create a simple HTML table from some kind of data.
"""
def __init__(self, **table_attrs):
"""
Creates a new table.
Args:
**table_attrs: attributes for the `<table>` elements. For the attribute names, leading and trailing `_` are
stripped and remaining `_` are transformed to hyphens, so ``HtmlTable(class_='example', data_order='<')``
will generate ``<table class="example" data-order="<"/>``
"""
self.titles = []
self.attrs = []
self.formatters = []
self.table_attrs = table_attrs
self.rows = []
self.row_attrs = []
def column(self, title='', format_spec=None, **attrs):
"""
Adds a column to this table.
Args:
title: The column header as a string (possibly containing an HTML fragment)
format_spec: How to format each column value. This can be either a format string (see :meth:`str.format`),
a format spec (see :func:`format`), a one-argument function or other callable or None (which will
result in ``format(value)``)
**attrs: Attributes to be supplied to each `<td>`
Returns: self
"""
self.titles.append(title)
if format_spec is None:
formatter = format
elif callable(format_spec):
formatter = format_spec
elif isinstance(format_spec, str) and '{' in format_spec:
formatter = format_spec.format
else:
formatter = lambda data: format(data, format_spec)
self.formatters.append(formatter)
self.attrs.append(attrs)
return self
def row(self, row, **row_attrs):
"""
Adds a row to this table's stored data. This data will be used by the zero-argument version of :meth:`format_table`
The rows are exposed as a list via the property `rows`
Args:
row: A tuple (or, in fact, any iterable) of values, prefereably as many as there are rows.
**row_attrs: HTML Attributes for the row. See the note for `__init__`
Returns:
self
"""
if len(row) > len(self.formatters):
raise IndexError('{} values in row, but only {} formatters: {}', len(row), len(self.formatters), row)
self.rows.append(row)
self.row_attrs.append(row_attrs)
return self
@staticmethod
def _build_attrs(attrdict: Dict):
return ''.join(
' {}="{}"'.format(attr.strip('_').replace('-', '_'), escape(value)) for attr, value in attrdict.items())
def _format_column(self, index, data):
attributes = self._build_attrs(self.attrs[index])
content = self.formatters[index](data)
return f'<td{attributes}>{content}</td>'
def _format_row(self, row: Iterable, **rowattrs) -> str:
attributes = self._build_attrs(rowattrs)
return f'<tr{attributes}>' + ''.join(
self._format_column(index, column) for index, column in enumerate(row)) + '</tr>'
def _format_rows(self, rows: Iterable[Iterable]):
for row in rows:
yield self._format_row(row)
def _format_header(self):
column_headers = ''.join('<th>{}</th>'.format(title) for title in self.titles)
return '<table class="pure-table"><thead>{}</thead><tbody>'.format(column_headers)
def _format_footer(self):
return '</tbody></table>'
def format_table(self, rows=None, row_attrs=None):
"""
Actually formats the table.
In the zero-argument form, this uses the rows added previously using the `row` method. Otherwise, the given
data is used.
Args:
rows: If given, the rows to format. This is a list of n-tuples, for n columns.
row_attrs: If given, this should be a list that contains a mapping with the attributes for each row.
Returns:
string containing HTML code for the table
"""
if rows is None:
rows = self.rows
row_attrs = self.row_attrs
if row_attrs is None:
row_attrs = repeat({})
return self._format_header() + ''.join(
(self._format_row(row, **attrs) for row, attrs in zip(rows, row_attrs))) + self._format_footer()
def write_html(filename: Path, content: str, head: str = None, breadcrumbs: List[Dict[str,str]] = [],
graph_id: str = None,
graph_options: Dict[str,object] = dict(controlIconsEnabled=True)) -> None:
"""
Writes a html page.
Args:
filename: out file path
content: formatted contents for the main part
head: heading, will become the last of the breadcrumbs
breadcrumbs: list of dictionaries with keys _caption_ and _link_, first (macrogenesis) and last (head) omitted
graph_id: if present, initialize javascript for graph with the given id
graph_options: if present, options for the svg viewer js
"""
if head is not None:
breadcrumbs = breadcrumbs + [dict(caption=head)]
breadcrumbs = [dict(caption='Makrogenese', link='/macrogenesis')] + breadcrumbs
prefix = """<?php include "../includes/header.php"?>
<section>"""
if graph_id is not None:
require = "requirejs(['faust_common', 'svg-pan-zoom'], function(Faust, svgPanZoom)"
init = f"""
graph = document.getElementById('{graph_id}');
bbox = graph.getBoundingClientRect();
if (bbox.height > (window.innerHeight - bbox.top)) {{
graph.height = window.innerHeight - bbox.top;
graph.width = '100%';
}}
svgPanZoom('#{graph_id}', {json.dumps(graph_options)})
"""
else:
require = "requirejs(['faust_common'], function(Faust)"
init = ''
suffix = f"""</section>
<script type="text/javascript">
{require} {{
document.getElementById('breadcrumbs').appendChild(Faust.createBreadcrumbs({json.dumps(breadcrumbs)}));
{init}
}});
</script>
<?php include "../includes/footer.php"?>"""
with open(filename, 'wt', encoding='utf-8') as f:
f.write(prefix)
f.write(content)
f.write(suffix)
def report_components(graphs: MacrogenesisInfo):
logger.info('Writing component overview to %s', target)
target.mkdir(parents=True, exist_ok=True)
report = f"""<h3>{len(graphs.conflicts)} stark zusammenhängende Komponenten</h3>
<p>Stark zusammenhängende Komponenten sind Teilgraphen, in denen jeder Knoten von
jedem anderen erreichbar ist. Hier ist keine Ordnung möglich, ohne dass Kanten entfernt
werden.</p>
"""
scc_table = _report_subgraphs(graphs.conflicts, target, 'scc-{0:02d}')
report += scc_table.format_table()
wccs = [nx.subgraph(graphs.working, component) for component in nx.weakly_connected_components(graphs.working)]
report += f"""<h3>{len(wccs)} schwach zusammenhängende Komponenten</h3>
<p>Zwischen unterschiedlichen schwach zusammenhängenden Komponenten gibt es keine Verbindungen.</p>"""
wcc_table = _report_subgraphs(wccs, target, 'wcc-{0:02d}')
report += wcc_table.format_table()
write_html(target / 'components.php', report, head="Komponenten")
def _report_subgraphs(removed_edges, out, pattern, breadcrumbs=[], head_pattern='%d'):
table = HtmlTable().column('Nummer', format_spec='<a href="' + pattern + '">{0}</a>') \
.column('Dokumente') \
.column('Relationen') \
.column('Entfernte Relationen') \
.column('Quellen', format_spec=lambda s: ", ".join(map(str, s))) \
.column('Entfernte Quellen', format_spec=lambda s: ", ".join(map(str, s)))
for index, subgraph in enumerate(removed_edges, start=1):
refs = len([node for node in subgraph.nodes if isinstance(node, Reference)])
all_edges = list(subgraph.edges(keys=True, data=True))
removed_edges = [(u, v, k, attr) for (u, v, k, attr) in all_edges if 'delete' in attr and attr['delete']]
relations = [(u, v, k, attr) for (u, v, k, attr) in all_edges if 'delete' not in attr or not attr['delete']]
sources = {attr['source'].citation for u, v, k, attr in relations if 'source' in attr}
conflict_sources = {attr['source'].citation for u, v, k, attr in removed_edges}
table.row((index, refs, len(relations), len(removed_edges), sources, conflict_sources))
write_dot(subgraph, out / (pattern + "-graph.dot").format(index))
write_html(out / (pattern + '.php').format(index),
f"""<object id="refgraph" class="refgraph" type="image/svg+xml"
data="{pattern.format(index)}-graph.svg"></object>\n""",
graph_id='refgraph', breadcrumbs=breadcrumbs, head=head_pattern.format(index))
return table
def write_bibliography_stats(graph: nx.MultiDiGraph):
bibls = defaultdict(Counter)
for u, v, attr in graph.edges(data=True):
if 'source' in attr:
bibls[attr['source'].uri][attr['kind']] += 1
kinds = sorted({str(kind) for bibl in bibls.values() for kind in bibl.keys()})
totals = Counter({ref: sum(types.values()) for ref, types in bibls.items()})
with open('sources.tsv', 'wt', encoding='utf-8') as out:
writer = csv.writer(out, delimiter='\t')
writer.writerow(['Reference', 'Weight', 'Total'] + kinds)
for bibl, total in totals.most_common():
writer.writerow([bibl, BiblSource(bibl).weight, total] + [bibls[bibl][kind] for kind in kinds])
def _fmt_node(node):
if isinstance(node, Reference):
return f'<a href="{node.filename.stem}">{node}</a>'
else:
return format(node)
def _edition_link(ref: Reference):
if isinstance(ref, Witness):
return f'<a href="/document?sigil={ref.sigil_t}">{ref}</a>'
elif isinstance(ref, Inscription):
return f'Inskription {ref.inscription} von {_edition_link(ref.witness)}'
elif isinstance(ref, UnknownRef):
return ''
elif isinstance(ref, AmbiguousRef):
return ", ".join(_edition_link(wit) for wit in ref.witnesses)
else:
return format(ref)
class RefTable(HtmlTable):
def __init__(self, base: nx.MultiDiGraph, **table_attrs):
super().__init__(**table_attrs)
(self.column('Nr.')
.column('Knoten davor')
.column('Objekt', format_spec=_fmt_node)
.column('Typ / Edition', format_spec=_edition_link)
.column('nicht vor', format_spec=lambda d: format(d) if d != EARLIEST else "")
.column('nicht nach', format_spec=lambda d: format(d) if d != LATEST else "")
.column('erster Vers')
.column('Aussagen')
.column('<a href="conflicts">Konflikte</a>'))
self.base = base
def reference(self, ref, index=None, write_subpage=False):
if ref in self.base:
if index is None:
index = self.base.node[ref]['index']
assertions = list(chain(self.base.in_edges(ref, data=True), self.base.out_edges(ref, data=True)))
conflicts = [assertion for assertion in assertions if 'delete' in assertion[2] and assertion[2]['delete']]
self.row((f'<a href="refs#idx{index}">{index}</a>', ref.rank, ref, ref, ref.earliest, ref.latest,
getattr(ref, 'min_verse', ''), len(assertions), len(conflicts)),
id=f'idx{index}', class_=type(ref).__name__)
if write_subpage:
self._last_ref_subpage(DAY, ref)
else:
self.row((index, 0, format(ref), ref, '', '', getattr(ref, 'min_verse', ''), ''),
class_='pure-fade-40', title='Keine Macrogenesedaten', id=f'idx{index}')
def _last_ref_subpage(self, DAY, ref):
"""Writes a subpage for ref, but only if it’s the last witness we just wrote"""
basename = target / ref.filename
relevant_nodes = {ref} | set(self.base.predecessors(ref)) | set(self.base.successors(ref))
if ref.earliest != EARLIEST:
relevant_nodes |= set(nx.shortest_path(self.base, ref.earliest - DAY, ref))
if ref.latest != LATEST:
relevant_nodes |= set(nx.shortest_path(self.base, ref, ref.latest + DAY))
ref_subgraph = self.base.subgraph(relevant_nodes)
write_dot(ref_subgraph, basename.with_name(basename.stem + '-graph.dot'), highlight=ref)
report = f"<!-- {repr(ref)} -->\n"
report += self.format_table(self.rows[-1:])
report += f"""<object id="refgraph" class="refgraph" type="image/svg+xml" data="{basename.with_name(basename.stem+'-graph.svg').name}"></object>\n"""
kinds = {'not_before': 'nicht vor',
'not_after': 'nicht nach',
'from_': 'von',
'to': 'bis',
'when': 'am',
'temp-syn': 'ca. gleichzeitig',
'temp-pre': 'entstanden nach',
'orphan': '(Verweis)',
None: '???'
}
assertionTable = (HtmlTable()
.column('berücksichtigt?')
.column('Aussage')
.column('Bezug', format_spec=_fmt_node)
.column('Quelle')
.column('Kommentare', format_spec="/".join)
.column('XML', format_spec=lambda xml: ":".join(map(str, xml))))
for (u, v, attr) in self.base.in_edges(ref, data=True):
delete_ = 'delete' in attr and attr['delete']
assertionTable.row((f'<a href="{_path_link(u, v).stem}">nein</a>' if delete_ else 'ja',
kinds[attr['kind']],
u + DAY if isinstance(u, date) else u,
attr['source'],
attr.get('comments', []),
attr['xml']),
class_='delete' if delete_ else str(attr['kind']))
kinds['temp-pre'] = 'entstanden vor'
for (u, v, attr) in self.base.out_edges(ref, data=True):
delete_ = 'delete' in attr and attr['delete']
assertionTable.row(('nein' if delete_ else 'ja',
kinds[attr['kind']],
v - DAY if isinstance(v, date) else v,
attr['source'],
attr.get('comments', []),
attr['xml']),
class_='delete' if delete_ else str(attr['kind']))
write_html(basename.with_suffix('.php'), report + assertionTable.format_table(),
breadcrumbs=[dict(caption='Referenzen', link='refs')],
head=str(ref), graph_id='refgraph')
class AssertionTable(HtmlTable):
def __init__(self, **table_attrs):
super().__init__(**table_attrs)
(self.column('berücksichtigt?')
.column('Subjekt', _fmt_node)
.column('Relation', RELATION_LABELS.get)
.column('Objekt', _fmt_node)
.column('Quelle', self._fmt_source)
.column('Kommentare', ' / '.join)
.column('XML', _fmt_xml))
def edge(self, u: Reference, v: Reference, attr: Dict[str,object]):
classes = [attr['kind']] if 'kind' in attr else []
if attr.get('ignore', False): classes.append('ignore')
if attr.get('delete', False): classes.append('delete')
self.row((
f'<a href="{_path_link(u, v)}">nein</a>' if attr.get('delete', False) else \
'ignoriert' if attr.get('ignore', False) else 'ja',
u,
attr['kind'],
v,
attr,
attr.get('comment', ''),
attr.get('xml', '')),
class_=' '.join(classes))
@staticmethod
def _fmt_source(attrs):
source: BiblSource = attrs['source']
result = f'<a href="{source.filename}">{source.citation}</a>'
if 'sources' in attrs:
result += ' ' + '; '.join(s.detail for s in attrs['sources'])
return result
def _fmt_xml(xml: Union[Tuple[str, int], Sequence[Tuple[str, int]]]):
if not xml:
return ""
if isinstance(xml[0], str):
return f"{xml[0].replace('macrogenesis/', '')}: {xml[1]}"
else:
result = []
for file, lines in groupby(xml, itemgetter(0)):
result.append(file.replace('macrogenesis/', '') + ': ' + ", ".join(map(str, map(itemgetter(1), lines))))
return "; ".join(result)
def report_refs(graphs: MacrogenesisInfo):
# Fake dates for when we don’t have any earliest/latest info
target.mkdir(exist_ok=True, parents=True)
nx.write_yaml(simplify_graph(graphs.base), str(target / 'base.yaml'))
nx.write_yaml(simplify_graph(graphs.working), str(target / 'working.yaml'))
nx.write_yaml(simplify_graph(graphs.dag), str(target / 'dag.yaml'))
nx.write_gpickle(graphs.dag, str(target / 'dag.gpickle'))
nx.write_gpickle(graphs.working, str(target / 'working.gpickle'))
nx.write_gpickle(graphs.base, str(target / 'base.gpickle'))
refs = graphs.order_refs()
overview = RefTable(graphs.base)
for index, ref in enumerate(refs, start=1):
overview.reference(ref, index, write_subpage=True)
write_html(target / 'refs.php', overview.format_table(), head="Referenzen")
# write_dot(simplify_graph(graphs.base), str(target / 'base.dot'), record=False)
# write_dot(simplify_graph(graphs.working), str(target / 'working.dot'), record=False)
# write_dot(simplify_graph(graphs.dag), str(target / 'dag.dot'), record=False)
def _invert_mapping(mapping: Mapping) -> Dict:
result = defaultdict(set)
for key, value in mapping.items():
result[value].add(key)
return result
def report_missing(graphs: MacrogenesisInfo):
refs = {node for node in graphs.base.nodes if isinstance(node, Reference)}
all_wits = {wit for wit in Witness.database.values() if isinstance(wit, Witness)}
used_wits = {wit for wit in refs if isinstance(wit, Witness)}
unknown_refs = {wit for wit in refs if isinstance(wit, UnknownRef)}
missing_wits = all_wits - used_wits
inscr = {inscr: inscr.witness for inscr in refs if isinstance(inscr, Inscription)}
wits_with_inscr = _invert_mapping(inscr)
report = f"""
<h2>Fehlende Zeugen</h2>
<p>Für {len(missing_wits)} von insgesamt {len(used_wits)} Zeugen <a href="#missing">liegen keine Makrogenesedaten
vor</a>. Bei {len(missing_wits & wits_with_inscr.keys())} davon sind zumindest Informationen über
Inskriptionen hinterlegt. Umgekehrt gibt es <a href="#unknown">zu {len(unknown_refs)} Referenzen in der
Makrogenese</a> keine Entsprechung in der Edition.</p>
<h3 id="missing">Zeugen ohne Makrogenesedaten</h3>
"""
missing_table = (HtmlTable()
.column('Zeuge ohne Daten', format_spec=_edition_link)
.column('Inskriptionen', format_spec=lambda refs: ", ".join(map(_fmt_node, refs))))
report += missing_table.format_table(
(ref, wits_with_inscr[ref]) for ref in sorted(missing_wits, key=lambda r: r.sort_tuple()))
report += '\n<h3 id="unknown">Unbekannte Referenzen</h3>'
unknown_table = (HtmlTable()
.column('Referenz')
.column('URI'))
report += unknown_table.format_table((ref, ref.uri) for ref in sorted(unknown_refs))
write_html(target / 'missing.php', report, head="Fehlendes")
def _path_link(*nodes) -> Path:
node_names: List[str] = []
for node in nodes:
if isinstance(node, Reference):
node_names.append(node.filename.stem)
elif isinstance(node, date):
node_names.append(node.isoformat())
else:
logger.warning('Unknown node type: %s (%s)', type(node), node)
node_names.append(str(hash(node)))
return Path("--".join(node_names) + '.php')
def _report_conflict(graphs: MacrogenesisInfo, u, v):
reportfile = _path_link(u, v)
graphfile = reportfile.with_name(reportfile.stem + '-graph.dot')
relevant_nodes = {u} | set(graphs.base.predecessors(u)) | set(graphs.base.successors(u)) \
| {v} | set(graphs.base.predecessors(v)) | set(graphs.base.successors(v))
counter_path = []
try:
counter_path = nx.shortest_path(graphs.dag, v, u)
relevant_nodes = set(counter_path)
counter_desc = " → ".join(map(_fmt_node, counter_path))
counter_html = f'<p><strong>Pfad in Gegenrichtung:</strong> {counter_desc}</p>'
except nx.NetworkXNoPath:
counter_html = f'<p>kein Pfad in Gegenrichtung ({_fmt_node(v)} … {_fmt_node(u)}) im Sortiergraphen</p>'
except nx.NodeNotFound:
logger.exception('Node not found!? %s or %s', u, v)
counter_html = ''
subgraph: nx.MultiDiGraph = nx.subgraph(graphs.base, relevant_nodes).copy()
# Highlight conflicting edges, counter path and the two nodes of the conflicting edge(s)
for v1, v2 in [(u, v)] + list(pairwise(counter_path)):
for k, attr in subgraph.get_edge_data(v1, v2).items():
attr['highlight'] = True
subgraph.node[u]['highlight'] = True
subgraph.node[v]['highlight'] = True
write_dot(subgraph, str(target / graphfile))
table = AssertionTable()
for k, attr in graphs.base.get_edge_data(u, v).items():
table.edge(u, v, attr)
write_html(target / reportfile,
f"""
{table.format_table()}
{counter_html}
<object id="refgraph" type="image/svg+xml" data="{graphfile.with_suffix('.svg')}"></object>
""",
graph_id='refgraph',
head=f'Entfernte Kante {u} → {v}', breadcrumbs=[dict(caption="Entfernte Kanten", link='conflicts')])
return reportfile
def report_conflicts(graphs: MacrogenesisInfo):
table = AssertionTable()
removed_edges = [(u, v, k, attr) for (u, v, k, attr) in graphs.base.edges(keys=True, data=True) if
'delete' in attr and attr['delete']]
for index, (u, v, k, attr) in enumerate(sorted(removed_edges, key=lambda t: getattr(t[0], 'index', 0)), start=1):
reportfile = _report_conflict(graphs, u, v)
table.edge(u, v, attr)
write_html(target / 'conflicts.php', table.format_table(), head='entfernte Kanten')
def report_sources(graphs: MacrogenesisInfo):
by_source = defaultdict(list)
for u, v, k, attr in graphs.base.edges(keys=True, data=True):
if 'source' in attr:
by_source[attr['source'].uri].append((u, v, k, attr))
def _fmt_source(uri):
source = BiblSource(uri)
return f'<a href="{source.filename}">{source}</a>'
sources_table = (HtmlTable()
.column('Quelle', format_spec=_fmt_source)
.column('Aussagen')
.column('Zeugen'))
for uri, edges in sorted(by_source.items()):
source = BiblSource(uri)
filename = target / (source.filename + '.php')
graphfile = filename.with_name(filename.stem + '-graph.dot')
logger.info('%d assertions from %s', len(edges), source.citation)
# subgraph = graphs.base.edge_subgraph([(u,v,k) for u,v,k,attr in edges])
subgraph = graphs.base.subgraph({u for u, v, k, attr in edges} | {v for u, v, k, attr in edges})
write_dot(subgraph, graphfile)
sources_table.row((uri, len(edges), len([node for node in subgraph.nodes if isinstance(node, Reference)])))
current_table = AssertionTable()
for u, v, k, attr in edges:
current_table.edge(u, v, attr)
write_html(target / (source.filename + '.php'),
f"""<object id="refgraph" type="image/svg+xml" data="{graphfile.with_suffix('.svg').name}"></object>
{current_table.format_table()}""",
graph_id='refgraph',
breadcrumbs=[dict(caption='Quellen', link='sources')],
head=source.citation)
write_html(target / 'sources.php', sources_table.format_table(), head='Quellen')
def report_index(graphs):
report = f"""
<p>
Dieser Bereich der Edition enthält experimentelle Informationen zur Makrogenese, er wurde zuletzt
am {date.today()} generiert.
</p>
<section class="center pure-g-r">
<div class="pure-u-1-5"></div>
<article class="pure-u-3-5 pure-center">
<p>
<a href="refs" class="pure-button pure-button-tile">Zeugen</a>
<a href="conflicts" class="pure-button pure-button-tile">entfernte Relationen</a>
<a href="components" class="pure-button pure-button-tile">Komponenten</a>
<a href="missing" class="pure-button pure-button-tile">Fehlendes</a>
<a href="sources" class="pure-button pure-button-tile">Quellen</a>
<a href="dag" class="pure-button pure-button-tile">sortierrelevanter Gesamtgraph</a>
<a href="tred" class="pure-button pure-button-tile">transitive Reduktion</a>
<a href="help" class="pure-button pure-button-tile">Legende</a>
</p>
</article>
<div class="pure-u-1-5"></div>
</section>
"""
write_html(target / "index.php", report)
logger.info('Writing DAG ...')
write_dot(graphs.dag, target / 'dag-graph.dot', record=True)
write_html(target / 'dag.php', '<object type="image/svg+xml" data="dag-graph.svg" id="refgraph"/>',
graph_id='refgraph', head='Effektiver Gesamtgraph (ohne Konflikte)',
graph_options=dict(controlIconsEnabled=True, maxZoom=200))
logger.info('Creating transitive reduction ...')
tred_base = nx.MultiDiGraph(nx.transitive_reduction(graphs.dag))
tred = nx.edge_subgraph(graphs.dag, tred_base.edges)
write_dot(tred, target / 'tred-graph.dot', record=True)
write_html(target / 'tred.php', '<object type="image/svg+xml" data="tred-graph.svg" id="refgraph"/>',
graph_id='refgraph', head='Transitive Reduktion',
graph_options=dict(controlIconsEnabled=True, maxZoom=200))
def report_help():
def demo_graph(u, v, extend=None, **edge_attr) -> nx.MultiDiGraph:
G = nx.MultiDiGraph() if extend is None else extend.copy()
G.add_edge(u, v, **edge_attr)
return G
w1 = Witness({'uri': 'faust://help/wit1', 'sigil': 'Zeuge 1', 'sigil_t': 'Zeuge_1'})
w2 = Witness({'uri': 'faust://help/wit2', 'sigil': 'Zeuge 2', 'sigil_t': 'Zeuge_2'})
d1 = date(1799, 1, 17)
d2 = date(1799, 2, 5)
d3 = date(1799, 1, 28)
g1 = demo_graph(w1, w2, kind='temp-pre', label='Quelle 1')
g1a = g1.copy()
g1a.add_edge(w2, w1, kind='temp-pre', delete=True, label='Quelle 2')
g1a.add_edge(w2, w1, kind='temp-pre', ignore=True, label='Quelle 3')
g2 = demo_graph(w1, w2, kind='temp-syn', label='Quelle 2')
g3 = demo_graph(d1 - DAY, w1, kind='not_before', source='Quelle 1')
g3.add_edge(w1, d2 + DAY, kind='not_after', source='Quelle 1')
g4 = demo_graph(d1 - DAY, w1, kind='from_', source='Quelle 2')
g4.add_edge(w1, d2 + DAY, kind='to_', source='Quelle 2')
g5 = demo_graph(d3 - DAY, w2, kind='when', source='Quelle 2')
g5.add_edge(w2, d3 + DAY, kind='when', source='Quelle 2')
i1 = Witness.get('faust://inscription/faustedition/2_IV_H.19/i_uebrige')
i1w = i1.witness
g_orphan = demo_graph(i1, i1w, kind='orphan', source='faust://orphan/adoption')
help_graphs = dict(pre=g1, conflict=g1a, syn=g2, dating=g3, interval=g4, when=g5, orphan=g_orphan)
for name, graph in help_graphs.items():
write_dot(graph, str(target / f'help-{name}.dot'))
report = f"""
<p>Die Graphen repräsentieren Aussagen aus der Literatur zu zeitlichen Verhältnissen von Zeugen.</p>
<p>Die <strong>Zeugen</strong> sind durch Ovale mit der Sigle oder Inskriptionsbezeichnung repräsentiert.
Zeugen, die nicht in der Edition sind, steht <code>siglentyp: </code> voran. Die Zeugenbezeichnungen
sind klickbar und führen zur Makrogeneseseite des entsprechenden Zeugen. Verwenden Sie den Link in der
Spalte <em>Edition</em> der oberen Tabelle, um zur Darstellung des Zeugen in der Dokumentenansicht zu
gelangen.</p>
<p><strong>Pfeile</strong> bedeuten immer <em>zeitlich vor</em>. Im Vergleich zu termini a quo bzw. ad quem
sind die Datumsangaben deshalb um einen Tag versetzt.</p>
<table class="pure-table">
<thead><th>Graph</th><th>Bedeutung</th></thead>
<tbody>
<tr><td><img src="help-pre.svg" /></td>
<td>Laut Quelle 1 entstand {w1} vor {w2}</td></tr>
<tr><td><img src="help-conflict.svg" /></td>
<td>Laut Quelle 1 entstand {w1} vor {w2},
laut Quelle 2 entstand {w2} vor {w1}.
Diese Aussage von Quelle 2 wird nicht berücksichtigt.
Die Aussage von Quelle Quelle 3 wird von vornherein ignoriert.</td></tr>
<tr><td><img src="help-syn.svg"/></td>
<td>Laut Quelle 2 entstand {w1} etwa zeitgleich zu {w2}.</td></tr>
<tr><td><img src="help-dating.svg"/></td>
<td>Laut Quelle 1 entstand {w1} nicht vor {d1} und nicht nach {d2}.
Der gepunktete Pfeil repräsentiert den Zeitstrahl, er führt immer zum nächsten im Graph repräsentierten Datum.
</td></tr>
<tr><td><img src="help-interval.svg"/></td>
<td>Laut Quelle 2 wurde vom {d1} bis zum {d2} an {w1} gearbeitet.</td></tr>
<tr><td><img src="help-when.svg"/></td>
<td>Laut Quelle 2 entstand {w2} am {d3}.</td></tr>
<tr><td><img src="help-orphan.svg"/></td>
<td>{i1w} wird in den Makrogenesedaten nur indirekt über {i1} referenziert
und über eine künstliche Kante angebunden.</td></tr>
</tbody>
</table>
"""
write_html(target / 'help.php', report, 'Legende')
def _yearlabel(ref: Reference):
earliest_year = ref.earliest.year
latest_year = ref.latest.year
if earliest_year == latest_year:
return str(earliest_year)
else:
sep = " … "
result = ""
if ref.earliest != EARLIEST:
result += str(earliest_year)
result += sep
if ref.latest != LATEST:
result += str(latest_year)
return result if result != sep else ""
class ByScene:
def __init__(self, graphs: MacrogenesisInfo):
scene_xml = etree.parse('scenes.xml')
self.scenes = scene_xml.xpath('//f:scene[@first-verse]', namespaces=faust.namespaces)
bargraph_info = requests.get('http://dev.digital-humanities.de/ci/job/faust-gen-fast/lastSuccessfulBuild/artifact/target/www/data/genetic_bar_graph.json').json()
self.intervals = {Witness.get('faust://document/faustedition/' + doc['sigil_t']): doc['intervals'] for doc in bargraph_info}
self.ordering = list(enumerate(graphs.order_refs()))
self.graphs = graphs
def report(self):
sceneTable = (HtmlTable()
.column('#')
.column('Szene')
.column('Verse', format_spec=lambda t: '{} – {}'.format(*t))
.column('Zeugen'))
for scene in self.scenes:
witnessTable = RefTable(self.graphs.base)
title = scene.xpath('f:title/text()', namespaces=faust.namespaces)[0]
start, end = int(scene.get('first-verse')), int(scene.get('last-verse'))
scene_wits = [(index, wit) for index, wit in self.ordering if self.relevant(wit, start, end)]
for index, witness in scene_wits:
witnessTable.reference(witness, index)
scene_wits = {wit for _, wit in scene_wits}
scene_nodes = scene_wits | {node for wit in scene_wits if wit in self.graphs.base
for node in chain(self.graphs.base.predecessors(wit),
self.graphs.base.successors(wit))
if isinstance(node, date)}
scene_subgraph = self.graphs.base.subgraph(scene_nodes)
basename = 'scene_' + scene.get('n').replace('.', '-')
subgraph_page = Path(basename + '-subgraph.php')
graph_name = Path(basename + '-graph.dot')
sceneTable.row((scene.get('n'), f'<a href="{basename}">{title}</a>', (start, end), len(scene_wits)))
write_dot(scene_subgraph, target / graph_name)
write_html(target / subgraph_page,
f"""<object id="refgraph" type="image/svg+xml" data="{graph_name.with_suffix('.svg')}"></object>""",
graph_id='refgraph',
head="Szenengraph", breadcrumbs=[dict(caption='nach Szene', link='scenes'),
dict(caption=title, link=basename)])
write_html(target / (basename + '.php'),
f"""
<p><a href="{subgraph_page.stem}">Szenengraph</a> ·
<a href="/genesis_bargraph?rangeStart={start}&rangeEnd={end}">Balkendiagramm</a></p>
{witnessTable.format_table()}""",
head=title, breadcrumbs=[dict(caption='nach Szene', link='scenes')])
write_html(target / "scenes.php", sceneTable.format_table(), head='nach Szene')
def relevant(self, witness: Reference, first_verse: int, last_verse: int) -> bool:
try:
for interval in self.intervals[witness]:
if first_verse <= interval['start'] <= last_verse or \
first_verse <= interval['end'] <= last_verse or \
interval['start'] <= first_verse <= interval['end'] or \
interval['start'] <= last_verse <= interval['end']:
return True
except KeyError:
return False
return False
def report_scenes(graphs):
ByScene(graphs).report()
def write_order_xml(graphs):
F = ElementMaker(namespace='http://www.faustedition.net/ns', nsmap=faust.namespaces)
root = F.order(
Comment('This file has been generated from the macrogenesis data. Do not edit.'),
*[F.item(format(witness),
index=format(index),
uri=witness.uri,
sigil_t=witness.sigil_t,
earliest=witness.earliest.isoformat() if witness.earliest != EARLIEST else '',
latest=witness.latest.isoformat() if witness.latest != LATEST else '',
yearlabel=_yearlabel(witness))
for index, witness in enumerate(graphs.order_refs(), start=1)
if isinstance(witness, Witness)],
generated=datetime.now().isoformat())
target.mkdir(parents=True, exist_ok=True)
root.getroottree().write(str(target / 'order.xml'), pretty_print=True)