Skip to content

Commit

Permalink
Deprecate e.g. Vector.new and search-and-replace all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Apr 11, 2022
1 parent 2107279 commit 0156352
Show file tree
Hide file tree
Showing 27 changed files with 212 additions and 194 deletions.
2 changes: 1 addition & 1 deletion grblas/_ss/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@ def _import_coo(
dtype = matrix.dtype
values, dtype = values_to_numpy_buffer(values, dtype)
if method == "import":
matrix = gb.Matrix.new(dtype, nrows=nrows, ncols=ncols, name=name)
matrix = gb.Matrix(dtype, nrows=nrows, ncols=ncols, name=name)
if is_iso:
matrix.ss.build_scalar(rows, cols, values[0])
else:
Expand Down
4 changes: 2 additions & 2 deletions grblas/_ss/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,9 +1334,9 @@ def compactify(self, how="first", size=None, *, reverse=False, asindex=False, na
)
if size is None and self._parent._nvals == 0 or size == 0:
if asindex:
return gb.Vector.new(UINT64, size=0, name=name)
return gb.Vector(UINT64, size=0, name=name)
else:
return gb.Vector.new(self._parent.dtype, size=0, name=name)
return gb.Vector(self._parent.dtype, size=0, name=name)
do_sort = how in {"first", "last"}
info = self._parent.ss.export("sparse", sort=do_sort)
if size is None:
Expand Down
6 changes: 3 additions & 3 deletions grblas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def _new_scalar(self, dtype, *, is_cscalar=False, name=None):
"""
from .scalar import Scalar

return Scalar.new(dtype, is_cscalar=is_cscalar, name=name)
return Scalar(dtype, is_cscalar=is_cscalar, name=name)

def _new_vector(self, dtype, size=0, *, name=None):
"""Create a new empty Vector.
Expand All @@ -610,7 +610,7 @@ def _new_vector(self, dtype, size=0, *, name=None):
"""
from .vector import Vector

return Vector.new(dtype, size, name=name)
return Vector(dtype, size, name=name)

def _new_matrix(self, dtype, nrows=0, ncols=0, *, name=None):
"""Create a new empty Matrix.
Expand All @@ -619,4 +619,4 @@ def _new_matrix(self, dtype, nrows=0, ncols=0, *, name=None):
"""
from .matrix import Matrix

return Matrix.new(dtype, nrows, ncols, name=name)
return Matrix(dtype, nrows, ncols, name=name)
2 changes: 1 addition & 1 deletion grblas/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def __delitem__(self, keys):
# Delete selection by assigning an empty scalar
from .scalar import Scalar

scalar = Scalar.new(
scalar = Scalar(
self.parent.dtype, is_cscalar=False, name="s_empty" # pragma: is_grbscalar
)
self._setitem(resolved_indexes, scalar, is_submask=False)
Expand Down
14 changes: 7 additions & 7 deletions grblas/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _update_matrix_dataframe(df, matrix, rows, row_offset, columns, column_offse
if mask is None:
submatrix = matrix
else:
submatrix = Matrix.new("UINT8", matrix._nrows, matrix._ncols, name="")
submatrix = Matrix("UINT8", matrix._nrows, matrix._ncols, name="")
if mask.structure:
submatrix(matrix.S)[...] = 0 if mask.complement else 1
else:
Expand All @@ -108,7 +108,7 @@ def _update_matrix_dataframe(df, matrix, rows, row_offset, columns, column_offse
columns = slice(None)
if type(matrix) is TransposedMatrix:
parent = matrix._matrix
submatrix = Matrix.new(parent.dtype, parent._nrows, parent._ncols, name="")
submatrix = Matrix(parent.dtype, parent._nrows, parent._ncols, name="")
if parent._nvals > 0:
# Get val to support iso-valued matrices
val = parent.reduce_scalar(monoid.any, allow_empty=False).new(name="")
Expand All @@ -119,14 +119,14 @@ def _update_matrix_dataframe(df, matrix, rows, row_offset, columns, column_offse
submatrix = submatrix.T
else:
if mask is None:
submatrix = Matrix.new(matrix.dtype, matrix._nrows, matrix._ncols, name="")
submatrix = Matrix(matrix.dtype, matrix._nrows, matrix._ncols, name="")
# Get val to support iso-valued matrices
if matrix._nvals > 0:
val = matrix.reduce_scalar(monoid.any).new(name="")
submatrix(matrix.S)[rows, columns] = val
submatrix(submatrix.S)[...] = matrix
else:
submatrix = Matrix.new("UINT8", matrix._nrows, matrix._ncols, name="")
submatrix = Matrix("UINT8", matrix._nrows, matrix._ncols, name="")
if mask.structure:
submatrix(matrix.S)[rows, columns] = 0 if mask.complement else 1
else:
Expand All @@ -149,22 +149,22 @@ def _update_vector_dataframe(df, vector, columns, column_offset, *, mask=None):
if mask is None:
subvector = vector
else:
subvector = Vector.new("UINT8", vector._size, name="")
subvector = Vector("UINT8", vector._size, name="")
if mask.structure:
subvector(vector.S)[...] = 0 if mask.complement else 1
else:
subvector(vector.S)[...] = 1 if mask.complement else 0
subvector(vector.V)[...] = 0 if mask.complement else 1
else:
if mask is None:
subvector = Vector.new(vector.dtype, vector._size, name="")
subvector = Vector(vector.dtype, vector._size, name="")
# Get val to support iso-valued vectors
if vector._nvals > 0:
val = vector.reduce(monoid.any).new(name="")
subvector(vector.S)[columns] = val
subvector(subvector.S)[...] = vector
else:
subvector = Vector.new("UINT8", vector._size, name="")
subvector = Vector("UINT8", vector._size, name="")
if mask.structure:
subvector(vector.S)[columns] = 0 if mask.complement else 1
else:
Expand Down
2 changes: 1 addition & 1 deletion grblas/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def from_networkx(G, nodelist=None, dtype=None, weight="weight", name=None):
nrows, ncols = A.shape
data = A.data
if data.size == 0:
return Matrix.new(A.dtype, nrows=nrows, ncols=ncols, name=name)
return Matrix(A.dtype, nrows=nrows, ncols=ncols, name=name)
if backend == "suitesparse":
is_iso = (data[[0]] == data).all()
if is_iso:
Expand Down
29 changes: 17 additions & 12 deletions grblas/matrix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import warnings

import numpy as np

Expand Down Expand Up @@ -173,7 +174,7 @@ def isequal(self, other, *, check_dtype=False):
else:
op = get_typed_op(binary.eq, self.dtype, other.dtype, kind="binary")

matches = Matrix.new(bool, self._nrows, self._ncols, name="M_isequal")
matches = Matrix(bool, self._nrows, self._ncols, name="M_isequal")
matches << self.ewise_mult(other, op)
# ewise_mult performs intersection, so nvals will indicate mismatched empty values
if matches._nvals != self._nvals:
Expand Down Expand Up @@ -335,7 +336,7 @@ def dup(self, dtype=None, *, mask=None, name=None):
if dtype is not None or mask is not None:
if dtype is None:
dtype = self.dtype
rv = Matrix.new(dtype, nrows=self._nrows, ncols=self._ncols, name=name)
rv = Matrix(dtype, nrows=self._nrows, ncols=self._ncols, name=name)
rv(mask=mask)[...] = self
else:
new_mat = ffi_new("GrB_Matrix*")
Expand Down Expand Up @@ -365,6 +366,10 @@ def new(cls, dtype, nrows=0, ncols=0, *, name=None):
GrB_Matrix_new
Create a new empty Matrix from the given type, number of rows, and number of columns
"""
warnings.warn(
"`Matrix.new(...)` is deprecated; please use `Matrix(...)` instead.",
DeprecationWarning,
)
return Matrix(dtype, nrows, ncols, name=name)

@classmethod
Expand Down Expand Up @@ -402,7 +407,7 @@ def from_values(
# Look for array-subtdype
new_dtype = lookup_dtype(np.dtype((new_dtype.np_type, values.shape[1:])))
# Create the new matrix
C = cls.new(new_dtype, nrows, ncols, name=name)
C = cls(new_dtype, nrows, ncols, name=name)
if values.ndim == 0:
if dup_op is not None:
raise ValueError(
Expand Down Expand Up @@ -480,7 +485,7 @@ def ewise_add(self, other, op=monoid.plus, *, require_monoid=True):
f"to rows of Matrix in {method_name}. Matrix.ncols (={self._ncols}) "
f"must equal Vector.size (={other._size})."
)
full = Vector.new(other.dtype, self._nrows, name="v_full")
full = Vector(other.dtype, self._nrows, name="v_full")
full[:] = 0
other = full.outer(other, binary.second).new(name="M_temp")
expr = MatrixExpression(
Expand Down Expand Up @@ -589,7 +594,7 @@ def ewise_union(self, other, op, left_default, right_default):
f"to rows of Matrix in {method_name}. Matrix.ncols (={self._ncols}) "
f"must equal Vector.size (={other._size})."
)
full = Vector.new(other.dtype, self._nrows, name="v_full")
full = Vector(other.dtype, self._nrows, name="v_full")
full[:] = 0
other = full.outer(other, binary.second).new(name="M_temp")
expr = MatrixExpression(
Expand Down Expand Up @@ -877,7 +882,7 @@ def _extract_element(self, resolved_indexes, dtype=None, *, is_cscalar, name=Non
if self._is_transposed:
rowidx, colidx = colidx, rowidx
if result is None:
result = Scalar.new(dtype, is_cscalar=is_cscalar, name=name)
result = Scalar(dtype, is_cscalar=is_cscalar, name=name)
if is_cscalar:
dtype_name = "UDT" if dtype._is_udt else dtype.name
if (
Expand Down Expand Up @@ -1155,15 +1160,15 @@ def _prep_for_assign(self, resolved_indexes, value, mask=None, is_submask=False)
# C[i, J](m) << c
# SS, SuiteSparse-specific: subassign
cfunc_name = "GrB_Row_subassign"
value_vector = Vector.new(value.dtype, size=mask.mask._size, name="v_temp")
value_vector = Vector(value.dtype, size=mask.mask._size, name="v_temp")
expr_repr = (
"[{1._expr_name}, [{3._expr_name} cols]](%s) << {0.name}" % mask.name
)
else:
# C(m)[i, J] << c
# C[i, J] << c
cfunc_name = "GrB_Row_assign"
value_vector = Vector.new(value.dtype, size=colsize, name="v_temp")
value_vector = Vector(value.dtype, size=colsize, name="v_temp")
expr_repr = "[{1._expr_name}, [{3._expr_name} cols]] = {0.name}"
# SS, SuiteSparse-specific: assume efficient vector with single scalar
value_vector << value
Expand All @@ -1183,12 +1188,12 @@ def _prep_for_assign(self, resolved_indexes, value, mask=None, is_submask=False)
# C[I, j](m) << c
# SS, SuiteSparse-specific: subassign
cfunc_name = "GrB_Col_subassign"
value_vector = Vector.new(value.dtype, size=mask.mask._size, name="v_temp")
value_vector = Vector(value.dtype, size=mask.mask._size, name="v_temp")
else:
# C(m)[I, j] << c
# C[I, j] << c
cfunc_name = "GrB_Col_assign"
value_vector = Vector.new(value.dtype, size=rowsize, name="v_temp")
value_vector = Vector(value.dtype, size=rowsize, name="v_temp")
# SS, SuiteSparse-specific: assume efficient vector with single scalar
value_vector << value

Expand Down Expand Up @@ -1364,7 +1369,7 @@ def __init__(
def construct_output(self, dtype=None, *, name=None):
if dtype is None:
dtype = self.dtype
return Matrix.new(dtype, self._nrows, self._ncols, name=name)
return Matrix(dtype, self._nrows, self._ncols, name=name)

def __repr__(self):
from .formatting import format_matrix_expression
Expand Down Expand Up @@ -1545,7 +1550,7 @@ def _repr_html_(self, collapse=False):
def new(self, dtype=None, *, mask=None, name=None):
if dtype is None:
dtype = self.dtype
output = Matrix.new(dtype, self._nrows, self._ncols, name=name)
output = Matrix(dtype, self._nrows, self._ncols, name=name)
if mask is None:
output.update(self)
else:
Expand Down
5 changes: 1 addition & 4 deletions grblas/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,7 @@ def identity(self):

with skip_record:
self._identity = (
Vector.new(self.type, size=1, name="")
.reduce(self, allow_empty=False)
.new()
.value
Vector(self.type, size=1, name="").reduce(self, allow_empty=False).new().value
)
return self._identity

Expand Down
23 changes: 14 additions & 9 deletions grblas/scalar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import warnings

import numpy as np

Expand Down Expand Up @@ -113,7 +114,7 @@ def __neg__(self):
dtype = self.dtype
if dtype.name[0] == "U" or dtype == BOOL or dtype._is_udt:
raise TypeError(f"The negative operator, `-`, is not supported for {dtype.name} dtype")
rv = Scalar.new(dtype, is_cscalar=self._is_cscalar, name=f"-{self.name or 's_temp'}")
rv = Scalar(dtype, is_cscalar=self._is_cscalar, name=f"-{self.name or 's_temp'}")
if self._is_empty:
return rv
rv.value = -self.value
Expand All @@ -125,7 +126,7 @@ def __invert__(self):
f"The invert operator, `~`, is not supported for {self.dtype.name} dtype. "
"It is only supported for BOOL dtype."
)
rv = Scalar.new(BOOL, is_cscalar=self._is_cscalar, name=f"~{self.name or 's_temp'}")
rv = Scalar(BOOL, is_cscalar=self._is_cscalar, name=f"~{self.name or 's_temp'}")
if self._is_empty:
return rv
rv.value = not self.value
Expand Down Expand Up @@ -253,7 +254,7 @@ def value(self):
if self._is_cscalar:
scalar = self
else:
scalar = Scalar.new(self.dtype, is_cscalar=True, name="s_temp")
scalar = Scalar(self.dtype, is_cscalar=True, name="s_temp")
dtype_name = "UDT" if is_udt else self.dtype.name
call(f"GrB_Scalar_extractElement_{dtype_name}", [_Pointer(scalar), self])
if is_udt:
Expand Down Expand Up @@ -328,10 +329,10 @@ def dup(self, dtype=None, *, is_cscalar=None, name=None):
new_scalar = Scalar(self.dtype, is_cscalar=False, name=name) # pragma: is_grbscalar
call("GrB_Scalar_dup", [_Pointer(new_scalar), self])
elif dtype is None:
new_scalar = Scalar.new(self.dtype, is_cscalar=is_cscalar, name=name)
new_scalar = Scalar(self.dtype, is_cscalar=is_cscalar, name=name)
new_scalar.value = self
else:
new_scalar = Scalar.new(dtype, is_cscalar=is_cscalar, name=name)
new_scalar = Scalar(dtype, is_cscalar=is_cscalar, name=name)
if not self._is_empty:
if new_scalar.is_cscalar and not new_scalar.dtype._is_udt:
# Cast value so we don't raise given explicit dup with dtype
Expand All @@ -357,6 +358,10 @@ def new(cls, dtype, *, is_cscalar=False, name=None):
"""
Create a new empty Scalar from the given type
"""
warnings.warn(
"`Scalar.new(...)` is deprecated; please use `Scalar(...)` instead.",
DeprecationWarning,
)
return Scalar(dtype, is_cscalar=is_cscalar, name=name)

@classmethod
Expand All @@ -383,7 +388,7 @@ def from_value(cls, value, dtype=None, *, is_cscalar=False, name=None):
argname="value",
extra_message="Literal scalars expected.",
)
new_scalar = cls.new(dtype, is_cscalar=is_cscalar, name=name)
new_scalar = cls(dtype, is_cscalar=is_cscalar, name=name)
new_scalar.value = value
return new_scalar

Expand Down Expand Up @@ -432,7 +437,7 @@ def _as_vector(self):
from .vector import Vector

if self._is_cscalar:
rv = Vector.new(self.dtype, size=1)
rv = Vector(self.dtype, size=1)
if not self._is_empty:
rv[0] = self
return rv
Expand All @@ -453,7 +458,7 @@ def _as_matrix(self):
from .matrix import Matrix

if self._is_cscalar:
rv = Matrix.new(self.dtype, ncols=1, nrows=1)
rv = Matrix(self.dtype, ncols=1, nrows=1)
if not self._is_empty:
rv[0, 0] = self
return rv
Expand Down Expand Up @@ -484,7 +489,7 @@ def construct_output(self, dtype=None, *, is_cscalar=None, name=None):
dtype = self.dtype
if is_cscalar is None:
is_cscalar = self._is_cscalar
return Scalar.new(dtype, is_cscalar=is_cscalar, name=name)
return Scalar(dtype, is_cscalar=is_cscalar, name=name)

def new(self, dtype=None, *, is_cscalar=None, name=None):
if is_cscalar is None:
Expand Down
8 changes: 4 additions & 4 deletions grblas/ss/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def diag(x, k=0, dtype=None, *, name=None):
typ = type(x)
if typ is Vector:
size = x._size + abs(k.value)
rv = Matrix.new(dtype, nrows=size, ncols=size, name=name)
rv = Matrix(dtype, nrows=size, ncols=size, name=name)
rv.ss.build_diag(x, k)
else:
if k.value < 0:
Expand All @@ -56,7 +56,7 @@ def diag(x, k=0, dtype=None, *, name=None):
size = min(x._ncols - k.value, x._nrows)
if size < 0:
size = 0
rv = Vector.new(dtype, size=size, name=name)
rv = Vector(dtype, size=size, name=name)
rv.ss.build_diag(x, k)
return rv

Expand Down Expand Up @@ -87,12 +87,12 @@ def concat(tiles, dtype=None, *, name=None):
dtype = tiles[0][0].dtype
nrows = sum(row_tiles[0]._nrows for row_tiles in tiles)
ncols = sum(tile._ncols for tile in tiles[0])
rv = Matrix.new(dtype, nrows=nrows, ncols=ncols, name=name)
rv = Matrix(dtype, nrows=nrows, ncols=ncols, name=name)
rv.ss._concat(tiles, m, n)
else:
if dtype is None:
dtype = tiles[0].dtype
size = sum(tile._nrows for tile in tiles)
rv = Vector.new(dtype, size=size, name=name)
rv = Vector(dtype, size=size, name=name)
rv.ss._concat(tiles, m)
return rv

0 comments on commit 0156352

Please sign in to comment.