nems.utils module

utils library

This module contains random utility functions called by a number of different NEMS libraries


Returns a string containing the present date as a string.

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

Bases: json.encoder.JSONEncoder

For serializing Numpy arrays safely as JSONs. Modified from: saving as byte64 doesn’t work, but using lists instead seems ok.


If input object is an ndarray it will be converted into a dict holding dtype, shape and the data. data is encoded as a list, which makes it text-readable.


Decodes a previously encoded numpy ndarray with proper shape and dtype, or an encoded KeywordRegistry.


dct – (dict) json encoded ndarray


(ndarray) if input was an encoded ndarray

nems.utils.adjust_uri_prefix(uri, use_nems_defaults=True)[source]

if get_setting(‘USE_NEMS_BAPHY_API’) is True: translate file system URI to http –or– if get_setting(‘USE_NEMS_BAPHY_API’) is False: translate http URI to file system

Warning! May be too hacky, and unclear where this should be evaluated! Currently run in uri.load_uri, which may be too low-level, as there may be situations where you want to hard-code a URI that doesn’t match the expectations of the current configuration.

nems.utils.recording_filename_hash(name, meta, uri_path='', uncompressed=False)[source]

name : string meta : dictionary (string keys only?) uri_path : base path uncompressed : boolean

if True, return .tgz file if False, return path to folder


<uri_path>/<batch>/<name>_<meta hash>.tgz

hashing function to generate recording filenames JSON encode meta, then append hash to name


vector of 1 followed by zerocount 0s


Returns (api, fn_name) given a string that would be used to import a function from a package.

nems.utils.shrinkage(mH, eH, sigrat=1, thresh=0)[source]

apply shrinkage transformation to estimated mean value (mH), based on the relative size of the standard error (eH)


This function can be redirected to a function that tracks progress externally, eg, in a queueing system

nems.utils.find_module(query: object, modelspec: object, find_all_matches: object = False, key: object = 'fn') object[source]

name : string modelspec : NEMS modelspec (list of dictionaries)

if True:

returns first index where modelspec[][‘fn’] contains name returns None if no match

if False

returns list of index(es) where modelspec[][‘fn’] contains name returns empty list [] if no match

nems.utils.escaped_split(string: str, delimiter: str)[source]

Splits string along delimiter, ignoring escaped delimiters. Useful for some arguments in keyword strings that need to use delimiter for other reason.

  • string – string to be split

  • delimiter – character to split on


list of strings

nems.utils.escaped_join(split_list, delimiter: str)[source]

Joins a list of strings along a delimiter.

  • split_list – list of strings to join

  • delimiter – delimiter to join on


joined string

nems.utils.get_channel_number(sig, channel=None)[source]
find number of channel in signal sig that matches channel name or number

specified in channel. default return 0

nems.utils.smooth(x, window_len=11, window='hanning', axis=- 1)[source]

smooth the data using a window with requested size.

This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal.


x: the input signal window_len: the dimension of the smoothing window; should be an odd integer window: the type of window from ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’

flat window will produce a moving average smoothing.


the smoothed signal


t=linspace(-2,2,0.1) x=sin(t)+randn(len(t))*0.1 y=smooth(x)

see also:

numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve scipy.signal.lfilter

TODO: the window parameter could be the window itself if an array instead of a string NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.


remove right and top lines from plot border


Count depth of nesting of a sequence. E.g. [[[1]]] has a depth of 3.


Given a list of strings, returns the common prefix to nearest _.


Given a list of strings, returns the common suffix to nearest _.

nems.utils.find_common(s_list, pre=True, suf=True)[source]

Given a list of strings, finds the common suffix and prefix, then returns a 3-tuple containing:

index 0, a new list with prefixes and suffixes removed index 1, the prefix that was found. index 2, the suffix that was found.

Takes s_list as list of strings (required), and pre and suf as Booleans (optional) to indicate whether prefix and suffix should be found. Both are set to True by default.

nems.utils.lookup_fn_at(fn_path, ignore_table=False)[source]

Private function that returns a function handle found at a given module. Basically, a way to import a single function. e.g.

myfn = _lookup_fn_at(‘nems.modules.fir.fir_filter’) myfn(data) …

Filter cellids or modelnames by simple space-separated search strings.


Search string, see syntax below.


List of items to compare the search string against. Ex: A list of cellids.


Collection with all non-matched entries removed.

space : OR & : AND ! : NEGATE (In that order of precedence)

Example: collection = [‘AMT001’, BRT000’, ‘TAR002’, ‘TAR003’]

search(‘AMT’, collection) >> [‘AMT001’]

search(‘AMT TAR’, collection) >> [‘AMT001’, ‘TAR002’, ‘TAR003’]

search (‘AMT TAR&!003’, collection) >> [‘AMT001’, ‘TAR002’]

search (‘AMT&TAR’, collection) >> []

nems.utils.load_settings(config_group='db_browser_last', configfile=None)[source]
nems.utils.save_settings(config_group='db_browser_last', settings={}, configfile=None)[source]