Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/tof/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@


def _input_to_dict(
obj: None
| dict[str, ComponentType]
| list[ComponentType]
| tuple[ComponentType, ...]
| ComponentType,
obj: None | list[ComponentType] | tuple[ComponentType, ...] | ComponentType,
kind: type,
):
if isinstance(obj, list | tuple):
out = {}
for item in obj:
out.update(_input_to_dict(item, kind=kind))
new = _input_to_dict(item, kind=kind)
for key in new.keys():
if key in out:
raise ValueError(f"More than one component named '{key}' found.")
out.update(new)
return out
elif isinstance(obj, kind):
return {obj.name: obj}
Expand Down
4 changes: 4 additions & 0 deletions src/tof/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def plot(self, bins: int = 300, **kwargs):
color = {}
for key, da in by_pulse.items():
sel = da[~da.masks["blocked_by_others"]]
if sel.size == 0:
continue
to_plot[key] = sel.hist({self.dim: bins})
if "blocked_by_me" in self.data.masks:
name = f"blocked-{key}"
Expand All @@ -29,6 +31,8 @@ def plot(self, bins: int = 300, **kwargs):
.hist({self.dim: to_plot[key].coords[self.dim]})
)
color[name] = "gray"
if not to_plot:
raise RuntimeError("Nothing to plot.")
return pp.plot(to_plot, **{**{"color": color}, **kwargs})

def min(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ def test_add_components_with_same_name_raises():
model.add(detector2)


def test_create_model_with_duplicate_component_names_raises():
chopper = dummy_chopper()
detector = dummy_detector()
with pytest.raises(
ValueError, match="More than one component named 'dummy_chopper' found"
):
tof.Model(source=dummy_source(), choppers=[chopper, chopper])
with pytest.raises(
ValueError, match="More than one component named 'dummy_detector' found"
):
tof.Model(source=dummy_source(), detectors=[detector, detector])


def test_iter():
chopper = dummy_chopper()
detector = dummy_detector()
Expand Down
42 changes: 42 additions & 0 deletions tests/result_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,45 @@ def test_componentdata_repr_does_not_raise():
res = model.run()
assert repr(res.choppers['chopper'].toa) is not None
assert repr(res.detectors['detector'].wavelength) is not None


def test_plot_reading_does_not_raise(model):
res = model.run()
res.choppers['chopper'].toa.plot()
res.choppers['chopper'].wavelength.plot()
res.detectors['detector'].toa.plot()
res.detectors['detector'].wavelength.plot()


def test_plot_reading_pulse_skipping_does_not_raise():
model = make_ess_model(pulses=3)
skip = tof.Chopper(
frequency=7 * Hz,
open=sc.array(dims=['cutout'], values=[0.0], unit='deg'),
close=sc.array(dims=['cutout'], values=[180.0], unit='deg'),
phase=0.0 * deg,
distance=10 * meter,
name='skip',
)
model.choppers['skip'] = skip
res = model.run()
res.choppers['chopper'].toa.plot()
res.choppers['chopper'].wavelength.plot()
res.detectors['detector'].toa.plot()
res.detectors['detector'].wavelength.plot()


def test_plot_reading_nothing_to_plot_raises():
model = make_ess_model(pulses=1)
skip = tof.Chopper(
frequency=1 * Hz,
open=sc.array(dims=['cutout'], values=[0.0], unit='deg'),
close=sc.array(dims=['cutout'], values=[1.0], unit='deg'),
phase=0.0 * deg,
distance=10 * meter,
name='skip',
)
model.choppers['skip'] = skip
res = model.run()
with pytest.raises(RuntimeError, match="Nothing to plot."):
res.detectors['detector'].toa.plot()
Loading