From 923467c7de8e734818aa4b46b6ded8d9140d4934 Mon Sep 17 00:00:00 2001 From: Christopher Mayes <31023527+ChristopherMayes@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:37:25 -0700 Subject: [PATCH] Separate _create property functions, add doc= (thanks @ken-lauer ) --- docs/examples/fields/field_examples.ipynb | 45 ++----------- pmd_beamphysics/fields/fieldmesh.py | 81 ++++++++++++----------- 2 files changed, 49 insertions(+), 77 deletions(-) diff --git a/docs/examples/fields/field_examples.ipynb b/docs/examples/fields/field_examples.ipynb index 01c6368..eef00a9 100644 --- a/docs/examples/fields/field_examples.ipynb +++ b/docs/examples/fields/field_examples.ipynb @@ -889,14 +889,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:18:50.442628Z", - "iopub.status.busy": "2024-10-17T23:18:50.442530Z", - "iopub.status.idle": "2024-10-17T23:18:50.451459Z", - "shell.execute_reply": "2024-10-17T23:18:50.451257Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "FM3D = FieldMesh.from_ansys_ascii_3d(\n", @@ -912,14 +905,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:18:50.452610Z", - "iopub.status.busy": "2024-10-17T23:18:50.452525Z", - "iopub.status.idle": "2024-10-17T23:18:50.454832Z", - "shell.execute_reply": "2024-10-17T23:18:50.454618Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "FM3D.attrs" @@ -935,14 +921,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:18:50.456002Z", - "iopub.status.busy": "2024-10-17T23:18:50.455927Z", - "iopub.status.idle": "2024-10-17T23:18:50.461253Z", - "shell.execute_reply": "2024-10-17T23:18:50.461050Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "FM3D.write(\"../data/rfgun_rectangular.h5\")" @@ -958,14 +937,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:18:50.462395Z", - "iopub.status.busy": "2024-10-17T23:18:50.462304Z", - "iopub.status.idle": "2024-10-17T23:18:50.464240Z", - "shell.execute_reply": "2024-10-17T23:18:50.464026Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "FM2D = FM3D.to_cylindrical()\n", @@ -982,14 +954,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "execution": { - "iopub.execute_input": "2024-10-17T23:18:50.465256Z", - "iopub.status.busy": "2024-10-17T23:18:50.465160Z", - "iopub.status.idle": "2024-10-17T23:18:50.467295Z", - "shell.execute_reply": "2024-10-17T23:18:50.467076Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import os\n", diff --git a/pmd_beamphysics/fields/fieldmesh.py b/pmd_beamphysics/fields/fieldmesh.py index 32bbed0..c1687c7 100644 --- a/pmd_beamphysics/fields/fieldmesh.py +++ b/pmd_beamphysics/fields/fieldmesh.py @@ -60,6 +60,50 @@ } +def _create_delta_property(name): + def getter(self): + return self.deltas[self.axis_index(name)] + + return property(getter, doc=f"Mesh spacing in {name}") + + +def _create_max_property(name): + def getter(self): + return self.maxs[self.axis_index(name)] + + def setter(self, value): + # Setting the max => shift the min + i = self.axis_index(name) + mins = list(self.attrs["gridOriginOffset"]) + mins[i] = mins[i] + float(value) - self.maxs[i] + self.attrs["gridOriginOffset"] = tuple(mins) + + return property( + getter, setter, doc=f"Mesh maximum in {name}. This can also be set." + ) + + +def _create_min_property(name): + def getter(self): + return self.mins[self.axis_index(name)] + + def setter(self, value): + mins = list(self.attrs["gridOriginOffset"]) + mins[self.axis_index(name)] = float(value) + self.attrs["gridOriginOffset"] = tuple(mins) + + return property( + getter, setter, doc=f"Mesh minimim in {name}. This can also be set." + ) + + +def _create_scaled_component_property(name): + def getter(self): + return self.scaled_component(name) + + return property(getter, doc=f"Scaled 3D mesh for {name} in {pg_units(name)}") + + class FieldMesh: """ Class for openPMD External Field Mesh data. @@ -833,12 +877,6 @@ def z(self): return self.coord_vec("z") # Deltas - def _create_delta_property(name): - def getter(self): - return self.deltas[self.axis_index(name)] - - return property(getter) - dx = _create_delta_property("x") dy = _create_delta_property("y") dz = _create_delta_property("z") @@ -846,19 +884,6 @@ def getter(self): dtheta = _create_delta_property("theta") # Maxs - def _create_max_property(name): - def getter(self): - return self.maxs[self.axis_index(name)] - - def setter(self, value): - # Setting the max => shift the min - i = self.axis_index(name) - mins = list(self.attrs["gridOriginOffset"]) - mins[i] = mins[i] + float(value) - self.maxs[i] - self.attrs["gridOriginOffset"] = tuple(mins) - - return property(getter, setter) - # Create max properties dynamically xmax = _create_max_property("x") ymax = _create_max_property("y") @@ -867,17 +892,6 @@ def setter(self, value): thetamax = _create_max_property("theta") # Mins - def _create_min_property(name): - def getter(self): - return self.mins[self.axis_index(name)] - - def setter(self, value): - mins = list(self.attrs["gridOriginOffset"]) - mins[self.axis_index(name)] = float(value) - self.attrs["gridOriginOffset"] = tuple(mins) - - return property(getter, setter) - # Create min properties dynamically xmin = _create_min_property("x") ymin = _create_min_property("y") @@ -886,13 +900,6 @@ def setter(self, value): thetamin = _create_min_property("theta") # Scaled components - # TODO: Check geometry - def _create_scaled_component_property(name): - def getter(self): - return self.scaled_component(name) - - return property(getter) - # Dynamically create scaled properties Bx = _create_scaled_component_property("Bx") By = _create_scaled_component_property("By")