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

Statically typing SPARKX #277

Merged
merged 3 commits into from
Oct 24, 2024
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
1 change: 0 additions & 1 deletion docs/source/classes/Jetscape/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ Jetscape
:members:
:inherited-members:

.. automethod:: Jetscape.create_loader
2 changes: 0 additions & 2 deletions docs/source/classes/Oscar/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ Oscar
:members:
:inherited-members:


.. automethod:: Oscar.create_loader
3 changes: 0 additions & 3 deletions docs/source/classes/loader/JetscapeLoader/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ JetscapeLoader
.. autoclass:: JetscapeLoader
:members:
:inherited-members:

.. automethod:: JetscapeLoader._get_num_skip_lines
.. automethod:: JetscapeLoader.event_end_lines
3 changes: 0 additions & 3 deletions docs/source/classes/loader/OscarLoader/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ OscarLoader
:members:
:inherited-members:


.. automethod:: OscarLoader.oscar_format
.. automethod:: OscarLoader.event_end_lines
6 changes: 3 additions & 3 deletions src/sparkx/BaseStorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sparkx.Filter import *
import numpy as np
from abc import ABC, abstractmethod
from sparkx.Particle import Particle
from typing import List, Union, Tuple, Optional
from sparkx.loader.BaseLoader import BaseLoader

