Skip to content

Commit

Permalink
avoid deprecated numpy API
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhrisca committed Nov 18, 2024
1 parent d470ad3 commit aa88fcc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 49 deletions.
14 changes: 7 additions & 7 deletions examples/mf4_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
]

sig = Signal(
np.core.records.fromarrays(samples, dtype=np.dtype(types)),
np.rec.fromarrays(samples, dtype=np.dtype(types)),
t,
name="Channel_lookup_with_axis",
unit="A",
Expand All @@ -166,7 +166,7 @@
types = [("Channel_lookup_with_default_axis", "(2, 3)<u8")]

sig = Signal(
np.core.records.fromarrays(samples, dtype=np.dtype(types)),
np.rec.fromarrays(samples, dtype=np.dtype(types)),
t,
name="Channel_lookup_with_default_axis",
unit="mA",
Expand Down Expand Up @@ -198,7 +198,7 @@
]

sig = Signal(
np.core.records.fromarrays(samples, dtype=np.dtype(types)),
np.rec.fromarrays(samples, dtype=np.dtype(types)),
t,
name="Channel_structure_composition",
comment="Structure channel composition",
Expand All @@ -221,7 +221,7 @@
("level44", np.float64),
]

l4_arr = np.core.records.fromarrays(l4_arr, dtype=types)
l4_arr = np.rec.fromarrays(l4_arr, dtype=types)

l3_arr = [
l4_arr,
Expand All @@ -235,7 +235,7 @@
("level33", l4_arr.dtype),
]

l3_arr = np.core.records.fromarrays(l3_arr, dtype=types)
l3_arr = np.rec.fromarrays(l3_arr, dtype=types)


l2_arr = [
Expand All @@ -245,14 +245,14 @@

types = [("level21", l3_arr.dtype), ("level22", l3_arr.dtype)]

l2_arr = np.core.records.fromarrays(l2_arr, dtype=types)
l2_arr = np.rec.fromarrays(l2_arr, dtype=types)


l1_arr = [l2_arr]

types = [("level11", l2_arr.dtype)]

l1_arr = np.core.records.fromarrays(l1_arr, dtype=types)
l1_arr = np.rec.fromarrays(l1_arr, dtype=types)


sigs.append(Signal(l1_arr, t, name="Nested_structures"))
Expand Down
29 changes: 18 additions & 11 deletions src/asammdf/blocks/mdf_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Any, overload
import xml.etree.ElementTree as ET

import numpy as np
from numpy import (
arange,
array,
Expand All @@ -38,8 +39,14 @@
unique,
zeros,
)
from numpy.core.defchararray import decode, encode
from numpy.core.records import fromarrays, fromstring

try:
decode = np.strings.decode
encode = np.strings.encode
except:
decode = np.char.decode
encode = np.char.encode

from numpy.typing import NDArray
from pandas import DataFrame
from typing_extensions import Literal, TypedDict
Expand Down Expand Up @@ -603,7 +610,7 @@ def _get_not_byte_aligned_data(self, data: bytes, group: Group, ch_nr: int) -> N
("", f"a{record_size - byte_size - byte_offset}"),
]

vals = fromstring(data, dtype=dtype(types))
vals = np.rec.fromstring(data, dtype=dtype(types))

vals = vals["vals"]

Expand Down Expand Up @@ -1576,7 +1583,7 @@ def append(
new_gp.sorted = True

try:
samples = fromarrays(new_fields, dtype=new_types)
samples = np.rec.fromarrays(new_fields, dtype=new_types)
block = samples.tobytes()
except:
struct_fields = []
Expand Down Expand Up @@ -1986,7 +1993,7 @@ def append(

new_gp.sorted = True

samples = fromarrays(new_fields, dtype=new_types)
samples = np.rec.fromarrays(new_fields, dtype=new_types)

block = samples.tobytes()

Expand Down Expand Up @@ -2037,7 +2044,7 @@ def append(
gp.sorted = True

if signals:
samples = fromarrays(fields, dtype=types)
samples = np.rec.fromarrays(fields, dtype=types)
else:
samples = array([])

Expand Down Expand Up @@ -2293,7 +2300,7 @@ def _append_dataframe(
gp.sorted = True

if df.shape[0]:
samples = fromarrays(fields, dtype=types)
samples = np.rec.fromarrays(fields, dtype=types)
else:
samples = array([])

Expand Down Expand Up @@ -2479,7 +2486,7 @@ def extend(self, index: int, signals: list[tuple[NDArray[Any], None]]) -> None:
# data block
new_types = dtype(new_types)

samples = fromarrays(new_fields, dtype=new_types)
samples = np.rec.fromarrays(new_fields, dtype=new_types)
samples = samples.tobytes()

record_size = new_gp.channel_group.samples_byte_nr
Expand Down Expand Up @@ -2534,7 +2541,7 @@ def extend(self, index: int, signals: list[tuple[NDArray[Any], None]]) -> None:
# data block
types = dtype(types)

samples = fromarrays(fields, dtype=types)
samples = np.rec.fromarrays(fields, dtype=types)
samples = samples.tobytes()

if cycles_nr:
Expand Down Expand Up @@ -2935,7 +2942,7 @@ def get(
arrays.append(sig.samples)
types.append((sig.name, sig.samples.dtype))

vals = fromarrays(arrays, dtype=types)
vals = np.rec.fromarrays(arrays, dtype=types)

elif dep.dependency_type >= v23c.DEPENDENCY_TYPE_NDIM:
shape = []
Expand Down Expand Up @@ -2971,7 +2978,7 @@ def get(
types = [(channel.name, vals.dtype, record_shape)]

types = dtype(types)
vals = fromarrays(arrays, dtype=types)
vals = np.rec.fromarrays(arrays, dtype=types)

if not samples_only or raster:
timestamps = self.get_master(
Expand Down
42 changes: 24 additions & 18 deletions src/asammdf/blocks/mdf_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@
where,
zeros,
)
from numpy.core.defchararray import decode, encode
from numpy.core.records import fromarrays, fromstring

try:
decode = np.strings.decode
encode = np.strings.encode
except:
decode = np.char.decode
encode = np.char.encode

from numpy.typing import NDArray
from pandas import DataFrame

Expand Down Expand Up @@ -2919,7 +2925,7 @@ def append(

types_ = [("o", uint32), ("s", sig_dtype, sig_shape[1:])]

data = fromarrays(values, dtype=types_)
data = np.rec.fromarrays(values, dtype=types_)

data_size = len(data) * data.itemsize
if data_size:
Expand Down Expand Up @@ -3226,7 +3232,7 @@ def append(
vals.append(signal.samples[field] + (signal.samples["day_of_week"] << 4))
else:
vals.append(signal.samples[field])
vals = fromarrays(vals).tobytes()
vals = np.rec.fromarrays(vals).tobytes()

if not vals.flags["C_CONTIGUOUS"]:
vals = np.ascontiguousarray(vals)
Expand Down Expand Up @@ -3580,7 +3586,7 @@ def append(

types_ = [("o", uint32), ("s", sig_dtype)]

data = fromarrays(values, dtype=types_)
data = np.rec.fromarrays(values, dtype=types_)

data_size = len(data) * data.itemsize
if data_size:
Expand Down Expand Up @@ -4152,7 +4158,7 @@ def _append_column_oriented(
vals.append(signal.samples[field] + (signal.samples["day_of_week"] << 4))
else:
vals.append(signal.samples[field])
samples = fromarrays(vals)
samples = np.rec.fromarrays(vals)

types.append((name, "V7"))
gp.single_channel_dtype = dtype("V7")
Expand Down Expand Up @@ -4468,7 +4474,7 @@ def _append_column_oriented(

types_ = [("o", uint32), ("s", sig_dtype)]

data = fromarrays(values, dtype=types_)
data = np.rec.fromarrays(values, dtype=types_)

data_size = len(data) * data.itemsize
if data_size:
Expand Down Expand Up @@ -4806,7 +4812,7 @@ def _append_dataframe(

types_ = [("", uint32), ("", sig.dtype)]

data = fromarrays(values, dtype=types_)
data = np.rec.fromarrays(values, dtype=types_)

data_size = len(data) * data.itemsize
if data_size:
Expand Down Expand Up @@ -4894,7 +4900,7 @@ def _append_dataframe(
gp.sorted = True

if df.shape[0]:
samples = fromarrays(fields, dtype=types)
samples = np.rec.fromarrays(fields, dtype=types)
else:
samples = array([])

Expand Down Expand Up @@ -5856,7 +5862,7 @@ def extend(self, index: int, signals: list[tuple[NDArray[Any], NDArray[Any] | No
vals = []
for field in ("ms", "min", "hour", "day", "month", "year"):
vals.append(signal[field])
vals = fromarrays(vals).tobytes()
vals = np.rec.fromarrays(vals).tobytes()

fields.append((vals, 7))

Expand Down Expand Up @@ -5972,7 +5978,7 @@ def extend(self, index: int, signals: list[tuple[NDArray[Any], NDArray[Any] | No

types_ = [("", uint32), ("", signal.dtype)]

values = fromarrays(values, dtype=types_)
values = np.rec.fromarrays(values, dtype=types_)

stream.seek(0, 2)
addr = stream.tell()
Expand Down Expand Up @@ -6157,7 +6163,7 @@ def _extend_column_oriented(self, index: int, signals: list[tuple[NDArray[Any],
vals = []
for field in ("ms", "min", "hour", "day", "month", "year"):
vals.append(signal[field])
samples = fromarrays(vals)
samples = np.rec.fromarrays(vals)

elif sig_type == v4c.SIGNAL_TYPE_STRUCTURE_COMPOSITION:
samples = signal
Expand All @@ -6174,7 +6180,7 @@ def _extend_column_oriented(self, index: int, signals: list[tuple[NDArray[Any],

types_ = [("", uint32), ("", signal.dtype)]

values = fromarrays(values, dtype=types_)
values = np.rec.fromarrays(values, dtype=types_)

addr = tell()
block_size = len(values) * values.itemsize
Expand Down Expand Up @@ -7059,7 +7065,7 @@ def _get_structure(
types = [(name_, arr.dtype, arr.shape[1:]) for name_, arr in zip(names, arrays)]
types = dtype(types)

vals = fromarrays(arrays, dtype=types)
vals = np.rec.fromarrays(arrays, dtype=types)

if master_is_required:
if count > 1:
Expand Down Expand Up @@ -7351,7 +7357,7 @@ def _get_array(
dtype_pair = (axisname, axis_values.dtype, shape)
types.append(dtype_pair)

vals = fromarrays(arrays, dtype(types))
vals = np.rec.fromarrays(arrays, dtype(types))

if master_is_required:
timestamps.append(self.get_master(gp_nr, fragment, one_piece=True))
Expand Down Expand Up @@ -7904,7 +7910,7 @@ def _get_scalar(
"summer_time",
"day_of_week",
]
vals = fromarrays(arrays, names=names)
vals = np.rec.fromarrays(arrays, names=names)

# CANopen time
elif data_type == v4c.DATA_TYPE_CANOPEN_TIME:
Expand Down Expand Up @@ -7959,7 +7965,7 @@ def _get_not_byte_aligned_data(self, data: bytes, group: Group, ch_nr: int) -> N
("", f"a{record_size - byte_size - byte_offset}"),
]

vals = fromstring(data, dtype=dtype(types))
vals = np.rec.fromstring(data, dtype=dtype(types))

vals = vals["vals"]

Expand Down Expand Up @@ -10350,7 +10356,7 @@ def _sort(
cols = param
lines = dtblock_raw_size // cols

nd = fromstring(new_data[: lines * cols], dtype=uint8)
nd = np.rec.fromstring(new_data[: lines * cols], dtype=uint8)
nd = nd.reshape((cols, lines))
new_data = nd.T.ravel().tobytes() + new_data[lines * cols :]

Expand Down
6 changes: 5 additions & 1 deletion src/asammdf/gui/widgets/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

import dateutil.tz
import numpy as np
import numpy.core.defchararray as npchar

try:
npchar = np.strings
except:
npchar = np.char
import pandas as pd
from PySide6 import QtCore, QtWidgets

Expand Down
6 changes: 5 additions & 1 deletion src/asammdf/gui/widgets/tabular_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
from traceback import format_exc

import numpy as np
import numpy.core.defchararray as npchar

try:
npchar = np.strings
except:
npchar = np.char
import pandas as pd
import pyqtgraph.functions as fn
from PySide6 import QtCore, QtGui, QtWidgets
Expand Down
6 changes: 5 additions & 1 deletion src/asammdf/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from typing import Any

import numpy as np
from numpy.core.defchararray import encode

try:
encode = np.strings.encode
except:
encode = np.char.encode
from numpy.typing import ArrayLike, DTypeLike, NDArray

from .blocks import v2_v3_blocks as v3b
Expand Down
Loading

0 comments on commit aa88fcc

Please sign in to comment.