visiannot.tools.ToolsData

Module with functions for loading and saving data files

Summary

Functions

visiannot.tools.ToolsData.convertIntervalsToTimeSeries(…)

Converts intervals as 2D array to a time series of 0 and 1 (1D array)

visiannot.tools.ToolsData.convertTimeSeriesToIntervals(…)

Gets the intervals of a 1D signal with a specific value

visiannot.tools.ToolsData.getAttributeGeneric(…)

Gets an attribute in a mat or h5 file

visiannot.tools.ToolsData.getAttributeH5(…)

Gets an attribute in a h5 file

visiannot.tools.ToolsData.getDataGeneric(path)

Loads data from a file with format mat, h5, txt or wav

visiannot.tools.ToolsData.getDataH5(path[, …])

Reads the whole content of a H5 file or a specific dataset/group

visiannot.tools.ToolsData.getDataInterval(path)

Loads file containing temporal intervals, output shape (n_{intervals},2)

visiannot.tools.ToolsData.getDataIntervalAsTimeSeries(…)

Loads file containing temporal intervals, output shape (n_{samples},)

visiannot.tools.ToolsData.getDataMat(path, key)

Loads data from a mat file

visiannot.tools.ToolsData.getTxtLines(path)

Loads a file as a list of lines

visiannot.tools.ToolsData.getWorkingDirectory(path)

Gets working directory when ViSiAnnoT is launched, which depends on wether it is launched as a Python script or an executable (generated with PyInstaller)

visiannot.tools.ToolsData.recursiveReadH5(…)

Recursive function to read data from a h5py file object while preserving nested architecture

API

Functions

visiannot.tools.ToolsData.convertIntervalsToTimeSeries(intervals, nframes)[source]

Converts intervals as 2D array to a time series of 0 and 1 (1D array)

Parameters
  • intervals (numpy array or list) – intervals in frame numbers, shape (n_{intervals}, 2)

  • nframes – number of frames of the time series

Returns

intervals as a time series, shape (n_{frames},)

Return type

numpy array

If the end time of an interval is -1 (second column of intervals, then the end time is set to nframes.

Example::
>>> a = np.array([[4, 5], [9, 12], [16, -1]])
>>> convertIntervalsToTimeSeries(a, 20)
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
       1., 1., 1., 1.])
visiannot.tools.ToolsData.convertTimeSeriesToIntervals(data, value)[source]

Gets the intervals of a 1D signal with a specific value

Parameters
  • data (numpy array) – 1D array

  • value – value that defines the intervals to retrieve from data

Returns

2D array with indexes of intervals (ending index is not included in the interval, as with range in Python)

Return type

numpy array

Example::
>>> a = np.array([0, 0, 0, 0, 5, 1, 1, 1, 1, 5, 5, 5, 0, 0, 0, 0])
>>> convertTimeSeriesToIntervals(a,0)
array([[ 0,  4],
       [12, 16]])
>>> convertTimeSeriesToIntervals(a,1)
array([[5, 9]])
>>> convertTimeSeriesToIntervals(a,5)
array([[ 4,  5],
       [ 9, 12]])
visiannot.tools.ToolsData.getAttributeGeneric(path, key)[source]

Gets an attribute in a mat or h5 file

Parameters
  • path (str) – path to the file

  • key_path (str) – key path to the attribute in the file

If the file is not mat or h5, it returns key.

Returns

attribute

visiannot.tools.ToolsData.getAttributeH5(path, key_path)[source]

Gets an attribute in a h5 file

Parameters
  • path (str) – path to the file

  • key_path (str) – key path to the attribute in the file

Returns

attribute

visiannot.tools.ToolsData.getDataGeneric(path, key='', **kwargs)[source]

Loads data from a file with format mat, h5, txt or wav

Parameters
  • path (str string containing the path to the data) – path to the data file

  • key (str) – key to access the data in case of mat or h5 file, for txt file it is ignored

  • kwargs – keyword arguments of numpy.loadtxt (in case of txt file) or ToolsAudio.getDataAudio() (in case of wav file)

Returns

data

Return type

numpy array

It raises an exception if the format is not supported.

visiannot.tools.ToolsData.getDataH5(path, root_path='/')[source]

Reads the whole content of a H5 file or a specific dataset/group

It calls the recursive function recursiveReadH5().

Parameters
  • path (str) – path to the file

  • root_path – path to the H5 group or H5 dataset where to start retrieving data, default '/' (file root)

Returns

three options:

  • (dict) – in case root_path points to a H5 group, all data contained in the H5 group

  • (numpy array) – in case root_path points to a H5 dataset

  • None – in case root_path points to a location that is not in the file

visiannot.tools.ToolsData.getDataInterval(path, key='')[source]

Loads file containing temporal intervals, output shape (n_{intervals},2)

The file format must be supported by ToolsData.getDataGeneric().

The data can be stored in two ways:

  • shape (n_{intervals},2), where each line contains the start frame and end frame of an interval, then no conversion is needed

  • shape (n_{samples},) with 0 and 1, then it is converted to shape (n_{intervals},2)

Parameters
  • path (str) – path to the data file

  • key (str) – key to access the data in case of mat or h5 file, for txt file it is ignored

Returns

numpy array of shape (n_{intervals},2) with intervals in frames number

Return type

numpy array

visiannot.tools.ToolsData.getDataIntervalAsTimeSeries(path, n_samples, key='')[source]

Loads file containing temporal intervals, output shape (n_{samples},)

The data can be stored in two ways:

  • shape (n_{intervals},2), where each line contains the start frame and end frame of an interval, then it is converted to shape (n_{samples},), so the number of frames must be specified (allowed formats: txt, mat, h5)

  • shape (n_{samples},) with 0 and 1, then no conversion is needed (allowed formats: mat, h5)

Parameters
  • path (str) – path to the data file

  • n_samples (int) – number of samples of the time series

  • key (str) – key to access the data in case of mat or h5 file, for txt file it is ignored

Returns

numpy array of shape (n_{samples},) with intervals as a time series of 0 and 1

Return type

numpy array

visiannot.tools.ToolsData.getDataMat(path, key)[source]

Loads data from a mat file

Parameters
  • path (str) – path to the data file

  • key (str) – key to access the data

Returns

data

Return type

numpy array

visiannot.tools.ToolsData.getTxtLines(path)[source]

Loads a file as a list of lines

Parameters

path – path to the text file

Returns

list of strings with the lines of the file

Return type

list

visiannot.tools.ToolsData.getWorkingDirectory(path)[source]

Gets working directory when ViSiAnnoT is launched, which depends on wether it is launched as a Python script or an executable (generated with PyInstaller)

Typically, path is the path to a Python module of visiannot that is being executed.

In case it is launched as a Python script, it returns the absolute path to the directory containing the module.

In case it is launched as an executable generated with PyInstaller, it returns the path to the temporary directory created by PyInstaller where are putted source code and related data files.

Parameters

path (str) – typically __file

visiannot.tools.ToolsData.recursiveReadH5(parent_item)[source]

Recursive function to read data from a h5py file object while preserving nested architecture

It reaches the last group level recursively.

If the parent item is a H5 dataset, then the function returns a numpy array. Otherwise it returns a dictionary, where the key corresponds to one H5 group and the value correponds to the H5 group content (may it be a nested group, a numpy array in case of H5 dataset or a string/int/float in case of H5 attribute). The attributes of a H5 dataset are not retrieved, it only works for attributes of a H5 group.

Parameters

parent_item – h5py file object

Returns

all data contained in parent_item

Return type

dict or numpy array