Debian Codegraph @

nixio/feature.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2016, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted under the terms of the BSD License. See
8 # LICENSE file in the root of the Project.
9 from .data_array import DataArray
10 from .data_frame import DataFrame
11 from .link_type import LinkType
12 from .exceptions import UnsupportedLinkType
13 from .util import util
14
15
16 class Feature:
17
18 def __init__(self, nixfile, nixparent, h5group):
19 util.check_entity_id(h5group.get_attr("entity_id"))
20 self._h5group = h5group
21 self._parent = nixparent
22 self._file = nixfile
23
24 @classmethod
25 def create_new(cls, nixfile, nixparent, h5parent, data, link_type):
26 if isinstance(data, DataFrame) and link_type == LinkType.Tagged:
27 raise UnsupportedLinkType(str(type(data)), LinkType.Tagged)
28 id_ = util.create_id()
29 h5group = h5parent.open_group(id_)
30 h5group.set_attr("entity_id", id_)
31 newfeature = cls(nixfile, nixparent, h5group)
32 newfeature.link_type = link_type
33 newfeature.data = data
34 newfeature._h5group.set_attr("created_at",
35 util.time_to_str(util.now_int()))
36 newfeature._h5group.set_attr("updated_at",
37 util.time_to_str(util.now_int()))
38
39 return newfeature
40
41 @property
42 def id(self):
43 return self._h5group.get_attr("entity_id")
44
45 @property
46 def file(self):
47 return self._file
48
49 @property
50 def link_type(self):
51 return LinkType(self._h5group.get_attr("link_type"))
52
53 @link_type.setter
54 def link_type(self, link_type):
55 if isinstance(link_type, str):
56 link_type = link_type.lower()
57 link_type = LinkType(link_type)
58 self._h5group.set_attr("link_type", link_type.value)
59 if self.file.auto_update_timestamps:
60 time = util.now_int()
61 self._h5group.set_attr("updated_at", util.time_to_str(time))
62
63 @property
64 def data(self):
65 if "data" not in self._h5group:
66 raise RuntimeError("Feature.data: Data object not found!")
67 objtype = self._h5group.get_attr("target_type")
68 if not objtype:
69 # Missing target_type property: Default to DataArray (older files)
70 objtype = "DataArray"
71 if objtype == "DataArray":
72 return DataArray(self.file, self._parent._parent,
73 self._h5group.open_group("data"))
74 if objtype == "DataFrame":
75 return DataFrame(self.file, self._parent._parent,
76 self._h5group.open_group("data"))
77 raise RuntimeError("Feature.data: Unknown target type defind: {}".format(objtype))
78
79 @data.setter
80 def data(self, dataobj):
81 if dataobj is None:
82 raise TypeError("Feature.data cannot be None.")
83 parblock = self._parent._parent
84 if isinstance(dataobj, DataArray):
85 if dataobj not in parblock.data_arrays:
86 raise RuntimeError("Feature.data: DataArray not found in Block!")
87 objtype = "DataArray"
88 elif isinstance(dataobj, DataFrame):
89 if dataobj not in parblock.data_frames:
90 raise RuntimeError("Feature.data: DataFrame not found in Block!")
91 if self.link_type == LinkType.Tagged:
92 raise UnsupportedLinkType(str(type(dataobj)), LinkType.Tagged)
93 objtype = "DataFrame"
94 else:
95 raise TypeError("Unknown data object type: {}".format(type(dataobj)))
96 self._h5group.set_attr("target_type", objtype)
97
98 if "data" in self._h5group:
99 del self._h5group["data"]
100 self._h5group.create_link(dataobj, "data")
101 if self.file.auto_update_timestamps:
102 time = util.now_int()
103 self._h5group.set_attr("updated_at", util.time_to_str(time))
104
105 @property
106 def created_at(self):
107 """
108 The creation time of the entity. This is a read-only property.
109 Use `force_created_at` in order to change the creation time.
110
111 :rtype: int
112 """
113 return util.str_to_time(self._h5group.get_attr("created_at"))
114
115 @property
116 def updated_at(self):
117 """
118 The time of the last update of the entity. This is a read-only
119 property. Use `force_updated_at` in order to change the update
120 time.
121
122 :rtype: int
123 """
124 return util.str_to_time(self._h5group.get_attr("updated_at"))
125
126 def __eq__(self, other):
127 """
128 Two Entities are considered equal when they have the same id.
129 """
130 if hasattr(other, "id"):
131 return self.id == other.id
132 return False
133
134 def __ne__(self, other):
135 return not self.__eq__(other)
136
137 def __hash__(self):
138 """
139 overwriting method __eq__ blocks inheritance of __hash__ in Python 3
140 hash has to be either explicitly inherited from parent class,
141 implemented or escaped
142 """
143 return hash(self.id)
144
145 def __str__(self):
146 return "Feature: {{data = {}, link_type = {} }}".\
147 format(self.data.name, self.link_type)
148
149 def __repr__(self):
150 return self.__str__()