Skip to content

Commit 2d688a8

Browse files
authored
Merge pull request #1554 from zm711/remove-copy
Deprecate Copy Behavior to support Quantities 16.0
2 parents de6b3b5 + 3e7de39 commit 2d688a8

16 files changed

+161
-485
lines changed

.github/workflows/io-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: true
2020
matrix:
21-
python-version: ['3.9', '3.11']
21+
python-version: ['3.9', '3.11.9']
2222
defaults:
2323
# by default run in bash mode (required for conda usage)
2424
run:

neo/core/analogsignal.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _new_AnalogSignalArray(
5252
signal,
5353
units=None,
5454
dtype=None,
55-
copy=True,
55+
copy=None,
5656
t_start=0 * pq.s,
5757
sampling_rate=None,
5858
sampling_period=None,
@@ -180,7 +180,7 @@ def __new__(
180180
signal,
181181
units=None,
182182
dtype=None,
183-
copy=True,
183+
copy=None,
184184
t_start=0 * pq.s,
185185
sampling_rate=None,
186186
sampling_period=None,
@@ -198,8 +198,13 @@ def __new__(
198198
199199
__array_finalize__ is called on the new object.
200200
"""
201+
if copy is not None:
202+
raise ValueError(
203+
"`copy` is now deprecated in Neo due to removal in NumPy 2.0 and will be removed in 0.15.0."
204+
)
205+
201206
signal = cls._rescale(signal, units=units)
202-
obj = pq.Quantity(signal, units=units, dtype=dtype, copy=copy).view(cls)
207+
obj = pq.Quantity(signal, units=units, dtype=dtype).view(cls)
203208

204209
if obj.ndim == 1:
205210
obj.shape = (-1, 1)
@@ -218,7 +223,7 @@ def __init__(
218223
signal,
219224
units=None,
220225
dtype=None,
221-
copy=True,
226+
copy=None,
222227
t_start=0 * pq.s,
223228
sampling_rate=None,
224229
sampling_period=None,
@@ -257,7 +262,7 @@ def __reduce__(self):
257262
np.array(self),
258263
self.units,
259264
self.dtype,
260-
True,
265+
None,
261266
self.t_start,
262267
self.sampling_rate,
263268
self.sampling_period,
@@ -547,6 +552,7 @@ def time_shift(self, t_shift):
547552

548553
return new_sig
549554

555+
# copy in splice is a deepcopy not a numpy copy so we can keep
550556
def splice(self, signal, copy=False):
551557
"""
552558
Replace part of the current signal by a new piece of signal.

neo/core/basesignal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def merge(self, other):
267267
stack,
268268
units=self.units,
269269
dtype=self.dtype,
270-
copy=False,
270+
copy=None,
271271
t_start=self.t_start,
272272
sampling_rate=self.sampling_rate,
273273
**kwargs,

neo/core/imagesequence.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ImageSequence(BaseSignal):
7070
7171
*Optional attributes/properties*:
7272
:dtype: (numpy dtype or str) Override the dtype of the signal array.
73-
:copy: (bool) True by default.
73+
:copy: deprecated
7474
7575
Note: Any other additional arguments are assumed to be user-specific
7676
metadata and stored in :attr:`annotations`.
@@ -103,7 +103,7 @@ def __new__(
103103
image_data,
104104
units=pq.dimensionless,
105105
dtype=None,
106-
copy=True,
106+
copy=None,
107107
t_start=0 * pq.s,
108108
spatial_scale=None,
109109
frame_duration=None,
@@ -121,14 +121,20 @@ def __new__(
121121
122122
__array_finalize__ is called on the new object.
123123
"""
124+
125+
if copy is not None:
126+
raise ValueError(
127+
"`copy` is now deprecated in Neo due to removal in NumPy 2.0 and will be removed in 0.15.0."
128+
)
129+
124130
if spatial_scale is None:
125131
raise ValueError("spatial_scale is required")
126132

127133
image_data = np.stack(image_data)
128134
if len(image_data.shape) != 3:
129135
raise ValueError("list doesn't have the correct number of dimensions")
130136

131-
obj = pq.Quantity(image_data, units=units, dtype=dtype, copy=copy).view(cls)
137+
obj = pq.Quantity(image_data, units=units, dtype=dtype).view(cls)
132138
obj.segment = None
133139
# function from analogsignal.py in neo/core directory
134140
obj.sampling_rate = _get_sampling_rate(sampling_rate, frame_duration)
@@ -144,7 +150,7 @@ def __init__(
144150
image_data,
145151
units=pq.dimensionless,
146152
dtype=None,
147-
copy=True,
153+
copy=None,
148154
t_start=0 * pq.s,
149155
spatial_scale=None,
150156
frame_duration=None,

neo/core/irregularlysampledsignal.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _new_IrregularlySampledSignal(
3838
units=None,
3939
time_units=None,
4040
dtype=None,
41-
copy=True,
41+
copy=None,
4242
name=None,
4343
file_origin=None,
4444
description=None,
@@ -93,8 +93,8 @@ class IrregularlySampledSignal(BaseSignal):
9393
dtype: numpy dtype | string | None, default: None
9494
Overrides the signal array dtype
9595
Does not affect the dtype of the times which must be floats
96-
copy: bool, default: True
97-
Whether copy should be set to True when making the quantity array
96+
copy: bool | None, default: None
97+
deprecated and no longer used (for NumPy 2.0 support). Will be removed.
9898
name: str | None, default: None
9999
An optional label for the dataset
100100
description: str | None, default: None
@@ -158,7 +158,7 @@ def __new__(
158158
units=None,
159159
time_units=None,
160160
dtype=None,
161-
copy=True,
161+
copy=None,
162162
name=None,
163163
file_origin=None,
164164
description=None,
@@ -171,6 +171,12 @@ def __new__(
171171
This is called whenever a new :class:`IrregularlySampledSignal` is
172172
created from the constructor, but not when slicing.
173173
"""
174+
175+
if copy is not None:
176+
raise ValueError(
177+
"`copy` is now deprecated in Neo due to removal in NumPy 2.0 and will be removed in 0.15.0."
178+
)
179+
174180
signal = cls._rescale(signal, units=units)
175181
if time_units is None:
176182
if hasattr(times, "units"):
@@ -199,7 +205,7 @@ def __init__(
199205
units=None,
200206
time_units=None,
201207
dtype=None,
202-
copy=True,
208+
copy=None,
203209
name=None,
204210
file_origin=None,
205211
description=None,
@@ -231,7 +237,7 @@ def __reduce__(self):
231237
self.units,
232238
self.times.units,
233239
self.dtype,
234-
True,
240+
None,
235241
self.name,
236242
self.file_origin,
237243
self.description,
@@ -544,6 +550,9 @@ def time_shift(self, t_shift):
544550
(the original :class:`IrregularlySampledSignal` is not modified).
545551
"""
546552
new_sig = deepcopy(self)
553+
# As of numpy 2.0/quantities 0.16 we need to copy the array itself
554+
# in order to be able to time_shift
555+
new_sig.times = self.times.copy()
547556

548557
new_sig.times += t_shift
549558

@@ -588,7 +597,7 @@ def merge(self, other):
588597
merged_annotations = merge_annotations(self.annotations, other.annotations)
589598
kwargs.update(merged_annotations)
590599

591-
signal = self.__class__(self.times, stack, units=self.units, dtype=self.dtype, copy=False, **kwargs)
600+
signal = self.__class__(self.times, stack, units=self.units, dtype=self.dtype, copy=None, **kwargs)
592601
signal.segment = self.segment
593602
signal.array_annotate(**self._merge_array_annotations(other))
594603

@@ -681,7 +690,7 @@ def concatenate(self, other, allow_overlap=False):
681690
times=new_times[sorting],
682691
units=self.units,
683692
dtype=self.dtype,
684-
copy=False,
693+
copy=None,
685694
t_start=t_start,
686695
t_stop=t_stop,
687696
**kwargs,

0 commit comments

Comments
 (0)