Skip to content

Commit

Permalink
deps/numpy-2-compat (#92)
Browse files Browse the repository at this point in the history
* explicitly pass encoding as bytes

* update changelog

* ruff check fixes
  • Loading branch information
alphasentaurii authored Sep 17, 2024
1 parent ccb3509 commit a36bffe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.1.2 (unreleased)
==================

preprocessor
------------
- explicitly pass `encoding=bytes` in transform.hypersonic_pliers for numpy 2 compatibility where this will no longer be the default for np.loadtxt [#92]


1.1.1 (2024-07-11)
==================

Expand Down
2 changes: 1 addition & 1 deletion spacekit/analyzer/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def atomic_vector_plotter(
x_units = x_units

# Scatter Plot
if type(signal) == np.array:
if isinstance(signal, np.array):
series_index = list(range(len(signal)))

converted_array = pd.Series(signal.ravel(), index=series_index)
Expand Down
2 changes: 1 addition & 1 deletion spacekit/extractor/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def split_arrays(self, data, t=0.6, v=0.85):
arrays
split sampled arrays
"""
if type(data) == pd.DataFrame:
if isinstance(data, pd.DataFrame):
sample = data.sample(frac=1)
else:
sample = data
Expand Down
8 changes: 3 additions & 5 deletions spacekit/extractor/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import csv
from zipfile import ZipFile
from astropy.io import fits, ascii
from astropy.table import Table
from botocore.config import Config
from decimal import Decimal
from boto3.dynamodb.conditions import Attr
Expand Down Expand Up @@ -1428,18 +1429,15 @@ def make_dataframe_line(self, json_filename_list):
ingest_key = fd_key.replace(" ", "_")
key_suffix = ingest_key.split(".")[-1]
if key_suffix not in ["data", "unit", "format", "dtype"]:
if (
str(type(json_data_item))
== "<class 'astropy.table.table.Table'>"
):
if isinstance(json_data_item, Table):
for coltitle in json_data_item.colnames:
ingest_value = json_data_item[coltitle].tolist()
id_key = title_suffix + ingest_key + "." + coltitle
ingest_dict["data"][id_key] = [ingest_value]
else:
ingest_value = json_data_item
id_key = title_suffix + ingest_key
if str(type(ingest_value)) == "<class 'list'>":
if isinstance(ingest_value, list):
ingest_dict["data"][id_key] = [ingest_value]
else:
ingest_dict["data"][id_key] = ingest_value
Expand Down
10 changes: 6 additions & 4 deletions spacekit/preprocessor/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def normalize_training_images(X_tr, X_ts, X_vl=None):


def array_to_tensor(arr, reshape=False, shape=(-1, 1)):
if type(arr) == tf.Tensor:
if isinstance(arr, tf.Tensor):
return arr
if reshape is True:
arr = arr.reshape(shape[0], shape[1])
Expand Down Expand Up @@ -869,7 +869,7 @@ def tensors_to_arrays(X_train, y_train, X_test, y_test):


def hypersonic_pliers(
path_to_train, path_to_test, y_col=[0], skip=1, dlm=",", subtract_y=0.0
path_to_train, path_to_test, y_col=[0], skip=1, dlm=",", encoding=bytes, subtract_y=0.0
):
"""Extracts data into 1-dimensional arrays, using separate target classes (y) for training and test data. Assumes y (target)
is first column in dataframe. If the target (y) classes in the raw data are 0 and 2, but you'd like them to be binaries (0
Expand All @@ -887,6 +887,8 @@ def hypersonic_pliers(
skiprows parameter for np.loadtxt, by default 1
dlm : str, optional
delimiter, by default ","
encoding: str, optional
explicitly passed encoding type to numpy.loadtxt, by default bytes
subtract_y : float, optional
subtract this value from all y-values, by default 1.0
Expand All @@ -895,15 +897,15 @@ def hypersonic_pliers(
np.ndarrays
X_train, X_test, y_train, y_test
"""
Train = np.loadtxt(path_to_train, skiprows=skip, delimiter=dlm)
Train = np.loadtxt(path_to_train, skiprows=skip, delimiter=dlm, encoding=encoding)
cols = list(range(Train.shape[1]))
xcols = [c for c in cols if c not in y_col]
# X_train = Train[:, 1:]
X_train = Train[:, xcols]
# y_train = Train[:, 0, np.newaxis] - subtract_y
y_train = Train[:, y_col, np.newaxis] - subtract_y

Test = np.loadtxt(path_to_test, skiprows=skip, delimiter=dlm)
Test = np.loadtxt(path_to_test, skiprows=skip, delimiter=dlm, encoding=encoding)
X_test = Test[:, xcols]
y_test = Test[:, y_col, np.newaxis] - subtract_y
# X_test = Test[:, 1:]
Expand Down

0 comments on commit a36bffe

Please sign in to comment.