From 98c5ecee5702f10d2daf630ec171f50a7abc632f Mon Sep 17 00:00:00 2001 From: Simon Adorf Date: Wed, 12 Apr 2023 11:55:39 -0700 Subject: [PATCH] Use more concise names for the array and store conversion functions. --- legate/raft/__init__.py | 6 +++--- legate/raft/core.py | 4 ++-- naive_bayes/multinomial.py | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/legate/raft/__init__.py b/legate/raft/__init__.py index 2ab49b3272..28fb076aa3 100644 --- a/legate/raft/__init__.py +++ b/legate/raft/__init__.py @@ -1,10 +1,11 @@ from .array_api import add, exp, fill, log, negative, subtract, sum_over_axis -from .core import array_to_store, convert, store_to_array +from .core import as_array, as_store, convert from .multiarray import bincount, categorize, matmul, multiply __all__ = [ "add", - "array_to_store", + "as_array", + "as_store", "bincount", "categorize", "convert", @@ -14,7 +15,6 @@ "matmul", "multiply", "negative", - "store_to_array", "subtract", "sum_over_axis", ] diff --git a/legate/raft/core.py b/legate/raft/core.py index 3832529ca8..a086611b2e 100644 --- a/legate/raft/core.py +++ b/legate/raft/core.py @@ -45,7 +45,7 @@ def __array_interface__(self): } -def array_to_store(array: np.ndarray) -> Store: +def as_store(array: np.ndarray) -> Store: store = context.create_store( pa.from_numpy_dtype(array.dtype), shape=array.shape, @@ -59,7 +59,7 @@ def array_to_store(array: np.ndarray) -> Store: return store -def store_to_array(store: Store) -> np.ndarray: +def as_array(store: Store) -> np.ndarray: if store.kind is Future: dtype = store.get_dtype() buf = store.storage.get_buffer(dtype.size) diff --git a/naive_bayes/multinomial.py b/naive_bayes/multinomial.py index 57397a0fea..5a9fcde3d3 100644 --- a/naive_bayes/multinomial.py +++ b/naive_bayes/multinomial.py @@ -11,13 +11,13 @@ from legate.raft import ( add, - array_to_store, + as_array, + as_store, convert, exp, fill, log, matmul, - store_to_array, subtract, sum_over_axis, ) @@ -34,12 +34,12 @@ def __init__(self, alpha: float = 1.0, fit_prior=True, class_prior=None): def fit(self, X, y): # Convert to a legate stores - X = array_to_store(X) + X = as_store(X) _, n_features = X.shape self.n_features_in_ = n_features labelbin = LabelBinarizer() - Y = array_to_store(labelbin.fit_transform(y)) + Y = as_store(labelbin.fit_transform(y)) self.classes_ = labelbin.classes_ assert Y.shape[1] != 1 @@ -69,12 +69,12 @@ def _update_feature_log_proba(self, alpha, dtype=pa.float64()): @property def feature_log_prob_(self): if self._feature_log_prob_ is not None: - return store_to_array(self._feature_log_prob_) + return as_array(self._feature_log_prob_) @property def class_log_prior_(self): if self._class_log_prior_ is not None: - return store_to_array(self._class_log_prior_) + return as_array(self._class_log_prior_) def _update_class_log_prior(self, class_prior=None): n_classes = len(self.classes_) @@ -104,17 +104,17 @@ def _predict_log_proba(self, X): return subtract(x1.transpose((1, 0)), x2).transpose((1, 0)) def predict_log_proba(self, X): - X = array_to_store(X) + X = as_store(X) ret = self._predict_log_proba(X) - return store_to_array(ret) + return as_array(ret) def predict_proba(self, X): - X = array_to_store(X) + X = as_store(X) ret = exp(self._predict_log_proba(X)) - return store_to_array(ret) + return as_array(ret) def predict(self, X): - X = array_to_store(X) - jll = store_to_array(self._joint_log_likelihood(X)) + X = as_store(X) + jll = as_array(self._joint_log_likelihood(X)) ret = self.classes_[np.argmax(jll, axis=1)] return ret