Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
scottshambaugh committed Nov 5, 2023
1 parent 5dfb712 commit 7b690f9
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/monaco/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def is_num(val : Any) -> bool:
return True


def length(x : Any) -> int:
def length(x : Any) -> int | None:
"""
Genericized length function that works on scalars (which have length 1).
Expand All @@ -116,7 +116,7 @@ def length(x : Any) -> int:
Returns
-------
x_len : int
The length of the input.
The length of the input. If not a sequence or scalar, returns None.
"""
if isinstance(x, Sized):
return len(x)
Expand Down
18 changes: 9 additions & 9 deletions src/monaco/mc_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self,
ncase : int,
ismedian : bool,
invars : dict[str, InVar],
constvals : dict[str, Any] = None,
constvals : dict[str, Any] | None = None,
keepsiminput : bool = True,
keepsimrawoutput : bool = True,
seed : int = np.random.get_state(legacy=False)['state']['key'][0],
Expand All @@ -78,21 +78,21 @@ def __init__(self,
self.keepsimrawoutput = keepsimrawoutput
self.seed = seed

self.starttime : datetime = None
self.endtime : datetime = None
self.runtime : timedelta = None
self.starttime : datetime | None = None
self.endtime : datetime | None = None
self.runtime : timedelta | None = None

self.filepath : Path = None
self.runsimid : int = None
self.filepath : Path | None = None
self.runsimid : int | None = None
self.haspreprocessed : bool = False
self.hasrun : bool = False
self.haspostprocessed : bool = False

self.invals : dict[str, InVal] = self.getInVals()
self.outvals : dict[str, OutVal] = dict()

self.siminput : tuple[Any] = None
self.simrawoutput : tuple[Any] = None
self.siminput : tuple[Any] | None = None
self.simrawoutput : tuple[Any] | None = None


def __repr__(self):
Expand Down Expand Up @@ -136,7 +136,7 @@ def addOutVal(self,
name : str,
val : Any,
split : bool = True,
valmap : dict[Any, float] = None
valmap : dict[Any, float] | None = None
) -> None:
"""
Generate an OutVal and add it to the dict of outvals.
Expand Down
8 changes: 4 additions & 4 deletions src/monaco/mc_multi_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def multi_plot(vars : list[InVar | OutVar],
cov_plot : bool = False,
cov_p : None | float | Iterable[float] = None,
invar_space : InVarSpace | Iterable[InVarSpace] = InVarSpace.NUMS,
fig : Figure = None,
fig : Figure | None = None,
title : str = '',
plotkwargs : dict = dict(),
) -> tuple[Figure, tuple[Axes, ...]]:
Expand Down Expand Up @@ -104,7 +104,7 @@ def multi_plot_2d_scatter_hist(varx : InVar | OutVar,
cov_p : None | float | Iterable[float] = None,
cumulative : bool = False,
invar_space : InVarSpace | Iterable[InVarSpace] = InVarSpace.NUMS,
fig : Figure = None,
fig : Figure | None = None,
title : str = '',
plotkwargs : dict = dict(),
) -> tuple[Figure, tuple[Axes, ...]]:
Expand Down Expand Up @@ -189,7 +189,7 @@ def multi_plot_grid_tri(vars : list[InVar | OutVar],
cov_p : None | float | Iterable[float] = None,
cumulative : bool = False,
invar_space : InVarSpace | Iterable[InVarSpace] = InVarSpace.NUMS,
fig : Figure = None,
fig : Figure | None = None,
title : str = '',
plotkwargs : dict = dict(),
) -> tuple[Figure, tuple[Axes, ...]]:
Expand Down Expand Up @@ -279,7 +279,7 @@ def multi_plot_grid_rect(varsx : InVar | OutVar | list[InVar | OutVar],
cov_p : None | float | Iterable[float] = None,
cumulative : bool = False,
invar_space : InVarSpace | Iterable[InVarSpace] = InVarSpace.NUMS,
fig : Figure = None,
fig : Figure | None = None,
title : str = '',
plotkwargs : dict = dict(),
) -> tuple[Figure, tuple[Axes, ...]]:
Expand Down
6 changes: 3 additions & 3 deletions src/monaco/mc_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,9 @@ def manage_axis(ax : Optional[Axes],


def apply_category_labels(ax : Axes,
varx : InVar | OutVar = None,
vary : InVar | OutVar = None,
varz : InVar | OutVar = None,
varx : InVar | OutVar | None = None,
vary : InVar | OutVar | None = None,
varz : InVar | OutVar | None = None,
) -> None:
"""
For nonnumeric Monte Carlo variables, use the `nummap` to label the axes.
Expand Down
4 changes: 2 additions & 2 deletions src/monaco/mc_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

def sampling(ndraws : int,
method : SampleMethod = SampleMethod.SOBOL_RANDOM,
ninvar : int = None,
ninvar_max : int = None,
ninvar : int | None = None,
ninvar_max : int | None = None,
seed : int = np.random.get_state(legacy=False)['state']['key'][0],
) -> np.ndarray:
"""
Expand Down
20 changes: 10 additions & 10 deletions src/monaco/mc_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __init__(self,
keepsimrawoutput : bool = True,
savesimdata : bool = False,
savecasedata : bool = False,
resultsdir : str | pathlib.Path = None,
resultsdir : str | pathlib.Path | None = None,
) -> None:

self.checkFcnsInput(fcns)
Expand Down Expand Up @@ -171,9 +171,9 @@ def __init__(self,
self.caseseeds : list[int] = []

self.inittime : datetime = datetime.now()
self.starttime : datetime = None
self.endtime : datetime = None
self.runtime : timedelta = None
self.starttime : datetime | None = None
self.endtime : datetime | None = None
self.runtime : timedelta | None = None

self.casespreprocessed : set[int] = set()
self.casesrun : set[int] = set()
Expand All @@ -187,11 +187,11 @@ def __init__(self,
self.ninvars : int = 0
self.noutvars : int = 0

self.corrcoeffs : np.ndarray = None
self.covs : np.ndarray = None
self.covvarlist : list[str] = None
self.corrcoeffs : np.ndarray | None = None
self.covs : np.ndarray | None = None
self.covvarlist : list[str] | None = None

self.runsimid : int = None
self.runsimid : int | None = None

self.ncases : int = ndraws + 1
self.setFirstCaseMedian(firstcaseismedian)
Expand Down Expand Up @@ -1288,7 +1288,7 @@ def importVars(self,

def importInVars(self,
filepath : str | pathlib.Path,
nummap : dict[Any, float] = None,
nummap : dict[Any, float] | None = None,
) -> None:
"""
Import draws from an external file as InVals.
Expand Down Expand Up @@ -1320,7 +1320,7 @@ def importInVars(self,

def importOutVars(self,
filepath : str | pathlib.Path,
valmap : dict[Any, float] = None,
valmap : dict[Any, float] | None = None,
) -> None:
"""
Import results from an external file as OutVals, convert to OutVars.
Expand Down
4 changes: 2 additions & 2 deletions src/monaco/mc_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self,
pct : float,
num : float,
dist : rv_discrete | rv_continuous,
nummap : dict[float, Any] = None,
nummap : dict[float, Any] | None = None,
ismedian : bool = False,
):

Expand Down Expand Up @@ -164,7 +164,7 @@ def __init__(self,
name : str,
ncase : int,
val : Any,
valmap : dict[Any, float] = None,
valmap : dict[Any, float] | None = None,
ismedian : bool = False,
):
super().__init__(name=name, ncase=ncase, ismedian=ismedian)
Expand Down
10 changes: 5 additions & 5 deletions src/monaco/mc_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ def stats(self) -> DescribeResult:

def addVarStat(self,
stat : VarStatType | Callable,
statkwargs : dict[str, Any] = None,
statkwargs : dict[str, Any] | None = None,
bootstrap : bool = False,
bootstrap_k : int = 10,
conf : float = 0.95,
seed : int = None,
name : str = None,
seed : int | None = None,
name : str | None = None,
) -> None:
"""
Add a variable statistic to this variable.
Expand Down Expand Up @@ -172,8 +172,8 @@ def clearVarStats(self) -> None:


def plot(self,
vary : InVar | OutVar = None,
varz : InVar | OutVar = None,
vary : InVar | OutVar | None = None,
varz : InVar | OutVar | None = None,
cases : None | int | Iterable[int] = None,
highlight_cases : None | int | Iterable[int] = empty_list(),
rug_plot : bool = False,
Expand Down
14 changes: 7 additions & 7 deletions src/monaco/mc_varstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ class VarStat:
def __init__(self,
var : Var,
stat : str | VarStatType | Callable,
statkwargs : dict[str, Any] = None,
statkwargs : dict[str, Any] | None = None,
bootstrap : bool = True,
bootstrap_k : int = 10,
conf : float = 0.95,
seed : int = np.random.get_state(legacy=False)['state']['key'][0],
name : str = None,
name : str | None = None,
):

self.var = var
Expand All @@ -149,11 +149,11 @@ def __init__(self,
raise ValueError(f'bootstrap_k = {bootstrap_k} must be >= 1')
self.bootstrap_k = bootstrap_k
self.conf = conf
self.confidence_interval_low_nums : np.ndarray = None
self.confidence_interval_high_nums : np.ndarray = None
self.confidence_interval_low_nums : list | np.ndarray = None
self.confidence_interval_high_nums : list | np.ndarray = None
self.confidence_interval_low_vals : list[Any] | np.ndarray = []
self.confidence_interval_high_vals : list[Any] | np.ndarray = []
self.bootstrap_n = None
self.bootstrap_n : int | None = None
self.seed = seed

if isinstance(stat, Callable):
Expand Down Expand Up @@ -270,7 +270,7 @@ def genStatsGaussianP(self) -> None:
def sigma(self,
x, # TODO: explicit typing here
sig : float,
axis : int = None,
axis : int | None = None,
) -> float:
"""
Calculate the sigma value of a normally distributed list of numbers.
Expand All @@ -290,7 +290,7 @@ def sigma(self,

def statsFunctionWrapper(self,
x : Any,
axis : int = None, # Needed for bootstrap vectorization
axis : int | None = None, # Needed for bootstrap vectorization
) -> Any:
"""
A wrapper function to allow using a bootstrap function that uses kwargs.
Expand Down
14 changes: 7 additions & 7 deletions src/monaco/order_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,13 @@ def get_iP(n : int,



def order_stat_var_check(n : int = None,
l : int = None,
u : int = None,
p : float = None,
k : int = None,
c : float = None,
nmax : int = None
def order_stat_var_check(n : int | None = None,
l : int | None = None,
u : int | None = None,
p : float | None = None,
k : int | None = None,
c : float | None = None,
nmax : int | None = None
) -> None:
"""
Check the validity of the inputs to the order statistic functions.
Expand Down

0 comments on commit 7b690f9

Please sign in to comment.