Coverage for qubalab/images/metadata/pixel_calibration.py: 96%
24 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-31 11:24 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-31 11:24 +0000
1from __future__ import annotations
2from dataclasses import dataclass
5@dataclass(frozen=True)
6class PixelLength:
7 """
8 Simple data class to store pixel size information, along one dimension.
10 Can be thought of as the pixel width, pixel height or pixel depth (z-spacing).
12 :param length: the length of the pixel, by default 1
13 :param unit: a text describing the unit of length, by default "pixels"
14 """
15 length: float = 1.0
16 unit: str = 'pixels'
18 def is_default(self) -> bool:
19 """
20 Returns True if this is a default value (length is 1.0 and unit is 'pixels')
22 :returns: whether this is a default pixel length
23 """
24 return self.length == 1.0 and self.unit == 'pixels'
26 @staticmethod
27 def create_microns(length: float) -> PixelLength:
28 """
29 Create a PixelLength with a unit of micrometers (µm).
31 :param length: the length of the pixel
32 :returns: a pixel length of the provided length with the 'micrometer' unit
33 """
34 return PixelLength(length=length, unit='micrometer')
36 @staticmethod
37 def create_unknown(length: float) -> PixelLength:
38 """
39 Create a PixelLength with an unknown unit.
41 :param length: the length of the pixel
42 :returns: a pixel length of the provided length with no unit
43 """
44 return PixelLength(length=length, unit=None)
47@dataclass(frozen=True)
48class PixelCalibration:
49 """
50 Simple data class for storing pixel calibration information.
52 :param length_x: the pixel size along the x-axis
53 :param length_y: the pixel size along the y-axis
54 :param length_z: the pixel size along the z-axis
55 """
56 length_x: PixelLength = PixelLength()
57 length_y: PixelLength = PixelLength()
58 length_z: PixelLength = PixelLength()
60 def is_calibrated(self) -> bool:
61 """
62 Indicate if this PixelCalibration has at least one non-default length.
64 :returns: whether this PixelCalibration has at least one non-default length
65 """
66 for size in [self.length_x, self.length_y, self.length_z]:
67 if not size.is_default():
68 return True
69 return False