Skip to content

Commit

Permalink
Merge pull request #130 from aglavic/fix_nxs
Browse files Browse the repository at this point in the history
NeXus creation fails when dictrionary of ORSO objects is present
  • Loading branch information
aglavic authored Jul 1, 2024
2 parents c88ab4c + c529619 commit 7eef00e
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 12 deletions.
124 changes: 124 additions & 0 deletions examples/prist5_10K_m_025.Rqz.ort
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# # ORSO reflectivity data file | 1.0 standard | YAML encoding | https://www.reflectometry.org/
# data_source:
# owner:
# name: Artur Glavic
# affiliation: null
# contact: b''
# experiment:
# title: Structural evolution of the CO2/Water interface
# instrument: Amor
# start_date: 2023-11-29T10:12:45
# probe: neutron
# facility: SINQ@PSI
# proposalID: '20230368'
# sample:
# name: prist4
# sample_parameters:
# tempMean: {magnitude: -9999.0}
# model:
# stack: vacuum | Al2O3 2 | Ti0.27Co0.73 9.3 | Si
# measurement:
# instrument_settings:
# incident_angle: {min: 0.0950000000000002, max: 1.495, unit: deg}
# wavelength: {min: 3.0, max: 12.0, unit: angstrom}
# mu: {magnitude: 0.25, unit: deg, comment: sample angle to referece direction}
# nu: {magnitude: 1.0450000000000002, unit: deg, comment: detector angle to referece
# direction}
# data_files:
# - file: raw/amor2023n000848.hdf
# timestamp: 2023-11-29T10:12:45
# amor_monitor: 1792.443302905
# scheme: angle- and energy-dispersive
# references: []
# amor_monitor: 1792.443302905
# reduction:
# software: {name: eos, version: '2.0'}
# timestamp: 2023-11-29T10:57:48.480339
# computer: amor.psi.ch
# call: eos.py -a 0.04 -F 0.0093,0.0101 -n 848 prist5_10K_m_025
# data_set: 0
# columns:
# - {name: Qz, unit: 1/angstrom, physical_quantity: normal momentum transfer}
# - {name: R, unit: '', physical_quantity: specular reflectivity}
# - {error_of: R, error_type: uncertainty, value_is: sigma}
# - {error_of: Qz, error_type: resolution, value_is: sigma}
# # Qz (1/angstrom) R () sR sQz
2.1999999999999997e-03 4.7241193897215048e-03 4.7241193897215048e-03 1.9999999999999987e-04
2.5999999999999999e-03 0.0000000000000000e+00 0.0000000000000000e+00 2.0000000000000009e-04
3.0000000000000001e-03 4.9441079014990305e-03 2.8632759811688313e-03 2.0000000000000009e-04
3.4000000000000002e-03 5.3957146642147798e-03 2.7033018099334193e-03 1.9999999999999987e-04
3.8000000000000000e-03 1.1948517768746966e-02 3.7903477990210636e-03 2.0000000000000009e-04
4.2000000000000006e-03 1.6302332669940297e-02 3.9723175648453228e-03 2.0000000000000009e-04
4.5999999999999999e-03 2.2652164871650581e-02 4.5763672802481776e-03 2.0000000000000009e-04
5.0000000000000001e-03 2.3020814468949415e-02 4.2537271133726200e-03 1.9999999999999966e-04
5.4000000000000003e-03 2.7305557750665344e-02 4.4287009711273589e-03 2.0000000000000009e-04
5.7999999999999996e-03 1.9457945835850306e-02 3.7817426451169783e-03 2.0000000000000009e-04
6.2000000000000006e-03 2.7645506109865266e-02 4.2679939914090732e-03 2.0000000000000009e-04
6.6000000000000000e-03 2.4223034347324195e-02 3.7655052701117517e-03 2.0000000000000009e-04
7.0000000000000001e-03 2.8408308819418325e-02 3.7377941755602716e-03 1.9999999999999966e-04
7.4000000000000003e-03 5.7460495308053604e-02 5.2154049057105455e-03 2.0000000000000009e-04
7.7999999999999996e-03 1.4898833519509819e-01 7.5478984035617898e-03 2.0000000000000009e-04
8.1999999999999990e-03 3.5204809294430212e-01 1.1146891694516669e-02 1.9999999999999966e-04
8.6000000000000000e-03 5.3176577607895481e-01 1.3517251795319620e-02 1.9999999999999966e-04
8.9999999999999993e-03 7.9145006465015877e-01 1.6276277463585134e-02 2.0000000000000052e-04
9.3999999999999986e-03 9.6803310478461035e-01 1.7827487993063780e-02 1.9999999999999966e-04
9.7999999999999997e-03 1.0324643310179094e+00 1.8104900945963596e-02 2.0000000000000052e-04
1.0204081632653062e-02 6.2876616833212340e-01 1.3873373556929145e-02 2.0408163265306107e-04
1.0620574760516453e-02 3.5069559151104790e-01 1.0136613757789808e-02 2.1241149521033023e-04
1.1054067607884473e-02 2.3477751708069897e-01 8.1099626399286761e-03 2.2108135215768900e-04
1.1505254040859348e-02 1.8964302712880826e-01 7.1805730148316446e-03 2.3010508081718756e-04
1.1974856246608712e-02 1.4285208497806512e-01 6.0162249267477130e-03 2.3949712493217482e-04
1.2463625889327434e-02 1.2188457497836602e-01 5.4352795853431970e-03 2.4927251778654805e-04
1.2972345313381616e-02 1.0530945504618572e-01 5.0275179459601553e-03 2.5944690626763297e-04
1.3501828795560456e-02 8.0387950876579850e-02 4.1975562768255060e-03 2.7003657591120889e-04
1.4052923848440476e-02 7.3136722795496253e-02 3.9582003984186967e-03 2.8105847696880976e-04
1.4626512576948251e-02 6.1298419781514651e-02 3.4783001152817426e-03 2.9253025153896592e-04
1.5223513090293080e-02 6.2409059365864682e-02 3.4798000624600761e-03 3.0447026180586197e-04
1.5844880971529533e-02 5.6512800965543043e-02 3.2223775371921573e-03 3.1689761943059103e-04
1.6491610807102167e-02 5.0440737935525240e-02 2.9021067247837801e-03 3.2983221614204389e-04
1.7164737778820625e-02 5.0277525044171101e-02 2.8252864493045079e-03 3.4329475557641313e-04
1.7865339320813300e-02 5.1689705097227867e-02 2.8118143416053952e-03 3.5730678641626538e-04
1.8594536844111803e-02 5.3007618770583402e-02 2.7685413804410695e-03 3.7189073688223724e-04
1.9353497531626573e-02 4.9070756677145402e-02 2.5677593751218504e-03 3.8706995063253133e-04
2.0143436206386842e-02 4.6878831709663782e-02 2.4119133732668076e-03 4.0286872412773761e-04
2.0965617276035284e-02 4.9199553772144550e-02 2.4276503561561644e-03 4.1931234552070561e-04
2.1821356756689787e-02 5.2041112703109127e-02 2.4556151556737898e-03 4.3642713513379616e-04
2.2712024379411822e-02 4.9251781726106690e-02 2.3335505076022252e-03 4.5424048758823873e-04
2.3639045782653120e-02 4.3172608947743572e-02 2.1138539656044499e-03 4.7278091565306109e-04
2.4603904794189984e-02 3.8304327635500499e-02 1.9158755178575817e-03 4.9207809588380086e-04
2.5608145806197739e-02 4.4442200106459967e-02 2.0186419327094269e-03 5.1216291612395451e-04
2.6653376247267036e-02 3.9587001147536376e-02 1.8488058581743547e-03 5.3306752494534232e-04
2.7741269155318757e-02 3.6677858215866437e-02 1.7473224966445143e-03 5.5482538310637659e-04
2.8873565855535847e-02 3.7663512267048754e-02 1.7682821318945074e-03 5.7747131711071743e-04
3.0052078747598535e-02 3.6755569252809664e-02 1.7350440273063527e-03 6.0104157495196979e-04
3.1278694206684193e-02 3.8546394417761670e-02 1.7965558626918197e-03 6.2557388413368373e-04
3.2555375602875386e-02 3.2866785139061716e-02 1.6217721152892931e-03 6.5110751205750897e-04
3.3884166443809073e-02 3.1513196803753502e-02 1.6061232808323794e-03 6.7768332887618160e-04
3.5267193645597203e-02 3.0280345918169513e-02 1.5885947028228238e-03 7.0534387291194475e-04
3.6706670937254229e-02 2.5036982048446406e-02 1.4176844815086535e-03 7.3413341874508514e-04
3.8204902404080934e-02 2.0831804563285421e-02 1.2803365468167761e-03 7.6409804808161980e-04
3.9764286175676081e-02 1.9376976329704945e-02 1.2712203272554497e-03 7.9528572351352314e-04
4.1387318264479181e-02 1.6729675515039004e-02 1.1715497338181137e-03 8.2774636528958404e-04
4.3076596560988542e-02 1.3996303090353096e-02 1.0654383375290077e-03 8.6153193121977015e-04
4.4834824992049299e-02 1.0621480384334052e-02 9.2468077823003954e-04 8.9669649984099042e-04
4.6664817848867640e-02 7.8203324635251256e-03 8.0931024548367521e-04 9.3329635697735405e-04
4.8569504291678570e-02 6.0428022894521575e-03 7.1838354133842033e-04 9.7139008583356848e-04
5.0551933038277694e-02 4.0958929714843932e-03 5.8416704469953131e-04 1.0110386607655557e-03
5.2615277243921690e-02 5.1115251619419473e-03 6.6580030308840434e-04 1.0523055448784409e-03
5.4762839580408292e-02 2.0207116612746484e-03 4.1438660592314769e-04 1.0952567916081637e-03
5.6998057522465770e-02 1.7115689314143780e-03 3.8529038925611664e-04 1.1399611504493146e-03
5.9324508849913360e-02 8.4963058831312487e-04 2.7095330976641694e-04 1.1864901769982721e-03
6.1745917374399620e-02 8.1421160351772355e-04 2.7251395576638693e-04 1.2349183474879948e-03
6.4266158899885323e-02 8.9204238262309603e-05 8.9204238263225523e-05 1.2853231779977048e-03
6.6889267426411256e-02 9.3397363465611626e-05 9.3397363464899779e-05 1.3377853485282282e-03
6.9619441607081112e-02 4.0599169078880156e-04 2.0303208241886721e-04 1.3923888321416208e-03
7.2461051468594620e-02 2.1832573520758229e-04 1.5439324988622013e-04 1.4492210293718943e-03
7.5418645406088280e-02 1.2684339163878999e-04 1.2684339163797725e-04 1.5083729081217731e-03
7.8496957463479650e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.5699391492695891e-03
8.1700914910968619e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.6340182982193738e-03
8.5035646131824488e-02 4.1657483226777538e-04 2.4090333523406435e-04 1.7007129226364950e-03
8.8506488831082628e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.7701297766216512e-03
9.2118998579290082e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.8423799715858030e-03
9.5878957704975398e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.9175791540995135e-03
9.9792384550076441e-02 0.0000000000000000e+00 0.0000000000000000e+00 1.9958476910015288e-03
1.0386554310314079e-01 0.0000000000000000e+00 0.0000000000000000e+00 2.0773108620628228e-03
24 changes: 12 additions & 12 deletions orsopy/fileio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Implementation of the base classes for the ORSO header.
"""

import sys
import datetime
import json
import os.path
import pathlib
import re
import sys
import warnings

from collections.abc import Mapping
Expand Down Expand Up @@ -39,9 +39,9 @@ def _noop(self, *args, **kw):
yaml.emitter.Emitter.process_tag = _noop

# make sure that datetime strings get loaded as str not datetime instances
yaml.constructor.SafeConstructor.yaml_constructors[
"tag:yaml.org,2002:timestamp"
] = yaml.constructor.SafeConstructor.yaml_constructors["tag:yaml.org,2002:str"]
yaml.constructor.SafeConstructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = (
yaml.constructor.SafeConstructor.yaml_constructors["tag:yaml.org,2002:str"]
)


class ORSOResolveError(ValueError):
Expand Down Expand Up @@ -284,7 +284,8 @@ def _resolve_type(hint: type, item: Any) -> Any:
return item
else:
warnings.warn(
f"Has to be one of {get_args(hint)} got {item}", ORSOSchemaWarning,
f"Has to be one of {get_args(hint)} got {item}",
ORSOSchemaWarning,
)
return str(item)
return None
Expand Down Expand Up @@ -424,7 +425,9 @@ def to_nexus(self, root=None, name=None):
# special handling for null datasets: no data
item_out = child_group.create_dataset(sub_name, dtype="f")
elif isinstance(t_value, dict):
item_out = child_group.create_dataset(sub_name, data=json.dumps(t_value))
item_out = child_group.create_dataset(
sub_name, data=json.dumps(t_value, default=lambda o: o.__dict__)
)
item_out.attrs["mimetype"] = JSON_MIMETYPE
else:
# raise ValueError(f"unserializable attribute found: {child_name}[{index}] = {t_value}")
Expand All @@ -440,7 +443,7 @@ def to_nexus(self, root=None, name=None):
elif t_value is None:
group.create_dataset(child_name, dtype="f")
elif isinstance(t_value, dict):
dset = group.create_dataset(child_name, data=json.dumps(t_value))
dset = group.create_dataset(child_name, data=json.dumps(t_value, default=lambda o: o.__dict__))
dset.attrs["mimetype"] = JSON_MIMETYPE
else:
warnings.warn(f"unserializable attribute found: {child_name} = {t_value}")
Expand Down Expand Up @@ -973,10 +976,7 @@ def _validate_header_data(dct_list: List[dict]):

vi = sys.version_info
if vi.minor < 7:
warnings.warn(
"Validation not possible with Python 3.6 with 2020-12 json schema",
ORSOSchemaWarning
)
warnings.warn("Validation not possible with Python 3.6 with 2020-12 json schema", ORSOSchemaWarning)

pth = os.path.dirname(__file__)
schema_pth = os.path.join(pth, "schema", "refl_header.schema.json")
Expand Down Expand Up @@ -1025,7 +1025,7 @@ def _todict(obj: Any, classkey: Any = None) -> dict:
"""
if isinstance(obj, dict):
data = {}
for (k, v) in obj.items():
for k, v in obj.items():
data[k] = _todict(v, classkey)
return data
elif isinstance(obj, Enum):
Expand Down
16 changes: 16 additions & 0 deletions orsopy/orb2ort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys

from .fileio import load_nexus, save_orso


def main():
for fn in sys.argv[1:]:
print(fn)
res = load_nexus(fn)
save_orso(res, fn.rsplit(".", 1)[0] + ".ort")


if __name__ == "__main__":
if len(sys.argv) == 1:
print("Convert ORSO text representation to Nexus.\nUsage: orsopy.py file1.orb file2.orb ...")
main()
16 changes: 16 additions & 0 deletions orsopy/ort2orb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys

from .fileio import load_orso, save_nexus


def main():
for fn in sys.argv[1:]:
print(fn)
res = load_orso(fn)
save_nexus(res, fn.rsplit(".", 1)[0] + ".orb")


if __name__ == "__main__":
if len(sys.argv) == 1:
print("Convert ORSO text representation to Nexus.\nUsage: orsopy.py file1.ort file2.ort ...")
main()

0 comments on commit 7eef00e

Please sign in to comment.