|
| 1 | +""" |
| 2 | +Collection of private functions to load properties and construct widgets. |
| 3 | +""" |
| 4 | + |
| 5 | +__all__: list[str] = [] |
| 6 | + |
| 7 | +import json |
| 8 | +import warnings |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import astropy.units as u |
| 12 | +from ipywidgets import widgets |
| 13 | + |
| 14 | +from plasmapy_calculator.widget_helpers import ( |
| 15 | + _calculate_button, |
| 16 | + _CheckBox, |
| 17 | + _clear_button, |
| 18 | + _create_label, |
| 19 | + _create_widget, |
| 20 | + _FloatBox, |
| 21 | + _FunctionInfo, |
| 22 | + _IonBox, |
| 23 | + _ParticleBox, |
| 24 | + _process_queue, |
| 25 | +) |
| 26 | + |
| 27 | +warnings.filterwarnings( |
| 28 | + action="ignore", |
| 29 | + message="Passing unrecognized arguments", |
| 30 | + category=DeprecationWarning, |
| 31 | +) # see issue 2370 for a deprecation warning from traitlets |
| 32 | + |
| 33 | +LIGHT_BLUE = "#00BFD8" |
| 34 | +""" |
| 35 | +LIGHT_BLUE: Constant for light blue color 00BFD8 |
| 36 | +""" |
| 37 | +LIGHT_GRAY = "#A9A9A9" |
| 38 | +""" |
| 39 | +LIGHT_GRAY: Constant for light gray color A9A9A9 |
| 40 | +""" |
| 41 | + |
| 42 | +grid_data = [ |
| 43 | + [ |
| 44 | + _create_label("Parameter", color=LIGHT_BLUE), |
| 45 | + _create_label("Value", color=LIGHT_BLUE), |
| 46 | + _create_label("Unit", color=LIGHT_BLUE), |
| 47 | + ], |
| 48 | + [ |
| 49 | + _create_label("B - Magnetic Field Magnitude:"), |
| 50 | + *_create_widget( |
| 51 | + _FloatBox, |
| 52 | + property_name="B", |
| 53 | + unit=u.T, |
| 54 | + opts=[u.T, u.G, u.uG], |
| 55 | + ), |
| 56 | + ], |
| 57 | + [ |
| 58 | + _create_label("Particle:"), |
| 59 | + _create_widget( |
| 60 | + _ParticleBox, |
| 61 | + property_name="particle", |
| 62 | + property_alias="particle_type", |
| 63 | + placeholder="Enter particle e.g. neutron,He", |
| 64 | + ), |
| 65 | + ], |
| 66 | + [ |
| 67 | + _create_label("Ion:"), |
| 68 | + _create_widget( |
| 69 | + _IonBox, |
| 70 | + property_name="ion", |
| 71 | + property_alias="ion_type", |
| 72 | + placeholder="Enter ion e.g. He 2+", |
| 73 | + ), |
| 74 | + ], |
| 75 | + [ |
| 76 | + _create_label("Convert to Hertz:"), |
| 77 | + _create_widget(_CheckBox, property_name="to_hz"), |
| 78 | + ], |
| 79 | + [ |
| 80 | + _create_label("_" * 20, color=LIGHT_BLUE), |
| 81 | + _create_label("Density Number"), |
| 82 | + _create_label("_" * 20, color=LIGHT_BLUE), |
| 83 | + ], |
| 84 | + [ |
| 85 | + _create_label("n - Standard Density Number:"), |
| 86 | + *_create_widget( |
| 87 | + _FloatBox, |
| 88 | + property_name="n", |
| 89 | + unit=u.m**-3, |
| 90 | + opts=[u.m**-3, u.cm**-3, u.mm**-3], |
| 91 | + ), |
| 92 | + ], |
| 93 | + [ |
| 94 | + _create_label("n<sub>e</sub> - Electron Density Number:"), |
| 95 | + *_create_widget( |
| 96 | + _FloatBox, |
| 97 | + property_name="n_e", |
| 98 | + unit=u.m**-3, |
| 99 | + opts=[u.m**-3, u.cm**-3, u.mm**-3], |
| 100 | + ), |
| 101 | + ], |
| 102 | + [ |
| 103 | + _create_label("n<sub>i</sub> - Ion Number Density:"), |
| 104 | + *_create_widget( |
| 105 | + _FloatBox, |
| 106 | + property_name="n_i", |
| 107 | + unit=u.m**-3, |
| 108 | + opts=[u.m**-3, u.cm**-3, u.mm**-3], |
| 109 | + ), |
| 110 | + ], |
| 111 | + [ |
| 112 | + _create_label("_" * 20, color=LIGHT_BLUE), |
| 113 | + _create_label("Temperature"), |
| 114 | + _create_label("_" * 20, color=LIGHT_BLUE), |
| 115 | + ], |
| 116 | + [ |
| 117 | + _create_label("T - Standard Temperature:"), |
| 118 | + _create_widget(_FloatBox, property_name="T", min=0, unit=u.K), |
| 119 | + _create_label("K", color=LIGHT_GRAY), |
| 120 | + ], |
| 121 | + [ |
| 122 | + _create_label("T<sub>e</sub> - Electron Temperature:"), |
| 123 | + _create_widget(_FloatBox, property_name="T_e", min=0, unit=u.K), |
| 124 | + _create_label("K", color=LIGHT_GRAY), |
| 125 | + ], |
| 126 | + [ |
| 127 | + _create_label("T<sub>i</sub> - Ion Temperature:"), |
| 128 | + _create_widget(_FloatBox, property_name="T_i", min=0, unit=u.K), |
| 129 | + _create_label("K", color=LIGHT_GRAY), |
| 130 | + ], |
| 131 | +] |
| 132 | +""" |
| 133 | +grid_data: Contains widgets layout for input parameters |
| 134 | +""" |
| 135 | + |
| 136 | + |
| 137 | +def _create_interactive_layout(): |
| 138 | + """ |
| 139 | + Interactive grid layout for input parameters populated in grid_data. |
| 140 | + """ |
| 141 | + grid = widgets.GridspecLayout(18, 3) |
| 142 | + grid.layout.margin = "10px" |
| 143 | + |
| 144 | + for i, row in enumerate(grid_data): |
| 145 | + for j, cell in enumerate(row): |
| 146 | + grid[i, j] = cell |
| 147 | + |
| 148 | + grid[-1, 0] = _calculate_button |
| 149 | + grid[-1, 1] = _clear_button |
| 150 | + return grid |
| 151 | + |
| 152 | + |
| 153 | +def _create_output_layout(): |
| 154 | + """ |
| 155 | + Tab layout for output parameters, populated from |
| 156 | + ``properties_metadata.json``. |
| 157 | + """ |
| 158 | + app = widgets.Tab() |
| 159 | + children = [] |
| 160 | + |
| 161 | + properties_metadata = Path("properties_metadata.json") |
| 162 | + |
| 163 | + with properties_metadata.open() as f: |
| 164 | + data = json.load(f) |
| 165 | + |
| 166 | + for i, title in enumerate(data): |
| 167 | + grid_layout = widgets.GridspecLayout(10, 2, width="100%") |
| 168 | + for j, prop in enumerate(data[title]): |
| 169 | + fn = _FunctionInfo(prop["module_name"], prop["function_name"]) |
| 170 | + if "spec_combo" in prop: |
| 171 | + for spec_combo in prop["spec_combo"]: |
| 172 | + fn.add_combo(spec_combo) |
| 173 | + grid_layout[j, 0] = _create_label(prop["function_name"] + ":") |
| 174 | + grid_layout[j, 1] = fn.get_output_widget() |
| 175 | + _process_queue.append(fn) |
| 176 | + children.append(grid_layout) |
| 177 | + app.set_title(i, title) |
| 178 | + app.children = children |
| 179 | + |
| 180 | + return app |
0 commit comments