Пример #1
0
def get_cells(path=None, **kwargs):
    """Get cells to use during the generation.

    Parameter:
        path: none | string (optional)
            The path to the directory in which to look for the cells.

    Return:
        cells: dictionary
            The cells to get.

    See also:
        circusort.io.generate_cells
        circusort.io.get_cell
    """

    if isinstance(path, (str, unicode)):
        path = normalize_path(path, **kwargs)
        if path[-6:] != "/cells":
            path = os.path.join(path, "cells")
        if os.path.isdir(path):
            try:
                cells = load_cells(path, **kwargs)
            except OSError:
                cells = generate_cells(**kwargs)
        else:
            cells = generate_cells(**kwargs)
    else:
        cells = generate_cells(**kwargs)

    return cells
Пример #2
0
    def plot(self, output=None, **kwargs):

        if output is None:

            raise NotImplementedError()  # TODO complete.

        else:

            path = normalize_path(output)
            if os.path.isdir(path):
                if 'rate' in self.parameters['train']:
                    self.plot_rate(output=path, **kwargs)
                if 'x' in self.parameters[
                        'position'] and 'y' in self.parameters['position']:
                    self.plot_position(output=path, **kwargs)
                if self.amplitude is not None:
                    self.amplitude.plot(output=path, **kwargs)
                if self.position is not None:
                    self.position.plot(output=path, **kwargs)
                self.train.plot(output=path, **kwargs)
                self.template.plot(output=path, **kwargs)
            else:
                raise NotImplementedError()  # TODO complete.

        return
Пример #3
0
    def plot_positions(self, output=None, ax=None, **kwargs):

        if output is not None and ax is None:
            plt.ioff()

        if ax is None:
            fig = plt.figure()
            gs = gds.GridSpec(1, 1)
            ax_ = fig.add_subplot(gs[0])
            self._plot_positions(ax_, **kwargs)
            gs.tight_layout(fig)
            if output is None:
                fig.show()
            else:
                path = normalize_path(output)
                if path[-4:] != ".pdf":
                    path = os.path.join(path, "positions.pdf")
                directory = os.path.dirname(path)
                if not os.path.isdir(directory):
                    os.makedirs(directory)
                fig.savefig(path)
        else:
            self._plot_positions(ax, **kwargs)

        return
Пример #4
0
    def plot(self, output=None, ax=None, **kwargs):
        """Plot data from file.

        Arguments:
            output: none | string (optional)
                The default value is None.
            ax: none | matplotlib.axes.Axes (optional)
                The default value is None.
            kwargs: dict (optional)
                Additional keyword arguments. See matplotlib.axes.Axes.plot for details.
        Return:
            ax: matplotlib.axes.Axes
        """

        if output is not None and ax is None:
            plt.ioff()

        if ax is None:
            fig = plt.figure()
            gs = gds.GridSpec(1, 1)
            ax_ = fig.add_subplot(gs[0])
            ax = self._plot(ax_, **kwargs)
            gs.tight_layout(fig)
            if output is None:
                fig.show()
            else:
                path = normalize_path(output)
                directory = os.path.dirname(path)
                if not os.path.isdir(directory):
                    os.makedirs(directory)
                fig.savefig(path)
        else:
            self._plot(ax, **kwargs)

        return ax
Пример #5
0
    def save(self, path, mode='default', **kwargs):
        """Save cells to files.

        Parameters:
            path: string
                The path to the directory in which to save the cells.
            mode: string (optional)
                The mode to use to save the cells. Either 'default', 'by cells' or 'by components'. The default value is
                'default'.
        """

        if mode == 'default' or mode == 'by cells':

            path = normalize_path(path, **kwargs)
            cells_directory = os.path.join(path, "cells")
            self.parameters.save(cells_directory, **kwargs)
            for k, cell in self.items():
                cell_directory = os.path.join(cells_directory, "{}".format(k))
                cell.save(cell_directory)

            # Update private attributes.
            self._mode = 'default'

        elif mode == 'by components':

            raise NotImplementedError()  # TODO complete.

        else:

            message = "Unknown mode value: {}".format(mode)
            raise ValueError(message)

        return
