Skip to content

Commit

Permalink
Replace .getsctype() with .dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
guyer committed Jun 18, 2024
1 parent 2c7136d commit 2eed418
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion fipy/terms/unaryTerm.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _getDefaultSolver(self, var, solver, *args, **kwargs):

def _checkVar(self, var):
if ((var is not None)
and (numerix.sctype2char(var.getsctype()) not in numerix.typecodes['Float'])):
and issubclass(var.dtype.type, numerix.floating)):
import warnings
warnings.warn("""sweep() or solve() are likely to produce erroneous results when `var` does not contain floats.""",
UserWarning, stacklevel=4)
Expand Down
15 changes: 6 additions & 9 deletions fipy/tools/dimensions/physicalField.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,24 +859,21 @@ def inUnitsOf(self, *units):
unit = units[i]
return tuple(result)

def getsctype(self, default=None):
@property
def dtype(self):
"""
Returns the NumPy sctype of the underlying array.
>>> PhysicalField(1, 'm').getsctype() == numerix.array(1).dtype
>>> issubclass(PhysicalField(1, 'm').dtype.type, numerix.integer)
True
>>> PhysicalField(1., 'm').getsctype() == numerix.array(1.).dtype
>>> issubclass(PhysicalField(1., 'm').dtype.type, numerix.floating)
True
>>> PhysicalField((1, 1.), 'm').getsctype() == numerix.array((1., 1.)).dtype
>>> issubclass(PhysicalField((1, 1.), 'm').dtype.type, numerix.floating)
True
"""

if not hasattr(self, 'typecode'):
self.typecode = numerix.asarray(self.numericValue).dtype

return self.typecode
return numerix.asarray(self.numericValue).dtype

def _getUnit(self):
"""
Expand Down
3 changes: 1 addition & 2 deletions fipy/variables/cellVariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def _OperatorVariableClass(self, baseClass=None):
>>> b = c.old + 3
>>> print(b)
[2]
>>> from builtins import str
>>> print(str(b.getsctype()) == str(numerix.array(1).dtype)
>>> issubclass(b.dtype.type, numerix.integer)
True
replacing with the same thing is no problem
Expand Down
5 changes: 3 additions & 2 deletions fipy/variables/coupledCellVariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __array__(self, t=None):
[6 7]
>>> print(v2)
[8 9]
>>> v.getsctype() == numerix.array(1).dtype
>>> issubclass(v.dtype.type, numerix.integer)
True
"""
Expand All @@ -95,7 +95,8 @@ def __abs__(self):
def __iter__(self):
return iter(self.value)

def getsctype(self, default=None):
@property
def dtype(self):
return self.numericValue.dtype

def ravel(self):
Expand Down
6 changes: 3 additions & 3 deletions fipy/variables/operatorVariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ def _testBinOp(self):
>>> a = -(numerix.sin(Variable() * Variable()))
Check that `getTypeCode()` works as expected.
Check that `dtype` works as expected.
>>> a = Variable(1.) * Variable(1)
>>> print(a.getsctype() == numerix.float64)
1
>>> issubclass(a.dtype.type, numerix.floating)
True
The following test is to correct an `--inline` bug that was
being thrown by the Cahn-Hilliard example. The fix for this
Expand Down
27 changes: 12 additions & 15 deletions fipy/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def setValue(self, value, unit=None, where=None):
"""
if where is not None:
tmp = numerix.empty(numerix.getShape(where), self.getsctype())
tmp = numerix.empty(numerix.getShape(where), self.dtype)
tmp[:] = value
tmp = numerix.where(where, tmp, self.value)
else:
Expand Down Expand Up @@ -767,24 +767,21 @@ def _getShape(self):

shape = property(_getShape)

def getsctype(self, default=None):
@property
def dtype(self):
"""
Returns the Numpy `sctype` of the underlying array.
Returns the Numpy `dtype` of the underlying array.
>>> Variable(1).getsctype() == numerix.array(1).dtype
>>> issubclass(Variable(1).dtype.type, numerix.integer)
True
>>> Variable(1.).getsctype() == numerix.array(1.).dtype
>>> issubclass(Variable(1.).dtype.type, numerix.floating)
True
>>> Variable((1, 1.)).getsctype() == numerix.array((1., 1.)))
>>> issubclass(Variable((1, 1.)).dtype.type, numerix.floating)
True
"""

if not hasattr(self, 'typecode'):
self.typecode = numerix.asarray(self.numericValue).dtypes

return self.typecode
return numerix.asarray(self.numericValue).dtype

@property
def itemsize(self):
Expand Down Expand Up @@ -926,10 +923,10 @@ def _execInline(self, comment=None):
self.typecode = argDict['result'].dtype
else:
if self._value is None:
if self.getsctype() == numerix.bool_:
if self.dtype is numerix.dtype(bool):
argDict['result'] = numerix.empty(dim, numerix.int8)
else:
argDict['result'] = numerix.empty(dim, self.getsctype())
argDict['result'] = numerix.empty(dim, self.dtype)
else:
argDict['result'] = self._value

Expand All @@ -943,8 +940,8 @@ def _execInline(self, comment=None):
if resultShape == ():
argDict['result'] = numerix.reshape(argDict['result'], resultShape)

if self.getsctype() == numerix.bool_:
argDict['result'] = numerix.asarray(argDict['result'], dtype=self.getsctype())
if issubclass(self.dtype.type, numerix.bool):
argDict['result'] = numerix.asarray(argDict['result'], dtype=self.dtype)

return argDict['result']

Expand Down

0 comments on commit 2eed418

Please sign in to comment.