Misc

Here are few random helper functions useful in some special situations.

listmode.misc.atoi(text)[source]

Convert integer text to int, otherwise return the original string. Used to parse long filenames with numbering in the filenme.

Parameters

text – Input string.

Returns

If text is convertible to integer this returns the integer, otherwise the original text.

listmode.misc.check_monotonousness(vector)[source]

Checks if values in a given vector are monotonously increasing. If they are not, the index of the first element that breaks the monotonousness is returned. None. :param vector: :return: Index of first out-of-place element in vector. None is returned if vector is monotonous.

listmode.misc.fromisoformat(dt_string)[source]

Converts string returned by python 3.7+ datetime.toisoformat() into a proper datetime object. This was needed because the isoformat seems to be fairly new thing in python and not present in 3.6 or earlier.

The string to be parsed is of the format: “2020-06-09T04:00:01.434322”

Parameters

dt_string – isoformat string

Returns

datetime object

listmode.misc.make_run_id(base_path, base_name)[source]

Make sure old data is never overwritten. Adds an incremented number to base name. If a datafile with the same name already exists, then the number is incremented.

Parameters
  • base_path

  • base_name

Returns

listmode.misc.natural_keys(text)[source]

alist.sort(key=natural_keys) sorts number strings in human order (‘1’ before ‘10’) and so on. Taken from: http://nedbatchelder.com/blog/200712/human_sorting.html

Parameters

text – input string

Returns

listmode.misc.parse_file(dir_name, file_name, raw_ext=None)[source]

Finds data files (matching optional file_name prefix) from directory. First event mode data files are searched for, if not found, then channel mode files and finally raw data files are searched for. All files in the directory (or matching a given base name) are returned

Problem is that some raw data formats (Caen, native channel data) have multiple files per measurement. This is fixed by having a wildcard expression of the postfix of the filename included in the extension like ch???.dat for caen data. Unique names are then automatically resolved by saving the base names as keys to a dictionary (which is ordered in py3 unlike the set?).

Parameters

dir_name – The directory containing the data

Param

file_name: optional base name for data files if they are named differently from directory.

Param

raw_ext: optional wildcard pattern for raw data files.

Returns

list of base names of data files in the directory (matching file_name prefix)

listmode.misc.parse_time(args)[source]

Parses command line time slice with optional timebase argument. Timebase argument is one of ‘s’, ‘m’, ‘h’ or ‘d’. Timebase argument, if it exists, is the last value in the list or tuple of (start, [stop], [timebase]) that defines the slice.

Parameters

args – A list or tuple of (start, [stop], [timebase]). All input arguments are strings.

Returns

returns parsed time slice (start, stop) in integer nanoseconds.

listmode.misc.parse_timebase(args)[source]

Parses the timebase string if it exists, supplies default if it does not and returns the timebase multiplier with the other arguments.

Parameters

args – A list or tuple of (start, [stop], [timebase]). All input arguments are strings.

Returns

timebase multiplier for ns-transformation and list of the other arguments.