Skip to content

Commit

Permalink
Add integer type for NdArray
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulVanSchayck committed Nov 14, 2024
1 parent 4ade6d1 commit a25348a
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/covjson_pydantic/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Coverage(CovJsonBaseModel, extra="allow"):
domain: Domain
parameters: Optional[Dict[str, Parameter]] = None
parameterGroups: Optional[List[ParameterGroup]] = None # noqa: N815
ranges: Dict[str, Union[NdArray[float], NdArray[str], TiledNdArray, AnyUrl]]
ranges: Dict[str, Union[NdArray[float], NdArray[int], NdArray[str], TiledNdArray, AnyUrl]]


class CoverageCollection(CovJsonBaseModel, extra="allow"):
Expand Down
3 changes: 3 additions & 0 deletions src/covjson_pydantic/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class DataType(str, Enum):
float = "float"
str = "string"
int = "integer"


NdArrayTypeT = TypeVar("NdArrayTypeT")
Expand All @@ -34,6 +35,8 @@ def check_data_type(self):
raise ValueError("dataType and NdArray type must both be float.")
if self.dataType == DataType.str and not t == typing.Optional[str]:
raise ValueError("dataType and NdArray type must both be string.")
if self.dataType == DataType.int and not t == typing.Optional[int]:
raise ValueError("dataType and NdArray type must both be integer.")

return self

Expand Down
3 changes: 3 additions & 0 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
("spec-domain-trajectory.json", Domain),
("ndarray-float.json", NdArray[float]),
("ndarray-string.json", NdArray[str]),
("ndarray-integer.json", NdArray[int]),
("spec-ndarray.json", NdArray[float]),
("spec-tiled-ndarray.json", TiledNdArray),
("continuous-data-parameter.json", Parameter),
Expand Down Expand Up @@ -68,6 +69,8 @@ def test_happy_cases(file_name, object_type):
("mixed-type-axes-2.json", Axes, r"Input should be a valid string"),
("mixed-type-ndarray-1.json", NdArray[float], r"Input should be a valid number"),
("mixed-type-ndarray-2.json", NdArray[str], r"dataType and NdArray type must both be float"),
("mixed-type-ndarray-3.json", NdArray[int], r"Input should be a valid integer"),
("mixed-type-ndarray-3.json", NdArray[float], r"dataType and NdArray type must both be integer"),
]


Expand Down
116 changes: 116 additions & 0 deletions tests/test_data/coverage-mixed-type-ndarray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"type": "Coverage",
"domain": {
"type": "Domain",
"domainType": "PointSeries",
"axes": {
"x": {
"values": [
5.3
]
},
"y": {
"values": [
53.2
]
},
"t": {
"values": [
"2022-01-01T04:10:00Z",
"2022-01-01T04:20:00Z",
"2022-01-01T04:30:00Z",
"2022-01-01T04:40:00Z",
"2022-01-01T04:50:00Z",
"2022-01-01T05:00:00Z"
]
}
}
},
"parameters": {
"float-parameter": {
"type": "Parameter",
"observedProperty": {
"label": {
"en": "float"
}
}
},
"string-parameter": {
"type": "Parameter",
"observedProperty": {
"label": {
"en": "string"
}
}
},
"integer-parameter": {
"type": "Parameter",
"observedProperty": {
"label": {
"en": "integer"
}
}
}
},
"ranges": {
"string-parameter": {
"type": "NdArray",
"dataType": "string",
"axisNames": [
"x",
"y",
"t"
],
"shape": [
1,
1,
2
],
"values": [
null,
"foo"
]
},
"float-parameter": {
"type": "NdArray",
"dataType": "float",
"axisNames": [
"x",
"y",
"t"
],
"shape": [
1,
1,
6
],
"values": [
null,
62,
63.136801411019825,
63.56289242989419,
63.98448554858814,
64.40077824091921
]
},
"integer-parameter": {
"type": "NdArray",
"dataType": "integer",
"axisNames": [
"x",
"y",
"t"
],
"shape": [
1,
1,
2
],
"values": [
1,
2
]
}
},
"extra:extra": "extra fields allowed"
}
15 changes: 15 additions & 0 deletions tests/test_data/mixed-type-ndarray-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "NdArray",
"dataType": "integer",
"axisNames": [
"y",
"x"
],
"shape": [
2
],
"values": [
1,
1.42
]
}
19 changes: 19 additions & 0 deletions tests/test_data/ndarray-integer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "NdArray",
"dataType": "integer",
"axisNames": [
"t",
"y",
"x"
],
"shape": [
1,
1,
3
],
"values": [
1,
2,
42
]
}

0 comments on commit a25348a

Please sign in to comment.