Skip to content

Commit

Permalink
Merge pull request #3287 from cphyc/lint-ipynb
Browse files Browse the repository at this point in the history
CI: Pre-commit to apply black/isort/flake8 on ipython notebooks
  • Loading branch information
matthewturk committed Oct 19, 2021
2 parents bc7c44a + e37ab0b commit 8c94e16
Show file tree
Hide file tree
Showing 24 changed files with 808 additions and 453 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Expand Up @@ -38,3 +38,6 @@ cae222aec845d0b400e2aa2804e75b2adef17ccd
ef783151bfd7c6777fa25e9e06f95fe47653b3aa
063e2fb630932cbdcbbbdba603c100a37e7e40f6
279b0551ccc9d9d4c114904b94bc705381c61105

# apply linting to ipynb files
ec8bb45ea1603f3862041fa9e8ec274afd9bbbfd
17 changes: 15 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -38,8 +38,8 @@ repos:
- repo: https://github.com/psf/black
rev: 21.9b0
hooks:
- id: black
language_version: python3
- id: black-jupyter

- repo: https://github.com/PyCQA/isort
rev: '5.9.3'
hooks:
Expand All @@ -66,3 +66,16 @@ repos:
rev: v1.9.0
hooks:
- id: rst-backticks
- repo: https://github.com/nbQA-dev/nbQA
rev: 0.10.0
hooks:
- id: nbqa-pyupgrade
args: [--nbqa-mutate, --py36-plus]
- id: nbqa-isort
args: [--nbqa-mutate]
- id: nbqa-flake8
args: [--extend-ignore=E402]
additional_dependencies: [
flake8-bugbear>=20.3.2, # GH PR 2851
flake8-logging-format
]
51 changes: 37 additions & 14 deletions doc/source/analyzing/Particle_Trajectories.ipynb
Expand Up @@ -18,11 +18,12 @@
"%matplotlib inline\n",
"import glob\n",
"from os.path import join\n",
"\n",
"import yt\n",
"from yt.config import ytcfg\n",
"\n",
"path = ytcfg.get(\"yt\", \"test_data_dir\")\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.mplot3d import Axes3D"
"import matplotlib.pyplot as plt"
]
},
{
Expand Down Expand Up @@ -80,7 +81,7 @@
"ds = yt.load(my_fns[0])\n",
"dd = ds.all_data()\n",
"indices = dd[\"all\", \"particle_index\"].astype(\"int\")\n",
"print (indices)"
"print(indices)"
]
},
{
Expand Down Expand Up @@ -118,8 +119,8 @@
},
"outputs": [],
"source": [
"print (trajs[\"all\", \"particle_position_x\"])\n",
"print (trajs[\"all\", \"particle_position_x\"].shape)"
"print(trajs[\"all\", \"particle_position_x\"])\n",
"print(trajs[\"all\", \"particle_position_x\"].shape)"
]
},
{
Expand Down Expand Up @@ -219,7 +220,13 @@
"outputs": [],
"source": [
"ds = yt.load(\"enzo_tiny_cosmology/DD0046/DD0046\")\n",
"slc = yt.SlicePlot(ds, \"x\", [(\"gas\", \"density\"), (\"gas\", \"dark_matter_density\")], center=\"max\", width=(3.0, \"Mpc\"))\n",
"slc = yt.SlicePlot(\n",
" ds,\n",
" \"x\",\n",
" [(\"gas\", \"density\"), (\"gas\", \"dark_matter_density\")],\n",
" center=\"max\",\n",
" width=(3.0, \"Mpc\"),\n",
")\n",
"slc.show()"
]
},
Expand Down Expand Up @@ -279,10 +286,22 @@
"outputs": [],
"source": [
"fig = plt.figure(figsize=(8.0, 8.0))\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"ax.plot(trajs[\"all\", \"particle_position_x\"][100], trajs[\"all\", \"particle_position_y\"][100], trajs[\"all\", \"particle_position_z\"][100])\n",
"ax.plot(trajs[\"all\", \"particle_position_x\"][8], trajs[\"all\", \"particle_position_y\"][8], trajs[\"all\", \"particle_position_z\"][8])\n",
"ax.plot(trajs[\"all\", \"particle_position_x\"][25], trajs[\"all\", \"particle_position_y\"][25], trajs[\"all\", \"particle_position_z\"][25])"
"ax = fig.add_subplot(111, projection=\"3d\")\n",
"ax.plot(\n",
" trajs[\"all\", \"particle_position_x\"][100],\n",
" trajs[\"all\", \"particle_position_y\"][100],\n",
" trajs[\"all\", \"particle_position_z\"][100],\n",
")\n",
"ax.plot(\n",
" trajs[\"all\", \"particle_position_x\"][8],\n",
" trajs[\"all\", \"particle_position_y\"][8],\n",
" trajs[\"all\", \"particle_position_z\"][8],\n",
")\n",
"ax.plot(\n",
" trajs[\"all\", \"particle_position_x\"][25],\n",
" trajs[\"all\", \"particle_position_y\"][25],\n",
" trajs[\"all\", \"particle_position_z\"][25],\n",
")"
]
},
{
Expand All @@ -300,7 +319,7 @@
},
"outputs": [],
"source": [
"plt.figure(figsize=(6,6))\n",
"plt.figure(figsize=(6, 6))\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"all\", \"particle_position_x\"][100])\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"all\", \"particle_position_x\"][8])\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"all\", \"particle_position_x\"][25])"
Expand Down Expand Up @@ -339,7 +358,7 @@
},
"outputs": [],
"source": [
"plt.figure(figsize=(6,6))\n",
"plt.figure(figsize=(6, 6))\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"gas\", \"density\"][100])\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"gas\", \"density\"][8])\n",
"plt.plot(trajs[\"all\", \"particle_time\"], trajs[\"gas\", \"density\"][25])\n",
Expand All @@ -361,8 +380,12 @@
},
"outputs": [],
"source": [
"trajs.write_out(\"halo_trajectories\") # This will write a separate file for each trajectory\n",
"trajs.write_out_h5(\"halo_trajectories.h5\") # This will write all trajectories to a single file"
"trajs.write_out(\n",
" \"halo_trajectories\"\n",
") # This will write a separate file for each trajectory\n",
"trajs.write_out_h5(\n",
" \"halo_trajectories.h5\"\n",
") # This will write all trajectories to a single file"
]
}
],
Expand Down
63 changes: 46 additions & 17 deletions doc/source/analyzing/domain_analysis/XrayEmissionFields.ipynb
Expand Up @@ -41,9 +41,13 @@
"source": [
"import yt\n",
"\n",
"ds = yt.load(\"GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150\", default_species_fields=\"ionized\")\n",
"ds = yt.load(\n",
" \"GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150\", default_species_fields=\"ionized\"\n",
")\n",
"\n",
"xray_fields = yt.add_xray_emissivity_field(ds, 0.5, 7.0, table_type='apec', metallicity=0.3)"
"xray_fields = yt.add_xray_emissivity_field(\n",
" ds, 0.5, 7.0, table_type=\"apec\", metallicity=0.3\n",
")"
]
},
{
Expand All @@ -69,7 +73,7 @@
"metadata": {},
"outputs": [],
"source": [
"print (xray_fields)"
"print(xray_fields)"
]
},
{
Expand All @@ -86,7 +90,7 @@
"outputs": [],
"source": [
"sp = ds.sphere(\"c\", (2.0, \"Mpc\"))\n",
"print (sp.quantities.total_quantity((\"gas\",\"xray_luminosity_0.5_7.0_keV\")))"
"print(sp.quantities.total_quantity((\"gas\", \"xray_luminosity_0.5_7.0_keV\")))"
]
},
{
Expand All @@ -104,9 +108,15 @@
},
"outputs": [],
"source": [
"slc = yt.SlicePlot(ds, 'z', [('gas', 'xray_emissivity_0.5_7.0_keV'),\n",
" ('gas', 'xray_photon_emissivity_0.5_7.0_keV')],\n",
" width=(0.75, \"Mpc\"))\n",
"slc = yt.SlicePlot(\n",
" ds,\n",
" \"z\",\n",
" [\n",
" (\"gas\", \"xray_emissivity_0.5_7.0_keV\"),\n",
" (\"gas\", \"xray_photon_emissivity_0.5_7.0_keV\"),\n",
" ],\n",
" width=(0.75, \"Mpc\"),\n",
")\n",
"slc.show()"
]
},
Expand All @@ -129,10 +139,17 @@
"source": [
"ds2 = yt.load(\"D9p_500/10MpcBox_HartGal_csf_a0.500.d\", default_species_fields=\"ionized\")\n",
"\n",
"# In this case, use the redshift and cosmology from the dataset, \n",
"# In this case, use the redshift and cosmology from the dataset,\n",
"# but in theory you could put in something different\n",
"xray_fields2 = yt.add_xray_emissivity_field(ds2, 0.5, 2.0, redshift=ds2.current_redshift, cosmology=ds2.cosmology,\n",
" metallicity=(\"gas\", \"metallicity\"), table_type='cloudy')"
"xray_fields2 = yt.add_xray_emissivity_field(\n",
" ds2,\n",
" 0.5,\n",
" 2.0,\n",
" redshift=ds2.current_redshift,\n",
" cosmology=ds2.cosmology,\n",
" metallicity=(\"gas\", \"metallicity\"),\n",
" table_type=\"cloudy\",\n",
")"
]
},
{
Expand All @@ -148,7 +165,7 @@
"metadata": {},
"outputs": [],
"source": [
"print (xray_fields2)"
"print(xray_fields2)"
]
},
{
Expand All @@ -166,8 +183,9 @@
},
"outputs": [],
"source": [
"prj = yt.ProjectionPlot(ds2, \"x\", (\"gas\",\"xray_intensity_0.5_2.0_keV\"),\n",
" center=\"max\", width=(40, \"kpc\"))\n",
"prj = yt.ProjectionPlot(\n",
" ds2, \"x\", (\"gas\", \"xray_intensity_0.5_2.0_keV\"), center=\"max\", width=(40, \"kpc\")\n",
")\n",
"prj.set_zlim(\"xray_intensity_0.5_2.0_keV\", 1.0e-32, 5.0e-24)\n",
"prj.show()"
]
Expand Down Expand Up @@ -197,8 +215,14 @@
"metadata": {},
"outputs": [],
"source": [
"xray_fields3 = yt.add_xray_emissivity_field(ds2, 0.5, 2.0, dist=(1.0,\"Mpc\"), metallicity=(\"gas\", \"metallicity\"), \n",
" table_type='cloudy')"
"xray_fields3 = yt.add_xray_emissivity_field(\n",
" ds2,\n",
" 0.5,\n",
" 2.0,\n",
" dist=(1.0, \"Mpc\"),\n",
" metallicity=(\"gas\", \"metallicity\"),\n",
" table_type=\"cloudy\",\n",
")"
]
},
{
Expand All @@ -209,8 +233,13 @@
},
"outputs": [],
"source": [
"prj = yt.ProjectionPlot(ds2, \"x\", (\"gas\", \"xray_photon_intensity_0.5_2.0_keV\"),\n",
" center=\"max\", width=(40, \"kpc\"))\n",
"prj = yt.ProjectionPlot(\n",
" ds2,\n",
" \"x\",\n",
" (\"gas\", \"xray_photon_intensity_0.5_2.0_keV\"),\n",
" center=\"max\",\n",
" width=(40, \"kpc\"),\n",
")\n",
"prj.set_zlim(\"xray_photon_intensity_0.5_2.0_keV\", 1.0e-24, 5.0e-16)\n",
"prj.show()"
]
Expand Down

0 comments on commit 8c94e16

Please sign in to comment.