Skip to content

Utils

Utils

filesys

create_dirs_on_path(f, create_parent_if_file=True)

function to create directories on given path if don't exist. Can be file or directory. If file needs create_parent_if_file flag

Parameters:

  • f (Path or str) –

    path to create dict on. can be dictionary or file

  • create_parent_if_file (bool, default: True ) –

    if f is a file create parent directory. Defaults to True.

Raises:

  • NotADirectoryError

    raises and error if f is file that has no suffix

Returns:

  • pathlib.Path: path with all directories created

Source code in src/simnetpy/utils/filesys.py
def create_dirs_on_path(f, create_parent_if_file=True):
    """
    function to create directories on given path if don't exist. Can be file or directory. 
    If file needs create_parent_if_file flag

    Args:
        f (pathlib.Path or str): path to create dict on. can be dictionary or file
        create_parent_if_file (bool, optional): if f is a file create parent directory. Defaults to True.

    Raises:
        NotADirectoryError: raises and error if f is file that has no suffix

    Returns:
        pathlib.Path: path with all directories created
    """
    p = pathlib.Path(f)
    if p.suffix != '':
        if create_parent_if_file:
            p.parent.mkdir(parents=True, exist_ok=True)
            return p
        else:
            raise NotADirectoryError

    p.mkdir(parents=True, exist_ok=True)
    return p

create_experiment_folder(path, timestamp=True, config_path=None, copysrc=True, src=SRCDIR)

Create experiment folder. Append timestamp to make name unique, copy src code to folder. copy config to folder

Parameters:

  • path (str or Path) –

    name/path for experiment output

  • src (_type_, default: SRCDIR ) –

    location of source code that generated the model. Defaults to HOME/phd/phd-year2/src/phd_year2.

  • config (str or Path) –

    location of config file.

Source code in src/simnetpy/utils/filesys.py
def create_experiment_folder(path, timestamp=True, config_path=None, copysrc=True, src=SRCDIR):
    """Create experiment folder. Append timestamp to make name unique, copy src code to folder. copy config to folder

    Args:
        path (str or pathlib.Path): name/path for experiment output
        src (_type_, optional): location of source code that generated the model. Defaults to HOME/phd/phd-year2/src/phd_year2.
        config (str or pathlib.Path): location of config file.
    """
    if timestamp:
        # add timestamp
        path = append_timestamp2path(path)

    # create folder
    path = create_dirs_on_path(path)

    if copysrc:
        # copy source code
        copy_src_to_folder(path, src=src)

    if config_path is not None:
        copyfile(config_path, path)
    return path

json_numpy_obj_hook(dct)

Decodes a previously encoded numpy ndarray with proper shape and dtype.

:param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray

Source code in src/simnetpy/utils/filesys.py
def json_numpy_obj_hook(dct):
    """Decodes a previously encoded numpy ndarray with proper shape and dtype.

    :param dct: (dict) json encoded ndarray
    :return: (ndarray) if input was an encoded ndarray
    """
    if isinstance(dct, dict) and '__ndarray__' in dct:
        # data = base64.b64decode(dct['__ndarray__'])
        data = dct['__ndarray__']
        return np.array(data, dct['dtype']).reshape(dct['shape'])
    return dct

plotting

sci_funcs

nanmean(x, allnanvalue=np.nan, **npkwds)

Function to compute np.nanmean and replace empty slice with user value. Defaults to np.nan i.e. np.nanmean([np.nan,np.nan]) = np.nan.

Parameters:

  • x (ndarray) –

    array to apply np.nanmean to.

  • allnanvalue (int, default: nan ) –

    Value in case of empty slice. Defaults to np.nan.

  • **npkwds

    keywords for np.nanmean function. e.g. axis=0 etc.

Returns: type: nan mean of array or allnanvalue in case of empty slice in array

Source code in src/simnetpy/utils/sci_funcs.py
def nanmean(x, allnanvalue=np.nan, **npkwds):
    """Function to compute np.nanmean and replace empty slice with 
    user value. Defaults to np.nan i.e. np.nanmean([np.nan,np.nan]) = np.nan.

    Args:
        x (np.ndarray): array to apply np.nanmean to.
        allnanvalue (int, optional): Value in case of empty slice. Defaults to np.nan.
        **npkwds: keywords for np.nanmean function. e.g. axis=0 etc.
    Returns:
        _type_: nan mean of array or allnanvalue in case of empty slice in array
    """
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore')
        y=np.nanmean(x, **npkwds)
        y=np.nan_to_num(y, nan=allnanvalue)  
    return y

non_nan_indices(X, offset=0)

Find indices that do not have all nan values. Amount of nans needed to be classified as a nan index can be adjusted with offset.

Parameters:

  • X (ndarray) –

    two dimensional (Nxd) array. nan counted per row.

  • offset (int, default: 0 ) –

    amount of columns with values to still be considered nan. i.e. a row with (d - offset) values missing is nan

Returns:

  • np.ndarray: 1d array of indices in range [0,N-1]

Source code in src/simnetpy/utils/sci_funcs.py
def non_nan_indices(X, offset=0):
    """Find indices that do not have all nan values.
      Amount of nans needed to be classified as a nan index can be adjusted with offset.

    Args:
        X (np.ndarray): two dimensional (Nxd) array. nan counted per row.
        offset (int): amount of columns with values to still be considered nan. 
                        i.e. a row with (d - offset) values missing is nan

    Returns:
        np.ndarray: 1d array of indices in range [0,N-1]
    """
    assert len(X.shape)==2, f"X must be 2 dimensional, not {len(X.shape)}"
    N, d = X.shape
    indices = np.arange(N)
    idx = np.isnan(X).sum(axis=1) >= d - offset
    idx = indices[~idx]
    return idx