Skip to content

Commit b5b2cfb

Browse files
committed
Fix Windows dtype issue and apply black formatting
- Replace 'long' with 'cnp.int64_t' to fix cross-platform compatibility - On Windows, 'long' is 32-bit while np.int64 is 64-bit causing mismatch - Apply black formatting to Python files
1 parent d0b1267 commit b5b2cfb

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

package/MDAnalysis/analysis/rdf.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ def __init__(
272272

273273
if self.norm not in ["rdf", "density", "none"]:
274274
raise ValueError(
275-
f"'{self.norm}' is an invalid norm. "
276-
"Use 'rdf', 'density' or 'none'."
275+
f"'{self.norm}' is an invalid norm. " "Use 'rdf', 'density' or 'none'."
277276
)
278277

279278
self.backend = backend
@@ -603,9 +602,7 @@ def __init__(
603602
backend="serial",
604603
**kwargs,
605604
):
606-
super(InterRDF_s, self).__init__(
607-
ags[0][0].universe.trajectory, **kwargs
608-
)
605+
super(InterRDF_s, self).__init__(ags[0][0].universe.trajectory, **kwargs)
609606

610607
warnings.warn(
611608
"The `u` attribute is superflous and will be removed "
@@ -619,8 +616,7 @@ def __init__(
619616

620617
if self.norm not in ["rdf", "density", "none"]:
621618
raise ValueError(
622-
f"'{self.norm}' is an invalid norm. "
623-
"Use 'rdf', 'density' or 'none'."
619+
f"'{self.norm}' is an invalid norm. " "Use 'rdf', 'density' or 'none'."
624620
)
625621

626622
if density:

package/MDAnalysis/lib/c_histogram.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ __all__ = ['histogram', 'OPENMP_ENABLED']
6464
cdef void _histogram_serial(
6565
const double[::1] distances,
6666
uint64_t n,
67-
long[::1] hist,
67+
cnp.int64_t[::1] hist,
6868
int nbins,
6969
double bin_width,
7070
double min_val,
@@ -79,7 +79,7 @@ cdef void _histogram_serial(
7979
1D memory view of distances
8080
n : uint64_t
8181
Number of distances
82-
hist : long[::1]
82+
hist : cnp.int64_t[::1]
8383
Output histogram array
8484
nbins : int
8585
Number of bins
@@ -108,12 +108,12 @@ cdef void _histogram_serial(
108108
cdef void _histogram_parallel(
109109
const double[::1] distances,
110110
uint64_t n,
111-
long[::1] hist,
111+
cnp.int64_t[::1] hist,
112112
int nbins,
113113
double bin_width,
114114
double min_val,
115115
double max_val,
116-
long[:, ::1] partial_hists,
116+
cnp.int64_t[:, ::1] partial_hists,
117117
int num_threads
118118
) noexcept nogil:
119119
"""
@@ -128,7 +128,7 @@ cdef void _histogram_parallel(
128128
1D memory view of distances
129129
n : uint64_t
130130
Number of distances
131-
hist : long[::1]
131+
hist : cnp.int64_t[::1]
132132
Output histogram array
133133
nbins : int
134134
Number of bins
@@ -138,7 +138,7 @@ cdef void _histogram_parallel(
138138
Minimum value of range
139139
max_val : double
140140
Maximum value of range
141-
partial_hists : long[:, ::1]
141+
partial_hists : cnp.int64_t[:, ::1]
142142
Preallocated array for thread-local histograms
143143
num_threads : int
144144
Number of OpenMP threads to use
@@ -250,19 +250,19 @@ def histogram(
250250
distances = distances.astype(np.float64)
251251

252252
# Create output arrays
253-
cdef cnp.ndarray[long, ndim=1] hist = np.zeros(nbins, dtype=np.int64)
253+
cdef cnp.ndarray[cnp.int64_t, ndim=1] hist = np.zeros(nbins, dtype=np.int64)
254254
cdef cnp.ndarray[double, ndim=1] edges = np.linspace(min_val, max_val, nbins + 1)
255255

256256
# Create memory views for efficient access
257257
cdef const double[::1] dist_view = distances
258-
cdef long[::1] hist_view = hist
258+
cdef cnp.int64_t[::1] hist_view = hist
259259

260260
# Declare variables for parallel execution
261261
cdef int num_threads = 0 # 0 means use OpenMP default
262262
cdef uint64_t chunk_size
263263
cdef uint64_t n_chunks
264-
cdef cnp.ndarray[long, ndim=2] partial_hists_arr
265-
cdef long[:, ::1] partial_hists_view
264+
cdef cnp.ndarray[cnp.int64_t, ndim=2] partial_hists_arr
265+
cdef cnp.int64_t[:, ::1] partial_hists_view
266266

267267
if use_parallel:
268268
# Calculate number of chunks and allocate partial histograms

testsuite/MDAnalysisTests/lib/test_c_histogram.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def test_correctness_uniform(self):
3131
data = np.random.uniform(0, 15, 10000).astype(np.float64)
3232

3333
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
34-
opt_hist, opt_edges = histogram(
35-
data, bins=75, range_vals=(0.0, 15.0)
36-
)
34+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
3735

3836
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
3937
assert_allclose(np_edges, opt_edges, rtol=1e-14)
@@ -44,9 +42,7 @@ def test_correctness_normal(self):
4442
data = np.random.normal(7.5, 2, 10000).clip(0, 15).astype(np.float64)
4543

4644
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
47-
opt_hist, opt_edges = histogram(
48-
data, bins=75, range_vals=(0.0, 15.0)
49-
)
45+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
5046

5147
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
5248
assert_allclose(np_edges, opt_edges, rtol=1e-14)
@@ -56,9 +52,7 @@ def test_edge_cases_zeros(self):
5652
data = np.zeros(1000, dtype=np.float64)
5753

5854
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
59-
opt_hist, opt_edges = histogram(
60-
data, bins=75, range_vals=(0.0, 15.0)
61-
)
55+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
6256

6357
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
6458
assert_allclose(np_edges, opt_edges, rtol=1e-14)
@@ -68,9 +62,7 @@ def test_edge_cases_max_values(self):
6862
data = np.ones(1000, dtype=np.float64) * 14.999
6963

7064
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
71-
opt_hist, opt_edges = histogram(
72-
data, bins=75, range_vals=(0.0, 15.0)
73-
)
65+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
7466

7567
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
7668
assert_allclose(np_edges, opt_edges, rtol=1e-14)
@@ -80,9 +72,7 @@ def test_boundary_values(self):
8072
data = np.array([0.0, 14.999, 15.0, 7.5] * 250, dtype=np.float64)
8173

8274
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
83-
opt_hist, opt_edges = histogram(
84-
data, bins=75, range_vals=(0.0, 15.0)
85-
)
75+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
8676

8777
# Allow for small differences at boundaries due to floating point precision
8878
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
@@ -93,9 +83,7 @@ def test_bin_edges_values(self):
9383
data = np.linspace(0, 15, 1001, dtype=np.float64)
9484

9585
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
96-
opt_hist, opt_edges = histogram(
97-
data, bins=75, range_vals=(0.0, 15.0)
98-
)
86+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
9987

10088
# Allow for small differences at boundaries due to floating point precision
10189
assert_allclose(np_hist, opt_hist, rtol=1e-14, atol=1)
@@ -122,12 +110,8 @@ def test_different_bin_counts(self):
122110
data = np.random.random(10000).astype(np.float64) * 15.0
123111

124112
for bins in [10, 50, 100, 200]:
125-
np_hist, np_edges = np.histogram(
126-
data, bins=bins, range=(0.0, 15.0)
127-
)
128-
opt_hist, opt_edges = histogram(
129-
data, bins=bins, range_vals=(0.0, 15.0)
130-
)
113+
np_hist, np_edges = np.histogram(data, bins=bins, range=(0.0, 15.0))
114+
opt_hist, opt_edges = histogram(data, bins=bins, range_vals=(0.0, 15.0))
131115

132116
assert_allclose(
133117
np_hist,
@@ -150,9 +134,7 @@ def test_different_ranges(self):
150134

151135
for range_val in [(0.0, 10.0), (0.0, 20.0), (5.0, 15.0)]:
152136
np_hist, np_edges = np.histogram(data, bins=75, range=range_val)
153-
opt_hist, opt_edges = histogram(
154-
data, bins=75, range_vals=range_val
155-
)
137+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=range_val)
156138

157139
assert_allclose(
158140
np_hist,
@@ -177,9 +159,7 @@ def test_non_contiguous_array(self):
177159

178160
assert not data_non_contig.flags["C_CONTIGUOUS"]
179161

180-
np_hist, np_edges = np.histogram(
181-
data_non_contig, bins=75, range=(0.0, 15.0)
182-
)
162+
np_hist, np_edges = np.histogram(data_non_contig, bins=75, range=(0.0, 15.0))
183163
opt_hist, opt_edges = histogram(
184164
data_non_contig, bins=75, range_vals=(0.0, 15.0)
185165
)
@@ -194,9 +174,7 @@ def test_scaling(self, size):
194174
data = np.random.random(size).astype(np.float64) * 15.0
195175

196176
np_hist, np_edges = np.histogram(data, bins=75, range=(0.0, 15.0))
197-
opt_hist, opt_edges = histogram(
198-
data, bins=75, range_vals=(0.0, 15.0)
199-
)
177+
opt_hist, opt_edges = histogram(data, bins=75, range_vals=(0.0, 15.0))
200178

201179
assert_allclose(
202180
np_hist,

0 commit comments

Comments
 (0)