Skip to content

Commit 86b6016

Browse files
authored
Merge pull request materialsinnovation#524 from SvenPVoigt/pair-correlations
Pair correlations
2 parents eabc223 + 611a064 commit 86b6016

11 files changed

+375
-174
lines changed

doc/API.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ API
66

77
.. jinja::
88

9-
{% set functions = ['plot_microstructures', 'generate_delta', 'generate_multiphase', 'generate_checkerboard', 'solve_cahn_hilliard', 'solve_fe', 'coeff_to_real'] | sort %}
9+
{% set functions = ['plot_microstructures', 'generate_delta', 'generate_multiphase', 'generate_checkerboard', 'solve_cahn_hilliard', 'solve_fe', 'coeff_to_real', 'paircorr_from_twopoint'] | sort %}
1010

1111
{% set classes = ['PrimitiveTransformer', 'LegendreTransformer', 'TwoPointCorrelation', 'FlattenTransformer', 'LocalizationRegressor', 'ReshapeTransformer'] | sort %}
1212

notebooks/cahn_hilliard.ipynb

+4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@
226226
}
227227
],
228228
"source": [
229+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
230+
"\n",
229231
"x_data"
230232
]
231233
},
@@ -315,6 +317,8 @@
315317
}
316318
],
317319
"source": [
320+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
321+
"\n",
318322
"y_data"
319323
]
320324
},

notebooks/fiber.ipynb

+4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@
213213
}
214214
],
215215
"source": [
216+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
217+
"\n",
216218
"x_data"
217219
]
218220
},
@@ -278,6 +280,8 @@
278280
}
279281
],
280282
"source": [
283+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
284+
"\n",
281285
"y_data"
282286
]
283287
},

notebooks/intro.ipynb

+37-16
Large diffs are not rendered by default.

notebooks/stress.ipynb

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@
234234
}
235235
],
236236
"source": [
237+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
238+
"\n",
237239
"x_data"
238240
]
239241
},
@@ -297,6 +299,8 @@
297299
}
298300
],
299301
"source": [
302+
"#PYTEST_VALIDATE_IGNORE_OUTPUT\n",
303+
"\n",
300304
"y_data"
301305
]
302306
},

pymks/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .fmks.correlations import FlattenTransformer
2626
from .fmks.correlations import TwoPointCorrelation
2727
from .fmks.data.checkerboard import generate_checkerboard
28+
from .fmks.pair_correlations import paircorr_from_twopoint
2829

2930
try:
3031
import sfepy # noqa: F401
@@ -105,4 +106,5 @@ def get_version():
105106
"FlattenTransformer",
106107
"TwoPointCorrelation",
107108
"generate_checkerboard",
109+
"paircorr_from_twopoint",
108110
]

pymks/fmks/correlations.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,10 @@ def correlations_multiple(data, correlations, periodic_boundary=True, cutoff=Non
245245
246246
>>> data = np.arange(18).reshape(1, 3, 3, 2)
247247
>>> out = correlations_multiple(data, [[0, 1], [1, 1]])
248-
>>> out
249-
dask.array<stack, shape=(1, 3, 3, 2), dtype=float64, chunksize=(1, 3, 3, 1)>
248+
>>> out.shape
249+
(1, 3, 3, 2)
250+
>>> out.chunks
251+
((1,), (3,), (3,), (2,))
250252
>>> answer = np.array([[[58, 62, 58], [94, 98, 94], [58, 62, 58]]]) + 1. / 3.
251253
>>> assert(out.compute()[...,0], answer)
252254
"""

pymks/fmks/func.py

+56-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def wrapper(arr, *args, **kwargs):
283283
chunks = arr.chunks
284284
return func(rechunk(chunks, arr), *args, **kwargs)
285285

286-
return wrapper
286+
return wraps(func)(wrapper)
287287

288288

289289
@curry
@@ -452,3 +452,58 @@ def wrapper(*args, **kwargs):
452452
return func(*args, **kwargs)
453453

454454
return wrapper
455+
456+
457+
def dist_mesh(shape):
458+
"""Calculate a mesh of distances
459+
460+
Assume dx=1 and the center pixel is shifted to the left for odd
461+
shaped axes.
462+
463+
Args:
464+
shape: the shape of the domain
465+
466+
Returns:
467+
an array of distances from the center
468+
469+
>>> assert np.allclose(
470+
... dist_mesh((3, 2, 4)),
471+
... np.sqrt(np.array(
472+
... [[[6, 3, 2, 3],
473+
... [5, 2, 1, 2]],
474+
... [[5, 2, 1, 2],
475+
... [4, 1, 0, 1]],
476+
... [[6, 3, 2, 3],
477+
... [5, 2, 1, 2]]]
478+
... ))
479+
... )
480+
481+
>>> dist_mesh((3, 5, 4)).shape
482+
(3, 5, 4)
483+
484+
"""
485+
486+
center = lambda x: np.reshape(np.array(x) // 2, (len(x),) + (1,) * len(x))
487+
488+
return sequence(
489+
fmap(lambda x: np.linspace(0, x - 1, x)),
490+
lambda x: np.array(np.meshgrid(*x, indexing="ij")),
491+
lambda x: x - center(x.shape[1:]),
492+
lambda x: np.linalg.norm(x, axis=0),
493+
)(shape)
494+
495+
496+
def sort_array(arr):
497+
"""Functional sort
498+
499+
Args:
500+
arr: array to sort
501+
502+
Returns:
503+
tuple of the sorted array and the index ordering
504+
505+
>>> sort_array(np.array([2, 1, 3]))
506+
(array([1, 2, 3]), array([1, 0, 2]))
507+
508+
"""
509+
return sequence(np.argsort, lambda x: (arr[x], x))(arr)

0 commit comments

Comments
 (0)