mxtaltools.common.utils

mxtaltools.common.utils.batch_compute_dipole(pos, batch, z, electronegativity_tensor)[source]

Compute a rough dipole moment for a flat batch of molecules, as the simple weighted sum of electronegativities. Do this in a batch fashion assuming the input is a flat list of graphs, indexed by batch

Parameters:
  • pos (torch.tensor(n,3)) – 3d coordinates

  • batch (torch.tensor(n)) – batch index

  • z (torch.tensor(n)) – atom types

  • electronegativity_tensor – fixed electronegativities for each atom type

Returns:

dipole_moment

Return type:

torch.tensor(num_graphs,3)

mxtaltools.common.utils.block_repeat_interleave(lengths: Tensor, repeats: Tensor) Tensor[source]

Repeat ragged blocks of a flat tensor in parallel on GPU.

Parameters:
  • lengths – (num_blocks,) tensor of block lengths.

  • repeats – (num_blocks,) tensor of repeat counts per block.

Returns:

1D tensor with each block repeated blockwise.

mxtaltools.common.utils.chunkify(lst: list, n: int)[source]

break up a list into n chunks of equal size (up to last chunk)

Parameters:
Returns:

list_of_chunks

Return type:

[n]

mxtaltools.common.utils.flatten_dict(dictionary, parent_key=False, separator='_')[source]

From : https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys Recursively convert a nested dictionary into a flattened dictionary

Parameters:
  • dictionary

  • parent_key

  • separator

Return type:

Dict with all nested dict flattened, with longer keys instead of nesting.

mxtaltools.common.utils.get_plotly_fig_size_mb(fig) float[source]
mxtaltools.common.utils.get_point_density(xy, bins=100, sigma=1.5, alpha=20)[source]
mxtaltools.common.utils.get_point_density_knn(xy, k=30, alpha=20)[source]
mxtaltools.common.utils.histogram_overlap_np(d1: ndarray, d2: ndarray)[source]

Compute the symmetric overlap of two histograms

Parameters:
  • d1 (np.array(n))

  • d2 (np.array(n))

Returns:

overlap

Return type:

float

mxtaltools.common.utils.invert_softplus_shift(y: Tensor, min_val: float | Tensor | None = 0.01, beta: float | None = 10) Tensor[source]
mxtaltools.common.utils.is_cuda_oom(e: Exception) bool[source]
mxtaltools.common.utils.log_rescale_positive(y: Tensor, cutoff: float = 0)[source]
mxtaltools.common.utils.namespace2dict(namespace_dict, higher_level='')[source]

Convert a dict from an optionally nested namespace to an optionally nested dict.

Parameters:
  • namespace_dict (dict for the higher level namespace)

  • higher_level (key for high level dict)

Return type:

Dict matching higher level namespace

mxtaltools.common.utils.normalize_np(x: ndarray)[source]

normalize an input by its span subtract min_x so that range is fixed on [0,1]

Parameters:

x (np.array)

Returns:

normed_x

Return type:

np.array

mxtaltools.common.utils.parse_to_torch(array: Tensor | ndarray | list, device: device | str, dtype=<class 'torch.float32'>) Tensor[source]
mxtaltools.common.utils.repeat_interleave(repeats: List[int], device: device | None = None)[source]

# todo why do we have this? There are builtin methods in torch. # TODO replace and deprecate Alternate implementation of torch.repeat_interleave borrowed from torch_geometric.data.collate

Parameters:
Return type:

Repeated tensor which has the same shape as input, except along the given axis.

mxtaltools.common.utils.sample_triangular_right(n_samples, start, stop, device='cpu')[source]

sample from the CDF of a uniform distribution the right-aligned triangular distribution

mxtaltools.common.utils.sample_uniform(num_samples, max_value, device)[source]
mxtaltools.common.utils.scale_bh_energy(bh_energy, beta: float | None = 100)[source]
mxtaltools.common.utils.siginv(x)[source]

inverts the sigmoid function

mxtaltools.common.utils.signed_log(y: tensor | ndarray) tensor | ndarray[source]
mxtaltools.common.utils.smooth_constraint(value: Tensor, threshold: float, mode: str, hardness: float)[source]
mxtaltools.common.utils.softmax_np(x: ndarray, temperature: float = 1)[source]

softmax function implemented in numpy

Parameters:
  • x (np.array)

  • temperature (float) – softmax temperature

Returns:

softmax_x

Return type:

np.array

mxtaltools.common.utils.softplus_shift(x: Tensor, min_val: float | Tensor | None = 0.01, beta: float | None = 10) Tensor[source]
mxtaltools.common.utils.standardize_np(data: ndarray, return_standardization: bool = False, known_mean=None, known_std=None)[source]

standardize an input 1D array by subtracting mean and dividing by standard deviation

Parameters:
  • data (np.array)

  • return_standardization (bool) – return the mean and std

  • known_mean (float, optional) – optionally use precomputed mean

  • known_std (float, optional) – optionally use precomputed standard deviation

Returns:

  • std_data (np.array)

  • mean and standard deviation (optional floats)

mxtaltools.common.utils.std_normal_to_uniform(rands)[source]
mxtaltools.common.utils.torch_ptp(tensor: tensor)[source]

torch implementation of np.ptp

Parameters:

tensor

Returns:

ptp

Return type:

float

mxtaltools.common.utils.uniform_to_std_normal(uniform)[source]