Expand Down Expand Up @@ -74,7 +75,7 @@ class BaseStorer(ABC):
"""

def __init__(
self, path: Union[str, List[List["Particle"]]], **kwargs
self, path: Union[str, List[List[Particle]]], **kwargs
) -> None:
"""
Parameters
Expand All @@ -94,7 +95,7 @@ def __init__(
self.loader_: Optional[BaseLoader] = None
self.num_output_per_event_: Optional[np.ndarray] = None
self.num_events_: Optional[int] = None
self.particle_list_: Optional[List] = None
self.particle_list_: List[List[Particle]] = [[]]
self.create_loader(path)
if self.loader_ is not None:
(
Expand Down Expand Up @@ -229,7 +230,6 @@ def uncharged_particles(self) -> "BaseStorer":
self : BaseStorer object
Containing uncharged particles in every event only
"""

self.particle_list_ = uncharged_particles(self.particle_list_)
self._update_num_output_per_event_after_filter()

Expand Down
22 changes: 8 additions & 14 deletions src/sparkx/CentralityClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np
import warnings
from typing import List, Union
from typing import Union, List


class CentralityClasses:
Expand Down Expand Up @@ -64,18 +64,13 @@ class CentralityClasses:
>>> centrality_obj.output_centrality_classes('centrality_output.txt')
"""

def __init__(
self,
events_multiplicity: Union[List[float], np.ndarray],
centrality_bins: Union[List[float], np.ndarray],
) -> None:
def __init__(self, events_multiplicity: Union[List[float], np.ndarray],
centrality_bins: Union[List[float], np.ndarray]) -> None:
if not isinstance(events_multiplicity, (list, np.ndarray)):
RenataKrupczak marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError(
"'events_multiplicity' is not list or numpy.ndarray"
)
"'events_multiplicity' is not list or numpy.ndarray")
if not isinstance(centrality_bins, (list, np.ndarray)):
raise TypeError("'centrality_bins' is not list or numpy.ndarray")

# Check if centrality_bins is sorted
if not all(
centrality_bins[i] <= centrality_bins[i + 1]
Expand Down Expand Up @@ -112,10 +107,10 @@ def __init__(
self.events_multiplicity_ = events_multiplicity
self.centrality_bins_ = unique_bins

self.dNchdetaMin_: list[float] = []
self.dNchdetaMax_: list[float] = []
self.dNchdetaAvg_: list[float] = []
self.dNchdetaAvgErr_: list[float] = []
self.dNchdetaMin_: List[float] = []
self.dNchdetaMax_: List[float] = []
self.dNchdetaAvg_: List[float] = []
self.dNchdetaAvgErr_: List[float] = []

self.__create_centrality_classes()

Expand Down Expand Up @@ -289,7 +284,6 @@ def output_centrality_classes(self, fname: str) -> None:
# Check if fname is a string
if not isinstance(fname, str):
raise TypeError("'fname' should be a string.")

# Write the information to the file
with open(fname, "w") as out_stream:
out_stream.write(
Expand Down
133 changes: 62 additions & 71 deletions src/sparkx/EventCharacteristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sparkx.Particle import Particle
from sparkx.Lattice3D import Lattice3D
import warnings
from typing import Union, List, Optional, Any
from typing import Union, Optional, Any, List, Tuple


class EventCharacteristics:
Expand Down Expand Up @@ -85,12 +85,9 @@ def __init__(
) -> None:
self.set_event_data(event_data)

def set_event_data(
self,
event_data: Union[
List[Particle], np.ndarray[Any, np.dtype[np.object_]], Lattice3D
],
) -> None:
def set_event_data(self,event_data: Union[List[Particle],
np.ndarray[Any,np.dtype[np.object_]],
Lattice3D]) -> None:
"""
Overwrites the event data.

Expand Down Expand Up @@ -303,28 +300,26 @@ def eccentricity(
return self.eccentricity_from_particles(
harmonic_n=harmonic_n,
harmonic_m=harmonic_m,
weight_quantity=weight_quantity,
)

def generate_eBQS_densities_Milne_from_OSCAR_IC(
self,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
z_min: float,
z_max: float,
Nx: int,
Ny: int,
Nz: int,
n_sigma_x: float,
n_sigma_y: float,
n_sigma_z: float,
sigma_smear: float,
eta_range: Union[list, tuple],
output_filename: str,
IC_info: Optional[str] = None,
) -> None:
weight_quantity=weight_quantity)

def generate_eBQS_densities_Milne_from_OSCAR_IC(self,
x_min: Union[float, int],
x_max: Union[float, int],
y_min: Union[float, int],
y_max: Union[float, int],
z_min: Union[float, int],
z_max: Union[float, int],
Nx: int,
Ny: int,
Nz: int,
n_sigma_x: Union[float, int],
n_sigma_y: Union[float, int],
n_sigma_z: Union[float, int],
sigma_smear: Union[float, int],
eta_range: Union[List[Union[int, float]],
Tuple[Union[float, int]]],
output_filename: str,
IC_info: Optional[str] = None) -> None:
"""
Generates energy, baryon, charge, and strangeness densities in Milne
coordinates from OSCAR initial conditions.
Expand Down Expand Up @@ -369,25 +364,29 @@ def generate_eBQS_densities_Milne_from_OSCAR_IC(
None
"""
if not all(
isinstance(val, (float, int))
for val in [x_min, x_max, y_min, y_max, z_min, z_max, sigma_smear]
):
isinstance(
val,
(float,
int)) for val in [
x_min,
x_max,
y_min,
y_max,
z_min,
z_max,
sigma_smear]):
raise TypeError("Coordinates and sigma_smear must be float or int")
if not all((isinstance(val, int) and val > 0) for val in [Nx, Ny, Nz]):
raise TypeError("Nx, Ny, Nz must be positive integers")
if not all(
(isinstance(val, (float, int)) and val > 0)
for val in [n_sigma_x, n_sigma_y, n_sigma_z]
):
if not all((isinstance(val, (float, int)) and val > 0)
for val in [n_sigma_x, n_sigma_y, n_sigma_z]):
raise TypeError(
"n_sigma_x, n_sigma_y, n_sigma_z must be positive float or int"
)
"n_sigma_x, n_sigma_y, n_sigma_z must be positive float or int")
if not isinstance(eta_range, (list, tuple)):
raise TypeError("eta_range must be a list or tuple")
if len(eta_range) != 3:
raise ValueError(
"eta_range must contain min, max, and number of grid points"
)
"eta_range must contain min, max, and number of grid points")
if not all(isinstance(val, (float, int)) for val in eta_range):
raise TypeError("Values in eta_range must be float or int")
if not isinstance(output_filename, str):
Expand All @@ -398,8 +397,8 @@ def generate_eBQS_densities_Milne_from_OSCAR_IC(
raise TypeError(
"The smearing function only works with EventCharacteristics derived from particles."
)
if not isinstance(self.event_data_, (list, np.ndarray)):
raise TypeError("The input is not a list nor a numpy.ndarray.")
if not isinstance(self.event_data_, list):
raise TypeError("The input is not a list.")

energy_density = Lattice3D(
x_min,
Expand Down Expand Up @@ -481,16 +480,9 @@ def generate_eBQS_densities_Milne_from_OSCAR_IC(
# range
x = energy_density.x_values_
y = energy_density.y_values_
eta = np.linspace(eta_range[0], eta_range[1], eta_range[2])
if (
1.05
* tau
* (
np.sinh(eta[int(eta_range[2] / 2.0)])
- np.sinh(eta[int(eta_range[2] / 2.0 - 1)])
)
< (z_max - z_min) / Nz
):
eta = np.linspace(eta_range[0], eta_range[1], int(eta_range[2]))
if (1.05 * tau * (np.sinh(eta[int(eta_range[2] / 2.)]) - np.sinh(
eta[int(eta_range[2] / 2. - 1)])) < (z_max - z_min) / Nz):
warnings.warn(
"Warning: The grid for z is not fine enough for the requested eta-grid."
)
Expand Down Expand Up @@ -559,23 +551,22 @@ def generate_eBQS_densities_Milne_from_OSCAR_IC(
)

def generate_eBQS_densities_Minkowski_from_OSCAR_IC(
self,
x_min: float,
x_max: float,
y_min: float,
y_max: float,
z_min: float,
z_max: float,
Nx: int,
Ny: int,
Nz: int,
n_sigma_x: float,
n_sigma_y: float,
n_sigma_z: float,
sigma_smear: float,
output_filename: str,
IC_info: Optional[str] = None,
) -> None:
self,
x_min: Union[float, int],
x_max: Union[float, int],
y_min: Union[float, int],
y_max: Union[float, int],
z_min: Union[float, int],
z_max: Union[float, int],
Nx: int,
Ny: int,
Nz: int,
n_sigma_x: Union[float, int],
n_sigma_y: Union[float, int],
n_sigma_z: Union[float, int],
sigma_smear: Union[float, int],
output_filename: str,
IC_info: Optional[str] = None) -> None:
"""
Generates energy, baryon, charge, and strangeness densities in
Minkowski coordinates from OSCAR initial conditions.
Expand Down Expand Up @@ -637,8 +628,8 @@ def generate_eBQS_densities_Minkowski_from_OSCAR_IC(
raise TypeError(
"The smearing function only works with EventCharacteristics derived from particles."
)
if not isinstance(self.event_data_, (list, np.ndarray)):
raise TypeError("The input is not a list nor a numpy.ndarray.")
if not isinstance(self.event_data_, list):
raise TypeError("The input is not a list.")

energy_density = Lattice3D(
x_min,
Expand Down
Loading
Loading