44from .averager import __check_weightoptions
55
66
7- class StatisticsError (Exception ):
7+ class StatisticsError (Exception ):
88 def __init__ (self , args = None ):
99 """Create an exception"""
1010 self .args = args
1111
1212 def __str__ (self ):
1313 """Calculate the string representation"""
1414 return str (self .args )
15+
1516 __repr__ = __str__
1617
1718
@@ -32,10 +33,10 @@ def __makeweights(x, w, axes):
3233 if not numpy .ma .isarray (w ):
3334 # Ok Krishna returned a list of 1D arrays.... Let's put it together
3435 axs = x .getAxisList ()
35- axes = cdms2 .order2index (axs , axes )[:len (cdms2 .orderparse (axes ))]
36+ axes = cdms2 .order2index (axs , axes )[: len (cdms2 .orderparse (axes ))]
3637 endax = []
3738 for i in range (len (axes )):
38- if w [i ] == ' unweighted' :
39+ if w [i ] == " unweighted" :
3940 w [i ] = numpy .ma .ones (len (axs [axes [i ]]), dtype = x .dtype .char )
4041 if i == 0 :
4142 wo = w [i ]
@@ -45,8 +46,8 @@ def __makeweights(x, w, axes):
4546 endax .append (axs [axes [i ]])
4647 w = cdms2 .MV2 .array (wo )
4748 w .setAxisList (endax )
48- # else:
49- # w.setAxisList(x.getAxisList())
49+ # else:
50+ # w.setAxisList(x.getAxisList())
5051 return w
5152
5253
@@ -64,29 +65,31 @@ def __checker(x, y, w, axes, smally=0):
6465 x = numpy .ma .array (x , copy = 0 )
6566 if not numpy .ma .isarray (y ) and y is not None :
6667 y = numpy .ma .array (y , copy = 0 )
67- if not numpy .ma .isarray (
68- w ) and w is not None and not isinstance (w , type ('' )):
69- if not isinstance (w [0 ], type ('' )):
68+ if not numpy .ma .isarray (w ) and w is not None and not isinstance (w , type ("" )):
69+ if not isinstance (w [0 ], type ("" )):
7070 w = numpy .ma .array (w , copy = 0 )
7171 else :
7272 if not xismv :
7373 raise StatisticsError (
74- 'Error if weights are a list then x must be an MV2 !!!' )
74+ "Error if weights are a list then x must be an MV2 !!!"
75+ )
7576 w = __makeweights (x , w , axes )
7677 wismv = 1
7778 elif w is not None :
7879 if not xismv :
7980 raise StatisticsError (
80- 'Error if weights are a list then x must be an MV2 !!!' )
81+ "Error if weights are a list then x must be an MV2 !!!"
82+ )
8183 w = __makeweights (x , w , axes )
8284 wismv = 1
8385
8486 if xismv * yismv * wismv != 1 :
8587 # We didn't pass all MV2s shapes have to match (unless None)
8688 if smally == 0 :
8789 if x .shape != numpy .ma .shape (y ) and y is not None :
88- raise StatisticsError ('Error x and y shape do not match !' +
89- str (x .shape ) + ',' + str (numpy .ma .shape (y )))
90+ raise StatisticsError (
91+ "Error x and y shape do not match !" + str (x .shape ) + "," + str (numpy .ma .shape (y ))
92+ )
9093 else :
9194 shy = list (y .shape )
9295 shy2 = y .shape
@@ -96,7 +99,9 @@ def __checker(x, y, w, axes, smally=0):
9699 for i in axes :
97100 myaxes .append (eval (i ))
98101 elif isinstance (axes , int ):
99- myaxes = [axes , ]
102+ myaxes = [
103+ axes ,
104+ ]
100105 else :
101106 myaxes = list (axes )
102107 for anaxis in myaxes [::- 1 ]:
@@ -109,25 +114,27 @@ def __checker(x, y, w, axes, smally=0):
109114 sh [i ] = myaxes [i ]
110115 y = numpy .ma .transpose (y , sh )
111116 if x .shape != numpy .ma .shape (y ) and y is not None :
112- raise StatisticsError ('Error x and y shape do not match (y shouldbe 1D less than x) !' +
113- str (x .shape ) + ',' + str (shy2 ) + ' Remember y must be 1D less than x' )
117+ err_msg = "Error x and y shape do not match (y shouldbe 1D less than x) !"
118+ raise StatisticsError (
119+ err_msg + str (x .shape ) + "," + str (shy2 ) + " Remember y must be 1D less than x"
120+ )
114121 if x .shape != numpy .ma .shape (w ) and w is not None :
115- raise StatisticsError ('Error x and weights shape do not match !' +
116- str (x .shape ) + ',' + str (numpy .ma .shape (w )) +
117- ' ATTENTION if you are trynig to pass a list of 1D arrays' +
118- 'for each dim, then x must be an MV2 !!!' )
122+ msg1 = "Error x and weights shape do not match !"
123+ msg2 = " ATTENTION if you are trying to pass a list of 1D arrays for each dim, then x must be an MV2 !!!"
124+ raise StatisticsError (msg1 + str (x .shape ) + "," + str (numpy .ma .shape (w )) + msg2 )
119125 if not isinstance (axes , type ([])):
120126 axes = cdms2 .orderparse (str (axes ))
121127 for i in axes :
122128 if len (x .shape ) < i :
123- raise StatisticsError ( ' Error you have ' + str (len (x .shape )) +
124- ' dimensions and try to work on dim:' + str ( i ) )
129+ err_msg = " Error you have " + str (len (x .shape )) + " dimensions and try to work on dim:" + str ( i )
130+ raise StatisticsError ( err_msg )
125131 else :
126132 if y is not None :
127133 x , y = grower (x , y )
128134 if x .shape != y .shape :
129135 raise StatisticsError (
130- 'Error x and y have different shapes' + str (x .shape ) + ', ' + str (y .shape ))
136+ "Error x and y have different shapes" + str (x .shape ) + ", " + str (y .shape )
137+ )
131138 ax = x .getAxisList ()
132139 xorder = x .getOrder (ids = 1 )
133140 # Now grows w
@@ -136,11 +143,13 @@ def __checker(x, y, w, axes, smally=0):
136143 for o in worder :
137144 if o not in xorder :
138145 raise StatisticsError (
139- 'Error weights have a dimension that is neither in x or y:' + o )
146+ "Error weights have a dimension that is neither in x or y:" + o
147+ )
140148 x , w = grower (x , w )
141149 if x .shape != w .shape :
142150 raise StatisticsError (
143- 'Error x and weights have different shapes' + str (x .shape ) + ', ' + str (w .shape ))
151+ "Error x and weights have different shapes" + str (x .shape ) + ", " + str (w .shape )
152+ )
144153 # Last thing convert the axes input to numbers
145154 if isinstance (axes , type (1 )):
146155 axes = str (axes )
@@ -149,24 +158,21 @@ def __checker(x, y, w, axes, smally=0):
149158 naxes = len (axesparse )
150159 for i in range (naxes ):
151160 o = axesparse [i ]
152- if isinstance (o , type ('' )):
161+ if isinstance (o , type ("" )):
153162 for j in range (len (xorder )):
154163 if xorder [j ] == o :
155164 axesparse [i ] = j
156165 # Well it must be a name for x y t....
157- if isinstance (axesparse [i ], type ('' )):
166+ if isinstance (axesparse [i ], type ("" )):
158167 for j in range (len (x .shape )):
159168 if o [1 :- 1 ] == x .getAxis (j ).id :
160169 axesparse [i ] = j
161170 # Everything failed the axis id must be not existing in the
162171 # slab...
163- if isinstance (axesparse [i ], type ('' )):
172+ if isinstance (axesparse [i ], type ("" )):
164173 raise StatisticsError (
165- 'Error axis id :' +
166- o +
167- ' not found in first slab: ' +
168- x .getOrder (
169- ids = 1 ))
174+ "Error axis id :" + o + " not found in first slab: " + x .getOrder (ids = 1 )
175+ )
170176 axes = axesparse
171177 # Now we have array those shape match, and a nice list of axes let's keep
172178 # going
@@ -176,8 +182,12 @@ def __checker(x, y, w, axes, smally=0):
176182 xorder = list (range (len (x .shape )))
177183 forder = []
178184 for i in range (naxes ):
179- forder .append (axes [i ])
180- n0 = n0 * xsh [axes [i ]]
185+ a = axes [i ]
186+ forder .append (a )
187+ try :
188+ n0 = n0 * xsh [a ]
189+ except IndexError :
190+ raise Exception ("Axis {} is out of bounds for dimension {}" .format (a , len (xsh )))
181191 fsh = [n0 ]
182192 ax2 = []
183193 for i in range (len (x .shape )):
0 commit comments