Пример #6
0
    def plot_y_position(self, output=None, ax=None, **kwargs):

        if output is not None and ax is None:
            plt.ioff()

        try:
            if ax is None:
                fig = plt.figure()
                gs = gds.GridSpec(1, 1)
                ax_ = plt.subplot(gs[0])
                self._plot_y_position(ax_, **kwargs)
                gs.tight_layout(fig)
                if output is None:
                    plt.show()
                else:
                    path = normalize_path(output)
                    if path[-4:] != ".pdf":
                        path = os.path.join(path, "parameters_y_position.pdf")
                    directory = os.path.dirname(path)
                    if not os.path.isdir(directory):
                        os.makedirs(directory)
                    plt.savefig(path)
            else:
                self._plot_y_position(ax, **kwargs)
        except TypeError:
            pass

        return
    def plot(self, output=None, ax=None, set_ax=False, **kwargs):
        # TODO add docstring.

        if output is not None and ax is None:
            plt.ioff()

        if ax is None:
            fig = plt.figure()
            gs = gds.GridSpec(1, 1)
            ax_ = fig.add_subplot(gs[0])
            self._plot(ax_, set_ax=True, **kwargs)
            gs.tight_layout(fig)
            if output is None:
                fig.show()
            else:
                path = normalize_path(output)
                if path[-4:] != ".pdf":
                    path = os.path.join(path, "amplitude.pdf")
                directory = os.path.dirname(path)
                if not os.path.isdir(directory):
                    os.makedirs(directory)
                fig.savefig(path)
        else:
            self._plot(ax, set_ax=set_ax, **kwargs)

        return
Пример #8
0
def get_parameters(path=None, defaults=None, types=None, default_type='string', **kwargs):
    """Get parameters from path.

    Parameter:
        path: none | string
            The path to the file from which to load the parameters. The default value is None.
        defaults: none | dictionary
            The default values of the parameters. The default value is None.
        types: none | dictionary
            The default types of the parameters. The default value is None.

    Return:
        parameters: dictionary
            The loaded parameters.
    """

    if isinstance(path, (str, unicode)):
        path = normalize_path(path, **kwargs)
        if os.path.isdir(path):
            path = os.path.join(path, "parameters.txt")
        if os.path.isfile(path):
            try:
                parameters = load_parameters(path, defaults=defaults, types=types, default_type=default_type)
            except (IOError, ValueError):
                parameters = generate_parameters(defaults=defaults, types=types)
        else:
            parameters = generate_parameters(defaults=defaults, types=types)
    else:
        parameters = generate_parameters(defaults=defaults, types=types)

    return parameters
def load_template_store(path, probe_file=None):
    """Load a template store from disk.

    Parameter:
        path: string
            The path to the HDF5 file from which to load the template store.
    Return:
        store: circusort.obj.TemplateStore
    """

    path = normalize_path(path)
    store = TemplateStore(path, probe_file=probe_file, mode='r')

    return store
def get_configurations(path=None, **kwargs):

    if isinstance(path, (str, unicode)):
        path = normalize_path(path, **kwargs)
        paths = list_configuration_paths(path)
        if paths:
            names = list_configuration_names(path)
            configurations = [get_configuration(path=path) for path in paths]
            for configuration, name in zip(configurations, names):
                configuration['general']['name'] = name
        else:
            raise NotImplementedError()  # TODO generate default configuration.
    else:
        raise NotImplementedError()  # TODO generate default configuration.

    return configurations
def load_time_measurements(path, name=None):
    # TODO add docstring.

    path = normalize_path(path)
    if os.path.isdir(path):
        filename = _get_filename(name)
        path = os.path.join(path, filename)
    if not os.path.isfile(path):
        message = "Time measurements file does not exist: {}".format(path)
        raise OSError(message)

    with h5py.File(path, mode='r', swmr=True) as file_:
        measurements = {
            dataset_name: file_[dataset_name].value
            for dataset_name in file_.keys()
        }

    return measurements
