File: dictlike.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (79 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Module providing `DictImportExport` and `PandasImportExport` (requiring a
working installation of pandas).
"""

import numpy as np

from .importexport import ImportExport


class DictImportExport(ImportExport):
    """
    An importer/exporter for variables in format of dict of numpy arrays.
    """

    @property
    def name(self):
        return "dict"

    @staticmethod
    def export_data(group, variables, units=True, level=0):
        data = {}
        for var in variables:
            data[var] = np.array(
                group.state(var, use_units=units, level=level + 1),
                copy=True,
                subok=True,
            )
        return data

    @staticmethod
    def import_data(group, data, units=True, level=0):
        for key, value in data.items():
            if group.variables[key].read_only:
                raise TypeError(f"Variable {key} is read-only.")
            group.state(key, use_units=units, level=level + 1)[:] = value


class PandasImportExport(ImportExport):
    """
    An importer/exporter for variables in pandas DataFrame format.
    """

    @property
    def name(self):
        return "pandas"

    @staticmethod
    def export_data(group, variables, units=True, level=0):
        # pandas is not a default brian2 dependency, only import it here
        try:
            import pandas as pd
        except ImportError as ex:
            raise ImportError(
                "Exporting to pandas needs a working installation"
                " of pandas. Importing pandas failed: " + str(ex)
            )
        if units:
            raise NotImplementedError(
                "Units not supported when exporting to pandas data frame"
            )
        # we take advantage of the already implemented exporter
        data = DictImportExport.export_data(group, variables, units=units, level=level)
        pandas_data = pd.DataFrame(data)
        return pandas_data

    @staticmethod
    def import_data(group, data, units=True, level=0):
        if units:
            raise NotImplementedError(
                "Units not supported when importing from pandas data frame"
            )
        colnames = data.columns
        array_data = data.values
        for e, colname in enumerate(colnames):
            if group.variables[colname].read_only:
                raise TypeError(f"Variable '{colname}' is read-only.")
            state = group.state(colname, use_units=units, level=level + 1)
            state[:] = np.squeeze(array_data[:, e])