Skip to content

Commit

Permalink
add new attributes.
Browse files Browse the repository at this point in the history
/!\ Duplicate Type on copy is already used as a class attribute
  • Loading branch information
MatthieuCMira committed Oct 8, 2024
1 parent 0615540 commit 7db1679
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
86 changes: 85 additions & 1 deletion geoh5py/data/data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .reference_value_map import BOOLEAN_VALUE_MAP, ReferenceValueMap


if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from ..objects import ObjectBase
from ..workspace import Workspace
from .data import Data
Expand All @@ -57,10 +57,14 @@ class DataType(EntityType):
:param workspace: An active Workspace.
:param primitive_type: The primitive type of the data.
:param color_map: The colormap used for plotting.
:param duplicate_on_copy: Force a copy on copy of the data entity.
:param duplicate_type_on_copy: Force a copy on copy of the data entity.
:param hidden: If the data are hidden or not.
:param mapping: The type of color stretching to plot the colormap.
:param number_of_bins: The number of bins used by the histogram.
:param precision: The decimals precision of the data to display.
:param scale: The type of scale of the data.
:param scientific_notation: If the data should be displayed in scientific notation.
:param transparent_no_data: If the no data values are displayed as transparent or not.
:param units: The type of the units of the data.
:param kwargs: Additional keyword arguments to set as attributes
Expand All @@ -73,8 +77,11 @@ class DataType(EntityType):
"Hidden": "hidden",
"Mapping": "mapping",
"Number of bins": "number_of_bins",
"Precision": "precision",
"Primitive type": "primitive_type",
"Transparent no data": "transparent_no_data",
"Scale": "scale",
"Scientific notation": "scientific_notation",
}
)

Expand All @@ -85,21 +92,29 @@ def __init__(
type[Data] | PrimitiveTypeEnum | str
) = PrimitiveTypeEnum.INVALID,
color_map: ColorMap | None = None,
duplicate_on_copy: bool = False,
duplicate_type_on_copy: bool = False,
hidden: bool = False,
mapping: ColorMapping = "equal_area",
number_of_bins: int | None = None,
precision: int = 2,
scale: str | None = None,
scientific_notation: bool = False,
transparent_no_data: bool = True,
units: str | None = None,
**kwargs,
):
super().__init__(workspace, **kwargs)
self.color_map = color_map
self.duplicate_on_copy = duplicate_on_copy
self.duplicate_type_on_copy = duplicate_type_on_copy
self.hidden = hidden
self.mapping = mapping
self.number_of_bins = number_of_bins
self.precision = precision
self.primitive_type = self.validate_primitive_type(primitive_type)
self.scale = scale
self.scientific_notation = scientific_notation
self.transparent_no_data = transparent_no_data
self.units = units

Expand Down Expand Up @@ -145,6 +160,24 @@ def color_map(self, color_map: ColorMap | dict | np.ndarray | None):

self.workspace.update_attribute(self, "color_map")

@property
def duplicate_on_copy(self) -> bool:
"""
If the data type should be duplicated on copy.
"""
return self._duplicate_on_copy

@duplicate_on_copy.setter
def duplicate_on_copy(self, value: bool):
if not isinstance(value, bool) and value not in [1, 0]:
raise TypeError(
f"Attribute 'duplicate_on_copy' must be a bool, not {type(value)}"
)

self._duplicate_on_copy = bool(value)
if self.on_file:
self.workspace.update_attribute(self, "attributes")

@property
def duplicate_type_on_copy(self) -> bool:
"""
Expand Down Expand Up @@ -271,6 +304,24 @@ def number_of_bins(self, n_bins: int | None):

self.workspace.update_attribute(self, "attributes")

@property
def precision(self) -> int:
"""
The decimals precision of the data to display.
"""
return self._precision

@precision.setter
def precision(self, value: int):
if not isinstance(value, int) or value < 0:
raise TypeError(
f"Attribute 'precision' must be an integer greater than 0, not {value}"
)

self._precision = value

self.workspace.update_attribute(self, "attributes")

@property
def primitive_type(self) -> PrimitiveTypeEnum:
"""
Expand Down Expand Up @@ -351,6 +402,39 @@ def primitive_type_from_values(values: np.ndarray | None) -> PrimitiveTypeEnum:
)
return primitive_type

@property
def scale(self) -> str | None:
"""
The type of scale of the data.
"""
return self._scale

@scale.setter
def scale(self, value: str | None):
if value not in ["linear", "log", None]:
raise ValueError(
f"Attribute 'scale' must be one of 'linear', 'log', NoneType, not {value}"
)
self._scale = value
self.workspace.update_attribute(self, "attributes")

@property
def scientific_notation(self) -> bool:
"""
If the data should be displayed in scientific notation.
"""
return self._scientific_notation

@scientific_notation.setter
def scientific_notation(self, value: bool):
if not isinstance(value, bool) and value not in [1, 0]:
raise TypeError(
f"Attribute 'scientific_notation' must be a bool, not {type(value)}"
)

self._scientific_notation = bool(value)
self.workspace.update_attribute(self, "attributes")

@staticmethod
def validate_primitive_type(
primitive_type: PrimitiveTypeEnum | str | type[Data],
Expand Down
12 changes: 12 additions & 0 deletions tests/data_instantiation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,15 @@ def test_data_type_attributes():

with pytest.raises(ValueError, match=r"Attribute 'primitive_type' should be one"):
data_type.validate_primitive_type(1)

with pytest.raises(TypeError, match=r"Attribute 'duplicate_on_copy'"):
data_type.duplicate_on_copy = "bidon"

with pytest.raises(TypeError, match=r"Attribute 'precision'"):
data_type.precision = "bidon"

with pytest.raises(ValueError, match=r"Attribute 'scale'"):
data_type.scale = "bidon"

with pytest.raises(TypeError, match=r"Attribute 'scientific_notation'"):
data_type.scientific_notation = "bidon"

0 comments on commit 7db1679

Please sign in to comment.