def save_time_measurements(path, measurements, name=None):
    # TODO add docstring.

    path = normalize_path(path)
    _, extension = os.path.splitext(path)
    if extension == '':
        filename = _get_filename(name)
        path = os.path.join(path, filename)
    directory = os.path.dirname(path)
    if not os.path.isdir(directory):
        os.makedirs(directory)

    with h5py.File(path, mode='w', swmr=True) as file_:
        for label in measurements:
            times = np.array(measurements[label])
            file_.create_dataset(name=label, data=times)

    return
Пример #13
0
    def plot(self, output=None, **kwargs):

        if output is None:
            raise NotImplementedError()  # TODO complete.
        else:
            path = normalize_path(output, **kwargs)
            cells_directory = os.path.join(path, "cells")
            parameters = get_cells_parameters(cells_directory)
            kwargs.update(parameters['general'])
            try:
                self.plot_rates(output=cells_directory, **kwargs)
            except NotImplementedError:
                pass  # TODO remove try ... except ...
            self.plot_trains(output=cells_directory, **kwargs)
            # TODO plot amplitudes.
            self.plot_positions(output=cells_directory, **kwargs)
            for k, cell in self.items():
                cell_directory = os.path.join(cells_directory, "{}".format(k))
                cell.plot(output=cell_directory, **kwargs)

        return
Пример #14
0
def load_templates(path):
    """Load templates.

    Parameter:
        path: string
            The path to the location from which to load the templates. Either a path to a directory (which contains
            multiple HDF5 files) or a HDF5 file.
    Return:
        templates: dictionary
            The dictionary of loaded templates.
    """

    path = normalize_path(path)
    if os.path.isfile(path):
        templates = _load_templates_from_file(path)
    elif os.path.isdir(path):
        templates = _load_templates_from_directory(path)
    else:
        message = "No such template file or directory: {}".format(path)
        raise OSError(message)

    return templates
Пример #15
0
    def save(self, path, **kwargs):

        # Normalize path.
        path = normalize_path(path, **kwargs)

        # Complete path (if necessary).
        if path[-4:] != ".txt":
            path = os.path.join(path, "parameters.txt")

        # Make directory (if necessary).
        directory = os.path.dirname(path)
        if not os.path.isdir(directory):
            os.makedirs(directory)

        # Prepare lines to be saved.
        lines = []
        for section in self.parameters.keys():
            line = "[{}]\n".format(section)
            lines.append(line)
            for option in self.parameters[section].keys():
                if option != 'current_directory' and not self._is_general(
                        section, option):
                    value = self.parameters[section][option]
                    line = "{} = {}\n".format(option, value)
                    lines.append(line)
            line = "\n"
            lines.append(line)

        # Open probe file.
        file_ = open(path, mode='w')
        # Write lines to save.
        file_.writelines(lines)
        # Close probe file.
        file_.close()

        return
Пример #16
0
def load_cells(path=None, mode='default', **kwargs):
    """Load cells from the specified path.

    Parameter:
        path: none | string (optional)
            The path to the directory from which to load the cells. The default value is None.
        mode: string (optional)
            The mode to use to load the cells. Either 'default' or 'by cells. The default value is 'default'.

    Return:
        cells: dictionary
            Dictionary of loaded cells.
    """

    if mode == 'by elements':

        template_directory = os.path.join(path, "templates")
        if not os.path.isdir(template_directory):
            message = "No such templates directory: {}".format(
                template_directory)
            raise OSError(message)
        template_paths = list_templates(template_directory)

        train_directory = os.path.join(path, "trains")
        if not os.path.isdir(train_directory):
            message = "No such trains directory: {}".format(train_directory)
            raise OSError(message)
        train_paths = list_trains(train_directory)

        position_directory = os.path.join(path, "positions")
        if not os.path.isdir(position_directory):
            message = "No such positions directory: {}".format(
                position_directory)
            raise OSError(message)
        position_paths = list_positions(position_directory)

        string = "Different number of templates and trains between {} and {}"
        message = string.format(template_directory, train_directory)
        assert len(template_paths) == len(train_paths), message

        string = "Different number of templates and positions between {} and {}"
        message = string.format(template_directory, position_directory)
        assert len(template_paths) == len(position_paths), message

        nb_cells = len(template_paths)
        cells = {}
        for k in range(0, nb_cells):
            template = load_template(template_paths[k])
            train = load_train(train_paths[k])
            position = load_position(position_paths[k])
            cell = Cell(template, train, position)
            cells[k] = cell
        cells = Cells(cells)

    elif mode in ['default', 'by cells']:

        path = normalize_path(path, **kwargs)
        if path[-6:] != "/cells":
            path = os.path.join(path, "cells")
        if not os.path.isdir(path):
            message = "No such cells directory: {}".format(path)
            raise OSError(message)
        parameters = get_cells_parameters(path)

        kwargs.update(parameters['general'])
        cell_directories = list_cells(path)
        cells = {
            k: get_cell(directory=cell_directory, **kwargs)
            for k, cell_directory in enumerate(cell_directories)
        }
        cells = Cells(cells, parameters=parameters)

    else:

        message = "Unknown mode value: {}".format(mode)
        raise ValueError(message)

    return cells
