From 81026bb8c3f44e86af264bae4b0392fe95f9ebde Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Sun, 16 Jul 2023 13:11:48 -0400 Subject: [PATCH 1/5] STY: add `black` formatter for Jupyter notebooks --- .pre-commit-config.yaml | 5 ++++ docs/source/notebooks/elements.ipynb | 41 ++++++++++++++++------------ docs/source/notebooks/madx.ipynb | 17 ++++++++---- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c54fb967..dd83b947 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,6 +11,11 @@ repos: rev: 23.3.0 hooks: - id: black + language_version: python3.10 + - id: black-jupyter + language_version: python3.10 + exclude: ^(.*\.py) + args: [--line-length=78] - repo: https://github.com/pycqa/flake8 rev: 6.0.0 hooks: diff --git a/docs/source/notebooks/elements.ipynb b/docs/source/notebooks/elements.ipynb index ef14de7b..76425c58 100644 --- a/docs/source/notebooks/elements.ipynb +++ b/docs/source/notebooks/elements.ipynb @@ -41,8 +41,7 @@ "connection = SirepoBluesky(\"http://localhost:8000\")\n", "\n", "data, schema = connection.auth(\"srw\", \"00000002\")\n", - "classes, objects = create_classes(connection.data,\n", - " connection=connection)\n", + "classes, objects = create_classes(connection.data, connection=connection)\n", "globals().update(**objects)\n", "\n", "aperture.horizontalSize.kind = \"hinted\"\n", @@ -71,10 +70,13 @@ "nrows = int(np.ceil(num_frames / ncols))\n", "\n", "fig = plt.figure()\n", - "grid = ImageGrid(fig, (1, 2, ncols, nrows),\n", - " nrows_ncols=(nrows, ncols),\n", - " axes_pad=0.5,\n", - " aspect=False)\n", + "grid = ImageGrid(\n", + " fig,\n", + " (1, 2, ncols, nrows),\n", + " nrows_ncols=(nrows, ncols),\n", + " axes_pad=0.5,\n", + " aspect=False,\n", + ")\n", "\n", "for ax, im in zip(grid, w9_image[:, ...]):\n", " ax.imshow(im, aspect=\"auto\")" @@ -103,13 +105,16 @@ "connection = SirepoBluesky(\"http://localhost:8000\")\n", "\n", "data, schema = connection.auth(\"shadow\", \"00000002\")\n", - "classes, objects = create_classes(connection.data,\n", - " connection=connection)\n", + "classes, objects = create_classes(connection.data, connection=connection)\n", "globals().update(**objects)\n", "\n", - "print(f\"Number of points before change: {data['models']['simulation']['npoint']}\")\n", - "data['models']['simulation']['npoint'] = 1000000\n", - "print(f\"Number of points after change: {data['models']['simulation']['npoint']}\")\n", + "print(\n", + " f\"Number of points before change: {data['models']['simulation']['npoint']}\"\n", + ")\n", + "data[\"models\"][\"simulation\"][\"npoint\"] = 1000000\n", + "print(\n", + " f\"Number of points after change: {data['models']['simulation']['npoint']}\"\n", + ")\n", "aperture.horizontalSize.kind = \"hinted\"\n", "w9.duration.kind = \"hinted\"\n", "\n", @@ -136,10 +141,13 @@ "nrows = int(np.ceil(num_frames / ncols))\n", "\n", "fig = plt.figure()\n", - "grid = ImageGrid(fig, (1, 2, ncols, nrows),\n", - " nrows_ncols=(nrows, ncols),\n", - " axes_pad=0.5,\n", - " aspect=False)\n", + "grid = ImageGrid(\n", + " fig,\n", + " (1, 2, ncols, nrows),\n", + " nrows_ncols=(nrows, ncols),\n", + " axes_pad=0.5,\n", + " aspect=False,\n", + ")\n", "\n", "for ax, im in zip(grid, w9_image[:, ...]):\n", " ax.imshow(im, aspect=\"auto\")" @@ -169,8 +177,7 @@ "\n", "data, schema = connection.auth(\"shadow\", \"00000002\")\n", "\n", - "classes, objects = create_classes(connection.data,\n", - " connection=connection)\n", + "classes, objects = create_classes(connection.data, connection=connection)\n", "globals().update(**objects)\n", "\n", "bsr = BeamStatisticsReport(name=\"bsr\", connection=connection)\n", diff --git a/docs/source/notebooks/madx.ipynb b/docs/source/notebooks/madx.ipynb index 101d03a5..ebefe1cf 100644 --- a/docs/source/notebooks/madx.ipynb +++ b/docs/source/notebooks/madx.ipynb @@ -35,14 +35,18 @@ "connection = SirepoBluesky(\"http://localhost:8000\")\n", "\n", "data, schema = connection.auth(\"madx\", \"00000002\")\n", - "classes, objects = create_classes(connection.data,\n", - " connection=connection,\n", - " extra_model_fields=[\"rpnVariables\", \"commands\"])\n", + "classes, objects = create_classes(\n", + " connection.data,\n", + " connection=connection,\n", + " extra_model_fields=[\"rpnVariables\", \"commands\"],\n", + ")\n", "globals().update(**objects)\n", "\n", - "madx_flyer = MADXFlyer(connection=connection,\n", - " root_dir=\"/tmp/sirepo-bluesky-data\",\n", - " report=\"elementAnimation250-20\")\n", + "madx_flyer = MADXFlyer(\n", + " connection=connection,\n", + " root_dir=\"/tmp/sirepo-bluesky-data\",\n", + " report=\"elementAnimation250-20\",\n", + ")\n", "\n", "(uid1,) = RE(bp.fly([madx_flyer]))\n", "hdr1 = db[uid1]\n", @@ -68,6 +72,7 @@ " yield from bps.mv(parameter.value, value)\n", " return (yield from bp.fly([madx_flyer]))\n", "\n", + "\n", "(uid2,) = RE(madx_plan())\n", "hdr2 = db[uid2]\n", "tbl2 = hdr2.table(stream_name=\"madx_flyer\", fill=True)\n", From 19b8fdab005123248ce7c98b0d97e1a9a20bc3c4 Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Mon, 17 Jul 2023 11:30:46 -0400 Subject: [PATCH 2/5] convert integer sirepo parameters to floats --- sirepo_bluesky/sirepo_ophyd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sirepo_bluesky/sirepo_ophyd.py b/sirepo_bluesky/sirepo_ophyd.py index 1df3874f..8f1c5acb 100644 --- a/sirepo_bluesky/sirepo_ophyd.py +++ b/sirepo_bluesky/sirepo_ophyd.py @@ -401,7 +401,7 @@ def create_classes(sirepo_data, connection, create_objects=True, extra_model_fie components[k] = Cpt( cpt_class, - value=v, + value=(float(v) if type(v) is int else v), sirepo_dict=sirepo_dict, sirepo_param=k, ) From 5d2634b025c3796e235ae45e83ed63b7e7114b15 Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Mon, 17 Jul 2023 12:10:02 -0400 Subject: [PATCH 3/5] suppress shadow stdout --- sirepo_bluesky/shadow_handler.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sirepo_bluesky/shadow_handler.py b/sirepo_bluesky/shadow_handler.py index fa902efe..3ff1fc44 100644 --- a/sirepo_bluesky/shadow_handler.py +++ b/sirepo_bluesky/shadow_handler.py @@ -1,3 +1,6 @@ +import contextlib +import os + import numpy as np import Shadow.ShadowLibExtensions as sd import Shadow.ShadowTools @@ -49,7 +52,9 @@ def read_shadow_file_col(filename, parameter=30): 32 S2-stokes = 2 |Es| |Ep| cos(phase_s-phase_p) 33 S3-stokes = 2 |Es| |Ep| sin(phase_s-phase_p) """ - data = Shadow.ShadowTools.getshcol(filename, col=parameter) + with open(os.devnull, "w") as devnull: + with contextlib.redirect_stdout(devnull): + data = Shadow.ShadowTools.getshcol(filename, col=parameter) mean_value = np.mean(data) @@ -73,7 +78,9 @@ def read_shadow_file(filename, histogram_bins=None): beam.load(filename) # 1=X spatial coordinate; 3=Z spatial coordinate - data_dict = beam.histo2(1, 3, nolost=1, nbins=histogram_bins) + with open(os.devnull, "w") as devnull: + with contextlib.redirect_stdout(devnull): + data_dict = beam.histo2(1, 3, nolost=1, nbins=histogram_bins) data = data_dict["histogram"] # This returns a list of N values (N=number of rays) From 8062893a6ecebb4298d723c1b8eb116e6312d4ec Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Mon, 17 Jul 2023 12:11:22 -0400 Subject: [PATCH 4/5] suppress shadow stdout --- sirepo_bluesky/shadow_handler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sirepo_bluesky/shadow_handler.py b/sirepo_bluesky/shadow_handler.py index 3ff1fc44..a906dde6 100644 --- a/sirepo_bluesky/shadow_handler.py +++ b/sirepo_bluesky/shadow_handler.py @@ -81,11 +81,11 @@ def read_shadow_file(filename, histogram_bins=None): with open(os.devnull, "w") as devnull: with contextlib.redirect_stdout(devnull): data_dict = beam.histo2(1, 3, nolost=1, nbins=histogram_bins) - data = data_dict["histogram"] + data = data_dict["histogram"] - # This returns a list of N values (N=number of rays) - photon_energy_list = Shadow.ShadowTools.getshcol(filename, col=11) # 11=Energy [eV] - photon_energy = np.mean(photon_energy_list) + # This returns a list of N values (N=number of rays) + photon_energy_list = Shadow.ShadowTools.getshcol(filename, col=11) # 11=Energy [eV] + photon_energy = np.mean(photon_energy_list) return { "data": data, From 67a258e2d62878b6e361319b02d293960e6c7734 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Tue, 18 Jul 2023 10:14:33 -0400 Subject: [PATCH 5/5] STY: use `black` default for max line len of 88 symbols and reformat --- .pre-commit-config.yaml | 2 +- docs/source/notebooks/elements.ipynb | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dd83b947..c5060334 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - id: black-jupyter language_version: python3.10 exclude: ^(.*\.py) - args: [--line-length=78] + args: [--line-length=88] - repo: https://github.com/pycqa/flake8 rev: 6.0.0 hooks: diff --git a/docs/source/notebooks/elements.ipynb b/docs/source/notebooks/elements.ipynb index 76425c58..bd8320fc 100644 --- a/docs/source/notebooks/elements.ipynb +++ b/docs/source/notebooks/elements.ipynb @@ -108,13 +108,9 @@ "classes, objects = create_classes(connection.data, connection=connection)\n", "globals().update(**objects)\n", "\n", - "print(\n", - " f\"Number of points before change: {data['models']['simulation']['npoint']}\"\n", - ")\n", + "print(f\"Number of points before change: {data['models']['simulation']['npoint']}\")\n", "data[\"models\"][\"simulation\"][\"npoint\"] = 1000000\n", - "print(\n", - " f\"Number of points after change: {data['models']['simulation']['npoint']}\"\n", - ")\n", + "print(f\"Number of points after change: {data['models']['simulation']['npoint']}\")\n", "aperture.horizontalSize.kind = \"hinted\"\n", "w9.duration.kind = \"hinted\"\n", "\n",