File: importexport.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 (93 lines) | stat: -rw-r--r-- 2,788 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Module defining the `ImportExport` class that enables getting state variable
data in and out of groups in various formats (see `Group.get_states` and
`Group.set_states`).
"""

import abc
from abc import abstractmethod


class ImportExport(metaclass=abc.ABCMeta):
    """
    Class for registering new import/export methods (via static methods). Also
    the base class that should be extended for such methods
    (`ImportExport.export_data`, `ImportExport.import_data`, and
    `ImportExport.name` have to be overwritten).

    See Also
    --------
    VariableOwner.get_states, VariableOwner.set_states

    """

    #: A dictionary mapping import/export methods names to `ImportExport` objects
    methods = dict()

    @staticmethod
    def register(importerexporter):
        """
        Register a import/export method. Registered methods can be referred to
        via their name.

        Parameters
        ----------
        importerexporter : `ImportExport`
            The importerexporter object, e.g. an `DictImportExport`.
        """
        if not isinstance(importerexporter, ImportExport):
            t = str(type(importerexporter))
            error_msg = (
                f"Given importerexporter of type {t} does not seem to "
                "be a valid importerexporter."
            )
            raise ValueError(error_msg)
        name = importerexporter.name
        if name in ImportExport.methods:
            raise ValueError(
                f"An import/export methods with the name '{name}'"
                "has already been registered"
            )
        ImportExport.methods[name] = importerexporter

    @staticmethod
    @abstractmethod
    def export_data(group, variables):
        """
        Asbtract static export data method with two obligatory parameters.
        It should return a copy of the current state variable values. The
        returned arrays are copies of the actual arrays that store the state
        variable values, therefore changing the values in the returned
        dictionary will not affect the state variables.

        Parameters
        ----------
        group : `Group`
            Group object.
        variables : list of str
            The names of the variables to extract.
        """
        raise NotImplementedError()

    @staticmethod
    @abstractmethod
    def import_data(group, data):
        """
        Import and set state variables.

        Parameters
        ----------
        group : `Group`
            Group object.
        data : dict_like
            Data to import with variable names.
        """
        raise NotImplementedError()

    @property
    @abc.abstractmethod
    def name(self):
        """
        Abstract property giving a method name.
        """
        pass