Пример #17
0
def load_spikes(*args, **kwargs):
    """Load spikes from disk.

    Arguments:
        args: list
            If mode is 'raw' then there are three arguments:
                times_path: string
                templates_path: string
                amplitudes_path: string
            else if mode is 'hdf5' then there is only one argument:
                path: string
        kwargs: dict (optional)
            nb_cells: none | integer (optional)
            mode: none | string (optional)
            t_max: none | float (optional)
    Return:
        spikes: circusort.obj.Spikes
    """

    nb_cells = kwargs.pop('nb_cells', None)
    mode = kwargs.pop('mode', None)
    t_max = kwargs.pop('t_max', None)
    if mode is None:
        if len(args) == 1:
            mode = 'hdf5'
        elif len(args) == 3:
            mode = 'raw'
        else:
            string = "load_spikes takes exactly 1 or 3 arguments (in 'raw' or 'hdf5' mode) ({} given)"
            message = string.format(len(args))
            raise TypeError(message)

    if mode == 'raw':

        if len(args) != 3:
            message = "load_spikes takes exactly 3 argument (in 'hdf5' mode) ({} given)".format(
                len(args))
            raise TypeError(message)
        times_path, templates_path, amplitudes_path = args

        # Normalize paths.
        times_path = normalize_path(times_path)
        if os.path.isdir(times_path):
            os.path.join(times_path, "spikes_times.raw")
        if not os.path.isfile(times_path):
            message = "File does not exist: {}".format(times_path)
            raise OSError(message)
        templates_path = normalize_path(templates_path)
        if os.path.isdir(templates_path):
            os.path.join(templates_path, "templates.h5")
        if not os.path.isfile(templates_path):
            message = "File does not exist: {}".format(templates_path)
            raise OSError(message)
        amplitudes_path = normalize_path(amplitudes_path)
        if os.path.isdir(amplitudes_path):
            os.path.join(amplitudes_path, "amplitudes.h5")
        if not os.path.isfile(amplitudes_path):
            message = "File does not exist: {}".format(amplitudes_path)
            raise OSError(message)

        data = {
            'times': np.fromfile(times_path, dtype=np.int32),
            'templates': np.fromfile(templates_path, dtype=np.int32),
            'amplitudes': np.fromfile(amplitudes_path, dtype=np.float32),
        }

    elif mode == 'hdf5':

        if len(args) != 1:
            message = "load_spikes takes exactly 1 argument (in 'raw' mode) ({} given)".format(
                len(args))
            raise TypeError(message)
        path, = args

        # Normalize path.
        path = normalize_path(path, **kwargs)
        if os.path.isdir(path):
            os.path.join(path, "spikes.h5")
        if not os.path.isfile(path):
            message = "File does not exist: {}".format(path)
            raise OSError(message)

        # Read data from HDF5 file.
        with h5py.File(path, mode='r', swmr=True) as file_:
            keys = ['times', 'templates', 'amplitudes']
            dtypes = ['int32', 'int32', 'float32']
            data = {
                key:
                file_[key].value if key in file_ else np.empty(0, dtype=dtype)
                for key, dtype in zip(keys, dtypes)
            }

    else:

        # Raise error.
        string = "Unknown mode value: {}"
        message = string.format(mode)
        raise ValueError(message)

    # Instantiate object.
    spikes = Spikes(nb_cells=nb_cells, t_max=t_max, **data)

    return spikes