Skip to content

Commit

Permalink
Move invalid value checking to load_one_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Chia Yu Lin committed Aug 4, 2023
1 parent 7fa7bfa commit 2150ef3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
32 changes: 13 additions & 19 deletions deeprankcore/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,16 @@ def hdf5_to_pandas( # noqa: MC0001, pylint: disable=too-many-locals
df_dict[feat + '_' + str(i)] = [f[entry_name][feat_type][feat][:][:,i] for entry_name in entry_names]
#apply transformation for each channel in this feature
if transform:
for row in df_dict[feat + '_' + str(i)]:
if(np.isnan(transform(row)).any()):
raise ValueError(f"NaN value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")
if(np.isneginf(transform(row)).any() or np.isposinf(transform(row)).any()):
raise ValueError(f"Infinte value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")
df_dict[feat + '_' + str(i)] = transform(row)
df_dict[feat + '_' + str(i)] = [transform(row) for row in df_dict[feat + '_' + str(i)]]
else:
df_dict[feat] = [
f[entry_name][feat_type][feat][:]
if f[entry_name][feat_type][feat][()].ndim == 1
else f[entry_name][feat_type][feat][()] for entry_name in entry_names]
#apply transformation
if transform:
for row in df_dict[feat]:
if(np.isnan(transform(row)).any()):
raise ValueError(f"NaN value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")
if(np.isneginf(transform(row)).any() or np.isposinf(transform(row)).any()):
raise ValueError(f"Infinte value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")
df_dict[feat] = transform(row)

df_dict[feat]=[transform(row) for row in df_dict[feat]]

df = pd.DataFrame(data=df_dict)

df_final = pd.concat([df_final, df])
Expand Down Expand Up @@ -823,7 +809,11 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl

# apply transformation
if transform:
vals = transform(vals)
with warnings.catch_warnings(record=True) as w:
vals = transform(vals)
if (len(w)):
raise ValueError(f"Invalid value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")

if vals.ndim == 1: # features with only one channel
vals = vals.reshape(-1, 1)
Expand Down Expand Up @@ -869,7 +859,11 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl

# apply transformation
if transform:
vals = transform(vals)
with warnings.catch_warnings(record=True) as w:
vals = transform(vals)
if (len(w)):
raise ValueError(f"Invalid value occurs when applying {self.features_transform} for feature {feat}."
"Please set an appropriate features_transform function.")

if vals.ndim == 1:
vals = vals.reshape(-1, 1)
Expand Down
15 changes: 9 additions & 6 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,14 +913,17 @@ def test_invalid_value_features_transform(self):

hdf5_path = "tests/data/hdf5/train.hdf5"
features_transform = {'all': {'transform': lambda t: np.log(t+10), 'standardize': True}}

transf_dataset = GraphDataset(
hdf5_path = hdf5_path,
target = 'binary',
features_transform = features_transform
)
with pytest.raises(ValueError):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'invalid value encountered in log')
GraphDataset(
hdf5_path = hdf5_path,
target = 'binary',
features_transform = features_transform
)
warnings.filterwarnings('ignore', r'divide by zero encountered in divide')
_compute_features_with_get(hdf5_path, transf_dataset)

def test_inherit_info_training_graphdataset(self):
hdf5_path = "tests/data/hdf5/train.hdf5"
feature_transform = {'all': {'transform': None, 'standardize': True}}
Expand Down

0 comments on commit 2150ef3

Please sign in to comment.