Skip to content

Commit bff8428

Browse files
authored
Merge pull request #562 from scipp/cif-conform-easy
Make CIF writer conform with expectations of EasyDiffraction
2 parents 17f727d + 5dfdc87 commit bff8428

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/scippneutron/io/cif.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ class CIFSchema:
150150
CORE_SCHEMA = CIFSchema(
151151
name='coreCIF',
152152
version='3.3.0',
153-
location='https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic',
153+
location='https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic',
154154
)
155155
PD_SCHEMA = CIFSchema(
156156
name='pdCIF',
157157
version='2.5.0',
158-
location='https://github.com/COMCIFS/Powder_Dictionary/blob/7608b92165f58f968f054344e67662e01d4b401a/cif_pow.dic',
158+
location='https://github.com/COMCIFS/Powder_Dictionary/blob/970c2b2850a923796db5f4e9b7207d10ab5fd8e5/cif_pow.dic',
159159
)
160160

161161

@@ -298,7 +298,9 @@ def with_reduced_powder_data(self, data: sc.DataArray, *, comment: str = '') ->
298298
``('tof', 'dspacing')``.
299299
The data array may also have a name in
300300
``('intensity_net', 'intensity_norm', 'intensity_total')``.
301-
If the name is not set, it defaults to ``'intensity_net'``.
301+
If the name is not set, it defaults to ``'intensity_norm'``,
302+
which is appropriate for typical outputs from data reduction.
303+
See https://github.com/COMCIFS/Powder_Dictionary/blob/master/cif_pow.dic
302304
303305
The data gets written as intensity along a single coord whose
304306
name matches the dimension name.
@@ -884,9 +886,16 @@ def _normalize_reduced_powder_name(name: str) -> str:
884886

885887
def _make_reduced_powder_loop(data: sc.DataArray, comment: str) -> Loop:
886888
coord_name, coord = _reduced_powder_coord(data)
887-
data_name = _normalize_reduced_powder_name(data.name or 'intensity_net')
889+
data_name = _normalize_reduced_powder_name(data.name or 'intensity_norm')
888890

889-
res = Loop({coord_name: sc.values(coord)}, comment=comment, schema=PD_SCHEMA)
891+
res = Loop(
892+
{
893+
'pd_data.point_id': sc.arange(data.dim, len(data), unit=None),
894+
coord_name: sc.values(coord),
895+
},
896+
comment=comment,
897+
schema=PD_SCHEMA,
898+
)
890899
if coord.variances is not None:
891900
res[coord_name + '_su'] = sc.stddevs(coord)
892901
res[data_name] = sc.values(data.data)
@@ -901,11 +910,13 @@ def _make_reduced_powder_loop(data: sc.DataArray, comment: str) -> Loop:
901910

902911

903912
def _make_powder_calibration_loop(data: sc.DataArray, comment: str) -> Loop:
904-
id_by_power = {0: 'tzero', 1: 'DIFC', 2: 'DIFA', -1: 'DIFB'}
913+
# All names are valid python identifiers
914+
id_by_power = {0: 'ZERO', 1: 'DIFC', 2: 'DIFA', -1: 'DIFB'}
905915
ids = sc.array(
906916
dims=[data.dim],
907917
values=[
908-
id_by_power.get(power, str(power)) for power in data.coords['power'].values
918+
id_by_power.get(power, f'c{power}'.replace('-', '_').replace('.', '_'))
919+
for power in data.coords['power'].values
909920
],
910921
)
911922
res = Loop(

tests/io/cif_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_write_block_core_schema_from_chunk():
678678
_audit_conform.dict_name
679679
_audit_conform.dict_version
680680
_audit_conform.dict_location
681-
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
681+
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
682682
683683
_audit.creation_method 'written by scippneutron'
684684
'''
@@ -702,7 +702,7 @@ def test_write_block_core_schema_from_loop():
702702
_audit_conform.dict_name
703703
_audit_conform.dict_version
704704
_audit_conform.dict_location
705-
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
705+
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
706706
707707
loop_
708708
_audit_author.name
@@ -845,7 +845,7 @@ def test_builder_writes_audit() -> None:
845845
_audit_conform.dict_name
846846
_audit_conform.dict_version
847847
_audit_conform.dict_location
848-
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
848+
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
849849
850850
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
851851
_audit.creation_method 'Written by scippneutron v{expected_version}'
@@ -866,7 +866,7 @@ def test_builder_writes_audit_with_one_reducer() -> None:
866866
_audit_conform.dict_name
867867
_audit_conform.dict_version
868868
_audit_conform.dict_location
869-
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
869+
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
870870
871871
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
872872
_audit.creation_method 'Written by scippneutron v{expected_version}'
@@ -888,7 +888,7 @@ def test_builder_writes_audit_with_multiple_reducers() -> None:
888888
_audit_conform.dict_name
889889
_audit_conform.dict_version
890890
_audit_conform.dict_location
891-
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
891+
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
892892
893893
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
894894
_audit.creation_method 'Written by scippneutron v{expected_version}'
@@ -936,12 +936,13 @@ def test_builder_with_reduced_powder_data():
936936
assert (
937937
tof_loop
938938
== '''loop_
939+
_pd_data.point_id
939940
_pd_meas.time_of_flight
940-
_pd_proc.intensity_net
941-
_pd_proc.intensity_net_su
942-
1.2 13.6 0.9
943-
1.4 26.0 1.0
944-
2.3 9.7 0.6
941+
_pd_proc.intensity_norm
942+
_pd_proc.intensity_norm_su
943+
0 1.2 13.6 0.9
944+
1 1.4 26.0 1.0
945+
2 2.3 9.7 0.6
945946
'''
946947
)
947948

@@ -964,11 +965,12 @@ def test_builder_with_reduced_powder_data_custom_unit():
964965
tof_loop
965966
== '''# Unit of intensity: [counts]
966967
loop_
968+
_pd_data.point_id
967969
_pd_meas.time_of_flight
968-
_pd_proc.intensity_net
969-
1.2 13.6
970-
1.4 26.0
971-
2.3 9.7
970+
_pd_proc.intensity_norm
971+
0 1.2 13.6
972+
1 1.4 26.0
973+
2 2.3 9.7
972974
'''
973975
)
974976

@@ -1034,7 +1036,7 @@ def test_builder_powder_calibration():
10341036
_pd_calib_d_to_tof.id
10351037
_pd_calib_d_to_tof.power
10361038
_pd_calib_d_to_tof.coeff
1037-
tzero 0 1.2
1039+
ZERO 0 1.2
10381040
DIFC 1 4.5
10391041
DIFB -1 6.7
10401042
'''

0 commit comments

Comments
 (0)