Skip to content

Commit

Permalink
Merge pull request #562 from scipp/cif-conform-easy
Browse files Browse the repository at this point in the history
Make CIF writer conform with expectations of EasyDiffraction
  • Loading branch information
jl-wynen authored Oct 24, 2024
2 parents 17f727d + 5dfdc87 commit bff8428
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
25 changes: 18 additions & 7 deletions src/scippneutron/io/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ class CIFSchema:
CORE_SCHEMA = CIFSchema(
name='coreCIF',
version='3.3.0',
location='https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic',
location='https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic',
)
PD_SCHEMA = CIFSchema(
name='pdCIF',
version='2.5.0',
location='https://github.com/COMCIFS/Powder_Dictionary/blob/7608b92165f58f968f054344e67662e01d4b401a/cif_pow.dic',
location='https://github.com/COMCIFS/Powder_Dictionary/blob/970c2b2850a923796db5f4e9b7207d10ab5fd8e5/cif_pow.dic',
)


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

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

res = Loop({coord_name: sc.values(coord)}, comment=comment, schema=PD_SCHEMA)
res = Loop(
{
'pd_data.point_id': sc.arange(data.dim, len(data), unit=None),
coord_name: sc.values(coord),
},
comment=comment,
schema=PD_SCHEMA,
)
if coord.variances is not None:
res[coord_name + '_su'] = sc.stddevs(coord)
res[data_name] = sc.values(data.data)
Expand All @@ -901,11 +910,13 @@ def _make_reduced_powder_loop(data: sc.DataArray, comment: str) -> Loop:


def _make_powder_calibration_loop(data: sc.DataArray, comment: str) -> Loop:
id_by_power = {0: 'tzero', 1: 'DIFC', 2: 'DIFA', -1: 'DIFB'}
# All names are valid python identifiers
id_by_power = {0: 'ZERO', 1: 'DIFC', 2: 'DIFA', -1: 'DIFB'}
ids = sc.array(
dims=[data.dim],
values=[
id_by_power.get(power, str(power)) for power in data.coords['power'].values
id_by_power.get(power, f'c{power}'.replace('-', '_').replace('.', '_'))
for power in data.coords['power'].values
],
)
res = Loop(
Expand Down
32 changes: 17 additions & 15 deletions tests/io/cif_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def test_write_block_core_schema_from_chunk():
_audit_conform.dict_name
_audit_conform.dict_version
_audit_conform.dict_location
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
_audit.creation_method 'written by scippneutron'
'''
Expand All @@ -702,7 +702,7 @@ def test_write_block_core_schema_from_loop():
_audit_conform.dict_name
_audit_conform.dict_version
_audit_conform.dict_location
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
loop_
_audit_author.name
Expand Down Expand Up @@ -845,7 +845,7 @@ def test_builder_writes_audit() -> None:
_audit_conform.dict_name
_audit_conform.dict_version
_audit_conform.dict_location
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
_audit.creation_method 'Written by scippneutron v{expected_version}'
Expand All @@ -866,7 +866,7 @@ def test_builder_writes_audit_with_one_reducer() -> None:
_audit_conform.dict_name
_audit_conform.dict_version
_audit_conform.dict_location
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
_audit.creation_method 'Written by scippneutron v{expected_version}'
Expand All @@ -888,7 +888,7 @@ def test_builder_writes_audit_with_multiple_reducers() -> None:
_audit_conform.dict_name
_audit_conform.dict_version
_audit_conform.dict_location
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/fc3d75a298fd7c0c3cde43633f2a8616e826bfd5/cif_core.dic
coreCIF 3.3.0 https://github.com/COMCIFS/cif_core/blob/6f8502e81b623eb0fd779c79efaf191d49fa198c/cif_core.dic
_audit.creation_date \d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\+00.00
_audit.creation_method 'Written by scippneutron v{expected_version}'
Expand Down Expand Up @@ -936,12 +936,13 @@ def test_builder_with_reduced_powder_data():
assert (
tof_loop
== '''loop_
_pd_data.point_id
_pd_meas.time_of_flight
_pd_proc.intensity_net
_pd_proc.intensity_net_su
1.2 13.6 0.9
1.4 26.0 1.0
2.3 9.7 0.6
_pd_proc.intensity_norm
_pd_proc.intensity_norm_su
0 1.2 13.6 0.9
1 1.4 26.0 1.0
2 2.3 9.7 0.6
'''
)

Expand All @@ -964,11 +965,12 @@ def test_builder_with_reduced_powder_data_custom_unit():
tof_loop
== '''# Unit of intensity: [counts]
loop_
_pd_data.point_id
_pd_meas.time_of_flight
_pd_proc.intensity_net
1.2 13.6
1.4 26.0
2.3 9.7
_pd_proc.intensity_norm
0 1.2 13.6
1 1.4 26.0
2 2.3 9.7
'''
)

Expand Down Expand Up @@ -1034,7 +1036,7 @@ def test_builder_powder_calibration():
_pd_calib_d_to_tof.id
_pd_calib_d_to_tof.power
_pd_calib_d_to_tof.coeff
tzero 0 1.2
ZERO 0 1.2
DIFC 1 4.5
DIFB -1 6.7
'''
Expand Down

0 comments on commit bff8428

Please sign in to comment.