Utils

colfi.utils

class colfi.utils.FilePath(filedir='ann', randn_num='', suffix='.pt', separator='_', raise_err=True)[source]

Bases: object

filePath()[source]
class colfi.utils.Logger(path='logs', fileName='log', stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Bases: object

Record the output of the terminal and write it to disk.

flush()[source]
write(message)[source]
class colfi.utils.LrDecay(iter_mid, iteration=10000, lr=0.1, lr_min=1e-06)[source]

Bases: object

Let the learning rate decay with iteration.

exp(gamma=0.999, auto_params=True)[source]

Exponential decay.

Parameters:

auto_params (bool) – If True, gamma is set automatically.

Returns:

lr * gamma^iteration

Return type:

float

poly(decay_step=500, power=0.999, cycle=True)[source]

Polynomial decay.

Returns:

(lr-lr_min) * (1 - iteration/decay_steps)^power +lr_min

Return type:

float

step(stepsize=1000, gamma=0.3, auto_params=True)[source]

Let the learning rate decays step by step, similar to ‘exp’.

colfi.utils.get_randn_suffix(randn_num=1.234)[source]
colfi.utils.logger(path='logs', fileName='log')[source]
colfi.utils.makeList(roots)[source]

Checks if the given parameter is a list.

Parameters:

roots (object) – The parameter to check. If it is not a list, creates a list with the parameter as an item in it.

Returns:

A list containing the parameter.

Return type:

list

colfi.utils.mkdir(path)[source]

Make a directory in a particular location if it is not exists.

Parameters:

path (str) – The path of a file.

Examples

>>> mkdir('/home/UserName/test')
>>> mkdir('test/one')
>>> mkdir('../test/one')
colfi.utils.remove_nan(obs, params)[source]

Remove the ‘nan’ in the numpy array, used for the simulated observations.

Parameters:
  • obs (array-like) – The simulated observations, Numpy array with one or multi dimension.

  • params (array-like) – The simulated parameters, Numpy array with one or multi dimension.

Returns:

  • obs_new (array-like) – The new observations that do not contain nan.

  • params_new (array-like) – The new parameters that do not contain nan.

colfi.utils.saveTorchPt(path, FileName, File)[source]

Save the .pt files using torch.save() funtion.

Parameters:
  • path (str) – The path of the file to be saved.

  • FileName (str) – The name of the file to be saved.

  • File (object) – The file to be saved.

colfi.utils.save_predict(path='ann', nde_type='ANN', randn_num=1.123, file_identity_str='', chain_true_path='', label_true='True', fiducial_params=[])[source]
colfi.utils.savenpy(path, FileName, File, dtype=<class 'numpy.float32'>)[source]

Save an array to a binary file in .npy format using numpy.save() function.

Parameters:
  • path (str) – The path of the file to be saved.

  • FileName (str) – The name of the file to be saved.

  • File (object) – The file to be saved.

  • dtype (str or object) – The type of the data to be saved. Default: numpy.float32.

colfi.utils.savetxt(path, FileName, File)[source]

Save the .txt files using numpy.savetxt() funtion.

Parameters:
  • path (str) – The path of the file to be saved.

  • FileName (str) – The name of the file to be saved.

  • File (object) – The file to be saved.