1313from .registry import unit_registry
1414from .decorators import with_doc
1515
16+ PREFERRED = [] # List of preferred quanitities for each symbol,
17+ # e.g. PREFERRED = [pq.mV, pq.pA, pq.UnitQuantity('femtocoulomb', 1e-15*pq.C, 'fC')]
18+ # Intended to be overwritten in down-stream packages
19+
1620if sys .version .startswith ('3' ):
1721 unicode = str
1822
@@ -195,10 +199,15 @@ def units(self, units):
195199 mag *= cf
196200 self ._dimensionality = to_u .dimensionality
197201
198- def rescale (self , units ):
202+ def rescale (self , units = None ):
199203 """
200204 Return a copy of the quantity converted to the specified units
201205 """
206+ if units is None :
207+ try :
208+ return self .rescale_preferred ()
209+ except Exception as e :
210+ raise Exception ('No argument passed to `.rescale` and %s' % e )
202211 to_dims = validate_dimensionality (units )
203212 if self .dimensionality == to_dims :
204213 return self .astype (self .dtype )
@@ -212,7 +221,15 @@ def rescale(self, units):
212221 % (from_u ._dimensionality , to_u ._dimensionality )
213222 )
214223 return Quantity (cf * self .magnitude , to_u )
215-
224+
225+ def rescale_preferred (self ):
226+ units_str = str (self .simplified .dimensionality )
227+ for preferred in PREFERRED :
228+ if units_str == str (preferred .simplified .dimensionality ):
229+ return self .rescale (preferred )
230+ raise Exception (("Preferred units for '%s' (or equivalent) not specified in "
231+ "quantites.quantity.PREFERRED." % self .dimensionality ))
232+
216233 @with_doc (np .ndarray .astype )
217234 def astype (self , dtype = None , ** kwargs ):
218235 '''Scalars are returned as scalar Quantity arrays.'''
0 commit comments