forked from apdavison/fairgraph
-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_openminds.py
955 lines (896 loc) · 38.8 KB
/
update_openminds.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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
import argparse
from collections import defaultdict
from glob import glob
from itertools import chain
import json
import re
import os
import shutil
import subprocess
import sys
from jinja2 import Environment, select_autoescape, FileSystemLoader
name_map = {
"scope": "model_scope", # this is because 'scope' is already a keyword
# we could rename the 'scope' keyword to 'stage'
# but we would have the same problem, as there is
# a property named 'stage'
# Suggested resolution: rename the property "scope" in openMINDS to "hasScope"
}
global_aliases = {
"short_name": "alias",
"full_name": "name",
"has_versions": "versions",
"has_entity": "entities",
"hashes": "hash"
}
reverse_name_map = {
"RRID": "identifies",
"about": {
"https://openminds.ebrains.eu/publications/LearningResource": "learningResources",
"https://openminds.ebrains.eu/core/Comment": "comments",
"https://openminds.ebrains.eu/publications/LivePaperVersion": "publication",
},
"abstractionLevel": "isAbstractionLevelOf",
"accessibility": "isAccessibilityOf",
"addExistingTerminology": "suggestedIn",
"ageCategory": "isAgeCategoryOf",
"anatomicalAxesOrientation": "isOrientationOf",
"anatomicalLocation": "isLocationOf",
"anatomicalLocationOfArray": "isLocationOf",
"anatomicalLocationOfElectrodes": "isLocationOf",
"anatomicalTarget": "isTargetOf",
"applicationCategory": "appliesTo",
"associatedAccount": "belongsTo",
"attribute": "isAttributeOf",
"author": "authored",
"backgroundStrain": "isBackgroundStrainOf",
"behavioralProtocol": "usedIn",
"biologicalSex": "isBiologicalSexOf",
"breedingType": "isBreedingTypeOf",
"chemicalProduct": "usedInAmount",
"citedPublication": "citedIn",
"commenter": "comments",
"components": "isComponentOf", # or "is_part_of" (TODO "components" redundant with "hasComponent"?)
"conductorMaterial": "isConductorOf",
"configuration": "isConfigurationOf",
"constructionType": "usedFor",
"contactInformation": "isContactInformationOf",
"contentType": "isDefinedBy",
"contentTypePattern": "identifiesContentOf",
"contributor": "contribution",
"coordinateSpace": "isCoordinateSpaceOf",
"coordinator": "coordinatedProjects",
"copyOf": "hasCopies",
"criteria": "basedOnProtocolExecution",
"criteriaQualityType": "usedByAnnotation", # or "isCriteriaQualityTypeOf"
"criteriaType": "usedByAnnotation",
"cultureMedium": "usedIn",
"cultureType": "isTypeOf",
"custodian": "isCustodianOf",
"dataLocation": "isLocationOf",
"dataType": "isDataTypeOf",
"defaultImage": "isDefaultImageFor",
"definedIn": "defines", # or "containsDefinitionOf",
"deliveredBy": "stimulationDevice",
"descendedFrom": "hasChildren", # equivalent to "hasParent" ?
"describedIn": "describes",
"developer": "developed",
"device": {
"https://openminds.ebrains.eu/ephys/ElectrodeArrayUsage": "usage",
"https://openminds.ebrains.eu/ephys/RecordingActivity": "usedIn",
"https://openminds.ebrains.eu/ephys/ElectrodePlacement": "placedBy",
"https://openminds.ebrains.eu/ephys/CellPatching": "usedIn",
"https://openminds.ebrains.eu/ephys/Recording": "usedFor",
"https://openminds.ebrains.eu/core/Measurement": "usedFor",
"https://openminds.ebrains.eu/specimenPrep/TissueSampleSlicing": "usedFor",
"https://openminds.ebrains.eu/specimenPrep/SlicingDeviceUsage": "usage",
"https://openminds.ebrains.eu/ephys/PipetteUsage": "usage",
"https://openminds.ebrains.eu/ephys/ElectrodeUsage": "usage",
},
"deviceType": "isTypeOf", # TODO: replace with "type"?
"digitalIdentifier": "identifies",
"diseaseModel": "isModeledBy",
"editor": "edited",
"educationalLevel": "appliesTo",
"environment": "usedFor", # or "isEnvironmentOf"
"environmentVariable": "definesEnvironmentOf",
"ethicsAssessment": "appliesTo",
"experimentalApproach": "usedIn",
"feature": "characterizes",
"fileRepository": "files",
"format": "isFormatOf",
"fullDocumentation": "fullyDocuments",
"funder": "funded",
"funding": "funded",
"generatedBy": "generationDevice",
"geneticStrainType": "isGeneticStrainTypeOf", # or "strain"
"groupedBy": "isUsedToGroup",
"groupingType": {
"https://openminds.ebrains.eu/core/FileBundle": "isUsedToGroup",
"https://openminds.ebrains.eu/core/FilePathPattern": "isDefinedBy",
},
"handedness": "subjectStates", # or "isHandednessOf"
"hardware": "usedBy", # or "isPartOfEnvironment"
"hasComponent": "isPartOf",
"hasEntity": "isPartOf",
"hasParent": "hasChildren", # equivalent to "descendedFrom"
"hasPart": "isPartOf",
"hasVersion": "isVersionOf",
"holder": "holdsCopyright", # or "intellectualProperty",
"hostedBy": "hosts",
"inRelationTo": "assessment", # equivalent to "about" ?
"input": "isInputTo",
"inputData": "isInputTo", # could use just "input" ?
"inputFormat": "isInputFormatOf",
"inspiredBy": "inspired",
"insulatorMaterial": "composes",
"isAlternativeVersionOf": "isAlternativeVersionOf", # ??!!
"isNewVersionOf": "isOldVersionOf",
"isPartOf": "hasParts", # hasComponent ?
"keyword": "describes",
"labelingCompound": "labels",
"language": "usedIn",
"laterality": "isLateralityOf",
"launchConfiguration": "isLaunchConfigurationOf",
"license": "isAppliedTo", # or "governsSharingOf"
"manufacturer": "manufactured", # or "product"
"material": "composes",
"measuredQuantity": "measurement",
"measuredWith": "usedToMeasure",
"memberOf": "hasMembers",
"metadataLocation": "describes",
"minValueUnit": "range",
"maxValueUnit": "range",
"molecularEntity": "composes",
"nativeUnit": "usedBy",
"operatingSystem": "usedBy",
"origin": "sample",
"output": "isOutputOf", # or "generatedBy"
"outputData": "isOutputOf", # replace with "output"?
"outputFormat": "isOutputFormatOf",
"owner": "isOwnerOf", # or "devices"
"pathology": "specimenState",
"performedBy": "activities",
"pipetteSolution": "usedIn",
"preferredDisplayColor": "preferredBy",
"preparationDesign": "usedFor",
"previewImage": "isPreviewOf",
"previousRecording": "nextRecording",
"productSource": "isSourceOf",
"programmingLanguage": "usedIn",
"protocol": "usedIn",
"provider": "isProviderOf", # or "provided",
"publisher": "published",
"qualitativeOverlap": "assessment",
"recipe": "defined", # or "defines"
"recordedWith": "usedToRecord",
"referenceData": "isReferenceFor",
"referenceDataAcquisition": "isReferenceFor",
"reinforcementType": "usedFor",
"relatedPublication": "relatedTo",
"relatedUBERONTerm": "defines",
"relevantFor": "hasProperties",
"repository": "containsContentOf",
"scope": "isScopeOf",
"scoreType": "isScoreTypeOf",
"serializationFormat": "usedBy",
"service": {
"https://openminds.ebrains.eu/core/ServiceLink": "linkedFrom",
"https://openminds.ebrains.eu/core/AccountInformation": "hasAccounts",
},
"setup": "usedIn",
"slicingDevice": "usedIn", # TODO: slicingDevice --> device?
"slicingPlane": "usedIn",
"software": "usedIn",
"sourceData": "isSourceDataOf",
"specialUsageRole": "file",
"species": "isSpeciesOf",
"specification": "specifies",
"specificationFormat": "isSpecificationFormatOf",
"stage": "isPartOf",
"startedBy": "started",
"status": "isStatusOf",
"stimulation": "usedIn",
"stimulus": "isStimulusFor",
"stimulusType": "usedIn",
"structurePattern": "structures",
"studiedSpecimen": "hasStudyResultsIn", # "isPartOfStudy"
"studiedState": "isStateOf",
"studyTarget": "studiedIn",
"targetIdentificationType": "isTypeOf",
"technique": "usedIn",
"tissueBathSolution": "usedIn",
"type": "isTypeOf",
"typeOfUncertainty": "value",
"unit": {
"https://openminds.ebrains.eu/ephys/Channel": "usedIn",
"https://openminds.ebrains.eu/core/QuantitativeValue": "value",
"https://openminds.ebrains.eu/core/QuantitativeValueArray": "value",
},
"usedSpecies": "commonCoordinateSpace",
"usedSpecimen": "usedIn",
"variation": "usedIn",
"vendor": "stocks",
"wasInformedBy": "informed",
}
number_names = {
"0": "zero",
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine"
}
def generate_python_name(json_name):
if json_name in name_map:
python_name = name_map[json_name]
else:
python_name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", json_name.strip())
python_name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", python_name).lower()
replacements = [
("-", "_"), (".", "_"), ("+", "plus"), ("#", "sharp"), (",", "comma"), ("(", ""), (")", "")
]
for before, after in replacements:
python_name = python_name.replace(before, after)
if python_name[0] in number_names: # Python variables can't start with a number
python_name = number_names[python_name[0]] + python_name[1:]
if not python_name.isidentifier():
raise NameError(f"Cannot generate a valid Python name from '{json_name}'")
return python_name
def generate_doc(prop, obj_title):
if obj_title.upper() == obj_title: # for acronyms, e.g. DOI
obj_title_readable = obj_title
elif "UBERON" in obj_title:
obj_title_readable = obj_title
else:
obj_title_readable = re.sub("([A-Z])", " \g<0>", obj_title).strip().lower()
doc = prop.get("description", "no description available")
doc = doc.replace("someone or something", f"the {obj_title_readable}")
doc = doc.replace("something or somebody", f"the {obj_title_readable}")
doc = doc.replace("something or someone", f"the {obj_title_readable}")
doc = doc.replace("a being or thing", f"the {obj_title_readable}")
return doc
def invert_dict(D):
newD = {}
for key, values in D.items():
for value in values:
newD[value] = key
return newD
DEFAULT_SPACES = {
"chemicals": {"default": "dataset"},
"core": invert_dict(
{
"common": [
"Affiliation",
"Comment",
"Configuration",
"Consortium",
"Funding",
"GRIDID",
"HANDLE",
"HardwareSystem",
"IdentifiersDotOrgID",
"ORCID",
"Organization",
"Person",
"Project",
"Query",
"RORID",
"TermSuggestion",
"WebResource",
"RRID",
"AccountInformation", # or does this go in "restricted"?
],
"files": [
"ContentTypePattern",
"File",
"FileBundle",
"FilePathPattern",
"FileRepositoryStructure",
"Hash",
],
"dataset": [
"Contribution",
"Copyright",
"DOI",
"Dataset",
"DatasetVersion",
"FileArchive",
"FileRepository",
"ISBN",
"ISSN",
"NumericalParameter",
"ParameterSet",
"PropertyValueList",
"Protocol",
"ExperimentalActivity",
"ProtocolExecution",
"QuantitativeValue",
"QuantitativeValueRange",
"QuantitativeValueArray",
"ResearchProductGroup",
"ServiceLink",
"StringParameter",
"Subject",
"SubjectGroup",
"SubjectGroupState",
"SubjectState",
"TissueSample",
"TissueSampleCollection",
"TissueSampleCollectionState",
"TissueSampleState",
"BehavioralProtocol",
"Stimulation",
"Strain",
"Setup",
],
"model": ["Model", "ModelVersion"],
"software": ["SWHID", "Software", "SoftwareVersion"],
"restricted": ["ContactInformation"],
"metadatamodel": ["MetaDataModel", "MetaDataModelVersion"],
"controlled": ["License", "ContentType"],
"webservice": ["WebService", "WebServiceVersion"],
}
),
"computation": {"default": "computation"},
"controlled_terms": {"default": "controlled"},
"sands": invert_dict(
{
"spatial": [
"AnatomicalEntity",
"Annotation",
"CoordinatePoint",
"CustomAnatomicalEntity",
"CustomAnnotation",
"CustomCoordinateSpace",
"Image",
"QualitativeRelationAssessment",
"QuantitativeRelationAssessment",
],
"atlas": [
"AnatomicalTargetPosition",
"AtlasAnnotation",
"BrainAtlas",
"BrainAtlasVersion",
"Circle",
"ColorMap",
"CommonCoordinateSpace",
"CommonCoordinateSpaceVersion",
"CoordinatePoint",
"Ellipse",
"ParcellationEntity",
"ParcellationTerminology",
"ParcellationTerminologyVersion",
"ParcellationEntityVersion",
"Rectangle",
"SingleColor",
],
}
),
"publications": {"default": "livepapers"},
"ephys": {"default": "in-depth"},
"chemicals": {"default": "in-depth"},
"specimen_prep": {"default": "in-depth"},
"stimulation": {"default": "in-depth"},
}
def get_default_space(schema_group, cls_name):
if schema_group not in DEFAULT_SPACES:
raise Exception(f"Please update DEFAULT_SPACES for the {schema_group} module")
if cls_name in DEFAULT_SPACES[schema_group]:
return DEFAULT_SPACES[schema_group][cls_name]
else:
try:
return DEFAULT_SPACES[schema_group]["default"]
except KeyError:
raise KeyError(f"An entry for '{cls_name}' is missing from DEFAULT_SPACES['{schema_group}']")
# in general, we use the required properties when deciding whether a given object already exists
# in the KG. Sometimes this method is inappropriate or undesired, and so for some classes
# we use a custom set of properties.
custom_existence_queries = {
"LaunchConfiguration": ("executable", "name"),
"Person": ("given_name", "family_name"),
"File": ("iri", "hash"),
"FileRepository": ("iri",),
"License": ("alias",),
"DOI": ("identifier",),
"GRIDID": ("identifier",),
"HANDLE": ("identifier",),
"ISBN": ("identifier",),
"ORCID": ("identifier",),
"RORID": ("identifier",),
"SWHID": ("identifier",),
"WebResource": ("iri",),
"Dataset": ("short_name",),
"DatasetVersion": ("short_name", "version_identifier"),
"MetaDataModel": ("short_name",),
"MetaDataModelVersion": ("short_name", "version_identifier"),
"Model": ("full_name",), # here we use 'full_name' instead of 'short_name' for backwards compatibility
"ModelVersion": ("full_name", "version_identifier"),
"Project": ("short_name",),
"Software": ("short_name",),
"SoftwareVersion": ("short_name", "version_identifier"),
"WebService": ("short_name",),
"WebServiceVersion": ("short_name", "version_identifier"),
"Protocol": ("name",),
"BrainAtlas": ("digital_identifier",),
"BrainAtlasVersion": ("short_name", "version_identifier"),
"CommonCoordinateSpace": ("short_name", "version_identifier"),
"ParcellationEntity": ("name",),
"ParcellationEntityVersion": ("name", "version_identifier"),
"ParcellationTerminologyVersion": ("short_name", "version_identifier"),
"CustomCoordinateSpace": ("name",),
"WorkflowRecipe": ("full_name",),
"WorkflowRecipeVersion": ("full_name", "version_identifier"),
"ValidationTest": ("full_name", "short_name"),
"ValidationTestVersion": ("short_name", "version_identifier"),
"LivePaper": ("full_name", "short_name"),
"LivePaperVersion": ("short_name", "version_identifier"),
"LivePaperResourceItem": ("name", "iri", "is_also_part_of"),
"ScholarlyArticle": ("name",),
"WorkflowExecution": ("stages",),
"Configuration": ("configuration",),
"Periodical": ("abbreviation",),
"AmountOfChemical": ("chemical_product", "amount"),
"QuantitativeValue": ("value", "unit", "uncertainties"),
}
def get_existence_query(cls_name, properties):
if cls_name in custom_existence_queries:
return custom_existence_queries[cls_name]
for prop in properties:
if prop["name"] == "lookup_label":
return ("lookup_label",)
required_property_names = []
for prop in properties:
if prop["required"]:
required_property_names.append(prop["name"])
return tuple(required_property_names)
def property_name_sort_key(property_name):
"""Sort the name prop to be first"""
priorities = {
"name": "0",
"fullName": "0",
"alias": "1",
"shortName": "1",
"lookup_label": "3",
}
return priorities.get(property_name, property_name)
def generate_class_name(iri):
assert isinstance(iri, str)
parts = iri.split("/")[-2:]
for i in range(len(parts) - 1):
parts[i] = generate_python_name(parts[i])
return "openminds." + ".".join(parts)
def get_controlled_terms_table(type_):
# todo: reimplement this using instances repo from Github rather than accessing KG
# from kg_core.kg import kg
# from kg_core.request import Stage, Pagination
# host = "core.kg.ebrains.eu"
# limit = 20
# try:
# token = os.environ["KG_AUTH_TOKEN"]
# except KeyError:
# warnings.warn(
# "Cannot get controlled terms."
# "Please obtain an EBRAINS auth token and put it in an environment variable 'KG_AUTH_TOKEN'"
# )
# return ""
# kg_client = kg(host).with_token(token).build()
# response = kg_client.instances.list(
# stage=Stage.RELEASED,
# target_type=type_,
# space="controlled",
# pagination=Pagination(start=0, size=limit),
# )
# if response.error:
# warnings.warn(f"Error trying to retrieve values for {type_}: {response.error}")
# return ""
# else:
# if response.total == 0:
# return ""
# lines = []
# if response.total > response.size:
# assert response.size == limit
# lines.extend(
# [
# "",
# f" Here we show the first {limit} possible values, an additional {response.total - limit} values are not shown.",
# ]
# )
# lines.extend(
# [
# "",
# " .. list-table:: **Possible values**",
# " :widths: 20 80",
# " :header-rows: 0",
# "",
# ]
# )
# for item in response.data:
# vocab = "https://openminds.ebrains.eu/vocab"
# name = item[f"{vocab}/name"]
# definition = item.get(f"{vocab}/definition", None)
# link = item.get(f"{vocab}/preferredOntologyIdentifier", None)
# if definition is None:
# definition = link or " "
# if link:
# name = f"`{name} <{link}>`_"
# lines.append(f" * - {name}")
# lines.append(f" - {definition}")
# lines.append("")
# return "\n".join(lines)
return ""
preamble = {
"File": """import os
import mimetypes
from numbers import Real
from pathlib import Path
from urllib.request import urlretrieve
from urllib.parse import quote, urlparse, urlunparse
from .hash import Hash
from .content_type import ContentType
from ..miscellaneous.quantitative_value import QuantitativeValue
from ...controlled_terms.unit_of_measurement import UnitOfMeasurement
from fairgraph.utility import accepted_terms_of_use, sha1sum
mimetypes.init()""",
"DatasetVersion": """from urllib.request import urlretrieve
from pathlib import Path
from ....utility import accepted_terms_of_use""",
"ModelVersion": """from fairgraph.errors import ResolutionFailure
from .model import Model""",
"ValidationTestVersion": """from fairgraph.errors import ResolutionFailure
from .validation_test import ValidationTest""",
"LivePaperVersion": """from fairgraph.errors import ResolutionFailure
from .live_paper import LivePaper""",
"ScholarlyArticle": """from fairgraph.utility import as_list
from .publication_issue import PublicationIssue
from .periodical import Periodical""",
"SoftwareVersion": """from fairgraph.errors import ResolutionFailure
from .software import Software""",
"WebServiceVersion": """from fairgraph.errors import ResolutionFailure
from .web_service import WebService""",
}
class FairgraphClassBuilder:
"""docstring"""
def __init__(self, schema_file_path: str, root_path: str, target_path_root: str):
self.template_name = "fairgraph_module_template.py.txt"
self.env = Environment(
loader=FileSystemLoader(os.path.dirname(os.path.realpath(__file__))), autoescape=select_autoescape()
)
_relative_path_without_extension = (
schema_file_path[len(root_path) :].replace(".schema.omi.json", "").split("/")
)
self.relative_path_without_extension = [
generate_python_name(part) for part in _relative_path_without_extension[1:]
]
with open(schema_file_path, "r") as schema_f:
self._schema_payload = json.load(schema_f)
self.target_path_root = target_path_root
def _target_file_without_extension(self) -> str:
return os.path.join(*self.relative_path_without_extension)
def translate(self, embedded=None, linked=None):
def get_type(prop):
type_map = {
"string": "str",
"integer": "int",
"number": "Real",
"date": "date",
"date-time": "datetime",
"time": "time",
"iri": "IRI",
"email": "str", # todo: add an Email class for validation?
"ECMA262": "str", # ...
}
if "_linkedTypes" in prop:
types = []
for item in prop["_linkedTypes"]:
openminds_module, class_name = item.split("/")[-2:]
openminds_module = generate_python_name(openminds_module)
types.append(f"openminds.{openminds_module}.{class_name}")
if len(types) == 1:
types = f'"{types[0]}"'
return types
elif "_embeddedTypes" in prop:
types = []
for item in prop["_embeddedTypes"]:
openminds_module, class_name = item.split("/")[-2:]
openminds_module = generate_python_name(openminds_module)
types.append(f"openminds.{openminds_module}.{class_name}")
if len(types) == 1:
types = f'"{types[0]}"'
return types
elif "type" in prop:
if isinstance(prop["type"], list):
return [type_map[item] for item in prop["type"]]
else:
if prop["type"] == "array":
return type_map[prop["items"]["type"]]
elif "_formats" in prop:
assert isinstance(prop["_formats"], list)
if len(prop["_formats"]) > 1:
types = f"[{', '.join([type_map[p] for p in prop['_formats']])}]"
return types
return type_map[prop["_formats"][0]]
else:
return type_map[prop["type"]]
else:
raise NotImplementedError
class_name = self._schema_payload["name"]
module_name = self.relative_path_without_extension[0]
if self._schema_payload["_type"] in embedded:
base_class = "EmbeddedMetadata"
default_space = None
standard_init_properties = ""
else:
base_class = "KGObject"
default_space = get_default_space(module_name, class_name)
standard_init_properties = "id=id, space=space, scope=scope, "
properties = []
plurals_special_cases = {
# because this is a single item (PropertyValueList), but that item contains a list
"environmentVariable": "environmentVariables",
}
aliases = {}
for iri, prop in self._schema_payload["properties"].items():
allow_multiple = prop.get("type", "") == "array"
if allow_multiple:
property_name = prop["namePlural"]
elif prop["name"] in plurals_special_cases:
property_name = plurals_special_cases[prop["name"]]
else:
property_name = prop["name"]
python_name = generate_python_name(property_name)
properties.append(
{
"name": python_name,
"type_str": get_type(prop), # compress using JSON-LD context
"iri": f"vocab:{prop['name']}",
"allow_multiple": allow_multiple,
"required": iri in self._schema_payload.get("required", []),
"doc": generate_doc(prop, class_name),
# "instructions": prop.get("_instruction", "no instructions available"),
"formatting": prop.get("formatting", None),
"multiline": prop.get("multiline", None),
"unique_items": prop.get("uniqueItems", False),
"min_items": prop.get("minItems", None),
"max_items": prop.get("maxItems", None),
}
)
if python_name in global_aliases:
aliases[global_aliases[python_name]] = python_name
reverse_properties = []
forward_property_names = set(prop["name"] for prop in properties)
conflict_resolution = {
"is_part_of": "is_also_part_of",
}
if linked:
linked_from = linked[self._schema_payload["_type"]]
for reverse_link_name in linked_from:
unique_forward_iris = set(linked_from[reverse_link_name][0])
types_str = [generate_class_name(iri) for iri in linked_from[reverse_link_name][2]]
if len(unique_forward_iris) == 1:
(forward_iri,) = unique_forward_iris
forward_link_names = set(linked_from[reverse_link_name][1])
if len(forward_link_names) == 1:
(forward_link_name,) = forward_link_names
else:
forward_link_name = sorted(forward_link_names)[0]
print(f"Multiple forward link names found: {forward_link_names}, using {forward_link_name}")
# if "FileRepository" in self._schema_payload["_type"]:
# breakpoint()
_forward_link_name_python = generate_python_name(forward_link_name)
iri = f"^vocab:{forward_iri}"
doc = f"reverse of '{_forward_link_name_python}'"
types_str = sorted(types_str)
if len(types_str) == 1:
types_str = f'"{types_str[0]}"'
else:
backwards_compatible = True
if backwards_compatible:
forward_iri = sorted(set(linked_from[reverse_link_name][0]))
forward_link_name = sorted(set(linked_from[reverse_link_name][1]))
types_str = sorted(types_str)
else:
# this is a better solution, since we keep the match between types and names
# in the order of the lists, but is not backwards compatible
forward_iri = linked_from[reverse_link_name][0]
forward_link_name = linked_from[reverse_link_name][1]
_forward_link_name_python = [generate_python_name(name) for name in forward_link_name]
iri = [f"^vocab:{part}" for part in forward_iri]
doc = "reverse of " + ", ".join(name for name in _forward_link_name_python)
reverse_name_python = generate_python_name(reverse_link_name)
if reverse_name_python in forward_property_names:
if reverse_name_python in conflict_resolution:
reverse_name_python = conflict_resolution[reverse_name_python]
else:
raise Exception(
"The following name appears as both a forward and reverse name "
f"for {class_name}: {reverse_name_python}"
)
reverse_properties.append(
{
"name": reverse_name_python,
"type_str": types_str,
"_forward_name_python": _forward_link_name_python,
"iri": iri,
"allow_multiple": True,
"required": False,
"doc": doc,
}
)
additional_methods = ""
if os.path.exists(f"additional_methods/{class_name}.py.txt"):
with open(f"additional_methods/{class_name}.py.txt") as fp:
additional_methods = fp.read()
self.context = {
"docstring": self._schema_payload.get("description", "<description not available>"),
"base_class": base_class,
"preamble": preamble.get(class_name, ""), # default value, may be updated below
"class_name": class_name,
"default_space": default_space,
"openminds_type": self._schema_payload["_type"],
"properties": sorted(properties, key=lambda p: p["name"]),
"reverse_properties": sorted(reverse_properties, key=lambda p: p["name"]),
"additional_methods": "",
"existence_query_properties": get_existence_query(class_name, properties),
"standard_init_properties": standard_init_properties,
"additional_methods": additional_methods,
"aliases": aliases,
"constructor_arguments": sorted(
[p["name"] for p in chain(properties, reverse_properties)] + list(aliases.keys()),
key=property_name_sort_key
)
}
import_map = {
"date": "from datetime import date",
"datetime": "from datetime import datetime",
"time": "from datetime import time",
"IRI": "from fairgraph.base import IRI",
"[datetime, time]": "from datetime import datetime, time",
}
extra_imports = set()
for prop in self.context["properties"]:
if isinstance(prop["type_str"], list):
for t in prop["type_str"]:
imp = import_map.get(t, None)
if imp:
extra_imports.add(imp)
else:
imp = import_map.get(prop["type_str"], None)
if imp:
extra_imports.add(imp)
if extra_imports:
self.context["preamble"] += "\n" + "\n".join(sorted(extra_imports))
if module_name == "controlled_terms":
self.context["docstring"] += get_controlled_terms_table(self._schema_payload["_type"])
def build(self, embedded=None, linked=None):
target_file_path = os.path.join(self.target_path_root, f"{self._target_file_without_extension()}.py")
os.makedirs(os.path.dirname(target_file_path), exist_ok=True)
self.translate(embedded=embedded, linked=linked)
with open(target_file_path, "w") as target_file:
contents = self.env.get_template(self.template_name).render(self.context)
target_file.write(contents)
return (self._target_file_without_extension().replace("/", "."), self.context["class_name"])
def get_edges(self):
embedded = set()
linked = {}
for prop in self._schema_payload["properties"].values():
for emb in prop.get("_embeddedTypes", []):
embedded.add(emb)
for lnk in prop.get("_linkedTypes", []):
reverse_link_name = prop["nameForReverseLink"]
if reverse_link_name is None:
reverse_link_name = reverse_name_map[prop["name"]]
allow_multiple = prop.get("type", "") == "array"
linked[lnk] = (
self._schema_payload["_type"],
prop["name"], # property name
prop["namePlural"] if allow_multiple else prop["name"], # forward link name
reverse_link_name,
) # linked from (cls, prop name, prop name plural, reverse name)
# if self._schema_payload["_type"].endswith("File"):
# breakpoint()
return embedded, linked
def main(openminds_root, ignore=[]):
target_path = os.path.join("..", "fairgraph", "openminds")
if os.path.exists(target_path):
shutil.rmtree(target_path)
openminds_root = os.path.realpath(openminds_root)
schema_file_paths = glob(os.path.join(openminds_root, f"**/*.schema.omi.json"), recursive=True)
python_modules = defaultdict(list)
# First pass - figure out which schemas are embedded and which are linked
embedded = set()
linked = defaultdict(dict)
for schema_file_path in schema_file_paths:
embedded_in, linked_from = FairgraphClassBuilder(schema_file_path, openminds_root, target_path).get_edges()
embedded.update(embedded_in)
for openminds_type, (link_type, property_name, forward_name, reverse_name) in linked_from.items():
if link_type not in embedded:
if isinstance(reverse_name, dict):
reverse_name = reverse_name[link_type]
if reverse_name in linked[openminds_type]:
linked[openminds_type][reverse_name][0].append(property_name)
linked[openminds_type][reverse_name][1].append(forward_name)
linked[openminds_type][reverse_name][2].append(link_type)
else:
linked[openminds_type][reverse_name] = ([property_name], [forward_name], [link_type])
conflicts = set(linked).intersection(embedded)
if conflicts:
print(f"Found schema(s) that are both linked and embedded: {conflicts}")
# conflicts should not happen in new versions.
for schema_identifier in conflicts:
linked.pop(schema_identifier, None)
# Second pass - create a Python module for each openMINDS schema
for schema_file_path in schema_file_paths:
module_path, class_name = FairgraphClassBuilder(schema_file_path, openminds_root, target_path).build(
embedded=embedded, linked=linked
)
parts = module_path.split(".")
parent_path = ".".join(parts[:-1])
python_modules[parent_path].append((parts[-1], class_name))
# Now create additional files, e.g. __init__.py
env = Environment(
loader=FileSystemLoader(os.path.dirname(os.path.realpath(__file__))), autoescape=select_autoescape()
)
openminds_modules = set()
for path, classes in python_modules.items():
dir_path = path.split(".")
openminds_modules.add(dir_path[0])
# first write __init__ for submodule (or top-level module if no submodules)
init_file_path = os.path.join(target_path, *(dir_path + ["__init__.py"]))
with open(init_file_path, "w") as fp:
for class_module, class_name in sorted(classes, key=lambda entry: entry[0]):
fp.write(f"from .{class_module} import {class_name}\n")
# now write __init__ for top-level module if submodules
child_dir = dir_path[-1]
dir_path = dir_path[:-1]
if len(dir_path) == 1:
init_file_path = os.path.join(target_path, *(dir_path + ["__init__.py"]))
with open(init_file_path, "a") as fp:
class_names = ", ".join(class_name for _, class_name in classes)
fp.write(f"from .{child_dir} import ({class_names})\n")
for om_module in openminds_modules:
with open("init_template.py.txt") as fp:
om_module_functions = fp.read()
init_file_path = os.path.join("..", "fairgraph", "openminds", om_module, "__init__.py")
with open(init_file_path, "r") as fp:
content = fp.read()
with open(init_file_path, "w") as fp:
om_module_header = [
"import sys\n",
"import inspect\n",
"from fairgraph.kgobject import KGObject\n",
"from fairgraph.embedded import EmbeddedMetadata\n\n",
]
fp.writelines(om_module_header)
fp.write(content)
fp.write(om_module_functions)
with open("../fairgraph/openminds/controlledterms.py", "w") as fp:
fp.writelines([
"from warnings import warn\n"
"from .controlled_terms import *\n"
"warn('The `controlledterms` module has been renamed to `controlled_terms`, please update your code', DeprecationWarning)"
])
with open("../fairgraph/openminds/specimenprep.py", "w") as fp:
fp.writelines([
"from warnings import warn\n"
"from .specimen_prep import *\n"
"warn('The `specimenprep` module has been renamed to `specimen_prep`, please update your code', DeprecationWarning)"
])
init_file_path = os.path.join("..", "fairgraph", "openminds", "__init__.py")
with open(init_file_path, "w") as fp:
fp.write(f"from . import ({', '.join(sorted(openminds_modules))})\n")
# Format with Black
subprocess.call([sys.executable, "-m", "black", "--quiet", target_path])
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=sys.argv[0],
description="Generate fairgraph classes from the EBRAINS openMINDS schema templates",
)
parser.add_argument("openminds_root", help="The path to the openMINDS directory")
parser.add_argument("--ignore", help="Names of schema groups to ignore", default=[], action="append")
args = vars(parser.parse_args())
main(**args)