Coverage for qubalab/images/metadata/pixel_calibration.py: 96%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-10-07 15:29 +0000

1from __future__ import annotations 

2from dataclasses import dataclass 

3from typing import Optional 

4 

5 

6@dataclass(frozen=True) 

7class PixelLength: 

8 """ 

9 Simple data class to store pixel size information, along one dimension. 

10 

11 Can be thought of as the pixel width, pixel height or pixel depth (z-spacing). 

12 

13 :param length: the length of the pixel, by default 1 

14 :param unit: a text describing the unit of length, by default "pixels". Can be None. 

15 """ 

16 

17 length: float = 1.0 

18 unit: Optional[str] = "pixels" 

19 

20 def is_default(self) -> bool: 

21 """ 

22 Returns True if this is a default value (length is 1.0 and unit is 'pixels') 

23 

24 :returns: whether this is a default pixel length 

25 """ 

26 return self.length == 1.0 and self.unit == "pixels" 

27 

28 @staticmethod 

29 def create_microns(length: float) -> PixelLength: 

30 """ 

31 Create a PixelLength with a unit of micrometers (µm). 

32 

33 :param length: the length of the pixel 

34 :returns: a pixel length of the provided length with the 'micrometer' unit 

35 """ 

36 return PixelLength(length=length, unit="micrometer") 

37 

38 @staticmethod 

39 def create_unknown(length: float) -> PixelLength: 

40 """ 

41 Create a PixelLength with an unknown unit. 

42 

43 :param length: the length of the pixel 

44 :returns: a pixel length of the provided length with no unit 

45 """ 

46 return PixelLength(length=length, unit=None) 

47 

48 

49@dataclass(frozen=True) 

50class PixelCalibration: 

51 """ 

52 Simple data class for storing pixel calibration information. 

53 

54 :param length_x: the pixel size along the x-axis 

55 :param length_y: the pixel size along the y-axis 

56 :param length_z: the pixel size along the z-axis 

57 """ 

58 

59 length_x: PixelLength = PixelLength() 

60 length_y: PixelLength = PixelLength() 

61 length_z: PixelLength = PixelLength() 

62 

63 def is_calibrated(self) -> bool: 

64 """ 

65 Indicate if this PixelCalibration has at least one non-default length. 

66 

67 :returns: whether this PixelCalibration has at least one non-default length 

68 """ 

69 for size in [self.length_x, self.length_y, self.length_z]: 

70 if not size.is_default(): 

71 return True 

72 return False