Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully lint Sarracen #95

Merged
merged 13 commits into from
Jul 21, 2024
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

6 changes: 6 additions & 0 deletions .github/linters/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
per-file-ignores =
sarracen/__init__.py:F401
sarracen/kernels/__init__.py:F401
sarracen/interpolate/__init__.py:F401
sarracen/disc/__init__.py:F401
5 changes: 2 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys

from pathlib import Path
Expand Down Expand Up @@ -53,8 +52,8 @@
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"
#html_theme = "mpl_sphinx_theme"
#html_theme = "pydata_sphinx_theme"
# html_theme = "mpl_sphinx_theme"
# html_theme = "pydata_sphinx_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
3 changes: 2 additions & 1 deletion sarracen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from .sarracen_dataframe import SarracenDataFrame

from .interpolate import interpolate_2d, interpolate_2d_line, interpolate_3d_proj, interpolate_3d_cross
from .interpolate import interpolate_2d, interpolate_2d_line, \
interpolate_3d_proj, interpolate_3d_cross
from .render import render, streamlines, arrowplot

import sarracen.disc
Expand Down
11 changes: 6 additions & 5 deletions sarracen/disc/surface_density.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import pandas as pd
import sys
from ..sarracen_dataframe import SarracenDataFrame
from .utils import _get_mass, _get_origin
from .utils import _bin_particles_by_radius, _get_bin_midpoints
Expand Down Expand Up @@ -123,7 +122,8 @@ def surface_density(data: 'SarracenDataFrame',
Notes
-----
The surface density averaging procedure for SPH is described in section
3.2.6 of Lodato & Price, MNRAS (2010), `doi:10.1111/j.1365-2966.2010.16526.x
3.2.6 of Lodato & Price, MNRAS (2010),
`doi:10.1111/j.1365-2966.2010.16526.x
<https://doi.org/10.1111/j.1365-2966.2010.16526.x>`_.
"""

Expand Down Expand Up @@ -287,8 +287,8 @@ def _calc_scale_height(data: 'SarracenDataFrame',
Lx, Ly, Lz = _calc_angular_momentum(data, rbins, origin, unit_vector=True)

zdash = rbins.map(Lx).to_numpy() * data[data.xcol] \
+ rbins.map(Ly).to_numpy() * data[data.ycol] \
+ rbins.map(Lz).to_numpy() * data[data.zcol]
+ rbins.map(Ly).to_numpy() * data[data.ycol] \
+ rbins.map(Lz).to_numpy() * data[data.zcol]

return zdash.groupby(rbins).std()

Expand Down Expand Up @@ -372,7 +372,8 @@ def honH(data: 'SarracenDataFrame',
origin: list = None,
retbins: bool = False):
"""
Calculates <h>/H, the averaged smoothing length divided by the scale height.
Calculates <h>/H, the averaged smoothing length divided by the scale
height.

The profile is computed by segmenting the particles into radial bins
(rings). The average smoothing length in each bin is divided by the scale
Expand Down
13 changes: 9 additions & 4 deletions sarracen/disc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@


def _get_mass(data: 'SarracenDataFrame'):
if data.mcol == None:
if data.mcol is None:
if 'mass' not in data.params:
raise KeyError("'mass' column does not exist in this SarracenDataFrame.")
raise KeyError("'mass' column does not exist in this "
"SarracenDataFrame.")
return data.params['mass']

return data[data.mcol]
Expand Down Expand Up @@ -66,7 +67,8 @@ def _bin_particles_by_radius(data: 'SarracenDataFrame',
r = np.sqrt((data[data.xcol] - origin[0]) ** 2
+ (data[data.ycol] - origin[1]) ** 2)
else:
raise ValueError("geometry should be either 'cylindrical' or 'spherical'")
raise ValueError("geometry should be either 'cylindrical' or "
"'spherical'")

# should we add epsilon here?
if r_in is None:
Expand All @@ -84,9 +86,12 @@ def _bin_particles_by_radius(data: 'SarracenDataFrame',


def _get_bin_midpoints(bin_edges: np.ndarray,
log: bool = False) -> np.ndarray:
log: bool = False) -> np.ndarray:
"""
Calculate the midpoint of bins given their edges.

Parameters
----------
bin_edges: ndarray
Locations of the bin edges.
log : bool, optional
Expand Down
7 changes: 4 additions & 3 deletions sarracen/interpolate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..interpolate.base_backend import BaseBackend
from ..interpolate.cpu_backend import CPUBackend
from ..interpolate.gpu_backend import GPUBackend
from ..interpolate.interpolate import interpolate_2d_line, interpolate_2d, interpolate_3d_proj,\
interpolate_3d_cross, interpolate_3d_vec, interpolate_3d_cross_vec, interpolate_3d_grid, interpolate_2d_vec, \
interpolate_3d_line
from ..interpolate.interpolate import interpolate_2d_line, interpolate_2d, \
interpolate_3d_line, interpolate_3d_proj, interpolate_3d_cross, \
interpolate_3d_vec, interpolate_3d_cross_vec, interpolate_3d_grid, \
interpolate_2d_vec
169 changes: 133 additions & 36 deletions sarracen/interpolate/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,171 @@ class BaseBackend:
"""Backend implementation of SPH interpolation functions."""

@staticmethod
def interpolate_2d_render(x: ndarray, y: ndarray, weight: ndarray, h: ndarray, weight_function: CPUDispatcher,
kernel_radius: float, x_pixels: int, y_pixels: int, x_min: float, x_max: float,
y_min: float, y_max: float, exact: bool) -> ndarray:
""" Interpolate 2D particle data to a 2D grid of pixels."""
def interpolate_2d_render(x: ndarray,
y: ndarray,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
exact: bool) -> ndarray:
""" Interpolate 2D data to a 2D grid of pixels."""
return zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_2d_render_vec(x: ndarray, y: ndarray, weight_x: ndarray, weight_y: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, x_pixels: int, y_pixels: int,
x_min: float, x_max: float, y_min: float, y_max: float,
def interpolate_2d_render_vec(x: ndarray,
y: ndarray,
weight_x: ndarray,
weight_y: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
exact: bool) -> Tuple[ndarray, ndarray]:
""" Interpolate 2D particle vector data to a pair of 2D grids of pixels. """
""" Interpolate 2D vector data to a pair of 2D pixel grids. """
return zeros((y_pixels, x_pixels)), zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_2d_line(x: ndarray, y: ndarray, weight: ndarray, h: ndarray, weight_function: CPUDispatcher,
kernel_radius: float, pixels: int, x1: float, x2: float, y1: float, y2: float) -> ndarray:
""" Interpolate 2D particle data to a 1D cross-sectional line. """
def interpolate_2d_line(x: ndarray,
y: ndarray,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
pixels: int,
x1: float,
x2: float,
y1: float,
y2: float) -> ndarray:
""" Interpolate 2D data to a 1D cross-sectional line. """
return zeros(pixels)

@staticmethod
def interpolate_3d_line(x: ndarray, y: ndarray, z: ndarray, weight: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, pixels: int, x1: float, x2: float,
y1: float, y2: float, z1: float, z2: float) -> ndarray:
""" Interpolate 3D particle data to a 1D cross-sectional line. """
def interpolate_3d_line(x: ndarray,
y: ndarray,
z: ndarray,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
pixels: int,
x1: float,
x2: float,
y1: float,
y2: float,
z1: float,
z2: float) -> ndarray:
""" Interpolate 3D data to a 1D cross-sectional line. """
return zeros(pixels)

@staticmethod
def interpolate_3d_projection(x: ndarray, y: ndarray, z: ndarray, weight: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, x_pixels: int, y_pixels: int,
x_min: float, x_max: float, y_min: float, y_max: float, exact: bool) -> ndarray:
""" Interpolate 3D particle data to a 2D grid of pixels, using column projection."""
def interpolate_3d_projection(x: ndarray,
y: ndarray,
z: ndarray,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
exact: bool) -> ndarray:
""" Interpolate 3D data to a 2D pixel grid using column projection."""
return zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_3d_projection_vec(x: ndarray, y: ndarray, weight_x: ndarray, weight_y: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, x_pixels: int,
y_pixels: int, x_min: float, x_max: float, y_min: float, y_max: float,
def interpolate_3d_projection_vec(x: ndarray,
y: ndarray,
weight_x: ndarray,
weight_y: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
exact: bool) -> Tuple[ndarray, ndarray]:
""" Interpolate 3D particle vector data to a pair of 2D grids of pixels, using column projection."""
""" Interpolate 3D vector data to a pair of 2D pixel grids using
column projection."""
return zeros((y_pixels, x_pixels)), zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_3d_cross(x: ndarray, y: ndarray, z: ndarray, z_slice: float, weight: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, x_pixels: int, y_pixels: int,
x_min: float, x_max: float, y_min: float, y_max: float) -> ndarray:
def interpolate_3d_cross(x: ndarray,
y: ndarray,
z: ndarray,
z_slice: float,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float) -> ndarray:
"""
Interpolate 3D particle data to a pair of 2D grids of pixels, using a 3D cross-section at a specific z value.
Interpolate 3D data to a pair of 2D pixel grids using a 3D
cross-section at a specific z value.
"""
return zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_3d_cross_vec(x: ndarray, y: ndarray, z: ndarray, z_slice: float, weight_x: ndarray,
weight_y: ndarray, h: ndarray, weight_function: CPUDispatcher, kernel_radius: float,
x_pixels: int, y_pixels: int, x_min: float, x_max: float, y_min: float,
def interpolate_3d_cross_vec(x: ndarray,
y: ndarray,
z: ndarray,
z_slice: float,
weight_x: ndarray,
weight_y: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float) -> Tuple[ndarray, ndarray]:
"""
Interpolate 3D particle vector data to a pair of 2D grids of pixels, using a 3D cross-section at a
specific z value.
Interpolate 3D vector data to a pair of 2D pixel grids using a 3D
cross-section at a specific z value.
"""
return zeros((y_pixels, x_pixels)), zeros((y_pixels, x_pixels))

@staticmethod
def interpolate_3d_grid(x: ndarray, y: ndarray, z: ndarray, weight: ndarray, h: ndarray,
weight_function: CPUDispatcher, kernel_radius: float, x_pixels: int, y_pixels: int,
z_pixels: int, x_min: float, x_max: float, y_min: float, y_max: float, z_min: float,
def interpolate_3d_grid(x: ndarray,
y: ndarray,
z: ndarray,
weight: ndarray,
h: ndarray,
weight_function: CPUDispatcher,
kernel_radius: float,
x_pixels: int,
y_pixels: int,
z_pixels: int,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
z_min: float,
z_max: float) -> ndarray:
"""
Interpolate 3D particle data to a 3D grid of pixels.
Interpolate 3D data to a 3D grid of pixels.
"""
return zeros((z_pixels, y_pixels, x_pixels))
Loading