Skip to content

Commit

Permalink
Added group (~ act) info to timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed May 20, 2019
1 parent 279e660 commit ec8d04e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
27 changes: 19 additions & 8 deletions src/macrogen/report.py
Expand Up @@ -2,6 +2,7 @@
import json
import os
import urllib
import re
from collections import defaultdict, Counter
from datetime import date, datetime
from html import escape
Expand Down Expand Up @@ -1214,19 +1215,29 @@ def _dating_table():
def report_timeline(graphs: MacrogenesisInfo):
witinfo = WitInscrInfo.get()

def rel_scenes(ref: Reference) -> List[str]:
def ref_info(ref) -> dict:
result = dict(start=ref.earliest.isoformat(), end=(ref.latest+DAY).isoformat(),
content=_fmt_node(ref), id=ref.filename.stem, index=graphs.index[ref])
info = witinfo.get().by_uri.get(ref.uri, None)
if info:
return sorted([scene.n for scene in info.max_scenes])
result['scenes'] = sorted([scene.n for scene in info.max_scenes])
result['groups'] = sorted(info.groups)
result['group'] = info.group
else:
return []
result['scenes'] = []
result['groups'] = []
result['group'] = None
return result

refs = graphs.order_refs()
data = [dict(start=ref.earliest.isoformat(), end=(ref.latest+DAY).isoformat(),
content=_fmt_node(ref), id=ref.filename.stem, scenes=rel_scenes(ref),
index=graphs.index[ref])
for ref in refs
if ref.earliest > EARLIEST and ref.latest < LATEST]
data = list() # FIXME list comprehension after #25 is resolved
known = set()
for ref in refs:
if ref.earliest > EARLIEST and ref.latest < LATEST:
info = ref_info(ref)
if info['id'] not in known:
data.append(info)
known.add(info['id'])
with (config.path.report_dir / 'timeline.json').open("wt") as data_out:
json.dump(data, data_out)
(config.path.report_dir / 'timeline.html').write_bytes(pkg_resources.resource_string('macrogen', 'timeline.html'))
Expand Down
29 changes: 23 additions & 6 deletions src/macrogen/timeline.html
Expand Up @@ -54,13 +54,18 @@
</div>

<script>
requirejs(['faust_common', 'timeline', 'jquery', 'json'],
function(Faust, timeline, $) {
requirejs(['faust_common', 'timeline', 'jquery', 'data/scene_line_mapping'],
function(Faust, timeline, $, sceneLineMapping) {
var arraysEqual = function(first, second) {
return (first.length === second.length) &&
first.every((item, index) => item === second[index]);
}

var scene_names = {};
for (const scene of sceneLineMapping)
scene_names[scene.id] = scene.title;


Faust.xhr.get('timeline.json')
.then((response) => {
let container = document.getElementById('timeline'),
Expand All @@ -79,7 +84,7 @@
if (item.isCluster) {
result = '<span class="cluster"><strong>' + item.items.length + '</strong> '
if (item.items[0].scenes.length > 0) {
result += ' aus ' + item.items[0].scenes[0];
result += ' aus ' + scene_names[item.items[0].scenes[0]];
}
result += '</span>';
} else {
Expand All @@ -88,12 +93,24 @@
return result;
},
cluster: {
maxItems: 3,
clusterCriteria: (first, second) => arraysEqual(first.scenes, second.scenes)
maxItems: 2,
// clusterCriteria: (first, second) => arraysEqual(first.scenes, second.scenes)
}
};
data.add(raw_data);
let tl = new timeline.Timeline(container, data, options);
groups = [ {id:'1',content:'Faust I',nestedGroups:['1.0.1','1.0.2','1.0.3','1.1']},
{id:'2',content:'Faust II',nestedGroups:['2.1','2.2','2.3','2.4','2.5']},
{id:'1.0.1',content:'Zueignung'},
{id:'1.0.2',content:'Vorspiel'},
{id:'1.0.3',content:'Prolog'},
{id:'1.1',content:'Faust I'},
{id:'2.1',content:'I. Akt'},
{id:'2.2', content:'II. Akt'},
{id:'2.3', content:'III. Akt'},
{id:'2.4', content:'IV. Akt'},
{id:'2.5', content:'V. Akt'}];

let tl = new timeline.Timeline(container, data, groups, options);
let params = Faust.url.getParameters();
if (params.focus) {
let items = params.focus.split(/,[+ ]*/);
Expand Down
7 changes: 6 additions & 1 deletion src/macrogen/witnesses.py
@@ -1,5 +1,5 @@
import re
from collections import defaultdict
from collections import defaultdict, Counter
from itertools import chain
from pathlib import Path
from typing import List, Optional, Dict, Union, Set
Expand Down Expand Up @@ -177,6 +177,7 @@ def _add_recursive(target: List, items: List, predicate):


class IntervalsMixin:
_group_re = re.compile(r"^(1\.0\.\d|\d\.\d).*")

def is_relevant_for(self, first: int, last: int):
assert isinstance(first, int) and isinstance(last, int), f"first ({first!r}) and last ({last!r}) must be ints!"
Expand All @@ -194,6 +195,8 @@ def _init_relevant_scenes(self):
relevant_scenes.add(scene)
self.relevant_scenes = frozenset(relevant_scenes)
self.max_scenes = self._reduce_scenes(relevant_scenes)
self.groups = {self._group_re.sub(r'\1', scene.n) for scene in self.relevant_scenes}
self.group = Counter(self.groups).most_common(1)[0][0] if self.relevant_scenes else None

@staticmethod
def _reduce_scenes(scenes: Set[Scene]) -> Set[Scene]:
Expand Down Expand Up @@ -239,6 +242,7 @@ def __init__(self):
self.documents = [DocumentCoverage(doc) for doc in bargraph]
self.by_scene: Dict[Scene, Union[InscriptionCoverage, DocumentCoverage]] = defaultdict(list)
self.by_uri: Dict[str, Union[InscriptionCoverage, DocumentCoverage]] = dict()
self.by_group: Dict[str, Union[InscriptionCoverage, DocumentCoverage]] = defaultdict(list)
for doc in config.progress(self.documents, desc='Analyzing documents', unit=' docs'):
self.by_uri[doc.uri] = doc
for inscription in doc.inscriptions:
Expand All @@ -247,6 +251,7 @@ def __init__(self):
self.by_scene[scene].append(inscription)
for scene in doc.relevant_scenes:
self.by_scene[scene].append(doc)
self.by_group[doc.group].append(doc)

_instance: 'WitInscrInfo' = None

Expand Down

0 comments on commit ec8d04e

Please sign in to comment.