Skip to content

Commit

Permalink
Merge pull request #137 from osyris-project/new-hist2d
Browse files Browse the repository at this point in the history
New hist2d
  • Loading branch information
nvaytet authored Jul 4, 2024
2 parents c90f15a + 4405d69 commit 615e33e
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 332 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import numpy as np
import osyris

data = osyris.RamsesDataset(8, path="data").load()
osyris.histogram2d(data["mesh"]["density"], data["mesh"]["B_field"],
norm="log", loglog=True)
osyris.hist2d(data["mesh"]["density"], data["mesh"]["B_field"],
norm="log", loglog=True)
```
![hist2d](https://osyris.readthedocs.io/en/stable/_images/plotting_histograms_13_1.png)

Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Plotting
.. autosummary::
:toctree: generated

histogram1d
histogram2d
hist1d
hist2d
map
plot
scatter
4 changes: 1 addition & 3 deletions docs/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
" data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True\n",
")"
"osyris.hist2d(data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/custom_datasets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram1d(dg[\"density\"])"
"osyris.hist1d(dg[\"density\"])"
]
},
{
Expand Down
8 changes: 2 additions & 6 deletions docs/loading_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
" data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True\n",
")"
"osyris.hist2d(data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True)"
]
},
{
Expand Down Expand Up @@ -91,9 +89,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
" data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True\n",
")"
"osyris.hist2d(data[\"mesh\"][\"density\"], data[\"mesh\"][\"B_field\"], norm=\"log\", loglog=True)"
]
},
{
Expand Down
60 changes: 28 additions & 32 deletions docs/plotting_histograms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"source": [
"## 1D histograms\n",
"\n",
"The `histogram1d` function provides a simple way to make 1D histogram plots.\n",
"When a vector quantity is supplied, by default `histogram1d` will use the norm of the vectors.\n",
"The `hist1d` function provides a simple way to make 1D histogram plots.\n",
"When a vector quantity is supplied, by default `hist1d` will use the norm of the vectors.\n",
"\n",
"### A 1D histogram of gas density\n",
"\n",
Expand All @@ -47,7 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram1d(mesh[\"density\"], logx=True)"
"osyris.hist1d(mesh[\"density\"], logx=True)"
]
},
{
Expand All @@ -66,7 +66,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram1d(mesh[\"density\"], logx=True, bins=np.logspace(-18.0, -13.0, 10))"
"osyris.hist1d(mesh[\"density\"], logx=True, bins=np.logspace(-18.0, -13.0, 10))"
]
},
{
Expand All @@ -75,7 +75,7 @@
"source": [
"### Weighted histogram\n",
"\n",
"By default, `histogram1d` will show a binned count of cells, but it does also support weights.\n",
"By default, `hist1d` will show a binned count of cells, but it does also support weights.\n",
"For example, creating a mass-weighted histogram of the gas density can be achieved via"
]
},
Expand All @@ -86,7 +86,7 @@
"outputs": [],
"source": [
"mesh[\"mass\"] = (mesh[\"density\"] * (mesh[\"dx\"] ** 3)).to(\"M_sun\")\n",
"osyris.histogram1d(mesh[\"density\"], weights=mesh[\"mass\"], logx=True)"
"osyris.hist1d(mesh[\"density\"], weights=mesh[\"mass\"], logx=True)"
]
},
{
Expand All @@ -107,7 +107,7 @@
"outputs": [],
"source": [
"bins = np.linspace(-0.15, 0.15, 40)\n",
"osyris.histogram1d(\n",
"osyris.hist1d(\n",
" mesh.layer(\"B_field\", alpha=0.5).x,\n",
" mesh.layer(\"B_field\", alpha=0.5).y,\n",
" mesh.layer(\"B_field\", alpha=0.5).z,\n",
Expand All @@ -129,7 +129,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram1d(\n",
"osyris.hist1d(\n",
" mesh.layer(\"B_field\", alpha=0.5, bins=40).x,\n",
" mesh.layer(\"B_field\", alpha=0.5, bins=10).y,\n",
" logy=True,\n",
Expand All @@ -142,8 +142,8 @@
"source": [
"## 2D histograms\n",
"\n",
"The `histogram2d` function can be used to make 2D histograms with two different quantities as input.\n",
"When a vector quantity is supplied, by default `histogram2d` will use the norm of the vectors\n",
"The `hist2d` function can be used to make 2D histograms with two different quantities as input.\n",
"When a vector quantity is supplied, by default `hist2d` will use the norm of the vectors\n",
"\n",
"### A 2D histogram of gas density vs magnetic field magnitude\n",
"\n",
Expand All @@ -156,7 +156,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(mesh[\"density\"], mesh[\"B_field\"], norm=\"log\", loglog=True)"
"osyris.hist2d(mesh[\"density\"], mesh[\"B_field\"], norm=\"log\", loglog=True)"
]
},
{
Expand All @@ -174,7 +174,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" norm=\"log\",\n",
Expand All @@ -192,7 +192,7 @@
"### Controlling the horizontal and vertical range\n",
"\n",
"To control the range covered by the horizontal and vertical binning,\n",
"the `xmin`, `xmax`, `ymin`, `ymax` arguments are provided."
"specify the bins manually."
]
},
{
Expand All @@ -201,13 +201,12 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" norm=\"log\",\n",
" loglog=True,\n",
" xmax=1.0e-16,\n",
" ymax=1.0e-3,\n",
" bins=(np.logspace(-19, -16, 301), np.logspace(-5, -3, 301)),\n",
")"
]
},
Expand All @@ -217,7 +216,7 @@
"source": [
"### Using a layer for the colormap instead of counting cells\n",
"\n",
"By default, `histogram2d` will show a binned count of cells.\n",
"By default, `hist2d` will show a binned count of cells.\n",
"However, the colors can represent the histogram of a supplied `Array` instead."
]
},
Expand All @@ -227,7 +226,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" mesh.layer(\"mass\", norm=\"log\"),\n",
Expand Down Expand Up @@ -255,7 +254,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" mesh.layer(\"level\", operation=\"mean\"),\n",
Expand All @@ -269,7 +268,7 @@
"source": [
"### Multiple layers\n",
"\n",
"One can use any number of layers to overlay, although anything being two layers is probably not very useful."
"One can use any number of layers to overlay, although anything beyond two layers is probably not very useful."
]
},
{
Expand All @@ -278,7 +277,7 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" mesh.layer(\"mass\", norm=\"log\"), # layer 1\n",
Expand All @@ -301,9 +300,7 @@
"### Controlling the resolution\n",
"\n",
"By default, the histograms have a resolution of 256x256 pixels.\n",
"This can be changed via the keyword argument `resolution`,\n",
"which can either be an integer (in which case the same resolution is applied to both the x and y dimension)\n",
"or a dict with the syntax `resolution={'x': nx, 'y': ny}`."
"To change the resolution, we use the `bins` argument:"
]
},
{
Expand All @@ -312,12 +309,12 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" norm=\"log\",\n",
" loglog=True,\n",
" resolution=64,\n",
" bins=64,\n",
")"
]
},
Expand Down Expand Up @@ -345,15 +342,15 @@
"\n",
"fig, ax = plt.subplots(2, 2, figsize=(12, 9))\n",
"\n",
"osyris.histogram1d(mesh[\"density\"], logx=True, ax=ax[0, 0])\n",
"osyris.histogram2d(\n",
"osyris.hist1d(mesh[\"density\"], logx=True, ax=ax[0, 0])\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"B_field\"],\n",
" norm=\"log\",\n",
" loglog=True,\n",
" ax=ax[0, 1],\n",
")\n",
"osyris.histogram1d(\n",
"osyris.hist1d(\n",
" mesh[\"B_field\"].x,\n",
" mesh[\"B_field\"].y,\n",
" mesh[\"B_field\"].z,\n",
Expand All @@ -362,7 +359,7 @@
" bins=np.linspace(-0.15, 0.15, 40),\n",
" ax=ax[1, 0],\n",
")\n",
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" mesh[\"density\"],\n",
" mesh[\"velocity\"],\n",
" mesh[\"mass\"],\n",
Expand All @@ -389,8 +386,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions docs/plotting_particles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.histogram2d(\n",
"osyris.hist2d(\n",
" part[\"position\"].x,\n",
" part[\"position\"].y,\n",
" part[\"mass\"],\n",
" resolution=512,\n",
" bins=512,\n",
" norm=\"log\",\n",
")"
]
Expand Down
8 changes: 4 additions & 4 deletions docs/recipes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"\n",
"Here, we want to make a map of the difference in 2D histograms between two snapshots (outputs `5` and `8`).\n",
"Because we do not necessarily have the same number of cells at the same place,\n",
"we first have to make uniform 2D maps using the `histogram2d` function,\n",
"we first have to make uniform 2D maps using the `hist2d` function,\n",
"which we can then directly compare.\n",
"\n",
"The `histogram2d` function actually returns an object that contains the raw data that was used to create the figure.\n",
"The `hist2d` function actually returns an object that contains the raw data that was used to create the figure.\n",
"By using the `plot=False` argument, we can silence the figure generation, and use the data in a custom figure.\n",
"\n",
"**Note:** For the comparison to make sense, both histograms have to use the same horizontal and vertical range."
Expand All @@ -48,15 +48,15 @@
"metadata": {},
"outputs": [],
"source": [
"hist5 = osyris.histogram2d(\n",
"hist5 = osyris.hist2d(\n",
" data5[\"mesh\"][\"density\"],\n",
" data5[\"mesh\"][\"B_field\"],\n",
" norm=\"log\",\n",
" loglog=True,\n",
" plot=False,\n",
")\n",
"\n",
"hist8 = osyris.histogram2d(\n",
"hist8 = osyris.hist2d(\n",
" data8[\"mesh\"][\"density\"],\n",
" data8[\"mesh\"][\"B_field\"],\n",
" norm=\"log\",\n",
Expand Down
10 changes: 5 additions & 5 deletions src/osyris/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Osyris contributors (https://github.com/osyris-project/osyris)

""" Osyris: A Python package for the analysis of astrophysical simulations
"""Osyris: A Python package for the analysis of astrophysical simulations
isort:skip_file
isort:skip_file
"""

import importlib.metadata
Expand All @@ -12,7 +12,7 @@
from .units import units
from .core import Array, Datagroup, Dataset, Plot, Vector, VectorBasis
from .io import RamsesDataset
from .plot import histogram1d, histogram2d, map, plot, scatter
from .plot import hist1d, hist2d, map, plot, scatter
from .spatial import extract_box, extract_sphere

try:
Expand All @@ -32,8 +32,8 @@
"RamsesDataset",
"config",
"units",
"histogram1d",
"histogram2d",
"hist1d",
"hist2d",
"scatter",
"map",
"plot",
Expand Down
6 changes: 3 additions & 3 deletions src/osyris/plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Osyris contributors (https://github.com/osyris-project/osyris)

from .histogram1d import histogram1d
from .histogram2d import histogram2d
from .hist1d import hist1d
from .hist2d import hist2d
from .map import map
from .plot import plot
from .render import render
from .scatter import scatter

__all__ = ["histogram1d", "histogram2d", "map", "plot", "render", "scatter"]
__all__ = ["hist1d", "hist2d", "map", "plot", "render", "scatter"]
Loading

0 comments on commit 615e33e

Please sign in to comment.