Utilities

hetero.utils.get_default_args(func)[source]

Gets the default arguments of a function

Parameters:

func (callable) – the function to be investigated

Returns:

default arguments of the provided function

Return type:

dict

class hetero.utils.NpEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
hetero.utils.timer(func)[source]

Print the runtime of the decorated function

hetero.utils.asave(path, arr, target_type)[source]

a simple array saver that safely cast arrays to the target type.

hetero.utils.get_wav_shape(wav_path)[source]
hetero.utils.get_base_freqs(stim, dt=1)[source]
hetero.utils.rescale_time(t, stim, target_base_freq=1.0, avg_method='geom')[source]
hetero.utils.compute_dataset_slices(L_trn, L_tst, base_stride, task_params)[source]

Note that the task_params are in the physical units and have to be multiplied by base_stride to get the steps.

hetero.utils.compute_complexity(u, y, subtasks, savepath)[source]
hetero.utils.read_metadata(metadata_path)[source]
hetero.utils.make_net_ids(means, stds)[source]
hetero.utils.add_complexity_trier(df, N=3)[source]
hetero.utils.add_homog_score(df, std=False)[source]
hetero.utils.aggregate_scores(root, sim_pattern, invalid_patterns=None, score_name=None, return_std=True, n_tiers=3, transform=True, transforms=(<function <lambda>>, <function <lambda>>))[source]
hetero.utils.sample_taus(size, mu, std, distr='lognormal', clip_below=False, rng=None, tol_prcntg=-1, verbose=False)[source]

The given mu and std are the desired mean and standard deviation and are different from the parameters passed to rng. It can also handle the case of std=0, by returning a constant vector of the desired size.

hetero.utils.estimate_poly2_max_from_stats(mu, std, verbose=False)[source]

If mn and mx are the minimum and maximum values, the pdf would have the following form:

pdf(x) = 1/Z * 1/(1+x**2) (0) Z = atan(mx) - atan(mn)

And the first and second moments of this truncated pdf are:

E[x] = 1/(2*Z) * log( (1+mx**2) / (1+mn**2) ) (1) E[x**2] = (mx - mn)/Z - 1 (2)

One must find mx and mn such that:

E[x] = mu E[x**2] = std**2 - mu**2

which is very hard to solve. Fortunately, it turns out that both statistics are mostly influenced by mx and not mn. Thus we can use approximations mn ≈ 0, which gives raise to:

E[x] ≈ log( 1+ mx**2 ) / (2 * atan(mx)) (3) E[x**2] ≈ mx / atan(mx) - 1 (4)

Now, we can numerically solve for mx. However, only one of these equations can be used. The reason is that the first and second moment of the truncated pdf are not independent. Setting mu will determine std and vice versa. Here we only use equation (3). The resulting std will be reported.

hetero.utils.poly2_sampler(mn, mx, size)[source]

Since numpy doesn’t have a sampler for 1/(1+x**2) distribution we use the “inverse transform sampling” method to do so. Read more about this method here:

https://en.wikipedia.org/wiki/Inverse_trigonometric_functions

hetero.utils.fftautocorr(x)[source]

This implements a circular autocorrelation as opposed to the zero-padded correlation of numpy as and scipy. It is adapted from https://stackoverflow.com/a/28285527/7106957.

x must be a real sequence.

hetero.utils.autocorr(x, maxlags=None)[source]

based on matplotlib’s acorr function. Always returns the normalized symmetric lag autocorrelation.

hetero.utils.find_top_idx(x)[source]
hetero.utils.find_base_lags(u, average='geometric')[source]
hetero.utils.canonicalize_value(val)[source]

Format values consistently for filenames.

Parameters:

val (int, float, str) – Value to format

Returns:

Formatted value: - Floats in scientific notation with no + signs - Strings in uppercase - Other types stringified

Return type:

str

hetero.utils.sanitize_token(s)[source]

Clean up strings for safe filenames.

Parameters:

s (str) – String to clean

Returns:

Safe filename string with only: - Alphanumeric characters - Underscores - Dashes - Dots

Return type:

str

hetero.utils.load_inputs(sim_path, load_noise=True, mmap=False)[source]
hetero.utils.load_target(sim_path, mmap=False)[source]
hetero.utils.load_complexity(sim_path)[source]
hetero.utils.load_weights(sim_path)[source]
hetero.utils.load_topology(sim_path)[source]
hetero.utils.load_nets(sim_path)[source]
hetero.utils.load_taus(sim_path)[source]
hetero.utils.load_ic(sim_path)[source]