Skip to content

Commit

Permalink
Preserves SpeasyVariable values dtype across dictionary serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
  • Loading branch information
jeandet committed Jul 2, 2024
1 parent 5854c88 commit 8d3e606
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion speasy/core/cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from speasy.config import cache as cache_cfg
from contextlib import ExitStack

cache_version = str_to_version("2.0")
cache_version = str_to_version("3.0")


class CacheItem:
Expand Down
20 changes: 13 additions & 7 deletions speasy/core/data_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,25 @@ def to_dictionary(self, array_to_list=False) -> Dict[str, object]:
"values": self.__values.tolist() if array_to_list else self.__values.copy(),
"meta": self.__meta.copy(),
"name": self.__name,
"is_time_dependent": self.is_time_dependent
"is_time_dependent": self.is_time_dependent,
"values_type": str(self.__values.dtype)
}

@staticmethod
def from_dictionary(dictionary: Dict[str, str or Dict[str, str] or List], dtype=np.float64) -> "DataContainer":
try:
return DataContainer(values=np.array(dictionary["values"], dtype=dtype), meta=dictionary["meta"],
name=dictionary["name"],
is_time_dependent=dictionary["is_time_dependent"])
return DataContainer(
values=np.array(dictionary["values"], dtype=dictionary.get("values_type", dtype)),
meta=dictionary["meta"],
name=dictionary["name"],
is_time_dependent=dictionary["is_time_dependent"]
)
except ValueError:
return DataContainer(values=np.array(dictionary["values"]), meta=dictionary["meta"],
name=dictionary["name"],
is_time_dependent=dictionary["is_time_dependent"])
return DataContainer(
values=np.array(dictionary["values"]), meta=dictionary["meta"],
name=dictionary["name"],
is_time_dependent=dictionary["is_time_dependent"]
)

@staticmethod
def reserve_like(other: 'DataContainer', length: int = 0) -> 'DataContainer':
Expand Down
2 changes: 1 addition & 1 deletion speasy/core/proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

log = logging.getLogger(__name__)
PROXY_ALLOWED_KWARGS = ['disable_proxy']
MINIMUM_REQUIRED_PROXY_VERSION = Version("0.10.0")
MINIMUM_REQUIRED_PROXY_VERSION = Version("0.11.0")
_CURRENT_PROXY_SERVER_VERSION = None

if proxy_cfg.url() == "" or proxy_cfg.enabled() == False:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_speasy_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def epoch_to_datetime64_s(epoch):
return np.datetime64(int(epoch * 1e9), 'ns')


def make_simple_var(start: float = 0., stop: float = 0., step: float = 1., coef: float = 1., meta=None):
def make_simple_var(start: float = 0., stop: float = 0., step: float = 1., coef: float = 1., meta=None,
dtype=np.float64):
time = np.arange(start, stop, step)
values = time * coef
values = values.astype(dtype)
return SpeasyVariable(axes=[VariableTimeAxis(values=epoch_to_datetime64(time))],
values=DataContainer(values=values, is_time_dependent=True, meta=meta), columns=["Values"])

Expand Down Expand Up @@ -252,6 +254,13 @@ def test_from_dict(self):
self.assertEqual(var1, var2)
self.assertEqual(var1, var3)

def test_from_dict_preserves_dtype(self):
for dtype in (np.float32, np.float64, np.int32, np.int64):
var = make_simple_var(1., 10., 1., 10., dtype=dtype)
var2 = from_dictionary(to_dictionary(var))
self.assertEqual(var.values.dtype, dtype)
self.assertEqual(var.values.dtype, var2.values.dtype)

def test_from_dataframe(self):
var1 = make_simple_var(1., 10., 1., 10.)
var2 = from_dataframe(to_dataframe(var1))
Expand Down

0 comments on commit 8d3e606

Please sign in to comment.