Skip to content
New issue

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

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

Already on GitHub? # to your account

Code updated to follow Google Python Style Guide #38

Merged
merged 4 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ Alternatively, you can specify all blob parameters exactly as you want by writin
drain time for blobs
- `blob_factory`: BlobFactory, optional,
object containing blob parameters

- `labels`: bool, optional,
if True, field with blob labels is returned
used for creating training data for supervised machine learning algorithms
- `label_border`: float, optional,
defines region of blob as region where density >= label_border * amplitude of Blob
only used if labels = True
### `DefaultBlobFactory()`
- `A_dist`: str, optional,
distribution of blob amplitudes
Expand Down Expand Up @@ -102,7 +107,6 @@ The following distributions are implemented:
- `ray`: rayleight distribution with mean 1
- `deg`: array on ones
- `zeros`: array of zeros

### `make_realization()`
- `file_name`: str, optional,
file name for .nc file containing data as xarray dataset
Expand All @@ -113,12 +117,6 @@ The following distributions are implemented:
!!! this is only a good approximation for blob_shape='exp' !!!
- `error`: float, optional,
numerical error at x = Lx when blob gets truncated
- `labels`: bool, optional,
if True, field with blob labels is returned
used for creating training data for supervised machine learning algorithms
- `label_border`: float, optional,
defines region of blob as region where density >= label_border * amplitude of Blob
only used if labels = True
### `show_model()`
- `ds`: xarray Dataset,
Model data
Expand Down
80 changes: 40 additions & 40 deletions blobmodel/blobs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import numpy as np
from nptyping import NDArray
from typing import Tuple
import warnings
from typing import Tuple
from nptyping import NDArray
import numpy as np


class Blob:
"""A single blob."""

