Skip to content

Commit

Permalink
Merge branch 'numpy/init' of github.com:devin-petersohn/modin into nu…
Browse files Browse the repository at this point in the history
…mpy/init
  • Loading branch information
devin-petersohn committed Nov 22, 2022
2 parents 6e192f5 + 1519be7 commit d342e36
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 10 deletions.
3 changes: 2 additions & 1 deletion modin/numpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .arr import *
from .math import *
from .math import *
from .constants import *
76 changes: 67 additions & 9 deletions modin/numpy/arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,58 @@ class array(object):
def __init__(self, object=None, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None, query_compiler=None):
if query_compiler is not None:
self._query_compiler = query_compiler
elif isinstance(object, list):
import modin.pandas as pd
qc = pd.DataFrame(object)._query_compiler
self._query_compiler = qc
else:
arr = numpy.array(object, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=ndmin, like=like)
pass

def _absolute(self, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.abs().to_numpy()
return result
result = self._query_compiler.abs()
return array(query_compiler=result)

def _add(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.add(x2._query_compiler).to_numpy()
return result
result = self._query_compiler.add(x2._query_compiler)
return array(query_compiler=result)

def _divide(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.truediv(x2._query_compiler)
return array(query_compiler=result)

def _float_power(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.add(x2._query_compiler)
return array(query_compiler=result)

def _floor_divide(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.floordiv(x2._query_compiler)
return array(query_compiler=result)

def _power(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.pow(x2._query_compiler)
return array(query_compiler=result)

def _all(self, axis=None, out=None, keepdims=None, where=None):
result = self._query_compiler.all(axis=axis).to_numpy()
return result
def _prod(self, axis=None, out=None, keepdims=None, where=None):
print("Series?", self._query_compiler.is_series_like())
if axis is None:
result = self._query_compiler.prod(axis=0).prod(axis=1)
return array(query_compiler=result)
else:
result = self._query_compiler.prod(axis=axis)
return array(query_compiler=result)

def _multiply(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.mul(x2._query_compiler)
return array(query_compiler=result)

def _remainder(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.mod(x2._query_compiler)
return array(query_compiler=result)

def _subtract(self, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
result = self._query_compiler.sub(x2._query_compiler).to_numpy()
return result
result = self._query_compiler.sub(x2._query_compiler)
return array(query_compiler=result)

def _sum(self, axis=None, dtype=None, out=None, keepdims=None, initial=None, where=None):
result = self._query_compiler.sum(axis=axis)
Expand All @@ -34,5 +67,30 @@ def _sum(self, axis=None, dtype=None, out=None, keepdims=None, initial=None, whe
return
return array(query_compiler=result)

def _get_shape(self):
return (len(self._query_compiler.index), len(self._query_compiler.columns))

def _set_shape(self, new_shape):
if not (isinstance(new_shape, int)) and not isinstance(new_shape, tuple):
raise TypeError(f"expected a sequence of integers or a single integer, got '{new_shape}'")
elif isinstance(new_shape, tuple):
for dim in new_shape:
if not isinstance(dim, int):
raise TypeError(f"'{type(dim)}' object cannot be interpreted as an integer")
from math import prod
new_dimensions = new_shape if isinstance(new_shape, int) else prod(new_shape)
if new_dimensions != prod(self._get_shape()):
raise ValueError(f"cannot reshape array of size {prod(self._get_shape)} into {new_shape if isinstance(new_shape, tuple) else (new_shape,)}")
if isinstance(new_shape, int):
qcs = []
for index_val in self._query_compiler.index[1:]:
qcs.append(self._query_compiler.getitem_row_array([index_val]).reset_index(drop=True))
self._query_compiler = self._query_compiler.getitem_row_array([self._query_compiler.index[0]]).reset_index(drop=True).concat(1, qcs, ignore_index=True)
else:
raise NotImplementedError("Reshaping from a 2D object to a 2D object is not currently supported!")

shape = property(_get_shape, _set_shape)


def __repr__(self):
return repr(self._query_compiler.to_numpy())
17 changes: 17 additions & 0 deletions modin/numpy/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from numpy import (
Inf,
Infinity,
NAN,
NINF,
NZERO,
NaN,
PINF,
PZERO,
e,
euler_gamma,
inf,
infty,
nan,
newaxis,
pi
)
51 changes: 51 additions & 0 deletions modin/numpy/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,50 @@ def all(a, axis=None, out=None, keepdims=None, where=None):
return numpy.all(a, axis=axis, out=out, keepdims=keepdims, where=where)


def divide(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_divide"):
return x1._divide(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.divide(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)


def float_power(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_float_power"):
return x1._float_power(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.float_power(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)


def floor_divide(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_floor_divide"):
return x1._floor_divide(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.floor_divide(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)


def power(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_power"):
return x1._power(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.power(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)


def prod(a, axis=None, out=None, keepdims=None, where=None):
if hasattr(a, "_prod"):
return a._prod(axis=axis, out=out, keepdims=keepdims, where=where)
return numpy.prod(a, axis=axis, out=out, keepdims=keepdims, where=where)


def multiply(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_multiply"):
return x1._multiply(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.multiply(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)


def remainder(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_remainder"):
return x1._remainder(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.remainder(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)

mod = remainder


def subtract(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_subtract"):
return x1._subtract(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
Expand All @@ -31,3 +75,10 @@ def sum(arr, axis=None, dtype=None, out=None, keepdims=None, initial=None, where
return arr._sum(axis)
else:
return numpy.sum(arr)


def true_divide(x1, x2, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True):
if hasattr(x1, "_divide"):
return x1._divide(x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
return numpy.divide(x1, x2, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)

0 comments on commit d342e36

Please sign in to comment.