diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index e07c6f90..8911dfdc 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -53,5 +53,5 @@ jobs: - name: Install and Test with pytest run: | export PATH="$pythonLocation:$PATH" - python -m pip install -e .[Dev] + python -m pip install -e .[Dev,Orso] pytest tests/ --cov=RATapi --cov-report=term diff --git a/RATapi/__init__.py b/RATapi/__init__.py index 8484ea45..1c0b820f 100644 --- a/RATapi/__init__.py +++ b/RATapi/__init__.py @@ -6,6 +6,6 @@ from RATapi.controls import Controls from RATapi.project import Project from RATapi.run import run -from RATapi.utils import convert, plotting +from RATapi.utils import convert, orso, plotting -__all__ = ["examples", "models", "events", "ClassList", "Controls", "Project", "run", "plotting", "convert"] +__all__ = ["examples", "models", "events", "ClassList", "Controls", "Project", "run", "plotting", "convert", "orso"] diff --git a/RATapi/classlist.py b/RATapi/classlist.py index c3e1d04f..b046f885 100644 --- a/RATapi/classlist.py +++ b/RATapi/classlist.py @@ -296,6 +296,19 @@ def extend(self, other: Sequence[T]) -> None: self._check_unique_name_fields(other) self.data.extend(other) + def union(self, other: Sequence[T]) -> None: + """Extend the ClassList by a sequence, ignoring input items with names that already exist.""" + if other and not (isinstance(other, Sequence) and not isinstance(other, str)): + other = [other] + + self.extend( + [ + item + for item in other + if hasattr(item, self.name_field) and getattr(item, self.name_field) not in self.get_names() + ] + ) + def set_fields(self, index: Union[int, slice, str, T], **kwargs) -> None: """Assign the values of an existing object's attributes using keyword arguments.""" self._validate_name_field(kwargs) diff --git a/RATapi/examples/data/c_PLP0011859_q.ort b/RATapi/examples/data/c_PLP0011859_q.ort new file mode 100644 index 00000000..7a7bf1fd --- /dev/null +++ b/RATapi/examples/data/c_PLP0011859_q.ort @@ -0,0 +1,441 @@ +# # ORSO reflectivity data file | 1.1 standard | YAML encoding | https://www.reflectometry.org/ +# # handwritten test file header created to test RAT orsopy integration! +# # example data from refnx: https://refnx.readthedocs.io/en/latest/getting_started.html#fitting-a-neutron-reflectometry-dataset +# data_source: +# owner: +# name: null +# affiliation: null +# measurement: +# instrument_settings: null +# data_files: null +# experiment: +# title: +# probe: neutron +# instrument: None +# start_date: 1970-01-01T00:00:00 +# sample: +# name: film on silicon +# model: +# stack: Si | SiO2 30 | film 250 | D2O +# materials: +# film: +# sld: 2.0e-6 +# roughness: 3 +# reduction: +# software: null +# timestamp: null +# 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 +0.00806022 0.709581 0.0850676 0.000331422 +0.00813662 0.862281 0.11237 0.000334619 +0.00826375 0.908647 0.0790047 0.000339939 +0.00837067 0.773292 0.0792728 0.000344412 +0.00845033 1.05797 0.125959 0.000347744 +0.00853083 1.01566 0.113295 0.000351111 +0.00861217 0.734717 0.0611566 0.000354512 +0.00869437 0.769216 0.0617058 0.000357949 +0.00877743 1.11574 0.11273 0.000361421 +0.00886136 0.972303 0.089716 0.000364929 +0.00894616 0.751214 0.0549393 0.000368473 +0.00903185 0.797649 0.0567122 0.000372053 +0.00911844 0.922189 0.0685841 0.000375671 +0.00920593 0.975755 0.0729395 0.000379325 +0.00929432 0.819504 0.0521617 0.000383017 +0.00938364 0.78832 0.0479473 0.000386748 +0.00947389 0.794701 0.0460224 0.000390516 +0.00956508 0.8744 0.0515164 0.000394323 +0.00965721 0.839662 0.0474285 0.00039817 +0.0097503 0.800872 0.0439958 0.000402055 +0.00984436 1.1171 0.073733 0.000405981 +0.00993939 0.888411 0.049541 0.000409947 +0.0100354 0.779129 0.0389873 0.000413953 +0.0101324 0.799968 0.0389974 0.000418001 +0.0102304 0.843124 0.041598 0.00042209 +0.0103294 0.961332 0.0492536 0.00042622 +0.0104868 0.880544 0.0299083 0.000432781 +0.0106327 0.755735 0.0320851 0.000438868 +0.0107359 0.971231 0.0453485 0.00044317 +0.0108401 0.895549 0.0390542 0.000447516 +0.0109454 0.862589 0.0358072 0.000451906 +0.0110518 0.890992 0.0361257 0.000456342 +0.0111593 0.900348 0.0367924 0.000460822 +0.0112679 0.845927 0.0321881 0.000465348 +0.0113776 0.943152 0.0365533 0.000469921 +0.0114884 0.995631 0.0390116 0.00047454 +0.0116004 0.969594 0.0363623 0.000479205 +0.0117135 0.905181 0.032041 0.000483919 +0.0118278 0.893381 0.0306119 0.00048868 +0.0119433 0.919602 0.0314677 0.00049349 +0.0120599 0.918998 0.0306097 0.000498349 +0.0121777 0.781056 0.0235542 0.000503257 +0.0122968 0.864915 0.0272025 0.000508215 +0.012417 0.843516 0.0255015 0.000513223 +0.0125385 0.998418 0.0319722 0.000518283 +0.0126612 0.88126 0.0260273 0.000523393 +0.0127852 0.883569 0.0255862 0.000528556 +0.0129105 0.93767 0.0274651 0.000533771 +0.013037 1.0192 0.0305107 0.000539039 +0.0131648 0.845526 0.0226269 0.00054436 +0.0132939 0.873804 0.0232468 0.000549735 +0.0134243 0.865953 0.0224906 0.000555165 +0.0135561 0.877982 0.0224491 0.00056065 +0.0136892 0.947545 0.0248985 0.00056619 +0.0138237 0.888154 0.0220962 0.000571787 +0.0139595 0.891362 0.0218148 0.00057744 +0.0140967 0.888456 0.0215092 0.00058315 +0.0142353 0.913717 0.0222586 0.000588919 +0.0143753 0.810364 0.0188289 0.000594745 +0.0145168 0.738548 0.0168827 0.000600631 +0.0146597 0.68651 0.0159742 0.000606576 +0.014804 0.58224 0.0135021 0.000612582 +0.0149498 0.446855 0.0100626 0.000618648 +0.015097 0.392461 0.00915549 0.000624775 +0.0152458 0.320517 0.00731959 0.000630965 +0.0153961 0.281006 0.00639909 0.000637217 +0.0155479 0.2401 0.00544239 0.000643532 +0.0157012 0.220881 0.00502437 0.000649911 +0.0158561 0.192033 0.00431441 0.000656354 +0.0160126 0.179849 0.00405159 0.000662863 +0.0161707 0.160069 0.00356202 0.000669437 +0.0163303 0.153129 0.00346777 0.000676078 +0.0164916 0.13422 0.003016 0.000682786 +0.0166545 0.12833 0.00288853 0.000689562 +0.016819 0.124794 0.00286182 0.000696406 +0.0169853 0.109127 0.0024831 0.000703319 +0.0171532 0.104429 0.00235392 0.000710302 +0.0173228 0.094683 0.00209162 0.000717355 +0.0175881 0.0896911 0.00143938 0.000728391 +0.0178419 0.0809144 0.00187278 0.000738945 +0.0180185 0.0746544 0.00171422 0.000746287 +0.0181968 0.0703661 0.00163423 0.000753704 +0.018377 0.0690445 0.0016053 0.000761195 +0.0185589 0.0627055 0.00146146 0.000768762 +0.0187427 0.0593915 0.00140007 0.000776405 +0.0189284 0.0575477 0.00136095 0.000784125 +0.0191159 0.0513833 0.00122668 0.000791923 +0.0193054 0.0492267 0.00117824 0.0007998 +0.0194967 0.0452174 0.00107962 0.000807755 +0.01969 0.0424556 0.00102225 0.000815792 +0.0198852 0.0412613 0.000994781 0.000823909 +0.0200824 0.0352333 0.00087779 0.000832108 +0.0202815 0.0335271 0.000836747 0.000840389 +0.0204827 0.0332684 0.000833959 0.000848754 +0.0206859 0.0316644 0.000789886 0.000857204 +0.0208912 0.02916 0.000749613 0.000865738 +0.0210985 0.0265201 0.000706383 0.000874359 +0.021308 0.0251829 0.000671389 0.000883066 +0.0215195 0.0238757 0.000642693 0.000891861 +0.0217331 0.0228929 0.000633068 0.000900745 +0.021949 0.0208646 0.000593128 0.000909718 +0.0221669 0.0208771 0.00059201 0.000918781 +0.0223871 0.0182228 0.000529205 0.000927936 +0.0226095 0.0177346 0.000525152 0.000937183 +0.0228341 0.0158714 0.000481877 0.000946523 +0.023061 0.0143255 0.000451619 0.000955957 +0.0232902 0.0142776 0.000452249 0.000965486 +0.0235217 0.0126624 0.000419845 0.000975112 +0.0237555 0.0122128 0.000413786 0.000984834 +0.0239917 0.0104608 0.000374532 0.000994654 +0.0242302 0.0106133 0.000381052 0.00100457 +0.0244712 0.00987903 0.000364128 0.00101459 +0.0247145 0.00837203 0.000331393 0.00102471 +0.0249603 0.00767048 0.000307705 0.00103493 +0.0252086 0.00734489 0.000304978 0.00104526 +0.0254594 0.00679865 0.000289868 0.00105569 +0.0257127 0.0059163 0.000267178 0.00106622 +0.0259685 0.00534498 0.000251536 0.00107686 +0.0262269 0.00512265 0.000247412 0.0010876 +0.026488 0.00475031 0.000237953 0.00109846 +0.0267516 0.00430715 0.00022385 0.00110942 +0.0270179 0.00401817 0.000220051 0.0011205 +0.0272868 0.00353915 0.000204653 0.00113168 +0.0275585 0.00381819 0.000207944 0.00114298 +0.0278329 0.00286475 0.000181921 0.0011544 +0.02811 0.0027958 0.000176691 0.00116592 +0.02839 0.0026215 0.000175002 0.00117757 +0.0286727 0.00248477 0.000169456 0.00118933 +0.0289583 0.00242009 0.000170925 0.00120121 +0.0292467 0.00235926 0.00017006 0.0012132 +0.0295381 0.00197856 0.000160021 0.00122532 +0.0298323 0.0019472 0.000158282 0.00123757 +0.0301296 0.00173593 0.000152731 0.00124993 +0.0304298 0.00189459 0.000160305 0.00126242 +0.030733 0.00169668 0.000152574 0.00127504 +0.0310392 0.00179369 0.000159245 0.00128778 +0.0313486 0.00178686 0.000155202 0.00130065 +0.031661 0.00187201 0.000158395 0.00131365 +0.0319766 0.00168818 0.000149984 0.00132678 +0.0322953 0.00173237 0.000154325 0.00134004 +0.0326173 0.0015376 0.000145396 0.00135344 +0.0329424 0.00154134 0.000149756 0.00136697 +0.0332708 0.00170033 0.000157208 0.00138064 +0.0336026 0.00214224 0.000172887 0.00139445 +0.0339376 0.00194402 0.000168689 0.0014084 +0.0342205 0.001914 0.000107714 0.00141748 +0.0346645 0.00198641 0.000143828 0.00143727 +0.0350219 0.00184974 0.000127974 0.00145177 +0.035385 0.00193264 0.000122708 0.00146643 +0.0357162 0.00218899 0.000149598 0.00148083 +0.0360773 0.00231432 0.000150345 0.00149568 +0.0364677 0.00183177 0.000104159 0.00151093 +0.0368263 0.00179715 0.000101292 0.00152596 +0.0371616 0.00227874 0.000141733 0.0015409 +0.0375421 0.00218339 0.00012946 0.00155639 +0.0379404 0.001799 9.62498e-05 0.00157213 +0.0383128 0.00182808 9.73464e-05 0.00158778 +0.038684 0.00194985 0.00010826 0.00160356 +0.0390692 0.00204214 0.000112245 0.00161957 +0.0394734 0.00165946 8.39554e-05 0.00163584 +0.0398633 0.00162031 8.07338e-05 0.00165215 +0.0402658 0.00158862 7.61769e-05 0.00166867 +0.0406608 0.00158251 7.82912e-05 0.0016853 +0.0410701 0.0014299 7.03445e-05 0.00170214 +0.0414781 0.00133612 6.49882e-05 0.00171913 +0.0418744 0.0017219 9.20006e-05 0.00173625 +0.0423049 0.00140489 6.78817e-05 0.00175363 +0.0427317 0.00114648 5.31515e-05 0.00177115 +0.0431546 0.00103013 4.8728e-05 0.00178884 +0.0435835 0.00101864 4.85163e-05 0.0018067 +0.0440155 0.00106025 5.14888e-05 0.00182475 +0.0444549 0.000911561 4.51767e-05 0.00184298 +0.0448983 0.000779536 3.74676e-05 0.00186138 +0.045347 0.000595757 2.86778e-05 0.00187998 +0.0457947 0.000647645 3.28669e-05 0.00189877 +0.0462512 0.000526839 2.66621e-05 0.00191774 +0.0467124 0.000436546 2.24095e-05 0.00193691 +0.0471783 0.000369605 1.92965e-05 0.00195626 +0.0476469 0.000331564 1.80855e-05 0.00197583 +0.0481208 0.000254584 1.44062e-05 0.00199557 +0.0485994 0.00020908 1.31243e-05 0.00201553 +0.0490828 0.000192937 1.26159e-05 0.00203569 +0.049571 0.000130922 9.99222e-06 0.00205605 +0.050064 0.000109823 9.08051e-06 0.00207663 +0.0505619 9.56548e-05 8.37009e-06 0.00209741 +0.0510655 7.92572e-05 7.69913e-06 0.00211839 +0.0515734 7.95605e-05 6.89856e-06 0.0021396 +0.0520864 7.22495e-05 6.10273e-06 0.00216102 +0.0526045 7.82513e-05 6.39136e-06 0.00218266 +0.0531279 8.63712e-05 6.56186e-06 0.00220452 +0.0536565 0.000133162 8.78984e-06 0.0022266 +0.0541905 0.000143632 8.17884e-06 0.00224891 +0.0547298 0.000158682 8.56534e-06 0.00227144 +0.0552745 0.000190007 9.75524e-06 0.00229421 +0.0558247 0.00023542 1.14172e-05 0.00231721 +0.0563804 0.000233932 1.03452e-05 0.00234044 +0.0569417 0.00024339 1.05955e-05 0.0023639 +0.0575087 0.000270858 1.12513e-05 0.00238761 +0.0580813 0.000310966 1.23355e-05 0.00241156 +0.0586597 0.000348327 1.35786e-05 0.00243575 +0.0592439 0.000332957 1.27559e-05 0.0024602 +0.059834 0.000354659 1.32236e-05 0.00248489 +0.06043 0.000354467 1.30661e-05 0.00250983 +0.0610319 0.000385515 1.40893e-05 0.00253502 +0.06164 0.000332509 1.20176e-05 0.00256048 +0.0622541 0.000333087 1.19715e-05 0.00258619 +0.0628744 0.000330307 1.19723e-05 0.00261217 +0.0635009 0.000322469 1.16291e-05 0.00263842 +0.0641337 0.000274462 9.94879e-06 0.00266493 +0.0647728 0.000279243 1.02537e-05 0.00269171 +0.0654184 0.000238938 8.96085e-06 0.00271877 +0.0660704 0.000233028 8.72665e-06 0.00274611 +0.066729 0.000186343 7.23614e-06 0.00277372 +0.0673942 0.000164949 6.6195e-06 0.00280162 +0.068066 0.000133244 5.54157e-06 0.00282981 +0.0687446 0.000115376 4.97162e-06 0.00285828 +0.06943 8.25033e-05 3.95115e-06 0.00288705 +0.0701223 6.71923e-05 3.57018e-06 0.00291611 +0.0708215 5.03322e-05 2.92623e-06 0.00294547 +0.0715278 3.33612e-05 2.5097e-06 0.00297513 +0.0722411 2.29052e-05 2.11638e-06 0.0030051 +0.0729338 1.87944e-05 1.86053e-06 0.00303317 +0.0736683 1.55344e-05 1.79821e-06 0.00306431 +0.0744015 1.82452e-05 1.95184e-06 0.00309508 +0.0751405 2.23562e-05 1.801e-06 0.00312604 +0.0758721 2.77362e-05 1.89014e-06 0.00315615 +0.0766402 3.93101e-05 2.30095e-06 0.00318888 +0.0773964 4.59669e-05 2.3324e-06 0.00322041 +0.0781061 5.54131e-05 2.45926e-06 0.00324807 +0.0788682 6.72605e-05 2.72957e-06 0.00327954 +0.0797953 7.65064e-05 2.77309e-06 0.00331828 +0.0807289 7.79472e-05 2.85741e-06 0.00335684 +0.0815334 8.50542e-05 2.9904e-06 0.00339073 +0.0823318 9.68569e-05 3.2227e-06 0.00342487 +0.0831605 9.4624e-05 3.16621e-06 0.00345952 +0.0840252 8.58564e-05 2.82446e-06 0.00349469 +0.0848648 8.10144e-05 2.66254e-06 0.00353001 +0.0857342 7.65592e-05 2.54978e-06 0.00356583 +0.0865553 7.38543e-05 2.48584e-06 0.00360169 +0.0874372 6.24971e-05 2.19092e-06 0.00363822 +0.0883163 5.49543e-05 2.00127e-06 0.00367507 +0.0891108 5.69454e-05 2.08158e-06 0.00371178 +0.0900416 4.2292e-05 1.72971e-06 0.00374959 +0.0909546 3.36754e-05 1.45891e-06 0.00378764 +0.0918504 2.54564e-05 1.24963e-06 0.00382594 +0.0927435 2.09221e-05 1.15976e-06 0.00386459 +0.0936289 1.53112e-05 1.06014e-06 0.00390356 +0.0945527 1.26565e-05 9.57753e-07 0.0039431 +0.0955178 9.33124e-06 8.62139e-07 0.0039832 +0.0964771 8.90152e-06 7.82897e-07 0.00402366 +0.0974189 1.07915e-05 8.93847e-07 0.0040644 +0.0984259 1.21368e-05 9.11308e-07 0.00410584 +0.0994201 1.5408e-05 9.07777e-07 0.0041476 +0.100425 1.93803e-05 9.60253e-07 0.00418981 +0.101434 2.09116e-05 1.04089e-06 0.00423243 +0.102499 2.51925e-05 1.07837e-06 0.0042757 +0.103498 2.8188e-05 1.13217e-06 0.00431908 +0.104546 2.9981e-05 1.17853e-06 0.0043631 +0.105608 3.05628e-05 1.15049e-06 0.0044076 +0.106682 2.81903e-05 1.09907e-06 0.00445257 +0.107758 2.69263e-05 1.0374e-06 0.00449797 +0.108831 2.54192e-05 1.01256e-06 0.0045438 +0.109916 2.2827e-05 9.3671e-07 0.00459012 +0.11104 1.76917e-05 7.9045e-07 0.00463704 +0.112117 1.54751e-05 7.6254e-07 0.00468423 +0.113244 1.17258e-05 6.68805e-07 0.00473208 +0.114319 1.05027e-05 6.59805e-07 0.00478019 +0.115464 8.12153e-06 5.84703e-07 0.00482905 +0.116645 4.98884e-06 5.26719e-07 0.00487853 +0.117762 4.90999e-06 5.36096e-07 0.00492824 +0.118925 5.30434e-06 5.24084e-07 0.00497863 +0.120129 5.68905e-06 4.91234e-07 0.00502965 +0.121346 6.44471e-06 5.06107e-07 0.00508123 +0.122586 7.45619e-06 5.05803e-07 0.0051334 +0.123823 8.24842e-06 5.0256e-07 0.00518608 +0.125091 1.06749e-05 5.84539e-07 0.00523939 +0.126361 1.13162e-05 5.72228e-07 0.00529323 +0.127643 1.1044e-05 5.76255e-07 0.00534765 +0.128932 1.06258e-05 5.65534e-07 0.00540265 +0.13021 9.33766e-06 5.25472e-07 0.00545814 +0.131547 8.9228e-06 5.00068e-07 0.00551441 +0.132854 6.43611e-06 4.44688e-07 0.00557113 +0.134162 6.18113e-06 4.55625e-07 0.00562844 +0.135515 4.68568e-06 4.03478e-07 0.00568647 +0.136824 4.66754e-06 3.80929e-07 0.00574496 +0.138204 4.2625e-06 3.76301e-07 0.00580426 +0.139589 3.8843e-06 3.57158e-07 0.00586419 +0.140989 3.69948e-06 3.79662e-07 0.00592478 +0.14243 3.65993e-06 3.54338e-07 0.00598611 +0.143851 4.55939e-06 3.66569e-07 0.00604802 +0.145263 4.58088e-06 3.47062e-07 0.00611055 +0.14675 4.82929e-06 3.50392e-07 0.00617393 +0.148225 4.93092e-06 3.5333e-07 0.00623794 +0.14971 4.781e-06 3.61256e-07 0.00630265 +0.151225 4.64484e-06 3.39711e-07 0.00636812 +0.152736 4.36508e-06 3.32459e-07 0.00643425 +0.154261 3.80706e-06 3.25378e-07 0.00650113 +0.155825 3.54415e-06 3.05388e-07 0.00656881 +0.157366 2.63269e-06 2.77355e-07 0.00663715 +0.158941 2.12968e-06 2.57061e-07 0.0067063 +0.160511 2.50908e-06 2.68417e-07 0.00677617 +0.162137 2.60626e-06 2.68354e-07 0.00684694 +0.163755 2.46796e-06 2.61811e-07 0.00691843 +0.165404 2.43769e-06 2.45708e-07 0.00699078 +0.167035 2.81846e-06 2.62635e-07 0.00706385 +0.168736 3.20191e-06 2.67722e-07 0.00713789 +0.17041 3.02096e-06 2.54866e-07 0.00721266 +0.172119 2.39855e-06 2.32464e-07 0.00728832 +0.173839 2.45824e-06 2.38546e-07 0.00736483 +0.175574 2.03366e-06 2.23196e-07 0.00744222 +0.177336 1.68404e-06 2.04828e-07 0.00752051 +0.179087 1.29383e-06 2.00765e-07 0.00759963 +0.180907 1.43967e-06 1.88207e-07 0.00767979 +0.182715 1.5402e-06 1.99998e-07 0.0077608 +0.184535 1.33849e-06 1.86482e-07 0.00784273 +0.186383 1.61599e-06 1.86369e-07 0.00792563 +0.188247 1.49347e-06 1.88386e-07 0.00800949 +0.190126 2.05968e-06 1.9207e-07 0.00809433 +0.192038 1.69015e-06 1.7677e-07 0.00818019 +0.193954 1.48837e-06 1.79274e-07 0.00826701 +0.1959 1.40382e-06 1.74728e-07 0.00835488 +0.19786 1.34689e-06 1.70569e-07 0.00844376 +0.199845 9.4771e-07 1.61779e-07 0.00853372 +0.201833 1.08955e-06 1.73502e-07 0.00862469 +0.203863 1.24635e-06 1.65466e-07 0.0087168 +0.205904 1.22737e-06 1.6449e-07 0.00880998 +0.207959 1.16896e-06 1.58456e-07 0.00890425 +0.210042 1.16172e-06 1.52198e-07 0.00899967 +0.21214 1.21689e-06 1.58854e-07 0.00909622 +0.214257 1.31757e-06 1.54607e-07 0.00919392 +0.216399 1.03279e-06 1.47292e-07 0.00929281 +0.21857 1.03066e-06 1.51473e-07 0.00939292 +0.220781 5.94535e-07 1.51172e-07 0.00949429 +0.222988 7.27996e-07 1.42928e-07 0.00959682 +0.225216 7.80631e-07 1.48525e-07 0.0097006 +0.227467 1.06339e-06 1.4993e-07 0.00980565 +0.229741 6.52697e-07 1.29668e-07 0.00991199 +0.232037 1.07438e-06 1.48453e-07 0.0100196 +0.234356 8.68763e-07 1.46862e-07 0.0101286 +0.236698 9.21868e-07 1.45082e-07 0.0102389 +0.239064 6.47149e-07 1.36809e-07 0.0103506 +0.241454 5.36393e-07 1.35688e-07 0.0104637 +0.243867 6.33717e-07 1.30403e-07 0.0105782 +0.246304 6.2927e-07 1.36557e-07 0.0106942 +0.248766 6.33292e-07 1.22453e-07 0.0108116 +0.251253 1.03705e-06 1.35409e-07 0.0109304 +0.253764 8.25286e-07 1.43161e-07 0.0110508 +0.256301 6.26825e-07 1.19257e-07 0.0111727 +0.258863 5.25259e-07 1.22741e-07 0.0112962 +0.26145 5.21832e-07 1.30918e-07 0.0114212 +0.264064 3.91659e-07 1.2137e-07 0.0115479 +0.266703 4.72439e-07 1.39973e-07 0.0116762 +0.269369 5.59536e-07 1.39151e-07 0.0118061 +0.272062 6.6407e-07 1.44867e-07 0.0119377 +0.274782 4.59378e-07 1.51466e-07 0.012071 +0.277529 3.66961e-07 1.43546e-07 0.0122061 +0.280303 5.31531e-07 1.48308e-07 0.012343 +0.283105 4.28914e-07 1.36145e-07 0.0124816 +0.285935 5.52006e-07 1.41983e-07 0.0126221 +0.288793 5.70264e-07 1.529e-07 0.0127644 +0.29168 5.04731e-07 1.36429e-07 0.0129086 +0.294596 6.61923e-07 1.40793e-07 0.0130548 +0.297541 7.60132e-07 1.60721e-07 0.0132029 +0.300516 4.68527e-07 1.34887e-07 0.0133529 +0.30352 4.4286e-07 1.47134e-07 0.0135051 +0.306554 4.89979e-07 1.4043e-07 0.0136592 +0.309619 3.60163e-07 1.33728e-07 0.0138155 +0.312714 2.90563e-07 1.48062e-07 0.0139739 +0.315841 5.19963e-07 1.51562e-07 0.0141344 +0.318998 4.66665e-07 1.56256e-07 0.0142972 +0.322187 3.88883e-07 1.57011e-07 0.0144622 +0.325408 4.46778e-07 1.55512e-07 0.0146295 +0.328662 3.23885e-07 1.52586e-07 0.0147991 +0.331947 3.78736e-07 1.5818e-07 0.0149711 +0.335266 3.88199e-07 1.53911e-07 0.0151455 +0.338618 4.42877e-07 1.49098e-07 0.0153224 +0.342003 2.47787e-07 1.46321e-07 0.0155017 +0.345423 2.85769e-07 1.35135e-07 0.0156836 +0.348876 4.75964e-07 1.53664e-07 0.0158681 +0.352364 4.49639e-07 1.53956e-07 0.0160552 +0.355887 2.47147e-07 1.44322e-07 0.0162449 +0.359445 2.16764e-07 1.49181e-07 0.0164375 +0.363039 5.20585e-07 1.57369e-07 0.0166327 +0.366669 5.24697e-07 1.54541e-07 0.0168309 +0.370335 3.64403e-07 1.55024e-07 0.0170319 +0.374037 4.59752e-07 1.65308e-07 0.0172358 +0.377777 5.35922e-07 1.82043e-07 0.0174427 +0.381554 4.26807e-07 1.77003e-07 0.0176527 +0.385369 4.03666e-07 1.8111e-07 0.0178658 +0.389222 2.55454e-07 1.59729e-07 0.018082 +0.393113 9.26972e-08 1.65402e-07 0.0183015 +0.397044 1.10672e-07 1.80888e-07 0.0185242 +0.401014 4.52163e-07 1.73191e-07 0.0187503 +0.405023 3.78066e-07 1.51597e-07 0.0189798 +0.409073 3.09136e-07 1.57195e-07 0.0192128 +0.413163 3.41774e-07 1.44818e-07 0.0194493 +0.417294 3.44924e-07 1.59581e-07 0.0196894 +0.421466 2.5184e-07 1.59757e-07 0.0199332 +0.42568 4.01737e-07 1.53814e-07 0.0201807 +0.429936 3.17279e-07 1.6913e-07 0.0204321 +0.434235 5.50631e-07 1.61142e-07 0.0206873 +0.438577 5.0851e-07 1.6499e-07 0.0209465 +0.442962 6.02593e-07 1.73835e-07 0.0212097 +0.447391 4.38454e-07 1.6535e-07 0.0214771 +0.451865 3.38757e-07 1.87639e-07 0.0217487 +0.456383 4.35846e-07 1.97826e-07 0.0220245 +0.460946 3.85579e-07 1.76143e-07 0.0223047 +0.465555 3.83415e-07 1.88454e-07 0.0225894 diff --git a/RATapi/examples/orso_integration/orso_integration.ipynb b/RATapi/examples/orso_integration/orso_integration.ipynb new file mode 100644 index 00000000..aa6f0797 --- /dev/null +++ b/RATapi/examples/orso_integration/orso_integration.ipynb @@ -0,0 +1,188 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ``orsopy`` Integration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "``python-RAT`` contains some integration with ``orsopy``, allowing for convenient interaction with the ``.ort`` file format. This integration is available through the `RATapi.utils.orso` submodule." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import RATapi.utils.orso" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creating models from the ORSO model description language\n", + "\n", + "The [ORSO model description format](https://www.reflectometry.org/advanced_and_expert_level/file_format/simple_model) allows the description of a standard slab model as a one-line string, provided that all the layer materials are defined [in the ORSO SLD database](https://slddb.esss.dk/slddb/).\n", + "\n", + "The function `RATapi.utils.orso.orso_model_to_rat` function can read a model and return an `ORSOSample` dataclass, which gives bulk in and bulk out parameters for the model, a list of all layers defined in the model, and all the parameters needed to define those layers as RAT models. \n", + "\n", + "**Note:** the ORSO format gives the thicknesses of materials in *nanometres*. When we convert them to RAT parameters, the units will be converted to Angstroms.\n", + "\n", + "For example, the string `air | Ni 100 | SiO2 0.5 | Si` describes a 1000 angstrom nickel film backed by a 5 angstrom silicon oxide layer. The bulk-in and bulk-out are air and silicon respectively. The roughnesses and SLDs will be calculated or taken from the ORSO SLD database." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create the RAT parameters and layers from this model\n", + "sample = RATapi.utils.orso.orso_model_to_rat(\"air | Ni 100 | SiO2 0.5 | Si\")\n", + "print(sample)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also set `absorption=True` and the model will account for absorption. For example if we change the nickel film for a boron carbide film and want to account for its relatively high absorption, we can add it to the output:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample = RATapi.utils.orso.orso_model_to_rat(\"vacuum | B4C 100 | SiO2 0.5 | Si\", absorption=True)\n", + "print(sample)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, ORSO supports defining repeated layers using parentheses. For example, if we had a polarising multilayer of 5 repetitions of 70 angstrom silicon and 70 angstrom iron, we could represent it as `air | 5 ( Si 7 | Fe 7 ) | Si`.\n", + "\n", + "RAT will only create the number of layers and parameters necessary, but the `ORSOSample` object's `model` attribute will give a list of layer names with the structure of the model preserved, which can be given as the layer model for a Contrast." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample = RATapi.utils.orso.orso_model_to_rat(\"air | 5 ( Si 7 | Fe 7 ) | Si\")\n", + "print(sample)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reading in data and models from .ort files" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "RAT can also load both data and model information from an .ort file. This is done through the `ORSOProject` object, which takes a file path and can also optionally account for absorption.\n", + "\n", + "The example data file we use here is example data for an unknown film on deposited on silicon." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pathlib\n", + "data_path = pathlib.Path(\"../data\")\n", + "\n", + "orso_data = RATapi.utils.orso.ORSOProject(data_path / \"c_PLP0011859_q.ort\")\n", + "print(orso_data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `ORSOProject` object contains two lists: `ORSOProject.data` and `ORSOProject.samples`. The former is a list of Data objects with each dataset defined in the file, and the latter is a list of `ORSOSample` objects (like above) with model information. Note that if the .ort file does not define a model for a dataset, that index of `ORSOProject.samples` will be None.\n", + "\n", + "It's then easy to access this data to create a RAT `Project` that represents our data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from RATapi.models import Background, Contrast, Parameter, Resolution\n", + "\n", + "dataset = orso_data.data[0]\n", + "sample = orso_data.samples[0]\n", + "\n", + "project = RATapi.Project(\n", + " name = \"Example Project\",\n", + " geometry = \"substrate/liquid\",\n", + " parameters = sample.parameters,\n", + " bulk_in = [sample.bulk_in],\n", + " bulk_out = [sample.bulk_out],\n", + " scalefactors = [Parameter(name=\"Scalefactor\", min=0, value=0.34, max=1.5)],\n", + " background_parameters = [Parameter(name=\"Background Parameter\", min=0, value=2e-6, max=1)],\n", + " backgrounds = [Background(name=\"Background\", type=\"constant\", source=\"Background Parameter\")],\n", + " resolutions = [Resolution(name=\"Data Resolution\", type=\"data\")],\n", + " data = [dataset],\n", + " layers = sample.layers,\n", + " contrasts = [Contrast(\n", + " name = \"prist4\",\n", + " data = dataset.name,\n", + " background = \"Background\",\n", + " bulk_in = sample.bulk_in.name,\n", + " bulk_out = sample.bulk_out.name,\n", + " scalefactor = \"Scalefactor\",\n", + " resolution = \"Data Resolution\",\n", + " model = sample.model,\n", + " )]\n", + ")\n", + "\n", + "controls = RATapi.Controls()\n", + "project, results = RATapi.run(project, controls)\n", + "RATapi.plotting.plot_ref_sld(project, results)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/RATapi/utils/orso.py b/RATapi/utils/orso.py new file mode 100644 index 00000000..50550619 --- /dev/null +++ b/RATapi/utils/orso.py @@ -0,0 +1,248 @@ +"""Readers from file formats.""" + +from dataclasses import dataclass +from itertools import count +from pathlib import Path +from textwrap import shorten +from typing import Union + +import orsopy +import prettytable +from orsopy.fileio import load_orso + +from RATapi import ClassList +from RATapi.models import AbsorptionLayer, Data, Layer, Parameter + + +class ORSOProject: + """A class to encapsulate model information and data from an .ort file. + + Parameters + ---------- + filepath : str or Path + The path to the .ort file. + absorption : bool, default None + Whether to account for absorption in the model data. + + """ + + def __init__(self, filepath: Union[str, Path], absorption: bool = False): + ort_data = load_orso(filepath) + datasets = [Data(name=dataset.info.data_source.sample.name, data=dataset.data) for dataset in ort_data] + # orso datasets in the same file can have repeated names! + # but classlists do not allow this + # use this dict to keep track of counts for repeated names + name_counts = {d.name: count(1) for d in datasets} + names = [d.name for d in datasets] + if len(names) > len(list(set(names))): + for i, data in enumerate(datasets): + if data.name in names[:i]: + data.name += f"-{next(name_counts[data.name])}" + self.data = ClassList(datasets) + self.samples = [ + orso_model_to_rat(dataset.info.data_source.sample.model, absorption=absorption) for dataset in ort_data + ] + + def __str__(self): + data_str = f"Data:\n{str(self.data)}\n\n" + if len(self.samples) == 1: + samples_str = f"Sample:\n{str(self.samples[0])}" + else: + table = prettytable.PrettyTable() + table.field_names = ["index", "bulk in", "bulk out", "parameters", "layers", "model"] + for index, sample in enumerate(self.samples): + if sample is None: + row = [index, "", "", "", "", ""] + else: + row = [ + index, + sample.bulk_in.name, + sample.bulk_out.name, + shorten(", ".join([p.name for p in sample.parameters]), width=20, placeholder="..."), + shorten(", ".join([layer.name for layer in sample.layers]), width=20, placeholder="..."), + shorten(str(sample.model), width=20, placeholder="..."), + ] + table.add_row(row) + samples_str = table.get_string() + + return data_str + samples_str + + +@dataclass +class ORSOSample: + """The stack data from an ORSO SampleModel, in RAT models.""" + + bulk_in: Parameter + bulk_out: Parameter + parameters: ClassList[Parameter] + layers: Union[ClassList[Layer], ClassList[AbsorptionLayer]] + model: list[str] + + def __str__(self): + return ( + "Bulk in:\n" + f"{str(self.bulk_in)}\n\n" + "Bulk out:\n" + f"{str(self.bulk_out)}\n\n" + "Parameters:\n" + f"{str(self.parameters)}\n\n" + "Layers:\n" + f"{str(self.layers)}\n\n" + "Model:\n" + f"{self.model}" + ) + + +def orso_model_to_rat( + model: Union[orsopy.fileio.model_language.SampleModel, str], absorption: bool = False +) -> Union[ORSOSample, None]: + """Get information from an ORSO SampleModel object. + + Parameters + ---------- + model : orsopy.fileio.model_language.SampleModel or str + The sample model to turn into a RAT data. If given as a string, + the string is interpreted as a layer stack in ORSO model language. + absorption : bool, default False + Whether to account for absorption in the model. + + Returns + ------- + ORSOSample + A dataclass containing the sample data, or None if the model is None. + + """ + if model is None: + return None + + if isinstance(model, str): + model = orsopy.fileio.model_language.SampleModel(stack=model) + + stack = model.resolve_to_layers() + # if bulk in or out is air, it has SLD predefined + # else we need to grab it from SLDDB + if bulk_in_sld := stack[0].material.sld is None: + bulk_in_sld = stack[0].material.get_sld() + + # resolve_to_layers loses the name of bulk in and out + bulk_in_name = model.stack.split("|")[0].strip() + bulk_in = Parameter( + name=f"{bulk_in_name} SLD", + min=bulk_in_sld.real, + value=bulk_in_sld.real, + max=bulk_in_sld.real, + fit=False, + ) + + if bulk_out_sld := stack[-1].material.sld is None: + bulk_out_sld = stack[-1].material.get_sld() + + bulk_out_name = model.stack.split("|")[-1].strip() + bulk_out = Parameter( + name=f"{bulk_out_name} SLD", + min=bulk_out_sld.real, + value=bulk_out_sld.real, + max=bulk_out_sld.real, + fit=False, + ) + + parameters = ClassList() + layers = ClassList() + contrast_model = [] + + for orso_layer in stack[1:-1]: + name = get_material_name(orso_layer.material, model) + contrast_model.append(name) + layer_params, layer = orso_layer_to_rat_layer(orso_layer, name, absorption) + parameters.union(layer_params) + layers.union(layer) + + return ORSOSample( + bulk_in=bulk_in, + bulk_out=bulk_out, + parameters=parameters, + layers=layers, + model=contrast_model, + ) + + +def orso_layer_to_rat_layer( + layer: orsopy.fileio.model_language.Layer, name: str, absorption: bool = False +) -> tuple[ClassList[Parameter], Layer]: + """Convert an ``orsopy`` layer to a RAT layer. + + Parameters + ---------- + layer : orsopy.fileio.model_language.Layer + An ``orsopy`` Layer. + name : str + The name of the material in the layer. + absorption : bool, default True + Whether absorption should be accounted for in the layer. + + Returns + ------- + ClassList[Parameter], Layer + The parameters required for the RAT layer and the layer itself. + + """ + thickness = layer.thickness.as_unit("angstrom") + roughness = layer.roughness.as_unit("angstrom") + sld = layer.material.get_sld() + + params = ClassList( + [ + Parameter(name=f"{name} Thickness", min=thickness, value=thickness, max=thickness, fit=False), + Parameter(name=f"{name} Roughness", min=roughness, value=roughness, max=roughness, fit=False), + Parameter(name=f"{name} SLD", min=sld.real, value=sld.real, max=sld.real, fit=False), + ] + ) + if absorption: + params.append(Parameter(name=f"{name} SLD imaginary", min=sld.imag, value=sld.imag, max=sld.imag, fit=False)) + layer = AbsorptionLayer( + name=name, + thickness=f"{name} Thickness", + roughness=f"{name} Roughness", + SLD_real=f"{name} SLD", + SLD_imaginary=f"{name} SLD imaginary", + ) + else: + layer = Layer( + name=name, + thickness=f"{name} Thickness", + roughness=f"{name} Roughness", + SLD=f"{name} SLD", + ) + + return params, layer + + +def get_material_name( + material: orsopy.fileio.model_language.Material, model: orsopy.fileio.model_language.SampleModel +) -> str: + """Get the name of a material in the model. + + Layers with custom property definitions may not have a formula, so this adjusts the name for that. + + Parameters + ---------- + material : Material + The material to get the name of. + model : SampleModel + The sample model from which the material came. + + Returns + ------- + str + The name of the material. + + """ + if material.formula is not None: + return material.formula + else: + matching_materials = [k for k, v in model.materials.items() if v == material] + if matching_materials: + return matching_materials[0] + else: + # orsopy should catch that this is the case before we get here, but just in case... + raise ValueError("ORSO model contains layers with undefined materials!") diff --git a/requirements.txt b/requirements.txt index 47a268b5..cee9a790 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,5 @@ StrEnum >= 0.4.15; python_version < '3.11' ruff >= 0.4.10 scipy >= 1.13.1 tqdm >= 4.66.5 +orsopy >= 1.2.1 +pint >= 0.24.4 diff --git a/setup.py b/setup.py index a3bb0ea4..d41a6ce8 100644 --- a/setup.py +++ b/setup.py @@ -172,11 +172,12 @@ def build_libraries(self, libraries): "pydantic >= 2.7.2", "matplotlib >= 3.8.3", "scipy >= 1.13.1", - "tqdm>=4.66.5", + "tqdm >= 4.66.5", ], extras_require={ ':python_version < "3.11"': ["StrEnum >= 0.4.15"], "Dev": ["pytest>=7.4.0", "pytest-cov>=4.1.0", "ruff>=0.4.10"], + "Orso": ["orsopy>=1.2.1", "pint>=0.24.4"], "Matlab_latest": ["matlabengine"], "Matlab_2025a": ["matlabengine == 25.1.*"], "Matlab_2024b": ["matlabengine == 24.2.2"], diff --git a/tests/test_data/bare_substrate.json b/tests/test_data/bare_substrate.json new file mode 100644 index 00000000..65e405c2 --- /dev/null +++ b/tests/test_data/bare_substrate.json @@ -0,0 +1 @@ +{"name": "substrate", "calculation": "normal", "model": "standard layers", "geometry": "air/substrate", "absorption": false, "parameters": [{"name": "Substrate Roughness", "min": 1.0, "value": 3.0, "max": 5.0, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_in": [{"name": "air SLD", "min": 4.42927130586151e-09, "value": 4.42927130586151e-09, "max": 4.42927130586151e-09, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_out": [{"name": "D2O SLD", "min": 6.360408603667384e-06, "value": 6.360408603667384e-06, "max": 6.360408603667384e-06, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "scalefactors": [{"name": "Scalefactor 1", "min": 0.02, "value": 0.23, "max": 0.25, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "domain_ratios": [], "background_parameters": [{"name": "Background Param 1", "min": 1e-07, "value": 1e-06, "max": 1e-05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "backgrounds": [{"name": "Background 1", "type": "constant", "source": "Background Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "resolution_parameters": [{"name": "Resolution Param 1", "min": 0.01, "value": 0.03, "max": 0.05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "resolutions": [{"name": "Resolution 1", "type": "constant", "source": "Resolution Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "custom_files": [], "data": [{"name": "Simulation", "data": [], "data_range": [], "simulation_range": [0.005, 0.7]}, {"name": "D2O substrate", "data": [[0.048866, 0.00012343, 1.3213e-06, 0.03], [0.051309, 0.00010063, 1.0803e-06, 0.03], [0.053874, 8.2165e-05, 8.8779e-07, 0.03], [0.056568, 6.4993e-05, 7.2018e-07, 0.03], [0.059396, 5.3958e-05, 6.0015e-07, 0.03], [0.062366, 4.359e-05, 5.0129e-07, 0.03], [0.065485, 3.578e-05, 4.1957e-07, 0.03], [0.068759, 2.913e-05, 3.5171e-07, 0.03], [0.072197, 2.3481e-05, 3.0586e-07, 0.03], [0.075807, 1.8906e-05, 2.6344e-07, 0.03], [0.079597, 1.4642e-05, 2.2314e-07, 0.03], [0.083577, 1.1589e-05, 1.8938e-07, 0.03], [0.087756, 9.5418e-06, 1.622e-07, 0.03], [0.092143, 7.5694e-06, 1.3809e-07, 0.03], [0.096751, 6.3831e-06, 1.2097e-07, 0.03], [0.10159, 5.0708e-06, 1.0333e-07, 0.03], [0.10667, 4.1041e-06, 8.9548e-08, 0.03], [0.112, 3.4253e-06, 7.983e-08, 0.03], [0.1176, 2.8116e-06, 7.1554e-08, 0.03], [0.12348, 2.3767e-06, 6.3738e-08, 0.03], [0.12966, 1.9241e-06, 5.6586e-08, 0.03], [0.13614, 1.5642e-06, 5.2778e-08, 0.03], [0.14294, 1.2922e-06, 4.973e-08, 0.03], [0.15009, 1.1694e-06, 5.1175e-08, 0.03], [0.1576, 9.7837e-07, 5.0755e-08, 0.03], [0.16548, 8.9138e-07, 5.3542e-08, 0.03], [0.17375, 7.942e-07, 5.4857e-08, 0.03], [0.18244, 7.9131e-07, 5.8067e-08, 0.03], [0.19156, 6.5358e-07, 5.7717e-08, 0.03], [0.20114, 6.297e-07, 5.7951e-08, 0.03], [0.21119, 5.013e-07, 5.5262e-08, 0.03], [0.22175, 5.0218e-07, 5.6461e-08, 0.03], [0.23284, 3.9299e-07, 5.0685e-08, 0.03], [0.24448, 3.5324e-07, 5.0194e-08, 0.03], [0.25671, 4.4475e-07, 5.6485e-08, 0.03], [0.26954, 5.1338e-07, 6.2247e-08, 0.03], [0.28302, 3.4918e-07, 4.9745e-08, 0.03], [0.29717, 4.3037e-07, 5.5488e-08, 0.03], [0.31203, 4.0099e-07, 5.3591e-08, 0.03], [0.32763, 3.8397e-07, 5.1303e-08, 0.03], [0.34401, 3.0995e-07, 4.5965e-08, 0.03], [0.36121, 3.9357e-07, 5.0135e-08, 0.03], [0.37927, 3.0997e-07, 4.368e-08, 0.03], [0.39824, 2.9656e-07, 4.2432e-08, 0.03], [0.41815, 2.1909e-07, 3.6117e-08, 0.03], [0.43906, 2.3153e-07, 3.6307e-08, 0.03], [0.46101, 3.3428e-07, 4.3874e-08, 0.03], [0.48406, 2.3441e-07, 3.7488e-08, 0.03], [0.50826, 1.5496e-07, 3.0585e-08, 0.03], [0.53368, 2.4708e-07, 3.9376e-08, 0.03], [0.56036, 2.2157e-07, 3.8258e-08, 0.03], [0.58838, 2.2798e-07, 4.6976e-08, 0.03], [0.61169, 6.0272e-07, 2.3239e-07, 0.03]], "data_range": [0.048866, 0.61169], "simulation_range": [0.048866, 0.61169]}], "layers": [], "domain_contrasts": [], "contrasts": []} \ No newline at end of file diff --git a/tests/test_data/bare_substrate.ort b/tests/test_data/bare_substrate.ort new file mode 100644 index 00000000..7e4ffb52 --- /dev/null +++ b/tests/test_data/bare_substrate.ort @@ -0,0 +1,83 @@ +# # ORSO reflectivity data file | 1.1 standard | YAML encoding | https://www.reflectometry.org/ +# # handwritten test file header created to test RAT orsopy integration! +# data_source: +# owner: +# name: null +# affiliation: null +# measurement: +# instrument_settings: null +# data_files: null +# experiment: +# title: Bare D2O substrate +# probe: neutron +# instrument: None +# start_date: 1970-01-01T00:00:00 +# sample: +# name: D2O substrate +# model: +# stack: air | D2O +# reduction: +# software: null +# timestamp: null +# 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 +4.8866e-02 1.2343e-04 1.3213e-06 0.03 +5.1309e-02 1.0063e-04 1.0803e-06 0.03 +5.3874e-02 8.2165e-05 8.8779e-07 0.03 +5.6568e-02 6.4993e-05 7.2018e-07 0.03 +5.9396e-02 5.3958e-05 6.0015e-07 0.03 +6.2366e-02 4.3590e-05 5.0129e-07 0.03 +6.5485e-02 3.5780e-05 4.1957e-07 0.03 +6.8759e-02 2.9130e-05 3.5171e-07 0.03 +7.2197e-02 2.3481e-05 3.0586e-07 0.03 +7.5807e-02 1.8906e-05 2.6344e-07 0.03 +7.9597e-02 1.4642e-05 2.2314e-07 0.03 +8.3577e-02 1.1589e-05 1.8938e-07 0.03 +8.7756e-02 9.5418e-06 1.6220e-07 0.03 +9.2143e-02 7.5694e-06 1.3809e-07 0.03 +9.6751e-02 6.3831e-06 1.2097e-07 0.03 +1.0159e-01 5.0708e-06 1.0333e-07 0.03 +1.0667e-01 4.1041e-06 8.9548e-08 0.03 +1.1200e-01 3.4253e-06 7.9830e-08 0.03 +1.1760e-01 2.8116e-06 7.1554e-08 0.03 +1.2348e-01 2.3767e-06 6.3738e-08 0.03 +1.2966e-01 1.9241e-06 5.6586e-08 0.03 +1.3614e-01 1.5642e-06 5.2778e-08 0.03 +1.4294e-01 1.2922e-06 4.9730e-08 0.03 +1.5009e-01 1.1694e-06 5.1175e-08 0.03 +1.5760e-01 9.7837e-07 5.0755e-08 0.03 +1.6548e-01 8.9138e-07 5.3542e-08 0.03 +1.7375e-01 7.9420e-07 5.4857e-08 0.03 +1.8244e-01 7.9131e-07 5.8067e-08 0.03 +1.9156e-01 6.5358e-07 5.7717e-08 0.03 +2.0114e-01 6.2970e-07 5.7951e-08 0.03 +2.1119e-01 5.0130e-07 5.5262e-08 0.03 +2.2175e-01 5.0218e-07 5.6461e-08 0.03 +2.3284e-01 3.9299e-07 5.0685e-08 0.03 +2.4448e-01 3.5324e-07 5.0194e-08 0.03 +2.5671e-01 4.4475e-07 5.6485e-08 0.03 +2.6954e-01 5.1338e-07 6.2247e-08 0.03 +2.8302e-01 3.4918e-07 4.9745e-08 0.03 +2.9717e-01 4.3037e-07 5.5488e-08 0.03 +3.1203e-01 4.0099e-07 5.3591e-08 0.03 +3.2763e-01 3.8397e-07 5.1303e-08 0.03 +3.4401e-01 3.0995e-07 4.5965e-08 0.03 +3.6121e-01 3.9357e-07 5.0135e-08 0.03 +3.7927e-01 3.0997e-07 4.3680e-08 0.03 +3.9824e-01 2.9656e-07 4.2432e-08 0.03 +4.1815e-01 2.1909e-07 3.6117e-08 0.03 +4.3906e-01 2.3153e-07 3.6307e-08 0.03 +4.6101e-01 3.3428e-07 4.3874e-08 0.03 +4.8406e-01 2.3441e-07 3.7488e-08 0.03 +5.0826e-01 1.5496e-07 3.0585e-08 0.03 +5.3368e-01 2.4708e-07 3.9376e-08 0.03 +5.6036e-01 2.2157e-07 3.8258e-08 0.03 +5.8838e-01 2.2798e-07 4.6976e-08 0.03 +6.1169e-01 6.0272e-07 2.3239e-07 0.03 + + diff --git a/tests/test_data/prist.json b/tests/test_data/prist.json new file mode 100644 index 00000000..3b36893e --- /dev/null +++ b/tests/test_data/prist.json @@ -0,0 +1 @@ +{"name": "Example Project", "calculation": "normal", "model": "standard layers", "geometry": "air/substrate", "absorption": false, "parameters": [{"name": "Substrate Roughness", "min": 1.0, "value": 3.0, "max": 5.0, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Al2O3 Thickness", "min": 20.0, "value": 20.0, "max": 20.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Al2O3 Roughness", "min": 3.0, "value": 3.0, "max": 3.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Al2O3 SLD", "min": 5.715275837467511e-06, "value": 5.715275837467511e-06, "max": 5.715275837467511e-06, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Ti0.27Co0.73 Thickness", "min": 93.0, "value": 93.0, "max": 93.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Ti0.27Co0.73 Roughness", "min": 3.0, "value": 3.0, "max": 3.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "Ti0.27Co0.73 SLD", "min": 6.720584953375607e-07, "value": 6.720584953375607e-07, "max": 6.720584953375607e-07, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_in": [{"name": "vacuum SLD", "min": 0.0, "value": 0.0, "max": 0.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_out": [{"name": "Si SLD", "min": 2.07370547828562e-06, "value": 2.07370547828562e-06, "max": 2.07370547828562e-06, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "scalefactors": [{"name": "Scalefactor", "min": 0.0, "value": 1.0, "max": 1.5, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "domain_ratios": [], "background_parameters": [{"name": "Background Parameter", "min": 0.0, "value": 0.025, "max": 1.0, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "backgrounds": [{"name": "Background", "type": "constant", "source": "Background Parameter", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "resolution_parameters": [{"name": "Resolution Param 1", "min": 0.01, "value": 0.03, "max": 0.05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "resolutions": [{"name": "Data Resolution", "type": "data", "source": "", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "custom_files": [], "data": [{"name": "Simulation", "data": [], "data_range": [], "simulation_range": [0.005, 0.7]}, {"name": "prist4", "data": [[0.0021999999999999997, 0.004724119389721505, 0.004724119389721505, 0.00019999999999999987], [0.0026, 0.0, 0.0, 0.0002000000000000001], [0.003, 0.0049441079014990305, 0.0028632759811688313, 0.0002000000000000001], [0.0034000000000000002, 0.00539571466421478, 0.0027033018099334193, 0.00019999999999999987], [0.0038, 0.011948517768746966, 0.0037903477990210636, 0.0002000000000000001], [0.004200000000000001, 0.016302332669940297, 0.003972317564845323, 0.0002000000000000001], [0.0046, 0.02265216487165058, 0.0045763672802481776, 0.0002000000000000001], [0.005, 0.023020814468949415, 0.00425372711337262, 0.00019999999999999966], [0.0054, 0.027305557750665344, 0.004428700971127359, 0.0002000000000000001], [0.0058, 0.019457945835850306, 0.0037817426451169783, 0.0002000000000000001], [0.006200000000000001, 0.027645506109865266, 0.004267993991409073, 0.0002000000000000001], [0.0066, 0.024223034347324195, 0.0037655052701117517, 0.0002000000000000001], [0.007, 0.028408308819418325, 0.0037377941755602716, 0.00019999999999999966], [0.0074, 0.057460495308053604, 0.0052154049057105455, 0.0002000000000000001], [0.0078, 0.1489883351950982, 0.00754789840356179, 0.0002000000000000001], [0.008199999999999999, 0.3520480929443021, 0.011146891694516669, 0.00019999999999999966], [0.0086, 0.5317657760789548, 0.01351725179531962, 0.00019999999999999966], [0.009, 0.7914500646501588, 0.016276277463585134, 0.00020000000000000052], [0.009399999999999999, 0.9680331047846104, 0.01782748799306378, 0.00019999999999999966], [0.0098, 1.0324643310179094, 0.018104900945963596, 0.00020000000000000052], [0.010204081632653062, 0.6287661683321234, 0.013873373556929145, 0.00020408163265306107], [0.010620574760516453, 0.3506955915110479, 0.010136613757789808, 0.00021241149521033023], [0.011054067607884473, 0.23477751708069897, 0.008109962639928676, 0.000221081352157689], [0.011505254040859348, 0.18964302712880826, 0.007180573014831645, 0.00023010508081718756], [0.011974856246608712, 0.14285208497806512, 0.006016224926747713, 0.00023949712493217482], [0.012463625889327434, 0.12188457497836602, 0.005435279585343197, 0.00024927251778654805], [0.012972345313381616, 0.10530945504618572, 0.005027517945960155, 0.00025944690626763297], [0.013501828795560456, 0.08038795087657985, 0.004197556276825506, 0.0002700365759112089], [0.014052923848440476, 0.07313672279549625, 0.003958200398418697, 0.00028105847696880976], [0.01462651257694825, 0.06129841978151465, 0.0034783001152817426, 0.0002925302515389659], [0.01522351309029308, 0.06240905936586468, 0.003479800062460076, 0.000304470261805862], [0.015844880971529533, 0.05651280096554304, 0.0032223775371921573, 0.00031689761943059103], [0.016491610807102167, 0.05044073793552524, 0.00290210672478378, 0.0003298322161420439], [0.017164737778820625, 0.0502775250441711, 0.002825286449304508, 0.00034329475557641313], [0.0178653393208133, 0.05168970509722787, 0.002811814341605395, 0.0003573067864162654], [0.018594536844111803, 0.0530076187705834, 0.0027685413804410695, 0.00037189073688223724], [0.019353497531626573, 0.0490707566771454, 0.0025677593751218504, 0.00038706995063253133], [0.020143436206386842, 0.04687883170966378, 0.0024119133732668076, 0.0004028687241277376], [0.020965617276035284, 0.04919955377214455, 0.0024276503561561644, 0.0004193123455207056], [0.021821356756689787, 0.05204111270310913, 0.00245561515567379, 0.00043642713513379616], [0.022712024379411822, 0.04925178172610669, 0.0023335505076022252, 0.00045424048758823873], [0.02363904578265312, 0.04317260894774357, 0.00211385396560445, 0.0004727809156530611], [0.024603904794189984, 0.0383043276355005, 0.0019158755178575817, 0.0004920780958838009], [0.02560814580619774, 0.04444220010645997, 0.002018641932709427, 0.0005121629161239545], [0.026653376247267036, 0.039587001147536376, 0.0018488058581743547, 0.0005330675249453423], [0.027741269155318757, 0.036677858215866437, 0.0017473224966445143, 0.0005548253831063766], [0.028873565855535847, 0.037663512267048754, 0.0017682821318945074, 0.0005774713171107174], [0.030052078747598535, 0.036755569252809664, 0.0017350440273063527, 0.0006010415749519698], [0.03127869420668419, 0.03854639441776167, 0.0017965558626918197, 0.0006255738841336837], [0.032555375602875386, 0.032866785139061716, 0.0016217721152892931, 0.000651107512057509], [0.03388416644380907, 0.0315131968037535, 0.0016061232808323794, 0.0006776833288761816], [0.0352671936455972, 0.030280345918169513, 0.0015885947028228238, 0.0007053438729119448], [0.03670667093725423, 0.025036982048446406, 0.0014176844815086535, 0.0007341334187450851], [0.038204902404080934, 0.02083180456328542, 0.001280336546816776, 0.0007640980480816198], [0.03976428617567608, 0.019376976329704945, 0.0012712203272554497, 0.0007952857235135231], [0.04138731826447918, 0.016729675515039004, 0.0011715497338181137, 0.000827746365289584], [0.04307659656098854, 0.013996303090353096, 0.0010654383375290077, 0.0008615319312197701], [0.0448348249920493, 0.010621480384334052, 0.0009246807782300395, 0.0008966964998409904], [0.04666481784886764, 0.007820332463525126, 0.0008093102454836752, 0.000933296356977354], [0.04856950429167857, 0.0060428022894521575, 0.0007183835413384203, 0.0009713900858335685], [0.050551933038277694, 0.004095892971484393, 0.0005841670446995313, 0.0010110386607655557], [0.05261527724392169, 0.005111525161941947, 0.0006658003030884043, 0.0010523055448784409], [0.05476283958040829, 0.0020207116612746484, 0.0004143866059231477, 0.0010952567916081637], [0.05699805752246577, 0.001711568931414378, 0.00038529038925611664, 0.0011399611504493146], [0.05932450884991336, 0.0008496305883131249, 0.00027095330976641694, 0.001186490176998272], [0.06174591737439962, 0.0008142116035177235, 0.00027251395576638693, 0.0012349183474879948], [0.06426615889988532, 8.92042382623096e-05, 8.920423826322552e-05, 0.0012853231779977048], [0.06688926742641126, 9.339736346561163e-05, 9.339736346489978e-05, 0.0013377853485282282], [0.06961944160708111, 0.00040599169078880156, 0.0002030320824188672, 0.0013923888321416208], [0.07246105146859462, 0.0002183257352075823, 0.00015439324988622013, 0.0014492210293718943], [0.07541864540608828, 0.00012684339163879, 0.00012684339163797725, 0.001508372908121773], [0.07849695746347965, 0.0, 0.0, 0.001569939149269589], [0.08170091491096862, 0.0, 0.0, 0.0016340182982193738], [0.08503564613182449, 0.0004165748322677754, 0.00024090333523406435, 0.001700712922636495], [0.08850648883108263, 0.0, 0.0, 0.0017701297766216512], [0.09211899857929008, 0.0, 0.0, 0.001842379971585803], [0.0958789577049754, 0.0, 0.0, 0.0019175791540995135], [0.09979238455007644, 0.0, 0.0, 0.001995847691001529], [0.10386554310314079, 0.0, 0.0, 0.0020773108620628228]], "data_range": [0.0021999999999999997, 0.10386554310314079], "simulation_range": [0.0021999999999999997, 0.10386554310314079]}], "layers": [{"name": "Al2O3", "thickness": "Al2O3 Thickness", "SLD": "Al2O3 SLD", "roughness": "Al2O3 Roughness", "hydration": "", "hydrate_with": "bulk out"}, {"name": "Ti0.27Co0.73", "thickness": "Ti0.27Co0.73 Thickness", "SLD": "Ti0.27Co0.73 SLD", "roughness": "Ti0.27Co0.73 Roughness", "hydration": "", "hydrate_with": "bulk out"}], "domain_contrasts": [], "contrasts": [{"name": "prist4", "data": "prist4", "background": "Background", "background_action": "add", "bulk_in": "vacuum SLD", "bulk_out": "Si SLD", "scalefactor": "Scalefactor", "resolution": "Data Resolution", "resample": false, "model": ["Al2O3", "Ti0.27Co0.73"]}]} \ No newline at end of file diff --git a/tests/test_data/prist5_10K_m_025.Rqz.ort b/tests/test_data/prist5_10K_m_025.Rqz.ort new file mode 100644 index 00000000..5b60b8fc --- /dev/null +++ b/tests/test_data/prist5_10K_m_025.Rqz.ort @@ -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 diff --git a/tests/test_orso_utils.py b/tests/test_orso_utils.py new file mode 100644 index 00000000..958abd88 --- /dev/null +++ b/tests/test_orso_utils.py @@ -0,0 +1,119 @@ +"""Tests for the RATapi.utils.orso module.""" + +import os +from io import StringIO +from pathlib import Path + +import numpy as np +import pytest +from orsopy.fileio.model_language import SampleModel + +from RATapi.examples.bayes_benchmark.bayes_benchmark import get_project +from RATapi.project import Project +from RATapi.utils.orso import ORSOProject, orso_model_to_rat + +TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data") + + +@pytest.fixture +def bare_subs(): + """The bare substrate project from the Bayes Benchmark example.""" + return get_project() + + +@pytest.fixture +def prist(): + """The project from the model data from prist5_10K_m_025.Rqz.ort""" + return Project.load(Path(TEST_DIR_PATH, "prist.json")) + + +@pytest.mark.parametrize( + "model", + [ + "air | FeO2 0.75 | Fe 10 | Si", + "vacuum | 5 (O3 3 | He2 4) | SiO2 0.75 | Si", + "Si | 5 (O2 2 | 3 (D2O 1 | H2O 1)) | air", + ], +) +@pytest.mark.parametrize("absorption", [True, False]) +def test_orso_model_to_rat(model, absorption): + """Test that orso_model_to_rat gives the expected parameters, layers and model.""" + + expected = SampleModel(model).resolve_to_layers()[1:-1] + expected_layers = [layer.material.formula for layer in expected] + expected_thicknesses = {layer.material.formula: layer.thickness for layer in expected} + expected_roughnesses = {layer.material.formula: layer.roughness for layer in expected} + actual = orso_model_to_rat(model, absorption=absorption) + + assert actual.model == expected_layers + + for layer in actual.layers: + assert layer.name in expected_layers + for layer in expected_layers: + assert layer in [actual_layer.name for actual_layer in actual.layers] + + expected_parameters = [] + # get set of parameters + for layer in set(expected_layers): + expected_parameters.extend([f"{layer} Thickness", f"{layer} Roughness", f"{layer} SLD"]) + if absorption: + expected_parameters.append(f"{layer} SLD imaginary") + + assert actual.parameters[f"{layer} Thickness"].value == expected_thicknesses[layer].as_unit("angstrom") + assert actual.parameters[f"{layer} Roughness"].value == expected_roughnesses[layer].as_unit("angstrom") + + assert set(p.name for p in actual.parameters) == set(expected_parameters) + + +@pytest.mark.parametrize( + "test_data", + [ + "bare_substrate.ort", + "prist5_10K_m_025.Rqz.ort", + ], +) +def test_load_ort_data(test_data): + """Test that .ort data is loaded correctly.""" + # manually get the test data for comparison + data_strings = [""] + parsing_data = False + with Path(TEST_DIR_PATH, test_data).open() as file: + for line in file: + if line[0] == "#": + if parsing_data: + parsing_data = False + data_strings.append("") + else: + continue + else: + parsing_data = True + data_strings[-1] += line + + expected_data = list(map(lambda s: np.loadtxt(StringIO(s)), data_strings)) + actual_data = ORSOProject(Path(TEST_DIR_PATH, test_data)).data + + assert len(actual_data) == len(expected_data) + for actual_dataset, expected_dataset in zip(actual_data, expected_data): + np.testing.assert_array_equal(actual_dataset.data, expected_dataset) + + +@pytest.mark.parametrize( + "test_data, expected_data", + [ + ["bare_substrate.ort", "bare_substrate.json"], + ["prist5_10K_m_025.Rqz.ort", "prist.json"], + ], +) +def test_load_ort_project(test_data, expected_data): + """Test that a project with model data is loaded correctly.""" + ort_data = ORSOProject(Path(TEST_DIR_PATH, test_data)) + sample = ort_data.samples[0] + exp_project = Project.load(Path(TEST_DIR_PATH, expected_data)) + + for class_list in ["bulk_in", "bulk_out"]: + assert getattr(sample, class_list) == getattr(exp_project, class_list)[0] + assert sample.parameters == exp_project.parameters[1:] + assert sample.layers == exp_project.layers + + for data, exp_data in zip(ort_data.data, exp_project.data[1:]): + np.testing.assert_array_equal(data.data, exp_data.data)