Skip to content

Commit b4328ab

Browse files
SWE-bench ComplexAnthony Shaw
authored andcommitted
Human gold patch for astropy__astropy-19335
PR: astropy#19335 Issue: astropy#19216 Base commit: fc0ebad Changed lines: 55
1 parent fc0ebad commit b4328ab

5 files changed

Lines changed: 30 additions & 20 deletions

File tree

‎astropy/io/fits/column.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from textwrap import indent
1515

1616
import numpy as np
17-
from numpy import char as chararray
1817

1918
from astropy.utils import lazyproperty
19+
from astropy.utils.compat import chararray, get_chararray
2020
from astropy.utils.exceptions import AstropyUserWarning
2121

2222
from .card import CARD_LENGTH, Card
@@ -696,14 +696,14 @@ def __init__(
696696
# input arrays can be just list or tuple, not required to be ndarray
697697
# does not include Object array because there is no guarantee
698698
# the elements in the object array are consistent.
699-
if not isinstance(array, (np.ndarray, chararray.chararray, Delayed)):
699+
if not isinstance(array, (np.ndarray, chararray, Delayed)):
700700
try: # try to convert to a ndarray first
701701
if array is not None:
702702
array = np.array(array)
703703
except Exception:
704704
try: # then try to convert it to a strings array
705705
itemsize = int(recformat[1:])
706-
array = chararray.array(array, itemsize=itemsize)
706+
array = get_chararray(array, itemsize=itemsize)
707707
except ValueError:
708708
# then try variable length array
709709
# Note: This includes _FormatQ by inheritance
@@ -1388,7 +1388,7 @@ def _convert_to_valid_data_type(self, array):
13881388
fsize = dims[-1]
13891389
else:
13901390
fsize = np.dtype(format.recformat).itemsize
1391-
return chararray.array(array, itemsize=fsize, copy=False)
1391+
return get_chararray(array, itemsize=fsize, copy=False)
13921392
else:
13931393
return _convert_array(array, np.dtype(format.recformat))
13941394
elif "L" in format:
@@ -2082,7 +2082,7 @@ def __new__(cls, input, dtype="S"):
20822082
try:
20832083
# this handles ['abc'] and [['a','b','c']]
20842084
# equally, beautiful!
2085-
input = [chararray.array(x, itemsize=1) for x in input]
2085+
input = [get_chararray(x, itemsize=1) for x in input]
20862086
except Exception:
20872087
raise ValueError(f"Inconsistent input data array: {input}")
20882088

@@ -2105,10 +2105,10 @@ def __setitem__(self, key, value):
21052105
"""
21062106
if isinstance(value, np.ndarray) and value.dtype == self.dtype:
21072107
pass
2108-
elif isinstance(value, chararray.chararray) and value.itemsize == 1:
2108+
elif isinstance(value, chararray) and value.itemsize == 1:
21092109
pass
21102110
elif self.element_dtype == "S":
2111-
value = chararray.array(value, itemsize=1)
2111+
value = get_chararray(value, itemsize=1)
21122112
else:
21132113
value = np.array(value, dtype=self.element_dtype)
21142114
np.ndarray.__setitem__(self, key, value)
@@ -2262,7 +2262,7 @@ def _makep(array, descr_output, format, nrows=None):
22622262
else:
22632263
rowval = [0] * data_output.max
22642264
if format.dtype == "S":
2265-
data_output[idx] = chararray.array(encode_ascii(rowval), itemsize=1)
2265+
data_output[idx] = get_chararray(encode_ascii(rowval), itemsize=1)
22662266
else:
22672267
data_output[idx] = np.array(rowval, dtype=format.dtype)
22682268

‎astropy/io/fits/fitsrec.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from functools import reduce
99

1010
import numpy as np
11-
from numpy import char as chararray
1211

1312
from astropy.utils import lazyproperty
13+
from astropy.utils.compat import chararray, get_chararray
1414

1515
from .column import (
1616
_VLF,
@@ -831,7 +831,7 @@ def _convert_p(self, column, field, recformat):
831831
dt = np.dtype(recformat.dtype + str(1))
832832
arr_len = count * dt.itemsize
833833
da = raw_data[offset : offset + arr_len].view(dt)
834-
da = np.char.array(da.view(dtype=dt), itemsize=count)
834+
da = get_chararray(da.view(dtype=dt), itemsize=count)
835835
dummy[idx] = decode_ascii(da)
836836
else:
837837
dt = np.dtype(recformat.dtype)
@@ -1204,7 +1204,7 @@ def _scale_back(self, update_heap_pointers=True):
12041204
if isinstance(self._coldefs, _AsciiColDefs):
12051205
self._scale_back_ascii(index, dummy, raw_field)
12061206
# binary table string column
1207-
elif isinstance(raw_field, chararray.chararray):
1207+
elif isinstance(raw_field, chararray):
12081208
self._scale_back_strings(index, dummy, raw_field)
12091209
# all other binary table columns
12101210
else:
@@ -1361,8 +1361,8 @@ def _get_recarray_field(array, key):
13611361
# This is currently needed for backwards-compatibility and for
13621362
# automatic truncation of trailing whitespace
13631363
field = np.recarray.field(array, key)
1364-
if field.dtype.char in ("S", "U") and not isinstance(field, chararray.chararray):
1365-
field = field.view(chararray.chararray)
1364+
if field.dtype.char in ("S", "U") and not isinstance(field, chararray):
1365+
field = field.view(chararray)
13661366
return field
13671367

13681368

‎astropy/io/fits/hdu/table.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from contextlib import suppress
1212

1313
import numpy as np
14-
from numpy import char as chararray
1514

1615
# This module may have many dependencies on astropy.io.fits.column, but
1716
# astropy.io.fits.column has fewer dependencies overall, so it's easier to
@@ -37,6 +36,7 @@
3736
from astropy.io.fits.header import Header, _pad_length
3837
from astropy.io.fits.util import _is_int, _str_to_num, path_like
3938
from astropy.utils import lazyproperty
39+
from astropy.utils.compat import chararray
4040

4141
from .base import DELAYED, ExtensionHDU, _ValidHDU
4242

@@ -1489,7 +1489,7 @@ def _binary_table_byte_swap(data):
14891489
formats.append(field_dtype)
14901490
offsets.append(field_offset)
14911491

1492-
if isinstance(field, chararray.chararray):
1492+
if isinstance(field, chararray):
14931493
continue
14941494

14951495
# only swap unswapped
@@ -1507,7 +1507,7 @@ def _binary_table_byte_swap(data):
15071507
coldata = data.field(idx)
15081508
for c in coldata:
15091509
if (
1510-
not isinstance(c, chararray.chararray)
1510+
not isinstance(c, chararray)
15111511
and c.itemsize > 1
15121512
and c.dtype.str[0] in swap_types
15131513
):

‎astropy/io/fits/tests/test_table.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import numpy as np
1111
import pytest
12-
from numpy import char as chararray
1312

1413
try:
1514
import objgraph
@@ -24,6 +23,7 @@
2423
from astropy.io.fits.verify import VerifyError
2524
from astropy.table import Table
2625
from astropy.units import Unit, UnitsWarning, UnrecognizedUnit
26+
from astropy.utils.compat import get_chararray
2727
from astropy.utils.exceptions import AstropyUserWarning
2828

2929
from .conftest import FitsTestCase
@@ -156,7 +156,7 @@ def test_open(self, home_is_data):
156156
fd = fits.open(self.data("test0.fits"))
157157

158158
# create some local arrays
159-
a1 = chararray.array(["abc", "def", "xx"])
159+
a1 = get_chararray(["abc", "def", "xx"])
160160
r1 = np.array([11.0, 12.0, 13.0], dtype=np.float32)
161161

162162
# create a table from scratch, using a mixture of columns from existing
@@ -319,7 +319,7 @@ def test_ascii_table(self):
319319

320320
# Test Start Column
321321

322-
a1 = chararray.array(["abcd", "def"])
322+
a1 = get_chararray(["abcd", "def"])
323323
r1 = np.array([11.0, 12.0])
324324
c1 = fits.Column(name="abc", format="A3", start=19, array=a1)
325325
c2 = fits.Column(name="def", format="E", start=3, array=r1)
@@ -1970,7 +1970,7 @@ def test_string_column_padding(self):
19701970
"p\x00\x00\x00\x00\x00\x00\x00\x00\x00"
19711971
)
19721972

1973-
acol = fits.Column(name="MEMNAME", format="A10", array=chararray.array(a))
1973+
acol = fits.Column(name="MEMNAME", format="A10", array=get_chararray(a))
19741974
ahdu = fits.BinTableHDU.from_columns([acol])
19751975
assert ahdu.data.tobytes().decode("raw-unicode-escape") == s
19761976
ahdu.writeto(self.temp("newtable.fits"))

‎astropy/utils/compat/numpycompat.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
earlier versions of Numpy.
55
"""
66

7+
import warnings
8+
79
import numpy as np
810

911
from astropy.utils import minversion
@@ -19,6 +21,8 @@
1921
"NUMPY_LT_2_4",
2022
"NUMPY_LT_2_4_1",
2123
"NUMPY_LT_2_5",
24+
"chararray",
25+
"get_chararray",
2226
]
2327

2428
# TODO: It might also be nice to have aliases to these named for specific
@@ -36,3 +40,9 @@
3640

3741

3842
COPY_IF_NEEDED = False if NUMPY_LT_2_0 else None
43+
44+
with warnings.catch_warnings():
45+
warnings.filterwarnings("ignore", category=DeprecationWarning)
46+
47+
get_chararray = np.char.array
48+
chararray = np.char.chararray

0 commit comments

Comments
 (0)