-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustering.py
1289 lines (1058 loc) · 43.9 KB
/
clustering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import json
from operator import le
import pickle
from pathlib import Path
from dataclasses import dataclass
from itertools import combinations
from logging import getLogger, basicConfig
from typing import Dict, List
import warnings
import numpy as np
from scipy.stats import mode
from matflow import load_workflow
from numpy.lib.shape_base import split
import plotly
import plotly.colors
from scipy.ndimage import zoom
from plotly import graph_objects
from cipher_input import CIPHERInput, InterfaceDefinition, MaterialDefinition
from utilities import (
get_coordinate_grid,
jsonify_dict,
unjsonify_dict,
write_MTEX_EBSD_file,
write_MTEX_JSON_file,
)
from field_viz import get_volumetric_slice, RVEFieldViz, get_plotly_discrete_colour_bar
from quats import quat2euler
logger = getLogger(__name__)
basicConfig(level="INFO", format="%(asctime)s %(levelname)s %(name)s: %(message)s")
@dataclass
class SliceSelection:
increment_idx: int
is_periodic: bool
eye: str
up: str
x: int = None
y: int = None
z: int = None
def run_matlab_script(script_name, args, nargout, script_dir=None):
"""Must be a function file (i.e. a script with a single top level function defined)."""
import matlab.engine
eng = matlab.engine.start_matlab()
if script_dir:
eng.cd(script_dir)
out = getattr(eng, script_name)(*args, nargout=nargout)
eng.quit()
return out
def tile_coordinates(coords):
"""Tile 2D coordinate grid."""
tile_X, tile_Y = np.meshgrid(np.arange(-1, 2), np.arange(-1, 2))
tile_disp = np.concatenate([tile_X[:, :, None], tile_Y[:, :, None]], axis=2)
tile_disp_broadcast = tile_disp.reshape((3, 3, 1, 1, 2))
coords_broadcast = coords.reshape(1, 1, coords.shape[0], coords.shape[1], 2)
coords_tiled = coords_broadcast + tile_disp_broadcast
coords_tiled_rs = np.concatenate(
np.concatenate(
coords_tiled,
axis=1,
).swapaxes(1, 2),
axis=0,
).swapaxes(0, 1)
return coords_tiled_rs
def tile_seed_points(seed_points, grid_size):
"""
Parameters
----------
seed_points : ndarray of shape (N, 2)
grid_size : list or ndarray of length (2,)
Returns
-------
two-tuple of:
tiled_seeds : ndarray of shape (9N, 2)
tiling_idx_lookup : ndarray of shape (9N,)
"""
X, Y = np.meshgrid(np.arange(-1, 2), np.arange(-1, 2))
tiling_arr = np.concatenate([X[:, :, None], Y[:, :, None]], axis=2).reshape(
9, 2
) * np.array(grid_size)
tiled_seeds = np.concatenate(tiling_arr[:, None] + seed_points[None])
tiling_idx_lookup = np.tile(np.arange(seed_points.shape[0]), 9)
return tiled_seeds, tiling_idx_lookup
def periodically_tessellate(seeds, grid_size, grid_coords=None):
"""
Parameters
----------
seeds : ndarray of shape (N, 2)
grid_size : list of length 2
grid_coords, ndarray of shape (M, 2), optional
If not specified, tessellate all grid coordinates defined on the grid of
`grid_size`.
"""
if grid_coords is None:
grid_coords_X, grid_coords_Y = np.meshgrid(
np.arange(0, grid_size[0]),
np.arange(0, grid_size[1]),
)
grid_coords = np.concatenate(
[
grid_coords_X[None],
grid_coords_Y[None],
]
).reshape(2, -1)
seeds_tiled, tile_index = tile_seed_points(seeds, grid_size)
tessellated_coords = []
tessellated_sub_grain_IDs = []
for grid_coord in grid_coords.T:
# Find distance to all tiled seed points:
dist = np.linalg.norm(seeds_tiled - grid_coord[None], axis=1)
closest_periodic_seed_idx = np.argmin(dist)
# Find original index of this seed:
closest_seed_idx = tile_index[closest_periodic_seed_idx]
tessellated_coords.append(grid_coord)
tessellated_sub_grain_IDs.append(closest_seed_idx)
tessellated_coords = np.array(tessellated_coords)
tessellated_sub_grain_IDs = np.array(tessellated_sub_grain_IDs)
return tessellated_coords, tessellated_sub_grain_IDs
def remap_periodic_boundary_IDs(im, IDs_A, IDs_B):
grain_ID_replacement_map = {}
for idx, old_grain_ID in enumerate(IDs_A):
new_grain_ID = IDs_B[idx]
if old_grain_ID not in grain_ID_replacement_map:
grain_ID_replacement_map.update({old_grain_ID: [new_grain_ID]})
else:
grain_ID_replacement_map[old_grain_ID].append(new_grain_ID)
# Count how many pixels should be changed to each new ID:
grain_ID_replacement_map_counts = {
k: {k2: v2 for k2, v2 in zip(*np.unique(v, return_counts=True))}
for k, v in grain_ID_replacement_map.items()
}
# Use the pixel majority to decide the final mapping:
grain_ID_replacement_map_final = {}
for k, v in grain_ID_replacement_map_counts.items():
new_ID_trial_max_count = 0
new_ID_trial = None
for new_ID_i, count_k in v.items():
if count_k > new_ID_trial_max_count:
new_ID_trial = new_ID_i
new_ID_trial_max_count = count_k
grain_ID_replacement_map_final.update({k: new_ID_trial})
# Remove trivial equal replacements:
grain_ID_replacement_map_final = {
k: v for k, v in grain_ID_replacement_map_final.items() if k != v
}
if grain_ID_replacement_map_final:
grain_ID_replace_old, grain_ID_replace_new = zip(
*grain_ID_replacement_map_final.items()
)
im = im.copy()
for replace_idx, old_ID in enumerate(grain_ID_replace_old):
im[im == old_ID] = grain_ID_replace_new[replace_idx]
return im
def partition_sub_grain_seeds_into_grains(sub_grain_seeds, grain_IDs):
"""Assign given seed points to the correct grain IDs.
If a given grain ID does not happen to have any seed points within in, a new single
seed point will be added."""
sub_grain_seeds_idx = []
extra_seeds = []
grain_coords = []
uniq_grain_IDs = np.unique(grain_IDs)
for grain_ID_i in uniq_grain_IDs:
sub_grain_seeds_idx_i = []
coords_i = np.array(np.where(grain_IDs == grain_ID_i))[::-1]
grain_coords.append(coords_i)
# Get only seeds within this grain:
for seed_j_idx, seed_j in enumerate(sub_grain_seeds):
if np.any(np.all(coords_i == seed_j.reshape(2, 1), axis=0)):
sub_grain_seeds_idx_i.append(seed_j_idx)
if not sub_grain_seeds_idx_i:
# Add a single seed point at the first coordinate, for complete tessellation:
extra_seeds.append(coords_i[:, 0])
extra_seed_idx = len(sub_grain_seeds) + len(extra_seeds) - 1
sub_grain_seeds_idx_i.append(extra_seed_idx)
sub_grain_seeds_idx.append(sub_grain_seeds_idx_i)
new_seeds = []
new_sub_grain_seeds_idx = []
is_dummy_seed = np.zeros(sub_grain_seeds.shape[0], dtype=int)
if extra_seeds:
sub_grain_seeds = np.vstack((sub_grain_seeds, extra_seeds))
is_dummy_seed = np.hstack((is_dummy_seed, np.ones(len(extra_seeds), dtype=int)))
# Reorder seeds:
new_is_dummy_seed = []
for sub_grain_seeds_idx_i in sub_grain_seeds_idx:
seeds_i = sub_grain_seeds[sub_grain_seeds_idx_i]
is_dummy_seed_i = is_dummy_seed[sub_grain_seeds_idx_i]
new_sub_grain_seeds_idx.append(
np.arange(len(new_seeds), len(new_seeds) + len(seeds_i))
)
new_seeds.extend(seeds_i)
new_is_dummy_seed.extend(is_dummy_seed_i)
new_seeds = np.array(new_seeds)
new_is_dummy_seed = np.array(new_is_dummy_seed, dtype=bool)
return new_seeds, new_sub_grain_seeds_idx, grain_coords, new_is_dummy_seed
def tessellate_sub_grain_seeds(
sub_grain_seeds,
sub_grain_seeds_idx,
grid_size,
grain_coords,
include_grain_idx=None,
):
sub_grain_IDs = np.ones(grid_size, dtype=int) * -1
for idx, sub_grain_seeds_idx_i in enumerate(sub_grain_seeds_idx):
if include_grain_idx is not None and idx not in include_grain_idx:
continue
tess_coords, tess_sub_grain_IDs = periodically_tessellate(
seeds=sub_grain_seeds[sub_grain_seeds_idx_i],
grid_size=grid_size,
grid_coords=grain_coords[idx],
)
sub_grain_IDs[tess_coords[:, 0], tess_coords[:, 1]] = sub_grain_seeds_idx_i[
tess_sub_grain_IDs
]
return sub_grain_IDs.T
class PhaseFieldModelPreProcessor:
"""Class to perform steps that prepare a phase field model geometry from a crystal-plasticity-deformed RVE."""
def __init__(self, workflow_dir, segmentation_sub_dir="segmentation"):
self.workflow_dir = Path(workflow_dir).resolve()
self.segmentation_sub_dir = self.workflow_dir.joinpath(segmentation_sub_dir)
self.segmentation_sub_dir.mkdir(exist_ok=True)
self.workflow = load_workflow(self.workflow_dir)
@property
def simulate_task(self):
return self.workflow.tasks.simulate_volume_element_loading
@property
def segmentation_dirs(self):
return list(self.workflow_dir.joinpath(self.segmentation_sub_dir).glob("*"))
@staticmethod
def format_segmentation_directory_name(
element_idx, method, slice_selection, **method_kwargs
):
slice_coords = {}
if slice_selection.x is not None:
slice_coords.update({"x": slice_selection.x})
if slice_selection.y is not None:
slice_coords.update({"y": slice_selection.y})
if slice_selection.z is not None:
slice_coords.update({"z": slice_selection.z})
slice_key = list(slice_coords.keys())[0]
slice_str = f"{slice_key}_{slice_coords[slice_key]}"
per_str = "periodic" if slice_selection.is_periodic else "non-periodic"
parametrised_path = (
f"element_{element_idx}__inc_{slice_selection.increment_idx}__"
f"slice_{slice_str}__{per_str}__method_{method}__"
f"eye_{slice_selection.eye}__up_{slice_selection.up}"
)
if method in ["MTEX-FMC", "MTEX-FMC-new"]:
C_Maha = method_kwargs["C_Maha"]
smoothing = method_kwargs["smoothing"]
parametrised_path += f"__C_Maha_{C_Maha:.2f}__smoothing_{smoothing}"
return parametrised_path
def get_clusterer(self, element_idx, method, slice_selection, parameters):
clusterer_method_map = {
"MTEX-FMC": MTEX_FMC_Clusterer,
}
clusterer = clusterer_method_map[method](
pre_processor=self,
element_idx=element_idx,
slice_selection=slice_selection,
method=method,
parameters=parameters,
)
return clusterer
class Clusterer:
"""Class to represent the parametrised clustering process of a deformed RVE.
Attributes
----------
data : dict
Any useful method-specific data that is generated during the clustering.
"""
def __init__(
self,
pre_processor,
element_idx,
method,
slice_selection,
parameters,
):
if not isinstance(slice_selection, SliceSelection):
slice_selection = SliceSelection(**slice_selection)
self.pre_processor = pre_processor
self.element_idx = element_idx
self.slice_selection = slice_selection
self.method = method
self.parameters = parameters
self.seg_dir = self._get_seg_dir()
# set in `prepare_segmentation`:
self.slice_data = None
# set in `do_segmentation`:
self.grain_IDs = None
self.grain_IDs_periodic_image = None # set if periodic
# set in `set_seed_points`:
self.seed_points = None
self.seed_points_args = None
# set in `tessellate_seed_points`:
self.tessellated_sub_grain_IDs = None
self.is_dummy_seed = None
self.sub_grain_seeds_idx = None
self.is_interface_low_angle_GB = None
@property
def element(self):
return self.pre_processor.simulate_task.elements[self.element_idx]
@property
def grid_size(self):
grid_size = self.element.get_parameter_dependency_value("volume_element")[
"grid_size"
]
# could have multiple grid size outputs (?):
if isinstance(grid_size, list) and not isinstance(grid_size[0], int):
grid_size = [i for i in grid_size if isinstance(i, list)][-1]
return grid_size
@property
def size(self):
# TODO: currently size is not a workflow parameter, so hard code this for now!
# return self.element.get_parameter_dependency_value('size')
return [1, 1, 1]
@property
def slice_grid_size(self):
grid_size = []
if self.slice_selection.x is None:
grid_size.append(self.grid_size[0])
if self.slice_selection.y is None:
grid_size.append(self.grid_size[1])
if self.slice_selection.z is None:
grid_size.append(self.grid_size[2])
return grid_size
@property
def slice_size(self):
size = []
if self.slice_selection.x is None:
size.append(self.size[0])
if self.slice_selection.y is None:
size.append(self.size[1])
if self.slice_selection.z is None:
size.append(self.size[2])
return size
def format_segmentations_directory_name(self):
slice_coords = {}
if self.slice_selection.x is not None:
slice_coords.update({"x": self.slice_selection.x})
if self.slice_selection.y is not None:
slice_coords.update({"y": self.slice_selection.y})
if self.slice_selection.z is not None:
slice_coords.update({"z": self.slice_selection.z})
slice_key = list(slice_coords.keys())[0]
slice_str = f"{slice_key}_{slice_coords[slice_key]}"
per_str = "periodic" if self.slice_selection.is_periodic else "non-periodic"
parametrised_path = (
f"element_{self.element_idx}__inc_{self.slice_selection.increment_idx}__"
f"slice_{slice_str}__{per_str}__method_{self.method}__"
f"eye_{self.slice_selection.eye}__up_{self.slice_selection.up}"
)
return parametrised_path
def _get_slice_data(self):
coords, elem_size = get_coordinate_grid(self.slice_size, self.slice_grid_size)
field_data = self.element.outputs.volume_element_response["field_data"]
ori_data = field_data["O"]["data"]["quaternions"]
phase = field_data["phase"]["data"]
ori = ori_data[self.slice_selection.increment_idx]
ori_slice = get_volumetric_slice(
ori,
x=self.slice_selection.x,
y=self.slice_selection.y,
z=self.slice_selection.z,
eye=self.slice_selection.eye,
up=self.slice_selection.up,
)["slice_data"]
phase_slice = get_volumetric_slice(
phase,
x=self.slice_selection.x,
y=self.slice_selection.y,
z=self.slice_selection.z,
eye=self.slice_selection.eye,
up=self.slice_selection.up,
)["slice_data"]
# print(f'phase_slice.shape: {phase_slice.shape}')
if self.slice_selection.is_periodic:
ori_slice = np.tile(ori_slice, (3, 3, 1))
phase_slice = np.tile(phase_slice, (3, 3))
coords = tile_coordinates(coords)
coords_flat = coords.reshape(-1, 2)
phase_flat = phase_slice.reshape(-1) + 1 # index from 1
ori_flat = ori_slice.reshape(-1, 4)
out = {
"coords_flat": coords_flat,
"quats_flat": ori_flat,
"phases_flat": phase_flat,
"phase_names": field_data["phase"]["meta"]["phase_names"],
}
return out
def get_field_data(self, data_name, data_component=None):
field_viz = RVEFieldViz(
volume_element_response=self.element.outputs.volume_element_response,
increment=self.slice_selection.increment_idx,
x=self.slice_selection.x,
y=self.slice_selection.y,
z=self.slice_selection.z,
eye=self.slice_selection.eye,
up=self.slice_selection.up,
data_name=data_name,
data_component=data_component,
)
return field_viz.plot_data
def show_field_data(self, data_name, data_component=None):
field_viz = RVEFieldViz(
volume_element_response=self.element.outputs.volume_element_response,
increment=self.slice_selection.increment_idx,
x=self.slice_selection.x,
y=self.slice_selection.y,
z=self.slice_selection.z,
eye=self.slice_selection.eye,
up=self.slice_selection.up,
data_name=data_name,
data_component=data_component,
)
if data_name == "phase":
colour_bar_info = get_plotly_discrete_colour_bar(
field_viz.data_meta["phase_names"],
plotly.colors.qualitative.D3,
)
colour_bar_info.update(
{
"hovertext": np.array(field_viz.data_meta["phase_names"])[
field_viz.plot_data
]
}
)
else:
colour_bar_info = {
"colorscale": "Viridis",
"colorbar": {},
}
colour_bar_info["colorbar"].update({"title": field_viz.title})
data = [
{
"type": "heatmap",
"z": field_viz.plot_data,
"zmin": field_viz.min_value_inc_slice,
"zmax": field_viz.max_value_inc_slice,
**colour_bar_info,
},
]
layout = {
"template": "none",
# 'paper_bgcolor': 'pink',
# 'plot_bgcolor': 'green',
"margin": {
"t": 50,
"r": 0,
"b": 50,
"l": 0,
},
"height": 500,
"width": 700,
"showlegend": False,
"xaxis": {
"scaleanchor": "y",
"constrain": "domain",
"title": {"text": field_viz.xlabel, "font": {"size": 22}},
"showgrid": False,
"showticklabels": True,
"tickmode": "array",
"tickvals": [0, field_viz.plot_data.shape[1] - 1],
"ticktext": [0, field_viz.plot_data.shape[1] - 1],
},
"yaxis": {
"title": {"text": field_viz.ylabel, "font": {"size": 22}},
"showgrid": False,
"showticklabels": True,
"tickmode": "array",
"tickvals": [0, field_viz.plot_data.shape[0] - 1],
"ticktext": [0, field_viz.plot_data.shape[0] - 1],
"autorange": "reversed", # so top-left is origin (like plt.imshow)
},
}
fig = graph_objects.FigureWidget(data=data, layout=layout)
return fig
@property
def JSON_path(self):
return self.seg_dir.joinpath("clusterer.json")
@property
def pickle_path(self):
return self.seg_dir.joinpath("clusterer.pkl")
@property
def is_segmented(self):
if self.seg_dir.is_dir():
return True
else:
return False
@property
def num_clustered_grains(self):
if not self.is_segmented:
raise ValueError("Not segmented yet; run `do_segmentation` first!")
return len(np.unique(self.grain_IDs))
def load(self, fmt="pickle"):
"""Load clusterer data from file."""
path = {"json": self.JSON_path, "pickle": self.pickle_path}[fmt]
logger.info(f"Loading {self.__class__.__name__!r} from file {str(path)}...")
if not self.is_segmented:
raise ValueError("Not segmented yet; run `do_segmentation` first!")
if fmt == "pickle":
with path.open("rb") as fh:
data = pickle.load(fh)
elif fmt == "json":
with path.open("rt") as fh:
data = json.load(fh)
self.grain_IDs = np.array(data.pop("grain_IDs"))
self.slice_data = unjsonify_dict(data.get("slice_data"))
if "grain_IDs_periodic_image" in data:
self.grain_IDs_periodic_image = np.array(data["grain_IDs_periodic_image"])
if "seed_points" in data:
self.seed_points = np.array(data["seed_points"])
self.seed_points_args = unjsonify_dict(data["seed_points_args"])
if "tessellated_sub_grain_IDs" in data:
self.tessellated_sub_grain_IDs = np.array(data["tessellated_sub_grain_IDs"])
self.is_dummy_seed = np.array(data["is_dummy_seed"])
self.sub_grain_seeds_idx = [
np.array(i) for i in data["sub_grain_seeds_idx"]
]
self.is_interface_low_angle_GB = np.array(data["is_interface_low_angle_GB"])
logger.info("Done.")
def save(self, fmt="pickle"):
path = {"json": self.JSON_path, "pickle": self.pickle_path}[fmt]
logger.info(f"Saving {self.__class__.__name__!r} to file {str(path)}...")
if not self.is_segmented:
return ValueError("Not segmented yet; run `do_segmentation` first!")
data = {
"slice_data": jsonify_dict(self.slice_data),
"grain_IDs": self.grain_IDs.tolist(),
}
if self.grain_IDs_periodic_image is not None:
data["grain_IDs_periodic_image"] = self.grain_IDs_periodic_image.tolist()
if self.seed_points is not None:
data["seed_points"] = self.seed_points.tolist()
data["seed_points_args"] = jsonify_dict(self.seed_points_args)
if self.tessellated_sub_grain_IDs is not None:
data["tessellated_sub_grain_IDs"] = self.tessellated_sub_grain_IDs.tolist()
data["is_dummy_seed"] = self.is_dummy_seed.tolist()
data["sub_grain_seeds_idx"] = [i.tolist() for i in self.sub_grain_seeds_idx]
data["is_interface_low_angle_GB"] = self.is_interface_low_angle_GB.tolist()
if fmt == "pickle":
with path.open("wb") as fh:
pickle.dump(data, fh)
elif fmt == "json":
with path.open("wt") as fh:
json.dump(data, fh, indent=2)
logger.info("Done.")
def _estimate_sub_grain_size(self):
rho = self.estimate_dislocation_density()
K = 1_000
cell_diameter = K / np.sqrt(rho)
cell_density = 1 / cell_diameter**3
def set_seed_points(self, method, pixel_length, redo=False, **kwargs):
if self.seed_points is not None and not redo:
logger.info("Seed points already set; exiting.")
return
self.seed_points_args = {
"method": method,
"pixel_length": pixel_length,
**kwargs,
}
rng = np.random.default_rng(kwargs.get("random_seed"))
if method == "random":
number = kwargs.get("number")
seeds = np.hstack(
[
rng.integers(0, self.slice_grid_size[0], (number, 1)),
rng.integers(0, self.slice_grid_size[1], (number, 1)),
]
)
elif method == "dislocation_density":
# TODO: link to relationship between cell size, L and rho (L ~= 1000/sqrt(rho)) - i.e. 10 microns
# choose probs as 1/L**3 "cell density"
# pixel size as ~0.5 micron? enough to resolve the sub grains
# so we want an L array, then a cell density (i.e. probability array)
# then make a random array (0-1) and choose cells if probability array is less than random array (?)
rho = self.estimate_dislocation_density()
K = 1_000
cell_diameter = K / np.sqrt(rho)
cell_diameter_normed = cell_diameter / pixel_length
cell_density = 1 / cell_diameter_normed**2
prob_field = cell_density.flatten()
number = int(prob_field.sum())
print(f"number of seed points set: {number}")
prob_field /= prob_field.sum() # probabilities array should sum to one
sample_index = rng.choice(
a=prob_field.size,
p=prob_field,
size=(number,),
replace=False, # to avoid coincident seed points!
)
# Take this index and adjust it so it matches the original array
seeds = np.vstack(np.unravel_index(sample_index, cell_density.shape)).T[
:, ::-1
] # swap x-y
self.seed_points = seeds
def estimate_dislocation_density(
self, alpha=0.3, shear_modulus=44e9, burgers_vector=0.3e-9
):
"""
Notes
-----
Equation for dislocation density is Eq. 2.2. from Humphreys & Hatherly (2004),
Chapter 2.
"""
# Get the stress field slice:
stress = self.element.outputs.volume_element_response["field_data"]["sigma_vM"][
"data"
]
stress = stress[self.slice_selection.increment_idx]
stress_data = get_volumetric_slice(
stress,
x=self.slice_selection.x,
y=self.slice_selection.y,
z=self.slice_selection.z,
eye=self.slice_selection.eye,
up=self.slice_selection.up,
)
stress_slice = stress_data["slice_data"]
# TODO: delta rho prop to delta stress?
rho = (stress_slice / (alpha * shear_modulus * burgers_vector)) ** 2
return rho
def tessellate_seed_points(self):
if self.seed_points is None:
raise ValueError("Seed points not set yet; run `set_seed_points` first!")
(
new_sub_grain_seeds,
sub_grain_seeds_idx,
grain_coords,
is_dummy_seed,
) = partition_sub_grain_seeds_into_grains(
self.seed_points,
self.grain_IDs,
)
sub_grain_IDs = tessellate_sub_grain_seeds(
new_sub_grain_seeds,
sub_grain_seeds_idx,
self.slice_grid_size,
grain_coords,
)
self.tessellated_sub_grain_IDs = sub_grain_IDs
self.seed_points = new_sub_grain_seeds
self.is_dummy_seed = is_dummy_seed
self.sub_grain_seeds_idx = sub_grain_seeds_idx
num_cipher_phases = len(np.unique(self.tessellated_sub_grain_IDs))
print(f"num_cipher_phases: {num_cipher_phases}")
is_low_angle_GB = np.zeros((num_cipher_phases, num_cipher_phases), dtype=int)
for grain in self.sub_grain_seeds_idx:
for row_idx, col_idx in combinations(grain, r=2):
is_low_angle_GB[[row_idx, col_idx], [col_idx, row_idx]] = 1
self.is_interface_low_angle_GB = is_low_angle_GB
def show_estimated_dislocation_density(self):
rho = self.estimate_dislocation_density()
fig = graph_objects.FigureWidget(
data=[
{
"type": "heatmap",
"z": rho,
"xaxis": "x1",
"yaxis": "y1",
"coloraxis": "coloraxis",
},
],
layout={
"height": 700,
"coloraxis1": {
"colorscale": "viridis",
},
"xaxis": {
"scaleanchor": "y",
"constrain": "domain",
},
"yaxis": {
"autorange": "reversed",
},
},
)
return fig
def show_tessellated_sub_grain_IDs(self):
fig = graph_objects.FigureWidget(
data=[
{
"type": "heatmap",
"z": self.grain_IDs,
"xaxis": "x1",
"yaxis": "y1",
"coloraxis": "coloraxis",
},
{
"type": "heatmap",
"z": self.tessellated_sub_grain_IDs,
"xaxis": "x2",
"yaxis": "y2",
"coloraxis": "coloraxis2",
},
{
"type": "scatter",
"x": self.seed_points[~np.array(self.is_dummy_seed), 0],
"y": self.seed_points[~np.array(self.is_dummy_seed), 1],
"text": np.arange(self.seed_points.shape[0]),
"name": "sub grain seed points",
"mode": "markers",
"marker": {
"color": "red",
"size": 6,
},
"xaxis": "x2",
"yaxis": "y2",
},
{
"type": "scatter",
"x": self.seed_points[np.array(self.is_dummy_seed), 0],
"y": self.seed_points[np.array(self.is_dummy_seed), 1],
"text": np.arange(self.seed_points.shape[0]),
"name": "sub grain seed points (dummy)",
"mode": "markers",
"marker": {
"color": "red",
"size": 4,
},
"xaxis": "x2",
"yaxis": "y2",
},
],
layout={
"height": 700,
"coloraxis": {
"colorscale": "viridis",
"colorbar": {
"title": "Grain ID",
"x": 0,
},
},
"coloraxis2": {
"colorscale": "viridis",
"colorbar": {
"title": "Sub-grain ID",
},
},
"xaxis": {
"scaleanchor": "y",
"constrain": "domain",
"domain": [0.1, 0.45],
"title": "Clustered grain IDs",
},
"xaxis2": {
"scaleanchor": "y",
"constrain": "domain",
"domain": [0.55, 0.9],
"title": "Tessellated sub grain IDs",
},
"yaxis": {
"autorange": "reversed",
},
"yaxis2": {
"anchor": "x2",
"scaleanchor": "y",
"autorange": "reversed",
},
},
)
return fig
def prepare_segmentation(self, redo=False):
if self.is_segmented and not redo:
logger.info("Already segmented; exiting.")
return
logger.info("Running segmentation...")
self.slice_data = self._get_slice_data()
self.seg_dir.mkdir(exist_ok=True)
return self.seg_dir
def get_cipher_input(
self,
materials: List[MaterialDefinition],
interfaces: List[InterfaceDefinition],
components: List,
outputs: List,
solution_parameters: Dict,
grid_size_power_of_two: int = 8,
intra_material_interface_segmented_label: str = "segmented",
intra_material_interface_tessellated_label: str = "tessellated",
interface_scale: int = 4,
):
if self.seed_points is None:
raise ValueError("Seed points not set yet; run `set_seed_points` first!")
cipher_maps = self.get_cipher_maps(
grid_size_power_of_two=grid_size_power_of_two,
intra_material_interface_segmented_label=intra_material_interface_segmented_label,
intra_material_interface_tessellated_label=intra_material_interface_tessellated_label,
)
defined_interfaces_by_name = {i.name: i for i in interfaces}
defined_interfaces_present = {i.name: False for i in interfaces}
all_interfaces = []
for interface in cipher_maps["interfaces"]:
mats = interface["materials"]
type_label = interface.get("type_label")
int_name = InterfaceDefinition.get_name(mats, type_label)
if int_name not in defined_interfaces_by_name:
raise ValueError(
f"Missing interface properties for interface name {int_name!r}."
) # TODO: test raise
int_phase_pairs = interface.get("phase_pairs")
defined_interfaces_by_name[int_name].phase_pairs = int_phase_pairs
all_interfaces.append(defined_interfaces_by_name[int_name])
defined_interfaces_present[int_name] = True
for int_name, is_present in defined_interfaces_present.items():
if not is_present:
warnings.warn(f"Defined interface {int_name!r} is not present.")
# materials are ordered the same as the DAMASK phases:
all_materials = []
defined_materials_by_name = {i.name: i for i in materials}
for k_idx, k in enumerate(cipher_maps["materials"]):
if k not in defined_materials_by_name:
raise ValueError(
f"Missing material definition for material name {k!r}."
) # TODO: test raise
phases_mat_k = np.where(cipher_maps["phase_material"] == k_idx)[0]
if not phases_mat_k.size:
warnings.warn(f"Defined material {k!r} has no phases.")