|
1 | 1 | import os |
2 | 2 | import numpy as np |
3 | 3 | import matplotlib.pyplot as plt |
4 | | -from typing import List, Optional |
5 | | - |
6 | | - |
7 | | -def _zoom_array(array): |
8 | | - """Apply zoom_around_mask from config if requested.""" |
9 | | - try: |
10 | | - from autoconf import conf |
11 | | - zoom_around_mask = conf.instance["visualize"]["general"]["general"]["zoom_around_mask"] |
12 | | - except Exception: |
13 | | - zoom_around_mask = False |
14 | | - |
15 | | - if zoom_around_mask and hasattr(array, "mask") and not array.mask.is_all_false: |
16 | | - try: |
17 | | - from autoarray.mask.derive.zoom_2d import Zoom2D |
18 | | - return Zoom2D(mask=array.mask).array_2d_from(array=array, buffer=1) |
19 | | - except Exception: |
20 | | - pass |
21 | | - return array |
22 | | - |
23 | | - |
24 | | -def _auto_mask_edge(array) -> Optional[np.ndarray]: |
25 | | - """Return edge-pixel (y, x) coords from array.mask, or None.""" |
26 | | - try: |
27 | | - if not array.mask.is_all_false: |
28 | | - return np.array(array.mask.derive_grid.edge.array) |
29 | | - except AttributeError: |
30 | | - pass |
31 | | - return None |
32 | | - |
33 | | - |
34 | | -def _numpy_lines(lines) -> Optional[List[np.ndarray]]: |
35 | | - """Convert lines (Grid2DIrregular or list) to list of (N,2) numpy arrays.""" |
36 | | - if lines is None: |
37 | | - return None |
38 | | - result = [] |
39 | | - try: |
40 | | - for line in lines: |
41 | | - try: |
42 | | - arr = np.array(line.array if hasattr(line, "array") else line) |
43 | | - if arr.ndim == 2 and arr.shape[1] == 2: |
44 | | - result.append(arr) |
45 | | - except Exception: |
46 | | - pass |
47 | | - except TypeError: |
48 | | - pass |
49 | | - return result or None |
50 | | - |
51 | | - |
52 | | -def _numpy_positions(positions) -> Optional[List[np.ndarray]]: |
53 | | - """Convert positions to list of (N,2) numpy arrays.""" |
54 | | - if positions is None: |
55 | | - return None |
56 | | - try: |
57 | | - arr = np.array(positions.array if hasattr(positions, "array") else positions) |
58 | | - if arr.ndim == 2 and arr.shape[1] == 2: |
59 | | - return [arr] |
60 | | - except Exception: |
61 | | - pass |
62 | | - if isinstance(positions, list): |
63 | | - result = [] |
64 | | - for p in positions: |
65 | | - try: |
66 | | - result.append(np.array(p.array if hasattr(p, "array") else p)) |
67 | | - except Exception: |
68 | | - pass |
69 | | - return result or None |
70 | | - return None |
71 | | - |
72 | | - |
73 | | -def _prepare_array(array): |
74 | | - """Zoom and extract (arr_2d, extent, mask) from an Array2D-like object. |
75 | | -
|
76 | | - Returns a plain (N, M) numpy array suitable for passing to |
77 | | - ``autoarray.plot.plots.array.plot_array``, along with the spatial *extent* |
78 | | - and edge-pixel *mask* overlays. |
79 | | - """ |
80 | | - array = _zoom_array(array) |
81 | | - try: |
82 | | - arr = array.native.array |
83 | | - extent = array.geometry.extent |
84 | | - except AttributeError: |
85 | | - arr = np.asarray(array) |
86 | | - extent = None |
87 | | - mask = _auto_mask_edge(array) if hasattr(array, "mask") else None |
88 | | - return arr, extent, mask |
89 | | - |
90 | | - |
91 | | -def plot_array( |
92 | | - array, |
93 | | - ax=None, |
94 | | - title="", |
95 | | - lines=None, |
96 | | - positions=None, |
97 | | - colormap="jet", |
98 | | - use_log10=False, |
99 | | - vmin=None, |
100 | | - vmax=None, |
101 | | - output_path=None, |
102 | | - output_filename="array", |
103 | | - output_format="png", |
104 | | -): |
105 | | - """Plot an Array2D (or numpy array) via autoarray's plot_array.""" |
106 | | - from autoarray.plot.plots.array import plot_array as _aa_plot_array |
107 | | - |
108 | | - arr, extent, mask = _prepare_array(array) |
109 | | - _aa_plot_array( |
110 | | - array=arr, |
111 | | - ax=ax, |
112 | | - extent=extent, |
113 | | - mask=mask, |
114 | | - positions=positions if isinstance(positions, list) else _numpy_positions(positions), |
115 | | - lines=lines if isinstance(lines, list) else _numpy_lines(lines), |
116 | | - title=title, |
117 | | - colormap=colormap, |
118 | | - use_log10=use_log10, |
119 | | - vmin=vmin, |
120 | | - vmax=vmax, |
121 | | - output_path=output_path if ax is None else None, |
122 | | - output_filename=output_filename, |
123 | | - output_format=output_format, |
124 | | - structure=array, |
125 | | - ) |
126 | | - |
127 | | - |
128 | | -def plot_grid( |
129 | | - grid, |
130 | | - ax=None, |
131 | | - title="", |
132 | | - output_path=None, |
133 | | - output_filename="grid", |
134 | | - output_format="png", |
135 | | -): |
136 | | - """Plot a Grid2D via autoarray's plot_grid.""" |
137 | | - from autoarray.plot.plots.grid import plot_grid as _aa_plot_grid |
138 | | - |
139 | | - _aa_plot_grid( |
140 | | - grid=np.array(grid.array), |
141 | | - ax=ax, |
142 | | - title=title, |
143 | | - output_path=output_path if ax is None else None, |
144 | | - output_filename=output_filename, |
145 | | - output_format=output_format, |
146 | | - ) |
147 | 4 |
|
148 | 5 |
|
149 | 6 | def _to_lines(*items): |
@@ -177,7 +34,6 @@ def _to_positions(*items): |
177 | 34 |
|
178 | 35 | def _save_subplot(fig, output_path, filename, output_format="png"): |
179 | 36 | """Save a subplot figure to disk (or show it when output_path is None).""" |
180 | | - # Normalise: format may be a list (e.g. ['png']) or a plain string. |
181 | 37 | if isinstance(output_format, (list, tuple)): |
182 | 38 | fmts = output_format |
183 | 39 | else: |
|
0 commit comments