Skip to content

Commit

Permalink
lowlevel: add make_continuous_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Sep 5, 2024
1 parent 394bc36 commit 42a7127
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
31 changes: 30 additions & 1 deletion lumicks/pylake/lowlevel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy as np

from lumicks.pylake.kymo import Kymo
from lumicks.pylake.scan import Scan
from lumicks.pylake.channel import empty_slice
from lumicks.pylake.channel import Slice, Continuous, empty_slice
from lumicks.pylake.point_scan import PointScan
from lumicks.pylake.detail.confocal import ConfocalFile, ScanMetaData
from lumicks.pylake.detail.imaging_mixins import _FIRST_TIMESTAMP


def create_confocal_object(
Expand Down Expand Up @@ -30,3 +33,29 @@ def create_confocal_object(
file = ConfocalFile(infowave, red_channel, green_channel, blue_channel)
confocal_cls = {0: PointScan, 1: Kymo, 2: Scan}
return confocal_cls[metadata.num_axes](name, file, infowave.start, infowave.stop, metadata)


def make_continuous_slice(data, start, dt, y_label="y", name="") -> Slice:
"""Make a continuous slice of data
Converts a raw `array_like` to a pylake `Slice`.
Parameters
----------
data : array_like
Source of data
start : int
Start timestamp in nanoseconds since epoch
dt : int
Timestep in nanoseconds
y_label : str
Label to show on the y-axis.
name : str
Name of the slice (used on plot titles).
"""
if start < _FIRST_TIMESTAMP:
raise ValueError(
f"Starting timestamp must be larger than {_FIRST_TIMESTAMP}. You provided: {start}."
)

return Slice(Continuous(np.asarray(data), start, dt), labels={"title": name, "y": y_label})
23 changes: 22 additions & 1 deletion lumicks/pylake/tests/test_channels/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import matplotlib as mpl

from lumicks.pylake import channel
from lumicks.pylake.lowlevel import make_continuous_slice
from lumicks.pylake.calibration import ForceCalibrationList


Expand Down Expand Up @@ -66,7 +67,7 @@ def test_calibration_timeseries_channels():
# This slices off everything
nested_slice = nested_slice[120:]
assert len(nested_slice.calibration) == 0
assert type(nested_slice.calibration) is list
assert isinstance(nested_slice.calibration, list)


def test_calibration_continuous_channels():
Expand Down Expand Up @@ -981,3 +982,23 @@ def start(self):

with pytest.raises(TypeError, match=invalid_range):
s[BadType(0)]


def test_low_level_construction():
start = 1388534400000000000
with pytest.raises(ValueError, match="Starting timestamp must be larger than"):
_ = make_continuous_slice(np.array([1, 2, 3]), start - 1, int(1e9 / 78125))

data = np.arange(200)
slc = make_continuous_slice(data, start, int(1e9 / 78125))
assert slc.start == start
assert slc._timesteps == int(1e9 / 78125)
assert slc.sample_rate == 78125
assert slc.labels["y"] == "y"
assert slc.labels["title"] == ""
np.testing.assert_equal(slc.data, data)
assert isinstance(slc._src, channel.Continuous)

slc = make_continuous_slice(data, start, int(1e9 / 78125), name="hi", y_label="there")
assert slc.labels["title"] == "hi"
assert slc.labels["y"] == "there"

0 comments on commit 42a7127

Please sign in to comment.