9
9
class Vector (object ):
10
10
""" Vector data container. Implements min, max and default values. """
11
11
12
- def __init__ (self , names , defaults = None , mins = None , maxs = None , \
13
- check_bounds = True , check_hitbounds = False , accept_nan = False ):
12
+ def __init__ (self , names , defaults = None , mins = None ,
13
+ maxs = None , check_bounds = True , check_hitbounds = False ,
14
+ accept_nan = False ):
14
15
15
16
# Set parameter names
16
17
if names is None :
@@ -26,8 +27,8 @@ def __init__(self, names, defaults=None, mins=None, maxs=None, \
26
27
self ._check_bounds = bool (check_bounds )
27
28
self ._check_hitbounds = bool (check_hitbounds )
28
29
if self ._check_hitbounds and not self ._check_bounds :
29
- raise ValueError ("check_hitbounds cannot be True while " + \
30
- "check_bounds is False" )
30
+ raise ValueError ("check_hitbounds cannot be True while " +
31
+ "check_bounds is False" )
31
32
self ._hitbounds = False
32
33
33
34
# Set number of parameters
@@ -36,40 +37,40 @@ def __init__(self, names, defaults=None, mins=None, maxs=None, \
36
37
self ._nval = nval
37
38
38
39
# Define names indexes
39
- self ._names_index = {nm :i for nm , i \
40
- in zip (self ._names , np .arange (nval ))}
40
+ self ._names_index = {nm : i for nm , i
41
+ in zip (self ._names , np .arange (nval ))}
41
42
42
43
# Set mins and maxs
43
44
self ._mins = - np .inf * np .ones (nval )
44
45
self ._maxs = np .inf * np .ones (nval )
45
46
46
- if not mins is None :
47
+ if mins is not None :
47
48
# Just convert mins to proper array
48
49
self ._mins , _ = self .__checkvalues__ (mins , False )
49
50
50
- if not maxs is None :
51
+ if maxs is not None :
51
52
# Convert maxs to proper array and checks it is greater than mins
52
53
maxs2 , hit = self .__checkvalues__ (maxs , True )
53
54
54
55
if hit :
55
- raise ValueError (( "Expected maxs within [{0}, {1}], " + \
56
- " got {2}" ). format ( self ._mins , self ._maxs , maxs ) )
56
+ raise ValueError ("Expected maxs within " +
57
+ f"[ { self ._mins } , { self ._maxs } ], got { maxs } ." )
57
58
self ._maxs = maxs2
58
59
59
60
# Set defaults values
60
- if not defaults is None :
61
+ if defaults is not None :
61
62
self ._defaults , hit = self .__checkvalues__ (defaults , True )
62
63
63
64
if hit :
64
- raise ValueError (("Expected defaults within [{0}, {1}]," + \
65
- " got {2}" ).format (self ._mins , self ._maxs , defaults ))
65
+ raise ValueError ("Expected defaults within " +
66
+ f"[{ self ._mins } , { self ._maxs } ]," +
67
+ f"got { defaults } ." )
66
68
else :
67
69
self ._defaults = np .clip (np .zeros (nval ), self ._mins , self ._maxs )
68
70
69
71
# Set values to defaults
70
72
self ._values = self ._defaults .copy ()
71
73
72
-
73
74
@classmethod
74
75
def from_dict (cls , dct ):
75
76
""" Create vector from dictionary """
@@ -87,16 +88,15 @@ def from_dict(cls, dct):
87
88
maxs .append (dct ["data" ][i ]["max" ])
88
89
values .append (dct ["data" ][i ]["value" ])
89
90
90
- vect = Vector (names , defaults , mins , maxs , \
91
- check_bounds = bool (dct ["check_bounds" ]), \
92
- check_hitbounds = bool (dct ["check_hitbounds" ]),
93
- accept_nan = bool (dct ["accept_nan" ]))
91
+ vect = Vector (names , defaults , mins , maxs ,
92
+ check_bounds = bool (dct ["check_bounds" ]),
93
+ check_hitbounds = bool (dct ["check_hitbounds" ]),
94
+ accept_nan = bool (dct ["accept_nan" ]))
94
95
vect ._hitbounds = bool (dct ["hitbounds" ])
95
96
vect .values = values
96
97
97
98
return vect
98
99
99
-
100
100
def __getattribute__ (self , name ):
101
101
# Except _names and _hitbounds to avoid infinite recursion
102
102
if name in ["_names" , "_check_hitbounds" , "_hitbounds" ]:
@@ -108,7 +108,6 @@ def __getattribute__(self, name):
108
108
109
109
return super (Vector , self ).__getattribute__ (name )
110
110
111
-
112
111
def __setattr__ (self , name , value ):
113
112
# Except _names and _hitbounds to avoid infinite recursion
114
113
if name in ["_names" , "_check_hitbounds" , "_hitbounds" ]:
@@ -128,27 +127,24 @@ def __setattr__(self, name, value):
128
127
or (value > self ._maxs [idx ])
129
128
130
129
# Store clipped value
131
- self .values [idx ] = min (max (value , \
132
- self . mins [ idx ]), self .maxs [idx ])
130
+ self .values [idx ] = min (max (value , self . mins [ idx ]),
131
+ self .maxs [idx ])
133
132
134
133
else :
135
134
super (Vector , self ).__setattr__ (name , value )
136
135
137
-
138
136
def __str__ (self ):
139
- return "vector [" + ", " .join (["{0}:{1:3.3e}" .format (key , self [key ]) \
140
- for key in self .names ]) + "]"
141
-
137
+ return "vector [" + ", " .join (["{0}:{1:3.3e}" .format (key , self [key ])
138
+ for key in self .names ]) + "]"
142
139
143
140
def __checkvalues__ (self , val , check_hitbounds ):
144
141
""" Check vector is of proper length and contains no nan"""
145
142
146
143
val = np .atleast_1d (val ).flatten ().astype (np .float64 )
147
144
148
145
if val .shape [0 ] != self .nval :
149
- raise ValueError ("Expected vector of length " + \
150
- "{0}, got {1}" .format (\
151
- self .nval , val .shape [0 ]))
146
+ raise ValueError ("Expected vector of length "
147
+ + f"{ self .nval } , got { val .shape [0 ]} ." )
152
148
153
149
if np .any (np .isnan (val )) and not self ._accept_nan :
154
150
raise ValueError ("Cannot process values with NaN" )
@@ -163,126 +159,111 @@ def __checkvalues__(self, val, check_hitbounds):
163
159
164
160
return val , hit
165
161
166
-
167
162
def __setitem__ (self , key , value ):
168
- if not key in self ._names_index :
169
- raise ValueError ("Expected key in {0}, got {1}" .format (\
170
- self ._names , key ))
163
+ if key not in self ._names_index :
164
+ raise ValueError (f"Expected key in { self ._names } , got { key } ." )
171
165
172
166
setattr (self , key , value )
173
167
174
-
175
168
def __getitem__ (self , key ):
176
- if not key in self ._names_index :
177
- raise ValueError ("Expected key in {0}, got {1}" .format (\
178
- self ._names , key ))
169
+ if key not in self ._names_index :
170
+ raise ValueError (f"Expected key in { self ._names } , got { key } ." )
179
171
180
172
return getattr (self , key )
181
173
182
-
183
174
@property
184
175
def nval (self ):
185
176
""" Number of values in vector """
186
177
return self ._nval
187
178
188
-
189
179
@property
190
180
def check_bounds (self ):
191
181
""" Are the bounds checked or not """
192
182
return self ._check_bounds
193
183
194
-
195
184
@property
196
185
def check_hitbounds (self ):
197
186
""" Is the hit to bounds checked or not """
198
187
return self ._check_hitbounds
199
188
200
-
201
189
@property
202
190
def hitbounds (self ):
203
191
""" Has the boundaries been reached when setting data ? """
204
192
return self ._hitbounds
205
193
206
-
207
194
@property
208
195
def accept_nan (self ):
209
196
""" Accept nan values """
210
197
return self ._accept_nan
211
198
212
-
213
199
@property
214
200
def names (self ):
215
201
""" Names of vector elements """
216
202
return self ._names
217
203
218
-
219
204
@property
220
205
def mins (self ):
221
206
""" Minimum values of vector elements """
222
207
return self ._mins
223
208
224
-
225
209
@property
226
210
def maxs (self ):
227
211
""" Maximum values of vector elements """
228
212
return self ._maxs
229
213
230
-
231
214
@property
232
215
def defaults (self ):
233
216
""" Default values of vector elements """
234
217
return self ._defaults
235
218
236
-
237
219
@property
238
220
def values (self ):
239
221
""" Get data """
240
222
return self ._values
241
223
242
-
243
224
@values .setter
244
225
def values (self , val ):
245
226
""" Set data """
246
- self ._values , self ._hitbounds = self .__checkvalues__ (val , \
247
- self ._check_hitbounds )
248
-
227
+ ck = self ._check_hitbounds
228
+ self ._values , self ._hitbounds = self .__checkvalues__ (val , ck )
249
229
250
230
def reset (self ):
251
231
""" Reset vector values to defaults """
252
232
self .values = self .defaults
253
233
254
-
255
234
def clone (self ):
256
235
""" Clone the vector """
257
- clone = Vector (self .names , self .defaults , self .mins , \
258
- self .maxs , self .check_hitbounds )
236
+ clone = Vector (self .names , self .defaults , self .mins ,
237
+ self .maxs , self .check_hitbounds )
259
238
260
239
clone .values = self .values .copy ()
261
240
262
241
return clone
263
242
264
-
265
243
def to_dict (self ):
266
244
""" Write vector data to json format """
267
245
268
- dct = {"nval" : self .nval , "hitbounds" :self .hitbounds , \
269
- "check_bounds" :self .check_bounds , \
270
- "check_hitbounds" :self .check_hitbounds , \
271
- "accept_nan" :self .accept_nan , \
272
- "data" :[]}
246
+ dct = {
247
+ "nval" : self .nval ,
248
+ "hitbounds" : self .hitbounds ,
249
+ "check_bounds" : self .check_bounds ,
250
+ "check_hitbounds" : self .check_hitbounds ,
251
+ "accept_nan" : self .accept_nan ,
252
+ "data" : []
253
+ }
273
254
274
255
for i in range (self .nval ):
275
- elem = {"name" :self .names [i ], \
276
- "value" :self .values [i ], \
277
- "min" :self .mins [i ], \
278
- "max" :self .maxs [i ], \
279
- "default" :self .defaults [i ]}
256
+ elem = {
257
+ "name" : self .names [i ],
258
+ "value" : self .values [i ],
259
+ "min" : self .mins [i ],
260
+ "max" : self .maxs [i ],
261
+ "default" : self .defaults [i ]
262
+ }
280
263
dct ["data" ].append (elem )
281
264
282
265
return dct
283
266
284
-
285
267
def to_series (self ):
286
268
""" Write vector data to json format """
287
269
return pd .Series (self .values , index = self .names )
288
-
0 commit comments