Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: remove NumPy dependency #12

Merged
merged 5 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions marray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

__version__ = "0.0.4"

import numpy as np # temporarily used in __repr__ and __str__
import textwrap
mdhaber marked this conversation as resolved.
Show resolved Hide resolved


def masked_array(xp):
Expand Down Expand Up @@ -86,20 +86,20 @@ def __setitem__(self, key, other):
self.mask[key] = getattr(other, 'mask', False)
return self.data.__setitem__(key, getattr(other, 'data', other))

def _data_mask_string(self, fun):
data_str = fun(self.data)
mask_str = fun(self.mask)
if len(data_str) + len(mask_str) <= 66:
return f"MaskedArray({data_str}, {mask_str})"
else:
return f"MaskedArray(\n {data_str},\n {mask_str}\n)"

## Visualization ##
def __repr__(self):
# temporary: fix for CuPy
# eventually: rewrite to avoid masked array
data = np.asarray(self.data)
mask = np.asarray(self.mask)
return np.ma.masked_array(data, mask).__repr__()
return self._data_mask_string(repr)

def __str__(self):
# temporary: fix for CuPy
# eventually: rewrite to avoid masked array
data = np.asarray(self.data)
mask = np.asarray(self.mask)
return np.ma.masked_array(data, mask).__str__()
return self._data_mask_string(str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should always be repr? That would mean there's no difference between __str__ and __repr__, but given that at least numpy is always a pseudo-repr anyways (so recreating objects is not possible) this might not be too much of an issue?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should always be repr?

But why not follow the convention of the underlying library, whatever it is?

import numpy as np
import torch
import jax.numpy as jnp
import cupy as cp
for xp in [np, torch, jnp, cp]:
    x = xp.asarray([1, 2, 3, 4])
    print(xp.__name__)
    print(str(x))
    print(repr(x))

# numpy
# [1 2 3 4]
# array([1, 2, 3, 4])
#
# torch
# tensor([1, 2, 3, 4])
# tensor([1, 2, 3, 4])
#
# jax.numpy
# [1 2 3 4]
# Array([1, 2, 3, 4], dtype=int32)
#
# cupy
# [1 2 3 4]
# array([1, 2, 3, 4])

Most of them have different str vs repr, so I don't see a reason not to do what they do.

but given that at least numpy is always a pseudo-repr anyways (so recreating objects is not possible) this might not be too much of an issue?

I don't think it's much of an issue, especially since this is just a placeholder until it's time to polish things.
But if we were to expose MaskedArray (and maybe accept dtype as a parameter), then with the repr output, the object could be recreated to the extent that the NumPy array object can be recreated.


## Linear Algebra Methods ##
def __matmul__(self, other):
Expand Down Expand Up @@ -188,6 +188,8 @@ class module:

mod = module()

mod.MaskedArray = MaskedArray

## Constants ##
constant_names = ['e', 'inf', 'nan', 'newaxis', 'pi']
for name in constant_names:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dynamic = ["version", "description"]
dependencies = ["numpy"]
dependencies = []
requires-python = ">=3.10"

[project.optional-dependencies]
test = ["pytest"]
test = ["numpy", "pytest"]

[project.urls]
Home = "https://github.com/mdhaber/marray"
Expand Down