Utils¶
Utility module
Data management¶
-
class
moldyn.utils.data_mng.
DynState
(treant, *, extraction_path='/home/docs/.local/share/open-moldyn/tmp')[source]¶ A Treant specialized for handling .npy and .json files for moldyn
-
POS
¶ standard name of the position file (“pos.npy”)
Type: str
-
POS_H
¶ standard name of the position history file (“pos_history.npy”)
Type: str
-
VEL
¶ standard name of the velocity file (“velocities.npy”)
Type: str
-
STATE_FCT
¶ standard name of the state function file (“state_fct.json”)
Type: str
-
PAR
¶ standard name of the parameter file (“parameters.json”)
Type: str
-
open
(file, mode='r')[source]¶ Open the file in this tree (ie. directory and subdir). Return the appropriate IO class depending of the type of the file.
If the type is not recognize, it opens the file and return the BytesIO or StringIO object.
Parameters: - file (str) – The name of the file. This file must be in this tree (ie. directory and subdir).
- mode (str (default='r')) –
- The mode with which to open the file :
- ’r’ to read
- ’w’ to write
- ’a’ to append
- ’b’ to read or write bytes (eg. ‘w+b’ to write bytes)
Returns: - If file is a .npy file, return a NumpyIO object.
- If file is a .json file, return a ParamIO object.
- Else, return a StringIO or a BytesIO depending on mode.
Note
This method is designed to be used wih a context manager like this
t = DynState(dirpath) # here t.open returns a NumpyIO object as the file is .npy with t.open("pos.npy", 'r') as IO: arr = IO.load() #load an array with t.open("pos.npy", 'w') as IO: IO.save(arr) #save an array
-
save_model
(model)[source]¶ Save the positions, the velocities and the parameters of the model.
The position and velocity arrays are saved as numpy files and the parameter dictionary as a .json file
Parameters: model (simulation.builder.Model) – The model to be saved.
-
-
class
moldyn.utils.data_mng.
NumpyIO
(dynState, file, mode)[source]¶ An interface to interact with numpy save files with a context manager.
-
dynState
¶ The treant that support the leaf (file) associated with it.
Type: datreant.Treant
-
mode
¶ The access mode of the file (usually ‘r’ or ‘w’ or ‘a’)
Type: str
-
file_name
¶ The file name of the .npy file associated with it.
Type: datreant.Leaf
-
file
¶ the file that is opened (contains None until entering a context manager)
Type: file object
Example
t = DynState(dirpath) # here t.open returns a NumpyIO object as the file is .npy with t.open("pos.npy", 'r') as IO: arr = IO.load() #load an array with t.open("pos.npy", 'w') as IO: IO.save(arr) #save an array
-
-
class
moldyn.utils.data_mng.
ParamIO
(dynState, file, **kwargs)[source]¶ An interface to interact with json files as a dictionary.
Designed to be used with context manager (the with statement) and datreant. Upon entering the with statement it will attempt to load the json file at self.file_name into itself (as a dict). When leaving this context, it will store itself into the json file.
Inherits dict.
It will try to update the tags and categories of the treant dynState.
-
dynState
¶ The treant that support the leaf (file) associated with it.
Type: datreant.Treant
-
file_name
¶ The file name of the json file associated with it.
Type: datreant.Leaf
Example
t = DynState(dirpath) # here t.open returns a ParamIO object as the file is .json with t.open("params.json") as IO: IO["my_key"] = "my_value" random_var = IO[some_key] # upon exiting the with block, the content of IO is stored # in the .json file
Parameters: - dynState (
Treant
) – - file (
Leaf
) – - kwargs –
-
__enter__
()[source]¶ Try to load the content of the json file into itself
Try to open the json file from file_name and load the informtions it contains into itself. Will catch FileNotFoundError and JSONDecodeError
Returns: self Return type: ParamIO
-
__exit__
(exc_type, exc_val, exc_tb)[source]¶ Open the json file file_name and dump itself into it.
Open the json file file_name and dump itself into it + update the tags and categories of dynState according to the CATEGORY_LIST of the module.
Parameters: - exc_type –
- exc_val –
- exc_tb –
-
from_dict
(rdict)[source]¶ Copy rdict into itself
Parameters: rdict (dict) – The remote dictionary from which to copy
-