MNeuEventLib 0.5.2__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- MNeuEventLib/__init__.py +2 -0
- MNeuEventLib/core.cp315-win_amd64.pyd +0 -0
- MNeuEventLib/make_default.py +163 -0
- MNeuEventLib/plotting.py +74 -0
- MNeuEventLib/test_helpers.py +40 -0
- files/muon_ref.nxs +0 -0
- mneueventlib-0.5.2.dist-info/METADATA +15 -0
- mneueventlib-0.5.2.dist-info/RECORD +11 -0
- mneueventlib-0.5.2.dist-info/WHEEL +4 -0
- mneueventlib-0.5.2.dist-info/licenses/LICENSE +28 -0
- mneueventlib-0.5.2.dist-info/sboms/MNeuEventLib.cyclonedx.json +3104 -0
MNeuEventLib/__init__.py
ADDED
|
Binary file
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This script can be used to generate a reference file.
|
|
3
|
+
The reference file is then used for filling out an
|
|
4
|
+
incomplete event file so it can be loaded into Mantid.
|
|
5
|
+
To use this you need an example histogram file with
|
|
6
|
+
the correct format.
|
|
7
|
+
|
|
8
|
+
If the reference file is saved as <ref> (assume
|
|
9
|
+
this includes the file path), then when saving
|
|
10
|
+
event data to histograms, the command would be
|
|
11
|
+
|
|
12
|
+
data.save(<output>, autofill=True, ref_file=<ref>)
|
|
13
|
+
|
|
14
|
+
where <output> is the name of the file you want
|
|
15
|
+
to save to, and data is the event data
|
|
16
|
+
object which would have been loaded from a
|
|
17
|
+
events data file.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import sys
|
|
21
|
+
import h5py
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
This data needs to be provided from the event
|
|
25
|
+
file. These datasets are calculated as part
|
|
26
|
+
of the histogramming process.
|
|
27
|
+
"""
|
|
28
|
+
skip = ['collection_time',
|
|
29
|
+
'corrected_time',
|
|
30
|
+
'counts',
|
|
31
|
+
'period_index',
|
|
32
|
+
'raw_time',
|
|
33
|
+
'spectrum_index',
|
|
34
|
+
'duration',
|
|
35
|
+
'end_time',
|
|
36
|
+
'good_frames',
|
|
37
|
+
'resolution',
|
|
38
|
+
'frames_requested',
|
|
39
|
+
'good_frames',
|
|
40
|
+
'good_frames_daq',
|
|
41
|
+
'raw_frames',
|
|
42
|
+
'start_time',
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
This is the period information for the example
|
|
47
|
+
histogram file. It is recommended that it includes
|
|
48
|
+
multiple periods and Dwell.
|
|
49
|
+
N: number of detectors
|
|
50
|
+
P: number of periods
|
|
51
|
+
PD: number of periods + Dwell
|
|
52
|
+
NP: number of detectors times number of periods
|
|
53
|
+
NPD: number of detectors times the number of periods and Dwell
|
|
54
|
+
"""
|
|
55
|
+
N = 64
|
|
56
|
+
P = 2
|
|
57
|
+
PD = 3
|
|
58
|
+
NP = N*P
|
|
59
|
+
NPD = N*PD
|
|
60
|
+
|
|
61
|
+
"""
|
|
62
|
+
These strings are copied from the
|
|
63
|
+
histogram file to the default file
|
|
64
|
+
"""
|
|
65
|
+
keep_strings = ['definition',
|
|
66
|
+
'probe',
|
|
67
|
+
'type',
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
def set_attributes(obj, dest):
|
|
71
|
+
"""
|
|
72
|
+
Copy attributes from one object to another.
|
|
73
|
+
:param obj: the example file's open object
|
|
74
|
+
:param dest: the ref file's open object
|
|
75
|
+
"""
|
|
76
|
+
for attr in obj.attrs:
|
|
77
|
+
dest.attrs.create(attr, obj.attrs[attr])
|
|
78
|
+
|
|
79
|
+
def read(obj, new_obj, key):
|
|
80
|
+
"""
|
|
81
|
+
Recursive copy of data, replacing any dataset
|
|
82
|
+
with a "default dataset" if the length
|
|
83
|
+
depends on the number of detectors, periods,
|
|
84
|
+
or Dwell. Otherwise it records the dataset values
|
|
85
|
+
as 0.
|
|
86
|
+
:param obj: the example file's open object
|
|
87
|
+
:param new_obj: the ref file's open object
|
|
88
|
+
:param key: the name of the dataset
|
|
89
|
+
"""
|
|
90
|
+
if isinstance(obj, h5py.Group):
|
|
91
|
+
print('group', key)
|
|
92
|
+
tmp = new_obj.require_group(key)
|
|
93
|
+
set_attributes(obj, tmp)
|
|
94
|
+
for new_key in obj.keys():
|
|
95
|
+
read(obj[new_key], tmp, new_key)
|
|
96
|
+
print('exit group')
|
|
97
|
+
print()
|
|
98
|
+
|
|
99
|
+
elif isinstance(obj, h5py.Dataset):
|
|
100
|
+
name = obj.name.split('/')[-1]
|
|
101
|
+
dtype = str(obj.dtype)
|
|
102
|
+
shape = obj.shape
|
|
103
|
+
val = obj[()]
|
|
104
|
+
if 'float' in dtype or 'int' in dtype:
|
|
105
|
+
if len(val) in [N, P, NP, PD, NPD]:
|
|
106
|
+
length = 'N'
|
|
107
|
+
if len(val) == P:
|
|
108
|
+
length = 'P'
|
|
109
|
+
elif len(val) == NP:
|
|
110
|
+
length = 'NP'
|
|
111
|
+
elif len(val) == PD:
|
|
112
|
+
length = 'PD'
|
|
113
|
+
elif len(val) == NPD:
|
|
114
|
+
length = 'NPD'
|
|
115
|
+
group = new_obj.require_group('dataset_' + name)
|
|
116
|
+
set_attributes(obj, group)
|
|
117
|
+
group.create_dataset('default', data=0)
|
|
118
|
+
group.create_dataset('dtype', data=dtype)
|
|
119
|
+
group.create_dataset('shape', data=length)
|
|
120
|
+
|
|
121
|
+
else:
|
|
122
|
+
tmp = new_obj.create_dataset(name,
|
|
123
|
+
shape=shape,
|
|
124
|
+
dtype=dtype,
|
|
125
|
+
fillvalue=0)
|
|
126
|
+
|
|
127
|
+
set_attributes(obj, tmp)
|
|
128
|
+
else: # assume a string
|
|
129
|
+
val = obj[()]
|
|
130
|
+
tmp = None
|
|
131
|
+
if name in keep_strings or (isinstance(val, str) and val=='ISIS'):
|
|
132
|
+
tmp = new_obj.create_dataset(name, data=val, dtype=dtype)
|
|
133
|
+
elif '.dat' in name:
|
|
134
|
+
tmp = new_obj.create_dataset(name, data=val, dtype=dtype)
|
|
135
|
+
elif obj.shape == [1]:
|
|
136
|
+
tmp = new_obj.create_dataset(name, data=[' '], dtype='S1')
|
|
137
|
+
else:
|
|
138
|
+
tmp = new_obj.create_dataset(name, data=val, dtype=dtype)
|
|
139
|
+
set_attributes(obj, tmp)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def make_default(example_filename='HIFI00207745.nxs', ref_filename='REF_file.nxs'):
|
|
143
|
+
"""Generate a reference NeXus file from an example histogram NeXus file."""
|
|
144
|
+
with h5py.File(example_filename, 'r') as file:
|
|
145
|
+
with h5py.File(ref_filename, 'w') as new_file:
|
|
146
|
+
# do top level manually
|
|
147
|
+
for key in file.keys():
|
|
148
|
+
new_obj = new_file.require_group(key)
|
|
149
|
+
set_attributes(file[key], new_obj)
|
|
150
|
+
for tmp in file[key].keys():
|
|
151
|
+
if tmp in ['selog']:
|
|
152
|
+
print('skip')
|
|
153
|
+
else:
|
|
154
|
+
read(file[key][tmp], new_obj, tmp)
|
|
155
|
+
if tmp == 'detector_1':
|
|
156
|
+
new_obj[tmp].attrs['NX_class'] = 'NXdata'
|
|
157
|
+
print('done')
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
if __name__ == '__main__':
|
|
161
|
+
example = sys.argv[1] if len(sys.argv) > 1 else 'HIFI00207745.nxs'
|
|
162
|
+
ref = sys.argv[2] if len(sys.argv) > 2 else 'REF_file.nxs'
|
|
163
|
+
make_default(example, ref)
|
MNeuEventLib/plotting.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Plotting functions for data."""
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from MNeuEventLib.core import Data
|
|
8
|
+
|
|
9
|
+
def plot_sample_log(data: Data, log_name: str):
|
|
10
|
+
"""
|
|
11
|
+
Plot a sample log.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
data: Data
|
|
16
|
+
The Data object containing the sample log.
|
|
17
|
+
log_name: str
|
|
18
|
+
The name of the sample log to plot.
|
|
19
|
+
"""
|
|
20
|
+
sample_log = data.dataset.get_sample_log(log_name)
|
|
21
|
+
fig, ax = plt.subplots()
|
|
22
|
+
ax.set_title(sample_log['name'])
|
|
23
|
+
ax.set_xlabel("Time (seconds)")
|
|
24
|
+
units = sample_log['unit']
|
|
25
|
+
unit_string = "" if units == "" else f"({units})"
|
|
26
|
+
ax.set_ylabel(f"Value {unit_string}")
|
|
27
|
+
|
|
28
|
+
values = sample_log['value']
|
|
29
|
+
if isinstance(values, list):
|
|
30
|
+
# a string log comes back as a list of str rather than an array, so
|
|
31
|
+
# we need to plot it differently
|
|
32
|
+
categories = list(dict.fromkeys(values))
|
|
33
|
+
ax.plot(sample_log['time'], values, drawstyle="steps-post", marker="o")
|
|
34
|
+
ax.set_yticks(categories)
|
|
35
|
+
ax.set_ylabel("Value")
|
|
36
|
+
else:
|
|
37
|
+
ax.plot(sample_log['time'], values)
|
|
38
|
+
|
|
39
|
+
plt.show()
|
|
40
|
+
|
|
41
|
+
def plot_amplitudes(data: Data, max_height: Optional[float] = None, n_bins: int = 10,
|
|
42
|
+
detector: Optional[int] = None):
|
|
43
|
+
"""
|
|
44
|
+
Plot a histogram of amplitudes.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
data: Data
|
|
49
|
+
The Data object containing the sample log.
|
|
50
|
+
max_height: float, optional
|
|
51
|
+
The maximum height to use.
|
|
52
|
+
Any data above this height will be placed in the last bin.
|
|
53
|
+
If not provided, calculates the maximum amplitude in the data.
|
|
54
|
+
n_bins: int, default 10
|
|
55
|
+
The number of equal-width bins to use.
|
|
56
|
+
detector: int, optional
|
|
57
|
+
The detector to plot amplitudes for.
|
|
58
|
+
If not provided, plots for all detectors.
|
|
59
|
+
"""
|
|
60
|
+
# get_amp_histogram returns the histogram and the max height used
|
|
61
|
+
hist, max_h = data.dataset.get_amp_histogram(max_height, n_bins, detector)
|
|
62
|
+
|
|
63
|
+
steps = np.linspace(0, max_h, len(hist)+1)
|
|
64
|
+
fig, ax = plt.subplots()
|
|
65
|
+
if detector is None:
|
|
66
|
+
ax.set_title("Amplitude histogram")
|
|
67
|
+
else:
|
|
68
|
+
ax.set_title(f"Amplitude histogram (detector {detector})")
|
|
69
|
+
ax.set_xlabel("Amplitude")
|
|
70
|
+
ax.set_ylabel("Counts")
|
|
71
|
+
ax.stairs(hist, steps, fill = True)
|
|
72
|
+
|
|
73
|
+
plt.show()
|
|
74
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import h5py
|
|
2
|
+
import numpy as np
|
|
3
|
+
import shutil
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def replace(f, key, values, dtype):
|
|
7
|
+
"""
|
|
8
|
+
A simple helper method to replace
|
|
9
|
+
a dataset (without attributes)
|
|
10
|
+
"""
|
|
11
|
+
del f[key]
|
|
12
|
+
f.create_dataset(key, data=values, dtype=dtype)
|
|
13
|
+
|
|
14
|
+
def make_single_period_data(multi_period,
|
|
15
|
+
single_period):
|
|
16
|
+
"""
|
|
17
|
+
This is a simple method to convert
|
|
18
|
+
multi-period data into single
|
|
19
|
+
period.
|
|
20
|
+
:param multi_period: the file path
|
|
21
|
+
to the multi-period data.
|
|
22
|
+
:param single_period: the file path
|
|
23
|
+
to write the single period data to
|
|
24
|
+
"""
|
|
25
|
+
shutil.copy(multi_period,
|
|
26
|
+
single_period)
|
|
27
|
+
f = h5py.File(single_period, 'a')
|
|
28
|
+
data = f['raw_data_1']
|
|
29
|
+
tmp = data['detector_1_events']
|
|
30
|
+
N = tmp['period_number'].len()
|
|
31
|
+
zeros = np.zeros(N)
|
|
32
|
+
replace(tmp, 'period_number', zeros, 'int64')
|
|
33
|
+
|
|
34
|
+
tmp = data['periods']
|
|
35
|
+
replace(tmp, 'labels', 'period_1', 'S8')
|
|
36
|
+
|
|
37
|
+
replace(tmp, 'number', 1, 'int32')
|
|
38
|
+
replace(tmp, 'type', [1], 'int32')
|
|
39
|
+
f.close()
|
|
40
|
+
return
|
files/muon_ref.nxs
ADDED
|
Binary file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: MNeuEventLib
|
|
3
|
+
Version: 0.5.2
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
7
|
+
Requires-Dist: numpy
|
|
8
|
+
Requires-Dist: matplotlib
|
|
9
|
+
Requires-Dist: ipykernel ; extra == 'docs'
|
|
10
|
+
Requires-Dist: nbsphinx ; extra == 'docs'
|
|
11
|
+
Provides-Extra: docs
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Summary: Event data filtering library for ISIS Neutron and Muon Source.
|
|
14
|
+
License-Expression: BSD-3-Clause
|
|
15
|
+
Requires-Python: >=3.8
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
MNeuEventLib/__init__.py,sha256=TjE7Yb4lgD9wC8I6Hr5pLT3EZ4vvHj3gSwaCtUB_vd8,69
|
|
2
|
+
MNeuEventLib/core.cp315-win_amd64.pyd,sha256=RqxG7A0Wn215gmdUJIJaA6gPVf-TOuMdCakvHlYCREM,31653888
|
|
3
|
+
MNeuEventLib/make_default.py,sha256=p5veFS8jM0GFKc4_mn8b16ZKniOeu0QsDeP2lP1O7y0,5421
|
|
4
|
+
MNeuEventLib/plotting.py,sha256=NtTljxUoeBWL4nYVdvJsXMM1VTe5SbmATAO4frjuSWE,2388
|
|
5
|
+
MNeuEventLib/test_helpers.py,sha256=ATXOqzc9PCvXHv2mikamnR2xpuNvYXBj2jpqWjnG41E,1061
|
|
6
|
+
files/muon_ref.nxs,sha256=bsEntPtaNOMM4wCB2M3Ibgj0UbEh6ueT81BJioF6yRg,132848
|
|
7
|
+
mneueventlib-0.5.2.dist-info/METADATA,sha256=eZnLpZCDjC3QJrCfs6lExnR5lX1pRQp-QD8zEPTEyPc,541
|
|
8
|
+
mneueventlib-0.5.2.dist-info/WHEEL,sha256=Q5QdGvjBfn8x6ihg-B4VQuuGKk7DT67L9DVrwEH_qj8,97
|
|
9
|
+
mneueventlib-0.5.2.dist-info/licenses/LICENSE,sha256=gBmiHRiOb3ftcpRW6lgHWs_WmDM8gpCw8E5awzaed04,1543
|
|
10
|
+
mneueventlib-0.5.2.dist-info/sboms/MNeuEventLib.cyclonedx.json,sha256=1zaLWF6ChVwRFS4Y2ouAkz0Jm3glG3cyf_XaiQWTPwE,105515
|
|
11
|
+
mneueventlib-0.5.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, ISIS Neutron and Muon Source
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|