-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
260 lines (225 loc) · 6.88 KB
/
classes.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
from base64 import b64encode
from dataclasses import dataclass, asdict, field
from abc import ABC
from typing import Optional
from uuid import uuid4
from jsonschema import validate
from json import loads
from pathlib import Path
current_dir = Path(__file__).parent
with open(Path(current_dir, 'microreact_project_schema_v1.json'), 'r') as schema_file:
MR_PROJECT_SCHEMA = loads(schema_file.read())
def validate_json(json_dict:dict):
return validate(json_dict, MR_PROJECT_SCHEMA)
@dataclass
class Meta:
name: str
# description: str
def to_dict(self):
return {'name': self.name}
class Element(ABC):
"""
Subclasses of this class are only used for creating NEW elements (not yet in Microreact).
__post_init__ in all subclasses must call super().set_id().
"""
id: str=''
def set_id(self):
if self.id == '':
self.id = str(uuid4())
return self.id
@dataclass
class Dataset(Element):
file: str
idFieldName: str
def __post_init__(self):
super().set_id()
def to_dict(self):
return asdict(self)
@dataclass
class File(Element):
type: str
body: Optional[str] = None
url: Optional[str] = None
name: Optional[str] = ''
format: Optional[str] = ''
mimetype: Optional[str] = ''
def __post_init__(self):
super().set_id()
if self.type == 'data':
if self.name == '':
self.name = 'metadata.csv'
if self.format == '':
self.format = 'text/csv'
if self.mimetype == '':
self.mimetype = 'data:application/vnd.ms-excel;base64'
elif self.type == 'tree':
if self.name == '':
self.name = 'tree.nwk'
if self.format == '':
self.format = 'text/x-nh'
if self.mimetype == '':
self.mimetype = 'data:application/octet-stream;base64'
else:
raise ValueError("Invalid file type: " + type)
def to_dict(self):
if self.body:
blob = b64encode(self.body.encode('utf-8'))
blob_str = str(blob)
blob_str = blob_str[2:-1]
return {
"id": self.id,
"type": self.type,
"name": self.name,
"format": self.format,
"blob": self.mimetype + ',' + blob_str
}
elif self.url:
return {
"id": self.id,
"type": self.type,
"name": self.name,
"format": self.format,
"url": self.url
}
else:
raise ValueError("File object must contain either body or url")
@dataclass
class Column:
field: str
fixed: bool
@dataclass
class Table(Element):
title: str
columns: list
file: str
dataset: str # TODO Should this be optional? There might be cases in my code where I don't use it
hidden: list
def __post_init__(self):
if self.hidden is None:
self.hidden = list()
super().set_id()
def get_col_list(self):
col_list = list()
for column in self.columns:
col_list.append({
"field": column,
"fixed": False,
"hidden": True if column in self.hidden else False
},)
return col_list
def to_dict(self):
return {
'title': self.title,
'id': self.id,
'columns': self.get_col_list(),
'file': self.file
}
@dataclass
class Matrix(Element):
file: str
title: str = "Matrix"
def __post_init__(self):
super().set_id()
def to_dict(self):
return {
'file': self.file,
'title': self.title,
'id': self.id
}
@dataclass
class Timeline(Element):
bounds: None
controls: False
nodeSize: 14
playing: False
speed: 1
laneField: None
unit: None
viewport: None
style: str = "bar"
title: str = "Timeline"
dataType: str = "year-month-day"
yearField: str = "Year"
def __post_init__(self):
super().set_id()
@dataclass
class Tree(Element):
file: File
type: str = "rc"
title: str = "Tree"
labelField: str = "ID"
highlightedId: str = None
def __post_init__(self):
super().set_id()
def to_dict(self):
return {
"id": self.id,
"file": self.file,
"type": self.type,
"title": self.title,
"labelField": self.labelField,
"highligthedId": self.highlightedId,
"showBranchLengths": True,
"branchLengthsDigits": 0,
"showLeafLabels": True,
"labelField": self.labelField,
}
@dataclass
class Project:
"""Main class for structuring data that will be sent to Microreact when creating a new project.
Use the built-in asdict method to convert data to a dict which in turn can be converted to JSON."""
meta: Meta
datasets: list
files: list
tables: list
trees: list
matrices: list = field(default_factory=list)
"""These element types are not necessary for a basic project;
however, empty lists must be present for the schema to validate."""
timelines: list = field(default_factory=list)
charts: list = field(default_factory=list)
filters: list = field(default_factory=list)
maps: list = field(default_factory=list)
networks: list = field(default_factory=list)
notes: list = field(default_factory=list)
panes: list = field(default_factory=list)
slicers: list = field(default_factory=list)
styles: list = field(default_factory=list)
timelines: list = field(default_factory=list)
views: list = field(default_factory=list)
# schema_url is not actively used for anything; it is just a reference.
schema_url: str="https://microreact.org/schema/v1.json"
def get_sections(self):
return [
'datasets',
'files',
'tables',
'trees',
'timelines',
'charts',
'filters',
'maps',
'networks',
'notes',
'panes',
'slicers',
'styles',
'timelines',
'views',
'matrices'
]
def dictify_section(self, section_name: str):
section_dict = dict()
for element in getattr(self, section_name):
assert getattr(element, 'id') not in section_dict # Make sure ids are not duplicated
section_dict[element.id] = element.to_dict()
return section_dict
def to_dict(self):
output_dict = {
'schema': self.schema_url,
'meta': self.meta.to_dict()
}
for section in self.get_sections():
output_dict[section] = self.dictify_section(section)
validate_json(output_dict)
return output_dict