-
Notifications
You must be signed in to change notification settings - Fork 0
/
dim.py
131 lines (101 loc) · 3.29 KB
/
dim.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: ISO-8859-1 -*-
u"""
dim
========
.. autoclass:: DimBase
"""
import numpy as np
class ValueArrayShapeInfoMismatchError(Exception):
pass
def flatten(sequence):
for item in sequence:
if isinstance(item, (list, tuple)):
for subitem in flatten(item):
yield subitem
else:
yield item
class DimBase(object):
sortprio = 0
def __init__(self, Name, data=None, unit=None, name=None, outputformat=None):
if isinstance(Name, DimBase):
dim_data = Name.data
dim_name = Name.name
dim_unit = Name.unit
dim_outputformat = Name.outputformat
else:
dim_data = data
dim_name = Name
dim_unit = unit
if outputformat is None:
dim_outputformat = "%.16e"
else:
dim_outputformat = outputformat
if data is not None:
dim_data = data
if unit is not None:
dim_unit = unit
if name is not None:
dim_name = name
if outputformat is not None:
dim_outputformat = outputformat
if isinstance(dim_data, int):
dim_data = range(dim_data)
if hasattr(dim_data, "tolist"):
dim_data = [dim_data.tolist()]
self._data = tuple(flatten(dim_data))
self._name = dim_name
self._unit = dim_unit
self._outputformat = dim_outputformat
@property
def data(self):
return np.asarray(self._data)
@property
def name(self):
return self._name
@property
def unit(self):
return self._unit
@property
def outputformat(self):
return self._outputformat
def fullsize(self):
return self.data.shape[0]
def __ValueArray__(self):
return (self.data, (self,))
def __cmp__(self, other):
a = (self.sortprio, self.name, self.__class__, self._data, self._unit, self._outputformat)
try:
b = (other.sortprio, other.name, other.__class__, other._data, other._unit, self._outputformat)
except AttributeError:
a = self.name
b = other
return cmp(a, b)
def __repr__(self):
return "%s(%r, shape=%r)"%(self.__class__.__name__,
self.name,
self.data.shape)
def __hash__(self):
return hash((self.sortprio, self.name, self.__class__, self._data, self._unit, self._outputformat))
def __getitem__(self, index):
if isinstance(index, slice) and (index == slice(None, None, None)):
return self
elif isinstance(index, slice):
a = self.__class__(self.name, self.data[index], unit=self.unit, outputformat=self.outputformat)
return a
elif isinstance(index, np.ndarray):
a = self.__class__(self.name, self.data[index], unit=self.unit, outputformat=self.outputformat)
return a
else:
raise IndexError("Must index with slice")
def copy(self):
return self
class DimSweep(DimBase):
pass
class DimRep(DimBase):
sortprio = 1
class _DimMatrix(DimBase):
sortprio = 1000
class DimMatrix_i(_DimMatrix):
sortprio = 1001
class DimMatrix_j(_DimMatrix):
sortprio = 1001