Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace the old data type with the same name and throw warning #541

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion dpdata/system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# %%
import glob
import os
import warnings
from copy import deepcopy
from typing import Any, Dict, Optional, Tuple, Union

Expand Down Expand Up @@ -963,7 +964,16 @@ def register_data_type(cls, *data_type: Tuple[DataType]):
*data_type : tuple[DataType]
data type to be regiestered
"""
cls.DTYPES = cls.DTYPES + tuple(data_type)
all_dtypes = cls.DTYPES + tuple(data_type)
dtypes_dict = {}
for dt in all_dtypes:
if dt.name in dtypes_dict:
warnings.warn(
f"Data type {dt.name} is registered twice; only the newly registered one will be used.",
UserWarning,
)
dtypes_dict[dt.name] = dt
cls.DTYPES = tuple(dtypes_dict.values())


def get_cell_perturb_matrix(cell_pert_fraction):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_custom_data_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

import dpdata
from dpdata.data_type import Axis, DataType


class TestDeepmdLoadDumpComp(unittest.TestCase):
Expand Down Expand Up @@ -44,6 +45,14 @@ def test_from_deepmd_hdf5(self):
x = dpdata.LabeledSystem("data_foo.h5", fmt="deepmd/hdf5")
np.testing.assert_allclose(x.data["foo"], self.foo)

def test_duplicated_data_type(self):
dt = DataType("foo", np.ndarray, (Axis.NFRAMES, 2, 4), required=False)
n_dtypes_old = len(dpdata.LabeledSystem.DTYPES)
with self.assertWarns(UserWarning):
dpdata.LabeledSystem.register_data_type(dt)
n_dtypes_new = len(dpdata.LabeledSystem.DTYPES)
self.assertEqual(n_dtypes_old, n_dtypes_new)


class TestDeepmdLoadDumpCompAny(unittest.TestCase):
def setUp(self):
Expand Down