Skip to content

Commit

Permalink
calibration: add voltage_data to item
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 4, 2024
1 parent 9447329 commit 722aa52
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 8 deletions.
13 changes: 11 additions & 2 deletions lumicks/pylake/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,20 @@ def from_field(hdf5, force_channel) -> "ForceCalibrationList":
items = []
for calibration_item in hdf5["Calibration"].values():
if force_channel in calibration_item:
attrs = dict(calibration_item[force_channel].attrs)
dset = calibration_item[force_channel]
attrs = dict(dset.attrs)

# Copy the timestamp at which the calibration was applied into the item
attrs["Timestamp (ns)"] = calibration_item.attrs.get("Timestamp (ns)")
items.append(ForceCalibrationItem(attrs))
items.append(
ForceCalibrationItem(
attrs,
channel=force_channel,
voltage_data=dset.get("voltage"),
sum_voltage_data=dset.get("sum_voltage"),
driving_data=dset.get("driving"),
)
)

return ForceCalibrationList._from_items(items)

Expand Down
54 changes: 54 additions & 0 deletions lumicks/pylake/force_calibration/calibration_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,67 @@
from functools import wraps
from collections import UserDict

from lumicks.pylake.channel import Slice, Continuous, empty_slice
from lumicks.pylake.force_calibration.calibration_models import DiodeCalibrationModel
from lumicks.pylake.force_calibration.detail.calibration_properties import (
CalibrationPropertiesMixin,
)


class ForceCalibrationItem(UserDict, CalibrationPropertiesMixin):
def __init__(
self,
dictionary=None,
*,
channel=None,
voltage_data=None,
sum_voltage_data=None,
driving_data=None,
):
super().__init__(dictionary)

def make_slice(data, labels):
return Slice(Continuous.from_dataset(data), labels=labels)

self.voltage = (
make_slice(
voltage_data,
labels={
"x": "Time (s)",
"y": "Uncalibrated Force (V)",
"title": f"Uncalibrated {channel}",
},
)
if voltage_data
else empty_slice
)

self.sum_voltage = (
make_slice(
sum_voltage_data,
labels={
"x": "Time (s)",
"y": "Sum voltage (V)",
"title": f"Sum voltage {channel[-2]}",
},
)
if sum_voltage_data
else empty_slice
)

self.driving = (
make_slice(
driving_data,
labels={
"x": "Time (s)",
"y": r"Driving data ($\mu$m)",
"title": f"Driving data for axis {channel[-1]}",
},
)
if driving_data
else empty_slice
)

@staticmethod
def _verify_full(method):
@wraps(method)
Expand Down
25 changes: 20 additions & 5 deletions lumicks/pylake/tests/data/mock_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class MockDataFile_v2(MockDataFile_v1):
def get_file_format_version(self):
return 2

def make_calibration_data(self, calibration_idx, group, attributes, application_timestamp=None):
def make_calibration_data(
self, calibration_idx, group, attributes, application_timestamp=None, channels=None
):
if "Calibration" not in self.file:
self.file.create_group("Calibration")

Expand All @@ -73,14 +75,27 @@ def make_calibration_data(self, calibration_idx, group, attributes, application_
for i, v in attributes.items():
field.attrs[i] = v

def make_fd(self, fd_name=None, metadata={}, attributes={}):
if channels:
for name, data in channels.items():
self.make_continuous_channel(
group=f"Calibration/{calibration_idx}/{group}",
name=name,
start=attributes["Start time (ns)"],
dt=np.int64(1e9 / 78125),
data=data,
)

def make_fd(self, fd_name=None, metadata=None, attributes=None):
if "FD Curve" not in self.file:
self.file.create_group("FD Curve")

if fd_name:
dset = self.file["FD Curve"].create_dataset(fd_name, data=metadata)
for i, v in attributes.items():
dset.attrs[i] = v
dset = self.file["FD Curve"].create_dataset(
fd_name, data={} if metadata is None else metadata
)
if attributes:
for i, v in attributes.items():
dset.attrs[i] = v

def make_marker(self, marker_name, attributes, payload=None):
if "Marker" not in self.file:
Expand Down
11 changes: 10 additions & 1 deletion lumicks/pylake/tests/test_file/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ def generate_attributes(stop_time, custom_fields=None):
mock_file.make_calibration_data("1", "Force 1y", generate_attributes(0))
mock_file.make_calibration_data("2", "Force 1y", generate_attributes(1))
mock_file.make_calibration_data("3", "Force 1y", reset_attrs)
mock_file.make_calibration_data("3b", "Force 1y", generate_attributes(8))
mock_file.make_calibration_data(
"3b",
"Force 1y",
generate_attributes(8),
channels={
"sum_voltage": np.arange(5.0),
"voltage": np.arange(15.0),
"driving": np.arange(25.0),
},
)
mock_file.make_calibration_data("4", "Force 1y", generate_attributes(10))
mock_file.make_calibration_data("5", "Force 1y", generate_attributes(100))
mock_file.make_calibration_data("1", "Force 1z", generate_attributes(0))
Expand Down
23 changes: 23 additions & 0 deletions lumicks/pylake/tests/test_file/test_file_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ def test_calibration(h5_file):
assert f.force1y.calibration[2].applied_at == 8
assert f.force1y.calibration[3].applied_at == 10
assert f.force2x.calibration[0].applied_at == 100
assert len(f.force1x.calibration[0].voltage.data) == 0
assert len(f.force1x.calibration[0].sum_voltage.data) == 0
assert len(f.force1x.calibration[0].driving.data) == 0

# Verify that one specific calibration has data associated with it
idx = 2
np.testing.assert_equal(f.force1y.calibration[idx].sum_voltage.data, np.arange(5.0))
assert f.force1y.calibration[idx].sum_voltage.sample_rate == 78125
assert f.force1y.calibration[idx].labels["x"] == "Time (s)"
assert f.force1y.calibration[idx].labels["y"] == "Sum voltage (V)"
assert f.force1y.calibration[idx].labels["title"] == "Sum voltage 1"

np.testing.assert_equal(f.force1y.calibration[idx].voltage.data, np.arange(15.0))
assert f.force1y.calibration[idx].voltage.sample_rate == 78125
assert f.force1y.calibration[idx].voltage.labels["x"] == "Time (s)"
assert f.force1y.calibration[idx].voltage.labels["y"] == "Uncalibrated Force (V)"
assert f.force1y.calibration[idx].voltage.labels["title"] == "Uncalibrated 1y"

np.testing.assert_equal(f.force1y.calibration[idx].driving.data, np.arange(25.0))
assert f.force1y.calibration[idx].driving.sample_rate == 78125
assert f.force1y.calibration[idx].driving.labels["x"] == "Time (s)"
assert f.force1y.calibration[idx].driving.labels["y"] == r"Driving data ($\mu$m)"
assert f.force1y.calibration[idx].driving.labels["title"] == "Driving data for axis y"


def test_marker(h5_file):
Expand Down

0 comments on commit 722aa52

Please sign in to comment.