def __init__(
self,
id: int,
blob_id: int,
blob_shape: str,
amplitude: float,
width_prop: float,
Expand All @@ -22,7 +22,7 @@ def __init__(
t_drain: float,
) -> None:
self.int = int
self.id = id
self.blob_id = blob_id
self.blob_shape = blob_shape
self.amplitude = amplitude
self.width_prop = width_prop
Expand Down Expand Up @@ -59,53 +59,53 @@ def discretize_blob(
if (self.width_perp > 0.1 * Ly or self.width_prop > 0.1 * Ly) and periodic_y:
warnings.warn("blob width big compared to Ly")

x_perp, y_perp = self.__rotate(
x_perp, y_perp = self._rotate(
origin=(self.pos_x, self.pos_y), x=x, y=y, angle=-self.theta
)
if not periodic_y:
return self.__single_blob(x_perp, y_perp, t, Ly, periodic_y)
return self._single_blob(x_perp, y_perp, t, Ly, periodic_y)
if np.sin(self.theta) == 0:
__x_border = Ly - self.pos_y
__adjusted_Ly = Ly
_x_border = Ly - self.pos_y
_adjusted_Ly = Ly
else:
__x_border = (Ly - self.pos_y) / np.sin(self.theta)
__adjusted_Ly = Ly / np.sin(self.theta)
_x_border = (Ly - self.pos_y) / np.sin(self.theta)
_adjusted_Ly = Ly / np.sin(self.theta)
if type(t) in [int, float]:
# t has dimensionality = 0, used for testing
__number_of_y_propagations = (
self.__prop_dir_blob_position(t) + __adjusted_Ly - __x_border
) // __adjusted_Ly
_number_of_y_propagations = (
self._prop_dir_blob_position(t) + _adjusted_Ly - _x_border
) // _adjusted_Ly
else:
__number_of_y_propagations = (
self.__prop_dir_blob_position(t)[0, 0] + __adjusted_Ly - __x_border
) // __adjusted_Ly
_number_of_y_propagations = (
self._prop_dir_blob_position(t)[0, 0] + _adjusted_Ly - _x_border
) // _adjusted_Ly
return (
self.__single_blob(
x_perp, y_perp, t, Ly, periodic_y, __number_of_y_propagations
self._single_blob(
x_perp, y_perp, t, Ly, periodic_y, _number_of_y_propagations
)
+ self.__single_blob(
+ self._single_blob(
x_perp,
y_perp,
t,
Ly,
periodic_y,
__number_of_y_propagations,
_number_of_y_propagations,
x_offset=Ly * np.sin(self.theta),
y_offset=Ly * np.cos(self.theta),
)
+ self.__single_blob(
+ self._single_blob(
x_perp,
y_perp,
t,
Ly,
periodic_y,
__number_of_y_propagations,
_number_of_y_propagations,
x_offset=-Ly * np.sin(self.theta),
y_offset=-Ly * np.cos(self.theta),
)
)

def __single_blob(
def _single_blob(
self,
x_perp: NDArray,
y_perp: NDArray,
Expand All @@ -118,30 +118,30 @@ def __single_blob(
) -> NDArray:
return (
self.amplitude
* self.__drain(t)
* self.__propagation_direction_shape(
* self._drain(t)
* self._propagation_direction_shape(
x_perp + x_offset,
t,
Ly,
periodic_y,
number_of_y_propagations=number_of_y_propagations,
)
* self.__perpendicular_direction_shape(
* self._perpendicular_direction_shape(
y_perp + y_offset,
Ly,
periodic_y,
number_of_y_propagations=number_of_y_propagations,
)
* self.__blob_arrival(t)
* self._blob_arrival(t)
)

def __drain(self, t: NDArray) -> NDArray:
def _drain(self, t: NDArray) -> NDArray:
return np.exp(-(t - self.t_init) / self.t_drain)

def __blob_arrival(self, t: NDArray) -> NDArray:
def _blob_arrival(self, t: NDArray) -> NDArray:
return np.heaviside(t - self.t_init, 1)

def __propagation_direction_shape(
def _propagation_direction_shape(
self,
x: NDArray,
t: NDArray,
Expand All @@ -152,22 +152,22 @@ def __propagation_direction_shape(
if periodic_y:
x_diffs = (
x
- self.__prop_dir_blob_position(t)
- self._prop_dir_blob_position(t)
+ number_of_y_propagations * Ly * np.sin(self.theta)
)
else:
x_diffs = x - self.__prop_dir_blob_position(t)
x_diffs = x - self._prop_dir_blob_position(t)

if self.blob_shape == "gauss":
return 1 / np.sqrt(np.pi) * np.exp(-(x_diffs ** 2 / self.width_prop ** 2))
elif self.blob_shape == "exp":
return np.exp(x_diffs) * np.heaviside(-1.0 * (x_diffs), 1)
else:
raise NotImplementedError(
self.__class__.__name__ + ".blob shape not implemented"
self.__class__.__name__ + ".blob_shape not implemented"
)

def __perpendicular_direction_shape(
def _perpendicular_direction_shape(
self,
y: NDArray,
Ly: float,
Expand All @@ -177,20 +177,20 @@ def __perpendicular_direction_shape(
if periodic_y:
y_diffs = (
y
- self.__perp_dir_blob_position()
- self._perp_dir_blob_position()
+ number_of_y_propagations * Ly * np.cos(self.theta)
)
else:
y_diffs = y - self.__perp_dir_blob_position()
y_diffs = y - self._perp_dir_blob_position()
return 1 / np.sqrt(np.pi) * np.exp(-(y_diffs ** 2) / self.width_perp ** 2)

def __prop_dir_blob_position(self, t: NDArray) -> NDArray:
def _prop_dir_blob_position(self, t: NDArray) -> NDArray:
return self.pos_x + (self.v_x ** 2 + self.v_y ** 2) ** 0.5 * (t - self.t_init)

def __perp_dir_blob_position(self) -> NDArray:
def _perp_dir_blob_position(self) -> NDArray:
return self.pos_y

def __rotate(
def _rotate(
self, origin: Tuple[float, float], x: NDArray, y: NDArray, angle: float
) -> Tuple[float, float]:
ox, oy = origin
Expand Down
Loading