|
16 | 16 | }
|
17 | 17 |
|
18 | 18 |
|
19 |
| -def plot_nanoparticle_from_arrays(radii: np.array, |
20 |
| - concentrations: np.array, |
21 |
| - dpi=150, |
22 |
| - as_np_array=False, |
23 |
| - elements=['Yb', 'Er', 'Nd']): |
| 19 | +def plot_nanoparticle(radii: np.ndarray | list[NanoParticleConstraint], |
| 20 | + concentrations: np.array = None, |
| 21 | + dopant_specifications: list[tuple] = None, |
| 22 | + dpi=150, |
| 23 | + as_np_array=False, |
| 24 | + elements=['Yb', 'Er', 'Nd'], |
| 25 | + ax: plt.Axes = None, |
| 26 | + emissions: float = None): |
24 | 27 | if 'Y' not in elements:
|
| 28 | + # Add Y, the host element |
25 | 29 | elements = elements + ['Y']
|
26 | 30 |
|
27 |
| - # Fill in the concentrations with Y |
28 |
| - concentrations_with_y = np.concatenate( |
29 |
| - (concentrations, 1 - concentrations.sum(axis=1, keepdims=True)), |
30 |
| - axis=1) |
31 |
| - |
| 31 | + if isinstance(radii[0], NanoParticleConstraint): |
| 32 | + # Convert this to an array |
| 33 | + radii = np.array([0] + [c.radius for c in radii]) |
| 34 | + if not isinstance(radii, np.ndarray): |
| 35 | + # If it is a list, it is already in the format we require |
| 36 | + raise TypeError( |
| 37 | + 'radii should be an array of radii or list of contraints') |
| 38 | + |
| 39 | + if concentrations is None and dopant_specifications is None: |
| 40 | + raise RuntimeError( |
| 41 | + 'Must specify one of concentrations or dopant specifications') |
| 42 | + elif dopant_specifications is not None: |
| 43 | + # convert this to an array |
| 44 | + n_layers = len(radii) - 1 |
| 45 | + dopant_dict = [{key: 0 for key in elements} for _ in range(n_layers)] |
| 46 | + for dopant in dopant_specifications: |
| 47 | + dopant_dict[dopant[0]][dopant[2]] = dopant[1] |
| 48 | + |
| 49 | + # Fill in the rest with 'Y' |
| 50 | + for layer in dopant_dict: |
| 51 | + layer['Y'] = 1 - sum(layer.values()) |
| 52 | + |
| 53 | + vals = [[layer[el] for el in elements] for layer in dopant_dict] |
| 54 | + concentrations = np.array(vals) |
| 55 | + elif concentrations is not None: |
| 56 | + # Add Y into the list |
| 57 | + if len(elements) != concentrations.shape[1]: |
| 58 | + concentrations = np.concatenate( |
| 59 | + (concentrations, |
| 60 | + 1 - concentrations.sum(axis=1, keepdims=True)), |
| 61 | + axis=1) |
| 62 | + |
| 63 | + concentrations = np.clip(concentrations, 0, 1) |
32 | 64 | colors = [
|
33 | 65 | DEFAULT_COLOR_MAP[el]
|
34 | 66 | if el in DEFAULT_COLOR_MAP else DEFAULT_COLOR_MAP['Other']
|
35 | 67 | for el in elements
|
36 | 68 | ]
|
37 |
| - # cmap = plt.colormaps["tab10"] |
38 |
| - # colors = cmap(np.arange(4)) |
39 |
| - # # colors[:3] = colors[1:] |
40 |
| - # colors[-1] = [1, 1, 1, 1] |
41 |
| - |
42 |
| - fig = plt.figure(figsize=(5, 5), dpi=dpi) |
43 |
| - ax = fig.subplots() |
44 | 69 |
|
45 |
| - for i in range(concentrations.shape[0], 0, -1): |
46 |
| - ax.pie(concentrations_with_y[i - 1], |
47 |
| - radius=radii[i] / radii[-1], |
48 |
| - colors=colors, |
49 |
| - wedgeprops=dict(edgecolor='k', linewidth=0.25), |
50 |
| - startangle=90) |
51 |
| - ax.legend(elements, loc='upper left', bbox_to_anchor=(0.84, 0.95)) |
52 |
| - plt.tight_layout() |
53 |
| - if as_np_array: |
54 |
| - # If we haven't already shown or saved the plot, then we need to |
55 |
| - # draw the figure first. |
56 |
| - fig.canvas.draw() |
57 |
| - |
58 |
| - # Now we can save it to a numpy array. |
59 |
| - data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) |
60 |
| - data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, )) |
61 |
| - |
62 |
| - # Close the figure to remove it from the buffer |
63 |
| - plt.close(fig) |
64 |
| - return data |
| 70 | + if ax is None: |
| 71 | + # make a new axis |
| 72 | + fig = plt.figure(figsize=(5, 5), dpi=dpi) |
| 73 | + ax = fig.subplots() |
| 74 | + |
| 75 | + for i in range(concentrations.shape[0], 0, -1): |
| 76 | + ax.pie(concentrations[i - 1], |
| 77 | + radius=radii[i] / radii[-1], |
| 78 | + colors=colors, |
| 79 | + wedgeprops=dict(edgecolor='w', linewidth=0.25), |
| 80 | + startangle=90) |
| 81 | + ax.legend(elements, loc='upper left', bbox_to_anchor=(0.84, 0.95)) |
| 82 | + if emissions: |
| 83 | + plt.text(0.1, |
| 84 | + 0.95, |
| 85 | + f'UV Intensity={np.power(10, -emissions)-100:.2f}', |
| 86 | + fontsize=20, |
| 87 | + transform=plt.gca().transAxes) |
| 88 | + plt.tight_layout() |
| 89 | + if as_np_array: |
| 90 | + # If we haven't already shown or saved the plot, then we need to |
| 91 | + # draw the figure first. |
| 92 | + fig.canvas.draw() |
| 93 | + |
| 94 | + # Now we can save it to a numpy array. |
| 95 | + data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) |
| 96 | + data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, )) |
| 97 | + |
| 98 | + # Close the figure to remove it from the buffer |
| 99 | + plt.close(fig) |
| 100 | + return data |
| 101 | + else: |
| 102 | + return fig |
65 | 103 | else:
|
66 |
| - return fig |
67 |
| - |
68 |
| - |
69 |
| -def plot_nanoparticle(constraints, |
70 |
| - dopant_specifications, |
71 |
| - dpi=150, |
72 |
| - as_np_array=False, |
73 |
| - elements=['Yb', 'Er', 'Nd']): |
74 |
| - if 'Y' not in elements: |
75 |
| - elements = elements + ['Y'] |
76 |
| - |
77 |
| - n_layers = len(constraints) |
78 |
| - radii = [0] + [constraint.radius for constraint in constraints] |
79 |
| - dopant_dict = [{key: 0 for key in elements} for _ in range(n_layers)] |
80 |
| - for dopant in dopant_specifications: |
81 |
| - dopant_dict[dopant[0]][dopant[2]] = dopant[1] |
82 |
| - |
83 |
| - # Fill in the rest with 'Y' |
84 |
| - for layer in dopant_dict: |
85 |
| - layer['Y'] = 1 - sum(layer.values()) |
86 |
| - |
87 |
| - vals = [[layer[el] for el in elements] for layer in dopant_dict] |
88 |
| - |
89 |
| - return plot_nanoparticle_from_arrays(np.array(radii), |
90 |
| - np.array(vals), |
91 |
| - dpi=dpi, |
92 |
| - as_np_array=as_np_array, |
93 |
| - elements=elements) |
94 |
| - |
95 |
| - |
96 |
| -def plot_nanoparticle_on_ax(ax, |
97 |
| - constraints, |
98 |
| - dopant_specifications, |
99 |
| - elements=['Yb', 'Er', 'Nd']): |
100 |
| - if 'Y' not in elements: |
101 |
| - elements = ['Y'] + elements |
102 |
| - |
103 |
| - n_layers = len(constraints) |
104 |
| - radii = [constraint.radius for constraint in constraints] |
105 |
| - dopant_dict = [{key: 0 for key in elements} for _ in range(n_layers)] |
106 |
| - for dopant in dopant_specifications: |
107 |
| - dopant_dict[dopant[0]][dopant[2]] = dopant[1] |
108 |
| - # Fill in the rest with 'Y' |
109 |
| - for layer in dopant_dict: |
110 |
| - layer['Y'] = np.round(1 - sum(layer.values()), 3) |
111 |
| - |
112 |
| - vals = [[layer[el] for el in elements] for layer in dopant_dict] |
113 |
| - cmap = plt.colormaps["tab10"] |
114 |
| - colors = cmap(np.arange(4) * 4) |
115 |
| - colors[0] = [1, 1, 1, 1] |
116 |
| - |
117 |
| - for i in list(range(n_layers - 1, -1, -1)): |
118 |
| - # print(vals[i]) |
119 |
| - ax.pie(vals[i], |
120 |
| - radius=radii[i] / radii[-1], |
121 |
| - colors=colors, |
122 |
| - wedgeprops=dict(edgecolor='k'), |
123 |
| - startangle=90) |
124 |
| - ax.legend(elements, loc='upper left', bbox_to_anchor=(1, 1)) |
| 104 | + for i in range(concentrations.shape[0], 0, -1): |
| 105 | + ax.pie(concentrations[i - 1], |
| 106 | + radius=radii[i] / radii[-1], |
| 107 | + colors=colors, |
| 108 | + wedgeprops=dict(edgecolor='w', linewidth=0.25), |
| 109 | + startangle=90) |
| 110 | + ax.legend(elements, loc='upper left', bbox_to_anchor=(0.84, 0.95)) |
| 111 | + if emissions: |
| 112 | + plt.text(0.1, |
| 113 | + 0.95, |
| 114 | + f'UV Intensity={np.power(10, -emissions)-100:.2f}', |
| 115 | + fontsize=20, |
| 116 | + transform=plt.gca().transAxes) |
125 | 117 |
|
126 | 118 |
|
127 | 119 | def update(data, ax):
|
128 |
| - constraints, dopants = data |
129 | 120 | ax.clear()
|
130 |
| - plot_nanoparticle_on_ax(ax, constraints, dopants) |
| 121 | + plot_nanoparticle(ax=ax, **data) |
131 | 122 |
|
132 | 123 |
|
133 | 124 | def make_animation(frames: List[Tuple[NanoParticleConstraint, Tuple]],
|
134 | 125 | name: str = 'animation.mp4',
|
135 |
| - fps: int = 30) -> None: |
| 126 | + fps: int = 30, |
| 127 | + dpi: int = 300) -> None: |
136 | 128 |
|
137 |
| - fig = plt.figure(dpi=150) |
| 129 | + fig = plt.figure(dpi=dpi) |
138 | 130 | ax = fig.subplots()
|
139 | 131 | anim = animation.FuncAnimation(fig, partial(update, ax=ax), frames=frames)
|
140 | 132 | anim.save(name, fps=fps)
|
|
0 commit comments