Skip to content

Commit 5cf7f6a

Browse files
authored
clib.conversion: Remove the as_c_contiguous function and use np.ascontiguousarray instead (#3492)
1 parent 331998d commit 5cf7f6a

File tree

2 files changed

+3
-46
lines changed

2 files changed

+3
-46
lines changed

pygmt/clib/conversion.py

+2-44
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def dataarray_to_matrix(grid):
123123
inc = [abs(i) for i in inc]
124124
grid = grid.sortby(variables=list(grid.dims), ascending=True)
125125

126-
matrix = as_c_contiguous(grid[::-1].to_numpy())
126+
matrix = np.ascontiguousarray(grid[::-1].to_numpy())
127127
region = [float(i) for i in region]
128128
inc = [float(i) for i in inc]
129129
return matrix, region, inc
@@ -200,53 +200,11 @@ def vectors_to_arrays(vectors):
200200
for vector in vectors:
201201
vec_dtype = str(getattr(vector, "dtype", ""))
202202
array = np.asarray(a=vector, dtype=dtypes.get(vec_dtype))
203-
arrays.append(as_c_contiguous(array))
203+
arrays.append(np.ascontiguousarray(array))
204204

205205
return arrays
206206

207207

208-
def as_c_contiguous(array):
209-
"""
210-
Ensure a numpy array is C contiguous in memory.
211-
212-
If the array is not C contiguous, a copy will be necessary.
213-
214-
Parameters
215-
----------
216-
array : 1-D array
217-
The numpy array
218-
219-
Returns
220-
-------
221-
array : 1-D array
222-
Array is C contiguous order.
223-
224-
Examples
225-
--------
226-
227-
>>> import numpy as np
228-
>>> data = np.array([[1, 2], [3, 4], [5, 6]])
229-
>>> x = data[:, 0]
230-
>>> x
231-
array([1, 3, 5])
232-
>>> x.flags.c_contiguous
233-
False
234-
>>> new_x = as_c_contiguous(x)
235-
>>> new_x
236-
array([1, 3, 5])
237-
>>> new_x.flags.c_contiguous
238-
True
239-
>>> x = np.array([8, 9, 10])
240-
>>> x.flags.c_contiguous
241-
True
242-
>>> as_c_contiguous(x).flags.c_contiguous
243-
True
244-
"""
245-
if not array.flags.c_contiguous:
246-
return array.copy(order="C")
247-
return array
248-
249-
250208
def sequence_to_ctypes_array(
251209
sequence: Sequence | None, ctype, size: int
252210
) -> ctp.Array | None:

pygmt/clib/session.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import xarray as xr
2020
from pygmt.clib.conversion import (
2121
array_to_datetime,
22-
as_c_contiguous,
2322
dataarray_to_matrix,
2423
sequence_to_ctypes_array,
2524
strings_to_ctypes_array,
@@ -1499,7 +1498,7 @@ def virtualfile_from_matrix(self, matrix):
14991498
# collected and the memory freed. Creating it in this context manager
15001499
# guarantees that the copy will be around until the virtual file is
15011500
# closed.
1502-
matrix = as_c_contiguous(matrix)
1501+
matrix = np.ascontiguousarray(matrix)
15031502
rows, columns = matrix.shape
15041503

15051504
family = "GMT_IS_DATASET|GMT_VIA_MATRIX"

0 commit comments

Comments
 (0)