Skip to content

Commit 9966036

Browse files
using readonly instead of user_editability; enabling injesting multip… (#486)
* using readonly instead of user_editability; enabling injesting multiple methods to the base classes and not only the normalization; Entry to inherit also from Task, so Workflow click drives there directly * moving changes to NeXusMeasurement which is not anymore using normalisation from Measurement and Activity * fix for the new normalisation function * nicer name layout * removing unneeded comments; fixing race condition at NeXusdataConverter ELN normalisation * m_copy the relevant sections to steps and results instead of using python reference * Fix attribute error. * remove attribute checks. --------- Co-authored-by: Rubel <rubel.mozumder@outlook.com>
1 parent 0ef8f7d commit 9966036

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

src/pynxtools/nomad/dataconverter.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
from pynxtools.dataconverter import convert as pynxtools_converter
1818
from pynxtools.dataconverter import writer as pynxtools_writer
1919
from pynxtools.dataconverter.template import Template
20-
from pynxtools.definitions.dev_tools.utils.nxdl_utils import (
21-
get_app_defs_names, # pylint: disable=import-error
22-
)
20+
from pynxtools.definitions.dev_tools.utils.nxdl_utils import get_app_defs_names # pylint: disable=import-error
2321

2422
m_package = Package(name="nexus_data_converter")
2523

@@ -96,6 +94,14 @@ def populate_nexus_subsection(
9694
archive.data.output = os.path.join(
9795
archive.m_context.raw_path(), output_file_path
9896
)
97+
# remove the nexus file and ensure that NOMAD knows that it is removed
98+
try:
99+
os.remove(archive.data.output)
100+
archive.m_context.process_updated_raw_file(
101+
archive.data.output, allow_modify=True
102+
)
103+
except Exception as e:
104+
pass
99105
pynxtools_writer.Writer(
100106
data=template, nxdl_f_path=nxdl_f_path, output_path=archive.data.output
101107
).write()
@@ -220,6 +226,15 @@ def normalize(self, archive, logger):
220226
],
221227
"output": os.path.join(raw_path, archive.data.output),
222228
}
229+
# remove the nexus file and ensure that NOMAD knows that it is removed
230+
try:
231+
os.remove(os.path.join(raw_path, archive.data.output))
232+
archive.m_context.process_updated_raw_file(
233+
archive.data.output, allow_modify=True
234+
)
235+
except Exception as e:
236+
pass
237+
# create the new nexus file
223238
try:
224239
pynxtools_converter.logger = logger
225240
pynxtools_converter.helpers.logger = logger
@@ -229,7 +244,7 @@ def normalize(self, archive, logger):
229244
"could not convert to nxs", mainfile=archive.data.output, exc_info=e
230245
)
231246
raise e
232-
247+
# parse the new nexus file
233248
try:
234249
archive.m_context.process_updated_raw_file(
235250
archive.data.output, allow_modify=True

src/pynxtools/nomad/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def parse(
513513
if archive.metadata.entry_type is None:
514514
archive.metadata.entry_type = app_def
515515
archive.metadata.domain = "nexus"
516-
archive.metadata.user_editable = False
516+
archive.metadata.readonly = True
517517

518518
# Normalise element info
519519
if archive.results is None:

src/pynxtools/nomad/schema.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
InstrumentReference,
4848
Measurement,
4949
)
50+
from nomad.datamodel.metainfo.workflow import Link, Task, Workflow
5051
from nomad.metainfo import (
5152
Attribute,
5253
Bytes,
@@ -118,11 +119,14 @@ def normalize(self, archive, logger):
118119
app_entry = getattr(self, "ENTRY")
119120
if len(app_entry) < 1:
120121
raise AttributeError()
121-
self.steps = app_entry
122+
self.steps = []
122123
for entry in app_entry:
124+
sec_c = entry.m_copy()
125+
self.steps.append(sec_c)
123126
for sec in entry.m_all_contents():
124127
if isinstance(sec, ActivityStep):
125-
self.steps.append(sec)
128+
sec_c = sec.m_copy()
129+
self.steps.append(sec_c)
126130
elif isinstance(sec, basesections.Instrument):
127131
ref = InstrumentReference(name=sec.name)
128132
ref.reference = sec
@@ -132,14 +136,52 @@ def normalize(self, archive, logger):
132136
ref.reference = sec
133137
self.samples.append(ref)
134138
elif isinstance(sec, ActivityResult):
135-
self.results.append(sec)
139+
sec_c = sec.m_copy()
140+
self.results.append(sec_c)
136141
if self.m_def.name == "Root":
137142
self.method = "Generic Experiment"
138143
else:
139144
self.method = self.m_def.name + " Experiment"
140145
except (AttributeError, TypeError):
141146
pass
142-
super(NexusMeasurement, self).normalize(archive, logger)
147+
super(basesections.Activity, self).normalize(archive, logger)
148+
149+
if archive.results.eln.methods is None:
150+
archive.results.eln.methods = []
151+
if self.method:
152+
archive.results.eln.methods.append(self.method)
153+
else:
154+
archive.results.eln.methods.append(self.m_def.name)
155+
if archive.workflow2 is None:
156+
archive.workflow2 = Workflow(name=self.name)
157+
# steps to tasks
158+
act_array = archive.workflow2.tasks
159+
existing_items = {(task.name, task.section) for task in act_array}
160+
new_items = [
161+
item.to_task()
162+
for item in self.steps
163+
if (item.name, item) not in existing_items
164+
]
165+
act_array.extend(new_items)
166+
# samples to inputs
167+
act_array = archive.workflow2.inputs
168+
existing_items = {(link.name, link.section) for link in act_array}
169+
new_items = [
170+
Link(name=item.name, section=item.reference)
171+
for item in self.samples
172+
if (item.name, item.reference) not in existing_items
173+
]
174+
act_array.extend(new_items)
175+
176+
# results to outputs
177+
act_array = archive.workflow2.outputs
178+
existing_items = {(link.name, link.section) for link in act_array}
179+
new_items = [
180+
Link(name=item.name, section=item)
181+
for item in self.results
182+
if (item.name, item) not in existing_items
183+
]
184+
act_array.extend(new_items)
143185

144186

145187
VALIDATE = False
@@ -952,7 +994,7 @@ def normalize_sample(self, archive, logger):
952994
"""Normalizer for sample section."""
953995
current_cls = __section_definitions[__rename_nx_for_nomad("NXsample")].section_cls
954996
self.name = self.__dict__["nx_name"] + (
955-
"(" + self.name__field + ")" if self.name__field else ""
997+
" (" + self.name__field + ")" if self.name__field else ""
956998
)
957999
# one could also copy local ids to identifier for search purposes
9581000
super(current_cls, self).normalize(archive, logger)
@@ -964,7 +1006,7 @@ def normalize_entry(self, archive, logger):
9641006
if self.start_time__field:
9651007
self.start_time = self.start_time__field
9661008
self.name = self.__dict__["nx_name"] + (
967-
"(" + self.title__field + ")" if self.title__field is not None else ""
1009+
" (" + self.title__field + ")" if self.title__field is not None else ""
9681010
)
9691011
# one could also copy local ids to identifier for search purposes
9701012
super(current_cls, self).normalize(archive, logger)
@@ -998,7 +1040,7 @@ def create_Entity(lab_id, archive, f_name):
9981040
data=entitySec,
9991041
m_context=archive.m_context,
10001042
metadata=EntryMetadata(
1001-
entry_type="identifier", domain="nexus"
1043+
entry_type="identifier", domain="nexus", readonly=True
10021044
), # upload_id=archive.m_context.upload_id,
10031045
)
10041046
with archive.m_context.raw_file(f_name, "w") as f_obj:
@@ -1039,8 +1081,12 @@ def get_entry_reference(archive, f_name):
10391081
__rename_nx_for_nomad("NXsample"): normalize_sample,
10401082
__rename_nx_for_nomad("NXsample_component"): normalize_sample_component,
10411083
__rename_nx_for_nomad("NXidentifier"): normalize_identifier,
1042-
__rename_nx_for_nomad("NXentry"): normalize_entry,
1043-
__rename_nx_for_nomad("NXprocess"): normalize_process,
1084+
__rename_nx_for_nomad("NXentry"): {
1085+
"normalize": normalize_entry,
1086+
},
1087+
__rename_nx_for_nomad("NXprocess"): {
1088+
"normalize": normalize_process,
1089+
},
10441090
__rename_nx_for_nomad("NXdata"): normalize_data,
10451091
}
10461092

@@ -1053,4 +1099,8 @@ def get_entry_reference(archive, f_name):
10531099

10541100
# Append the normalize method from a function
10551101
if normalize_func:
1056-
section.section_cls.normalize = normalize_func
1102+
if isinstance(normalize_func, dict):
1103+
for key, value in normalize_func.items():
1104+
setattr(section.section_cls, key, value)
1105+
else:
1106+
section.section_cls.normalize = normalize_func

0 commit comments

Comments
 (0)