Model Classes
- class floatcsep.model.Model(name, zenodo_id=None, giturl=None, repo_hash=None, authors=None, doi=None, **kwargs)[source]
Bases:
ABCThe Model class represents a forecast generating system. It can represent a source code, a collection or a single forecast, etc. A Model can be instantiated from either the filesystem or host repositories.
- Parameters:
name (str) – Name of the model
model_path (str) – Relative path of the model (file or code) to the work directory
zenodo_id (int) – Zenodo ID or record of the Model
giturl (str) – Link to a git repository
repo_hash (str) – Specific commit/branch/tag hash.
doi (str) – Digital Object Identifier metadata:
- as_dict(excluded=('name', 'repository', 'workdir', 'environment'))[source]
- Returns:
Dictionary with relevant attributes. Model can be re-instantiated from this dict
- abstractmethod create_forecast(tstring, **kwargs)[source]
Creates a forecast based on the model’s logic.
- Parameters:
tstring (str)
- Return type:
None
- classmethod factory(model_cfg)[source]
Factory method. Instantiate first on any explicit option provided in the model configuration.
- classmethod from_dict(record, **kwargs)[source]
Returns a Model instance from a dictionary containing the required attributes. Can be used to quickly instantiate from a .yml file.
- Parameters:
record (dict) –
Contains the keywords from the
__init__method.Note
Must have either an explicit key name, or it must have exactly one key with the model’s name, whose values are the remaining
__init__keywords.- Returns:
A Model instance
- class floatcsep.model.TimeIndependentModel(name, model_path, forecast_unit=1, store_db=False, **kwargs)[source]
Bases:
ModelA Model whose forecast is invariant in time. A TimeIndependentModel is commonly represented by a single forecast as static data.
- Parameters:
- create_forecast(tstring, **kwargs)[source]
Creates a forecast from the model source and a given time window.
Note
Dummy function for this class, although eventually could also be a source code (e.g., a Smoothed-Seismicity-Model built from the input-catalog).
- Parameters:
tstring (str)
- Return type:
None
- class floatcsep.model.TimeDependentModel(name, model_path, func=None, func_kwargs=None, args_file='args.txt', input_cat='catalog.csv', fmt='csv', **kwargs)[source]
Bases:
ModelModel that creates varying forecasts depending on a time window. Requires either a collection of Forecasts or a function/source code that returns a Forecast.
- Parameters:
name (str) – The name of the model
model_path (str) – The path to either the source code, or the folder containing static forecasts.
func (str | Callable) – A function/command that runs the model.
func_kwargs (dict) – The keyword arguments to run the model. They are usually (over)written into the file {model_path}/input/{args_file}
args_file (str) – Name of the arguments file that will be used to create forecasts
input_cat (str) – Name of the file that will be used as input catalog to create forecasts
**kwargs – Additional keyword parameters, such as a
prefix(str) for the resulting forecast file paths,args_file(str) as the path for the model arguments file orinput_catthat indicates where the input catalog will be placed for the model.fmt (str)
- create_forecast(tstring, **kwargs)[source]
Creates a forecast from the model source and a given time window.
Note
The argument
tstringis formatted according to how the Experiment handles time_windows, specified in the functionstimewindow2str()andstr2timewindow()- Parameters:
tstring (str) – String representing the start and end of the forecast, formatted as ‘YY1-MM1-DD1_YY2-MM2-DD2’.
**kwargs
- Return type:
None
- get_forecast(tstring=None, region=None)[source]
Wrapper that returns a forecast, by accessing the model’s forecast repository.
Note
The argument
tstringis formatted according to how the Experiment handles time_windows, specified in the functionstimewindow2str()andstr2timewindow()- Parameters:
- Return type:
GriddedForecast | CatalogForecast | List[GriddedForecast] | List[CatalogForecast]
- get_source(zenodo_id=None, giturl=None, **kwargs)[source]
Search, download or clone the model source in the filesystem from git or zenodo, respectively.
- prepare_args(start, end, **kwargs)[source]
When the model is a source code, the args file is a plain text file with the required input arguments. At minimum, it consists of the start and end of the forecast timewindow, but it can also contain other arguments (e.g., minimum magnitude, number of simulations, cutoff learning magnitude, etc.)
- prepare_extra_input(start, end, **kwargs)[source]
- When the model is a source code, and the run is in parallel, additional data located in
the model’s input folder is copied onto the mounted folder (in /tmp or /results)
- stage(time_windows=None, run_mode='sequential', stage_dir='results', run_id='run')[source]
Retrieve model artifacts and Set up its interface with the experiment.
Get the model from filesystem, Zenodo or Git. Prepares the directory
If source code, creates the computational environment (conda, venv or Docker)
Prepares the registry tree: filepaths/keys corresponding to existing forecasts and those to be generated, as well as input catalog and arguments file.
- Return type:
None