From c311d16ff9db600815acdf23b005fa4ad388607c Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 2 Sep 2024 22:59:20 -0400 Subject: [PATCH] fix: only apply comp_prec for floating dtypes (#711) Fix #703. ## Summary by CodeRabbit - **New Features** - Improved handling of data types during the reshaping process, ensuring type conversion only occurs for floating-point data. - **Bug Fixes** - Enhanced robustness of data processing by preventing unnecessary type casting for non-floating-point data types. Signed-off-by: Jinzhe Zeng --- dpdata/deepmd/comp.py | 4 +++- dpdata/deepmd/hdf5.py | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/dpdata/deepmd/comp.py b/dpdata/deepmd/comp.py index 5ba3914e..3d656b70 100644 --- a/dpdata/deepmd/comp.py +++ b/dpdata/deepmd/comp.py @@ -204,7 +204,9 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True): f"Shape of {dtype.name} is not (nframes, ...), but {dtype.shape}. This type of data will not converted to deepmd/npy format." ) continue - ddata = np.reshape(data[dtype.name], [nframes, -1]).astype(comp_prec) + ddata = np.reshape(data[dtype.name], [nframes, -1]) + if np.issubdtype(ddata.dtype, np.floating): + ddata = ddata.astype(comp_prec) for ii in range(nsets): set_stt = ii * set_size set_end = (ii + 1) * set_size diff --git a/dpdata/deepmd/hdf5.py b/dpdata/deepmd/hdf5.py index 78f4e33f..6b587893 100644 --- a/dpdata/deepmd/hdf5.py +++ b/dpdata/deepmd/hdf5.py @@ -252,9 +252,10 @@ def dump( for dt, prop in data_types.items(): if dt in data: if prop["dump"]: - reshaped_data[dt] = np.reshape(data[dt], prop["shape"]).astype( - comp_prec - ) + ddata = np.reshape(data[dt], prop["shape"]) + if np.issubdtype(ddata.dtype, np.floating): + ddata = ddata.astype(comp_prec) + reshaped_data[dt] = ddata # dump frame properties: cell, coord, energy, force and virial nsets = nframes // set_size