covsirphy.util package
Submodules
covsirphy.util.alias module
- class Alias(target_class=None)[source]
Bases:
Term
Remember and parse aliases, just like defaultdict.
- Parameters:
target_class (
Any
) – class of targets or None (all objects)
covsirphy.util.config module
covsirphy.util.error module
- exception EmptyError(name, details=None)[source]
Bases:
_ValidationError
Error when the dataframe is empty un-expectedly.
- exception ExperimentalWarning[source]
Bases:
Warning
Class to explain the method/function is experimental and its name, features and arguments may changed later.
- exception NAFoundError(name, value=None, details=None)[source]
Bases:
_ValidationError
Error when NA values are included un-expectedly.
- exception NotEnoughDataError(name, value, required_n, details=None)[source]
Bases:
_ValidationError
Error when we do not have enough data for analysis.
- exception NotIncludedError(key_name, container_name, details=None)[source]
Bases:
_ValidationError
Error when a necessary key was not included in a container.
- exception NotNoneError(name, value, details=None)[source]
Bases:
_ValidationError
Error when a value must be None but not None un-expectedly.
- exception NotRegisteredError(name, details=None)[source]
Bases:
UnExecutedError
Error when no records have been registered yet.
- exception NotSubclassError(name, target, parent, details=None)[source]
Bases:
_ValidationError
Error when an object is not a subclass of the parent class un-expectedly.
- exception ScenarioNotFoundError(name, details=None)[source]
Bases:
_BaseException
Error when unregistered scenario name was specified.
- exception SubsetNotFoundError(geo=None, country=None, country_alias=None, province=None, start_date=None, end_date=None, date=None, details=None)[source]
Bases:
_BaseException
Error when subset was failed with specified arguments.
- Parameters:
geo (tuple(list[str] or tuple(str) or str) or str or None) – location names to filter or None
country (str) – country name
country_alias (str or None) – country name used in the dataset
province (str or None) – province name
start_date (str or None) – start date, like 22Jan2020
end_date (str or None) – end date, like 01Feb2020
date (str or None) – specified date, like 22Jan2020
details (str or None) – details of error
- exception UnExecutedError(name, details=None)[source]
Bases:
_BaseException
Error when we have unexecuted methods that we need to run in advance.
- exception UnExpectedLengthError(name, value, length, details=None)[source]
Bases:
_ValidationError
Error when a sequence has un-expended length.
- exception UnExpectedNoneError(name, details=None)[source]
Bases:
_ValidationError
Error when a value is None un-expectedly.
- exception UnExpectedTypeError(name, target, expected, details=None)[source]
Bases:
_ValidationError
Error when an object cannot be converted to an instance un-expectedly.
- exception UnExpectedValueError(name, value, candidates, details=None)[source]
Bases:
_ValidationError
Error when unexpected value was applied as the value of an argument.
- exception UnExpectedValueRangeError(name, target, value_range, details=None)[source]
Bases:
_ValidationError
Error when the value is out of value range.
covsirphy.util.evaluator module
- class Evaluator(y_true, y_pred, how='inner', on=None)[source]
Bases:
object
Evaluate residual errors.
- Parameters:
y_true (pandas.DataFrame or pandas.Series) – correct target values
y_pred (pandas.DataFrame or pandas.Series) – estimated target values
how (str) – “all” (use all records) or “inner” (intersection will be used)
on (str or list[str] or None) – column names to join on or None (join on index)
- Raises:
NAFoundError – either @y_true or @pred has NA values
- classmethod metrics()[source]
Return the list of metric names.
- Returns:
list[str] – list of metric names
- score(metric='RMSLE')[source]
Calculate score with specified metric.
- Parameters:
metric (str) – ME, MAE, MSE, MSLE, MAPE, RMSE, RMSLE, R2
- Raises:
UnExpectedValueError – un-expected metric was applied
ValueError – ME was selected as metric when the targets have multiple columns
- Returns:
float – score with the metric
Note
ME: maximum residual error MAE: mean absolute error MSE: mean square error MSLE: mean squared logarithmic error MAPE: mean absolute percentage error RMSE: root mean squared error RMSLE: root mean squared logarithmic error R2: the coefficient of determination
covsirphy.util.filer module
- class Filer(directory, prefix=None, suffix=None, numbering=None)[source]
Bases:
object
Produce filenames and manage files.
- Parameters:
Examples
>>> import covsirphy as cs >>> filer = cs.Filer(directory="output", prefix="jpn", suffix=None, numbering="01") >>> filer.png("records") {"filename": "<absolute path>/output/jpn_01_records.png"} >>> filer.jpg("records") {"filename": "<absolute path>/output/jpn_01_records.jpg"} >>> filer.json("backup") {"filename": "<absolute path>/output/jpn_01_backup.json"} >>> filer.geojson("geometry", "driver": "GeoJson") {"filename": "<absolute path>/output/jpn_01_geometry.geojson", "driver": "GeoJson"} >>> filer.csv("records", index=True) {"path_or_buf": "<absolute path>/output/jpn_01_records.csv", index: True}
- csv(title, **kwargs)[source]
Create CSV filename and register it.
- Parameters:
title (str) – title of the filename, like ‘records’
kwargs – keyword arguments to be included in the output
- Returns:
dict[str, str] – absolute filename (key: ‘path_or_buf’) and kwargs
- files(ext=None)[source]
List-up filenames.
- Parameters:
ext (str or None) – file extension or None (all)
- Returns:
list[str] – list of files
- geojson(title, **kwargs)[source]
Create GeoJSON filename and register it.
- Parameters:
title (str) – title of the filename, like ‘records’
kwargs – keyword arguments to be included in the output
- Returns:
dict[str, str] – absolute filename (key: ‘filename’) and kwargs
- jpg(title, **kwargs)[source]
Create JPG filename and register it.
- Parameters:
title (str) – title of the filename, like ‘records’
kwargs – keyword arguments to be included in the output
- Returns:
dict[str, str] – absolute filename (key: ‘filename’) and kwargs
covsirphy.util.stopwatch module
covsirphy.util.term module
- class Term[source]
Bases:
object
Term definition.
- A = '_actual'
- ACTUAL = 'Actual'
- AREA_ABBR_COLS = ['ISO3', 'Country', 'Province']
- AREA_COLUMNS = ['Country', 'Province']
- C = 'Confirmed'
- CI = 'Infected'
- CITY = 'City'
- COLUMNS = ['Date', 'Country', 'Province', 'Confirmed', 'Infected', 'Fatal', 'Recovered']
- COUNTRY = 'Country'
- DATE = 'Date'
- DATE_FORMAT = '%d%b%Y'
- DATE_FORMAT_DESC = 'DDMmmYYYY'
- DSIFR_COLUMNS = ['Date', 'Susceptible', 'Infected', 'Fatal', 'Recovered']
- E = 'Exposed'
- END = 'End'
- F = 'Fatal'
- FIG_COLUMNS = ['Infected', 'Fatal', 'Recovered', 'Fatal or Recovered', 'Vaccinated', 'Exposed', 'Waiting']
- FITTED = 'Fitted'
- FR = 'Fatal or Recovered'
- FUTURE = 'Future'
- ID = 'ID'
- INITIAL = 'Initial'
- ISO3 = 'ISO3'
- MAIN = 'Main'
- MODERATE = 'Moderate'
- MONO_COLUMNS = ['Confirmed', 'Fatal', 'Recovered']
- N = 'Population'
- NA = '-'
- NLOC_COLUMNS = ['Date', 'Confirmed', 'Infected', 'Fatal', 'Recovered']
- ODE = 'ODE'
- OTHERS = 'Others'
- P = '_predicted'
- PARAM_DICT = 'param_dict'
- PAST = 'Past'
- PHASE = 'Phase'
- PRODUCT = 'Product'
- PROVINCE = 'Province'
- R = 'Recovered'
- RATE_COLUMNS = ['Fatal per Confirmed', 'Recovered per Confirmed', 'Fatal per (Fatal or Recovered)']
- RT = 'Rt'
- RT_FULL = 'Reproduction number'
- RUNTIME = 'Runtime'
- S = 'Susceptible'
- SEP = '/'
- SERIES = 'Scenario'
- SEVERE = 'Severe'
- START = 'Start'
- STEP_N = 'step_n'
- STR_COLUMNS = ['Date', 'Country', 'Province']
- SUB_COLUMNS = ['Date', 'Confirmed', 'Infected', 'Fatal', 'Recovered', 'Susceptible']
- SUFFIX_DICT = {1: 'st', 2: 'nd', 3: 'rd'}
- T = 'Elapsed'
- TAU = 'tau'
- TENSE = 'Type'
- TESTS = 'Tests'
- TESTS_DIFF = 'Tests_diff'
- TRIALS = 'Trials'
- TS = 't'
- V = 'Vaccinated'
- VAC = 'Vaccinations'
- VAC_BOOSTERS = 'Vaccinations_boosters'
- VALUE_COLUMNS = ['Confirmed', 'Infected', 'Fatal', 'Recovered']
- V_FULL = 'Vaccinated_full'
- V_ONCE = 'Vaccinated_once'
- W = 'Waiting'
- Y0_DICT = 'y0_dict'
covsirphy.util.validator module
- class Validator(target, name='target', accept_none=True)[source]
Bases:
object
Validate objects and arguments.
- Parameters:
- Raises:
NAFoundError – @accept_none is False, but @target is None
Note
When @accept_none is True and @target is None, default values will be returned with instance methods.
- dataframe(time_index=False, columns=None, empty_ok=True)[source]
Ensure the target is a dataframe.
- Parameters:
- Raises:
UnExpectedTypeError – the target is not a dataframe or that has un-expected-type index
EmptyError – empty when @empty_ok is False
NotIncludedError – expected columns were not included
- Return type:
- Returns:
pandas.DataFrame – the target itself
- date(value_range=(None, None), default=None)[source]
Convert a value to a date object.
- Parameters:
- Raises:
UnExpectedNoneError – the default value is None when the target is None
UnExpectedTypeError – the target cannot be converted to a date object
UnExpectedValueRangeError – the value is out of value range
- Return type:
- Returns:
converted date
- dict(default=None, required_keys=None, errors='coerce')[source]
Ensure the target is a dictionary.
- Parameters:
- Raises:
UnExpectedTypeError – the target is not a dictionary
NAFoundError – values of the required keys are not specified when @errors=”raise”
- Return type:
- Returns:
the target is self with default values and required keys
Note
All keys of @default will be included and the target will overwrite it.
Note
If some keys of @required_keys are not included and @errors=”coerce”, None will be set as the values of the keys.
- float(value_range=(0, None), default=None, digits=None)[source]
Convert a value to a float value.
- Parameters:
- Raises:
UnExpectedNoneError – the default value is None when the target is None
UnExpectedTypeError – the target cannot be converted to a float value
UnExpectedValueRangeError – the value is out of value range
- Return type:
- Returns:
converted float value
- instance(expected)[source]
Ensure that the target is an instance of a specified class.
- Parameters:
expected (
Any
) – expected class or sequence of expected classes- Raises:
UnExpectedTypeError – the target is not an instance of the class
- Return type:
- Returns:
the target itself
- int(value_range=(0, None), default=None, round_ok=False)[source]
Convert a value to an integer.
- Parameters:
- Raises:
UnExpectedNoneError – the default value is None when the target is None
UnExpectedTypeError – the target cannot be converted to an integer or round-off error exists when @round_ok is False
UnExpectedValueRangeError – the value is out of value range
- Return type:
- Returns:
converted float value
- kwargs(functions, default=None)[source]
Find keyword arguments of the functions.
- Parameters:
- Raises:
UnExpectedTypeError – the target is not a dictionary
- Return type:
- Returns:
dict – keyword arguments of the functions
- sequence(default=None, flatten=False, unique=False, candidates=None, length=None)[source]
Convert a sequence (list, tuple) to a list.
- Parameters:
default (
Optional
[Iterable
[Any
]]) – default value when the target is Noneflatten (
bool
) – whether flatten the sequence or notunique (
bool
) – whether remove duplicated values or not, the first value will remaincandidates (
Optional
[Iterable
[Any
]]) – list of candidates or None (no limitations)length (
int
|None
) – length of the sequence or None (no limitations)
- Raises:
UnExpectedTypeError – the target cannot be converted to a list or failed in flattening
UnExpectedValueError – the target has a value which is not included in the candidates
UnExpectedLengthError – the number of elements is not the same as @length
- Return type:
- Returns:
converted list or empty list (when both of the target and @default are None)
- subclass(parent)[source]
Ensure the target is a subclass of the parent class.
- Parameters:
parent (
Any
) – parent class or sequence of parent classes- Raises:
NotSubclassError – the target is not the subclass
- Return type:
- Returns:
the target itself
- tau(default=None)[source]
Validate the value can be used as tau value [min].
- Parameters:
default (
int
|None
) – default value when the target is None- Raises:
UnExpectedTypeError – the target cannot be converted to an integer
UnExpectedValueRangeError – the value is out of value range
- Return type:
- Returns:
converted float value or None (when both of the target and @default are None)