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

Active Learning #593

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
93 changes: 67 additions & 26 deletions mala/common/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ def load_from_file(

Parameters
----------
file : string or ZipExtFile
file : string or ZipExtFile or dict
File to which the parameters will be saved to.

save_format : string
Expand Down Expand Up @@ -1585,37 +1585,49 @@ def load_from_file(
json_dict = json.load(open(file, encoding="utf-8"))
else:
json_dict = json.load(file)
loaded_parameters = cls._process_loaded_dict(
json_dict, no_snapshots, force_no_ddp
)

elif save_format == "dict":
loaded_parameters = cls._process_loaded_dict(
file, no_snapshots, force_no_ddp
)

loaded_parameters = cls()
for key in json_dict:
if (
isinstance(json_dict[key], dict)
and key != "openpmd_configuration"
):
# These are the other parameter classes.
sub_parameters = globals()[
json_dict[key]["_parameters_type"]
].from_json(json_dict[key])
setattr(loaded_parameters, key, sub_parameters)

# We iterate a second time, to set global values, so that they
# are properly forwarded.
for key in json_dict:
if (
not isinstance(json_dict[key], dict)
or key == "openpmd_configuration"
):
if key == "use_ddp" and force_no_ddp is True:
setattr(loaded_parameters, key, False)
else:
setattr(loaded_parameters, key, json_dict[key])
if no_snapshots is True:
loaded_parameters.data.snapshot_directories_list = []
else:
raise Exception("Unsupported parameter save format.")

return loaded_parameters

@classmethod
def _process_loaded_dict(cls, parameters_dict, no_snapshots, force_no_ddp):
parameters_object = cls()
for key in parameters_dict:
if (
isinstance(parameters_dict[key], dict)
and key != "openpmd_configuration"
):
# These are the other parameter classes.
sub_parameters = globals()[
parameters_dict[key]["_parameters_type"]
].from_json(parameters_dict[key])
setattr(parameters_object, key, sub_parameters)

# We iterate a second time, to set global values, so that they
# are properly forwarded.
for key in parameters_dict:
if (
not isinstance(parameters_dict[key], dict)
or key == "openpmd_configuration"
):
if key == "use_ddp" and force_no_ddp is True:
setattr(parameters_object, key, False)
else:
setattr(parameters_object, key, parameters_dict[key])
if no_snapshots is True:
parameters_object.data.snapshot_directories_list = []
return parameters_object

@classmethod
def load_from_pickle(cls, file, no_snapshots=False):
"""
Expand Down Expand Up @@ -1666,3 +1678,32 @@ def load_from_json(cls, file, no_snapshots=False, force_no_ddp=False):
no_snapshots=no_snapshots,
force_no_ddp=force_no_ddp,
)

@classmethod
def load_from_dict(
cls, param_dict, no_snapshots=False, force_no_ddp=False
):
"""
Load a Parameters object from a file.

Parameters
----------
param_dict : dictionary
Dictionary containing parameters to be loaded

no_snapshots : bool
If True, than the snapshot list will be emptied. Useful when
performing inference/testing after training a network.

Returns
-------
loaded_parameters : Parameters
The loaded Parameters object.

"""
return Parameters.load_from_file(
param_dict,
save_format="dict",
no_snapshots=no_snapshots,
force_no_ddp=force_no_ddp,
)
51 changes: 44 additions & 7 deletions mala/common/physical_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import json
import numpy as np
from mala.common.parallelizer import get_comm, get_rank
from mala.common.parallelizer import get_comm, get_rank, printout

from mala.version import __version__ as mala_version

Expand Down Expand Up @@ -69,7 +69,7 @@ def si_unit_conversion(self):
##############################

def read_from_numpy_file(
self, path, units=None, array=None, reshape=False
self, path, units=None, array=None, reshape=False, selection_mask=None
):
"""
Read the data from a numpy file.
Expand All @@ -86,6 +86,11 @@ def read_from_numpy_file(
If not None, the array to save the data into.
The array has to be 4-dimensional.

selection_mask : None or [boolean]
If None, entire snapshot is loaded, else it is used as a
mask to select which examples are loaded


Returns
-------
data : numpy.ndarray or None
Expand All @@ -97,17 +102,42 @@ def read_from_numpy_file(
if array is None:
loaded_array = np.load(path)[:, :, :, self._feature_mask() :]
self._process_loaded_array(loaded_array, units=units)
return loaded_array

# Select portion of array if mask provided
if selection_mask is not None:
original_dims = loaded_array.shape

# Pseudo-flatten to apply mask without causing dimensionality mismatch later on
loaded_array = loaded_array.reshape(
(-1, 1, 1, original_dims[-1])
)[selection_mask]
return loaded_array
else:
return loaded_array
else:
if reshape:
array_dims = np.shape(array)
array[:, :] = np.load(path)[
:, :, :, self._feature_mask() :
].reshape(array_dims)
if selection_mask is not None:
array[:, :] = np.load(path)[
:, :, :, self._feature_mask() :
].reshape((len(selection_mask), -1))[selection_mask]
else:
array[:, :] = np.load(path)[
:, :, :, self._feature_mask() :
].reshape(array_dims)
else:
array_dims = np.shape(array)
array[:, :, :, :] = np.load(path)[
:, :, :, self._feature_mask() :
]

# Select portion of array if mask provided
if selection_mask is not None:
# Pseudo-flatten to apply mask without causing
# dimensionality mismatch later on
array = array.reshape((-1, 1, 1, array_dims[-1]))[
selection_mask
]
self._process_loaded_array(array, units=units)

def read_from_openpmd_file(self, path, units=None, array=None):
Expand Down Expand Up @@ -252,7 +282,9 @@ def read_from_openpmd_file(self, path, units=None, array=None):
else:
self._process_loaded_array(array, units=units)

def read_dimensions_from_numpy_file(self, path, read_dtype=False):
def read_dimensions_from_numpy_file(
self, path, read_dtype=False, selection_mask=None
):
"""
Read only the dimensions from a numpy file.

Expand All @@ -265,6 +297,11 @@ def read_dimensions_from_numpy_file(self, path, read_dtype=False):
If True, the dtype is read alongside the dimensions.
"""
loaded_array = np.load(path, mmap_mode="r")
if selection_mask is not None:
original_dims = loaded_array.shape
loaded_array = loaded_array.reshape((-1, 1, 1, original_dims[-1]))[
selection_mask
]
if read_dtype:
return (
self._process_loaded_dimensions(np.shape(loaded_array)),
Expand Down
Loading