simulation_api.controller¶
Package contents¶
Submodules¶
simulation_api.controller.main¶
This file manages all requests that are made to our app
-
async
simulation_api.controller.main.api_results_sim_id_pickle(sim_id: str)[source]¶ Download pickle of previously requested simulation.
- Parameters
sim_id (str) – ID of the simulation.
- Returns
FileResponsecontaining the simulation results in pickle format.- Return type
starlette.responses.FileResponse
-
async
simulation_api.controller.main.api_results_sim_id_plot(sim_id: str, value: Union[simulation_api.controller.schemas.PlotQueryValues_HO, simulation_api.controller.schemas.PlotQueryValues_ChenLee])[source]¶ Download plot of previously requested simulation.
Note one query param is required here.
Here we use FileResponse from starlette.responses
- Parameters
sim_id (str) – ID of the simulation.
value (PlotQueryValues) – Query values. Must be a member of one of the Enum classes given in PlotQueryValues.
- Returns
FileResponsecontaining the requested plot.- Return type
starlette.responses.FileResponse
-
async
simulation_api.controller.main.api_simulate_sim_system(sim_system: simulation_api.controller.schemas.SimSystem, sim_params: simulation_api.controller.schemas.SimRequest, background_tasks: starlette.background.BackgroundTasks, db: sqlalchemy.orm.session.Session = Depends(get_db)) → simulation_api.controller.schemas.SimIdResponse[source]¶ In this route the client can request a simulation.
When the client requests a simulation via
/api/simulate/{sim_system}, he/she obtains relevant information on how to get the results of the simulation.Note
Here we use
fastapi.BackgroudTasksto make the simulation in the background.- Parameters
sim_system (SimSystem) – System to be simulated.
sim_params (SimRequest) – Simulation request information with schema given by SimRequest and decalared in schemas.py.
background_tasks (BackgroundTasks) – Background task FastAPI manager (Class). This is handled internally.
db (Session) – Database Session, needed to interact with database. This is handled internally.
- Returns
sim_id_response – Contains relevant information about the simulation and how to get the results.
- Return type
-
async
simulation_api.controller.main.api_simulate_status_sim_id(sim_id: str, db: sqlalchemy.orm.session.Session = Depends(get_db)) → simulation_api.controller.schemas.SimStatus[source]¶ Obtains status of requested simulation.
- Parameters
sim_id (str) – ID of the simulation.
db (Session) – Database Session, needed to interact with database. This is handled internally.
- Returns
Status information of the simulation and how to get the results.
- Return type
-
async
simulation_api.controller.main.custom_http_exception_handler(request: starlette.requests.Request, exc: starlette.exceptions.HTTPException)[source]¶ Handles 404 exceptions by rendering a template.
-
simulation_api.controller.main.get_db()[source]¶ Starts and ends session in each route that needs database access
-
async
simulation_api.controller.main.index(request: starlette.requests.Request)[source]¶ Index frontend web page.
- Parameters
request (Request) – HTTP request, used internally by FastAPI.
- Returns
Renders greeting template and hyperlinks to simulation rquests and results.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.results(request: starlette.requests.Request, db: sqlalchemy.orm.session.Session = Depends(get_db))[source]¶ Renders web page showing a list of all the available simulation results.
- Parameters
request (Request) – HTTP request, used internally by FastAPI.
db (Session) – Database Session, needed to interact with database. This is handled internally.
- Returns
Template displaying all the available simulation results.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.results_sim_system_sim_id(request: starlette.requests.Request, sim_system: simulation_api.controller.schemas.SimSystem, sim_id: str)[source]¶ Shows graphic results of a simulation in frontend for a given
sim_id.- Parameters
request (Request) – HTTP request, used internally by FastAPI.
sim_system (SimSystem) – System to be simulated.
sim_id (str) – ID of the simulation.
- Returns
Template displaying all the generated plots for a given simulation.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.simulate(request: starlette.requests.Request)[source]¶ Simulate web page.
Here the clients can select between the available systems to simulate their preferred one.
- Parameters
request (Request) – HTTP request, used internally by FastAPI.
- Returns
Template displaying the available systems to be simulated.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.simulate_id_sim_id(request: starlette.requests.Request, sim_id: str)[source]¶ Shows simulation id after asking for simulation in frontend form.
- Parameters
request (Request) – HTTP request, used internally by FastAPI.
sim_id (str) – ID of the simulation.
- Returns
Template displaying the simulation ID and a hyperlink to further information about the simulation.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.simulate_sim_system(request: starlette.requests.Request, sim_system: simulation_api.controller.schemas.SimSystem, error_message: Optional[str] = '')[source]¶ Simulation’s form web page.
The clients input the desired parameters for the simulation of their choosing.
- Parameters
request (Request) – HTTP request, used internally by FastAPI.
sim_system (SimSystem) – System to be simulated.
error_message (str) – Internally used by the backend to display error messages in forntend form.
- Returns
Template displaying the simulation request form.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
-
async
simulation_api.controller.main.simulate_sim_system_post(request: starlette.requests.Request, sim_system: simulation_api.controller.schemas.SimSystem, background_tasks: starlette.background.BackgroundTasks, db: sqlalchemy.orm.session.Session = Depends(get_db), sim_sys: simulation_api.controller.schemas.SimSystem = Form(Ellipsis), username: str = Form(Ellipsis), t0: float = Form(Ellipsis), tf: float = Form(Ellipsis), dt: float = Form(Ellipsis), t_steps: int = Form(Ellipsis), method: simulation_api.controller.schemas.IntegrationMethods = Form(Ellipsis))[source]¶ Receives the simulation request information from the frontend form and requests the simulation to the backend.
This route receives the form requesting a simulation (filled in frontend via GET in route
/simulate/{sim_system}). The simulation is internally requested using the functionsimulation_api.controller.tasks._api_simulation_request(). Finally the client is redirected to the “Simulation ID” frontend web page, where further information about the simulation is displayed.- Parameters
request (Request) – HTTP request, used internally by FastAPI.
sim_system (SimSystem) – System to be simulated.
background_tasks (BackgroundTasks) – Needed to request simulation to the backend (in background). Handled internally by the API.
db (Session) – Database Session, needed to interact with database. This is handled internally.
sim_sys (SimSystem) – Form entry: system to be simulated. Must be one of the members of SimSystem.
username (str) – Form entry: name of the user requesting the simulation.
t0 (float) – Form entry: initial time of simulation.
tf (float) – Form entry: final time of simulation.
dt (float) – Form entry: timestep of simulation.
t_steps – Form entry: number of time steps. If different from 0 or
None,dtis ignored.method (IntegrationMethods) – Form entry: method of integration. Must be a member of IntegrationMethods.
- Returns
If the client made a mistake filling the form renders the simulation request form again. Otherwise, redirects the client to “Simulation ID” frontend web page.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponseorstarlette.responses.RedirectResponse
Note
The values of the form accessed by
fastapi.Formare only declared as parameters so that pydantic checks the types, but they are not used directly to request the simulation. Here, we access the form directly by using the methodfastapi.Request.form()as can be seen in the first lines of this function. This allows us a better control over the data and also to handle different type of forms –which depend on the simulation because parameters and initial conditions are intrinsically different for different systems.
-
async
simulation_api.controller.main.simulate_status_sim_id(request: starlette.requests.Request, sim_id: str, db: sqlalchemy.orm.session.Session = Depends(get_db))[source]¶ Shows simulation status for a given simulation via its
sim_id.- Parameters
request (Request) – HTTP request, used internally by FastAPI.
sim_id (str) – ID of the simulation.
db (Session) – Database Session, needed to interact with database. This is handled internally.
- Returns
Template displaying the simulation status and a hyperlinks to simulation results in several formats. If simulation id is not available (not yet in database), renders a message about the situation.
- Return type
fastapi.templating.Jinja2Templates.TemplateResponse
simulation_api.controller.schemas¶
This module defines all the models/schemas needed in our application, except the database models required for sqlalchemy ORM.
-
class
simulation_api.controller.schemas.ChenLeeParams(*, a: float, b: float, c: float)[source]¶ Bases:
pydantic.main.BaseModelList of parameters of the Chen-Lee Attractor system.
Note
For more information about Chen-Lee Attactor’s parameters see
ChenLeeAttractor.Warning
This needs update each time a new simulation is added: add an appropiate new class similar to this one or to
HOParams.-
a: float¶ \(\omega_x\) parameter.
-
b: float¶ \(\omega_y\) parameter.
-
c: float¶ \(\omega_z\) parameter.
-
-
class
simulation_api.controller.schemas.ChenLeeSimForm(*, username: str = 'Pepito', t0: float = 0.0, tf: float = 6.283185307179586, dt: float = 0.3141592653589793, t_steps: int = 0, method: simulation_api.controller.schemas.IntegrationMethods = 'RK45', sim_sys: simulation_api.controller.schemas.SimSystem = 'Chen-Lee-Attractor', ini0: float = 10.0, ini1: float = 10.0, ini2: float = 0.0, param0: float = 3.0, param1: float = - 5.0, param2: float = - 1.0)[source]¶ Bases:
simulation_api.controller.schemas.SimFormSchema used to Request Chen Lee Simulation in Frontend via form.
Note
For more information about Chen-Lee Attactor simulation see
ChenLeeAttractor.-
dt: Optional[float]¶ Time step size of simulation.
-
ini0: Optional[float]¶ \(\omega_x\) initial condition.
-
ini1: Optional[float]¶ \(\omega_y\) initial condition.
-
ini2: Optional[float]¶ \(\omega_z\) initial condition.
-
method: Optional[simulation_api.controller.schemas.IntegrationMethods]¶ Integration method used in simulation.
-
param0: Optional[float]¶ Parameter of name
ChenLeeParams.a.
-
param1: Optional[float]¶ Parameter of name
ChenLeeParams.b.
-
param2: Optional[float]¶ Parameter of name
ChenLeeParams.c.
-
sim_sys: simulation_api.controller.schemas.SimSystem¶ System to be simulated.
-
t0: Optional[float]¶ Initial time of simulation.
-
t_steps: Optional[int]¶ Number of time steps. If different from 0, dt is ignored.
-
tf: Optional[float]¶ Final time of simulation.
-
username: Optional[str]¶
-
-
class
simulation_api.controller.schemas.HOParams(*, m: float, k: float)[source]¶ Bases:
pydantic.main.BaseModelList of parameters of the Harmonic Oscillator system.
Note
For more information about Harmonic Oscillator’s parameters see
HarmonicOsc1D.Warning
This needs update each time a new simulation is added: add an appropiate new class similar to this one or to
ChenLeeParams.-
k: float¶ Force constant of object.
-
m: float¶ Mass of object.
-
-
class
simulation_api.controller.schemas.HOSimForm(*, username: str = 'Pepito', t0: float = 0.0, tf: float = 6.283185307179586, dt: float = 0.3141592653589793, t_steps: int = 0, method: simulation_api.controller.schemas.IntegrationMethods = 'RK45', sim_sys: simulation_api.controller.schemas.SimSystem = 'Harmonic-Oscillator', ini0: float = 1.0, ini1: float = 0.0, param0: float = 1.0, param1: float = 1.0)[source]¶ Bases:
simulation_api.controller.schemas.SimFormSchema used to Request Harmonic Oscillator Simulation in Frontend via form.
Note
For more information about Chen-Lee Attactor simulation see
HarmonicOsc1D.-
dt: Optional[float]¶ Time step size of simulation.
-
ini0: Optional[float]¶ \(q\) initial value.
-
ini1: Optional[float]¶ \(p\) initial value.
-
method: Optional[simulation_api.controller.schemas.IntegrationMethods]¶ Integration method used in simulation.
-
param0: Optional[float]¶ Parameter of name
HOParams.m.
-
param1: Optional[float]¶ Parameter of name
HOParams.k.
-
sim_sys: simulation_api.controller.schemas.SimSystem¶ System to be simulated.
-
t0: Optional[float]¶ Initial time of simulation.
-
t_steps: Optional[int]¶ Number of time steps. If different from 0, dt is ignored.
-
tf: Optional[float]¶ Final time of simulation.
-
username: Optional[str]¶
-
-
class
simulation_api.controller.schemas.IntegrationMethods(value)[source]¶ Bases:
str,enum.EnumList of available integration methods
Note
For more information about these integration methods see scipy.integrate.solve_ivp.
Warning
Please update this class with relvant simulation methods available in scipy.integrate.solve_ivp –only the ones that do not require more parameters than the ones provided in
SimRequest.-
RK23= 'RK23'¶ Explicit Runge-Kutta method of order 3(2).
-
RK45= 'RK45'¶ Explicit Runge-Kutta method of order 5(4).
-
-
class
simulation_api.controller.schemas.IntegrationMethodsFrontend(value)[source]¶ Bases:
str,enum.EnumList of captions of available integration methods. These are displayed in frontend simulation form.
Warning
This class needs update each time a new simulation is added: add an appropiate new attribute.
-
RK23= 'Runge-Kutta 3(2)'¶
-
RK45= 'Runge-Kutta 5(4)'¶
-
-
class
simulation_api.controller.schemas.ParamType(value)[source]¶ Bases:
str,enum.EnumThese are the possible values of
param_typecolumn inparameterstable insimulations.dbdatabase.-
ini_cndtn= 'initial condition'¶
-
param= 'parameter'¶
-
-
class
simulation_api.controller.schemas.ParameterDBSch(*, sim_id: str = None, param_type: simulation_api.controller.schemas.ParamType = None, param_key: str = None, ini_cndtn_id: int = None, value: float = None, param_id: int = None)[source]¶ Bases:
simulation_api.controller.schemas.ParameterDBSchBaseModel for API type checking when reading a row in
parameterstable insimulations.dbdatabase.-
class
Config[source]¶ Bases:
objectThis class is needed for database reading optimization (thanks to Object Relational Mapper –ORM.)
Note
Read more in FastAPI docs.
-
orm_model= True¶
-
-
ini_cndtn_id: Optional[int]¶
-
param_id: Optional[int]¶
-
param_key: Optional[str]¶
-
param_type: Optional[simulation_api.controller.schemas.ParamType]¶
-
sim_id: Optional[str]¶
-
value: Optional[float]¶
-
class
-
class
simulation_api.controller.schemas.ParameterDBSchBase(*, sim_id: str = None, param_type: simulation_api.controller.schemas.ParamType = None, param_key: str = None, ini_cndtn_id: int = None, value: float = None)[source]¶ Bases:
pydantic.main.BaseModelBasemodel for API type checking when querrying
parameterstable insimulations.dbdatabase.-
ini_cndtn_id: Optional[int]¶
-
param_key: Optional[str]¶
-
param_type: Optional[simulation_api.controller.schemas.ParamType]¶
-
sim_id: Optional[str]¶
-
value: Optional[float]¶
-
-
class
simulation_api.controller.schemas.ParameterDBSchCreate(*, sim_id: str, param_type: simulation_api.controller.schemas.ParamType, param_key: str = None, ini_cndtn_id: int = None, value: float)[source]¶ Bases:
simulation_api.controller.schemas.ParameterDBSchBaseModel for API type checking when creating a row in
parameterstable insimulations.dbdatabase.-
ini_cndtn_id: Optional[int]¶
-
param_key: Optional[str]¶
-
param_type: simulation_api.controller.schemas.ParamType¶
-
sim_id: str¶
-
value: float¶
-
-
class
simulation_api.controller.schemas.PlotDBSch(*, sim_id: str = None, plot_query_value: str = None, plot_id: int = None)[source]¶ Bases:
simulation_api.controller.schemas.PlotDBSchBaseModel for API type checking when reading a row in
plotstable insimulations.dbdatabase.-
class
Config[source]¶ Bases:
objectThis class is needed for database reading optimization (thanks to Object Relational Mapper –ORM.)
Note
Read more in FastAPI docs.
-
orm_model= True¶
-
-
plot_id: Optional[int]¶
-
plot_query_value: Optional[str]¶
-
sim_id: Optional[str]¶
-
class
-
class
simulation_api.controller.schemas.PlotDBSchBase(*, sim_id: str = None, plot_query_value: str = None)[source]¶ Bases:
pydantic.main.BaseModelBasemodel for API type checking when querrying
plotstable insimulations.dbdatabase.-
plot_query_value: Optional[str]¶
-
sim_id: Optional[str]¶
-
-
class
simulation_api.controller.schemas.PlotDBSchCreate(*, sim_id: str, plot_query_value: str)[source]¶ Bases:
simulation_api.controller.schemas.PlotDBSchBaseModel for API type checking when creating a ‘plot information’ row in
plotstable insimulations.dbdatabase.-
plot_query_value: str¶
-
sim_id: str¶
-
-
simulation_api.controller.schemas.PlotQueryValues¶ Union of the classes defining Enums of plot query values for each system. This is needed in
SimStatus.alias of Union[simulation_api.controller.schemas.PlotQueryValues_HO, simulation_api.controller.schemas.PlotQueryValues_ChenLee]
-
class
simulation_api.controller.schemas.PlotQueryValues_ChenLee(value)[source]¶ Bases:
str,enum.EnumList of tags of each different plot generated automatically by the backend when a Chen-Lee simulation is requested.
These tags are used as the possible values of the querry param
valuein route/api/results/{sim_id}/plot?value=<plot_query_value>.Note
simualtion_API.controller.schemas.SimIdResponse.sim_idmust be related to the Chen Lee system.-
project= 'project'¶
-
threeD= 'threeD'¶
-
-
class
simulation_api.controller.schemas.PlotQueryValues_HO(value)[source]¶ Bases:
str,enum.EnumList of tags of each different plot generated automatically by the backend when a Harmonic Oscillator simulation is requested.
These tags are used as the possible values of the querry param
valuein route/api/results/{sim_id}/plot?value=<plot_query_value>.Note
simualtion_API.controller.shcemas.SimIdResponse.sim_idmust be related to the Harmonic Oscillator system.-
coord= 'coord'¶
-
phase= 'phase'¶
-
-
class
simulation_api.controller.schemas.SimForm(*, username: str = 'Pepito', t0: float = 0.0, tf: float = 6.283185307179586, dt: float = 0.3141592653589793, t_steps: int = 0, method: simulation_api.controller.schemas.IntegrationMethods = 'RK45')[source]¶ Bases:
pydantic.main.BaseModelBasemodel of schema used to Request a Simulation in Frontend.
Warning
Needs update each time a new simulation is added:
Create a new appropiate class similar to
HOSimFormor toChenLeeSimForm.Add the class to the
dictSimFormDictdefined somewhere in this module.
-
dt: Optional[float]¶ Time step size of simulation.
-
method: Optional[simulation_api.controller.schemas.IntegrationMethods]¶ Integration method used in simulation.
-
t0: Optional[float]¶ Initial time of simulation.
-
t_steps: Optional[int]¶ Number of time steps. If different from 0, dt is ignored.
-
tf: Optional[float]¶ Final time of simulation.
-
username: Optional[str]¶
-
simulation_api.controller.schemas.SimFormDict= {'Chen-Lee-Attractor': <class 'simulation_api.controller.schemas.ChenLeeSimForm'>, 'Harmonic-Oscillator': <class 'simulation_api.controller.schemas.HOSimForm'>}¶ Maps the name of each available system to its simulation form model.
Warning
Needs update each time a new simulation is added: add a new appropiate item to this dict.
-
class
simulation_api.controller.schemas.SimIdResponse(*, sim_id: str = None, user_id: int = None, username: str = None, sim_sys: simulation_api.controller.schemas.SimSystem = None, sim_status_path: str = None, sim_pickle_path: str = None, message: str = None)[source]¶ Bases:
pydantic.main.BaseModelSchema for the response of a simulation request (requested via POST in route
/api/simulate/{sim_sys}.)Note
The request of the simulation must follow the model
SimRequest.-
message: Optional[str]¶ Explanatory message.
-
sim_id: Optional[str]¶ ID of simulation.
-
sim_pickle_path: Optional[str]¶ Path to GET (download) a pickle with the results of the simulation.
-
sim_status_path: Optional[str]¶ Path to GET the status of the simulation.
-
sim_sys: Optional[simulation_api.controller.schemas.SimSystem]¶ Simulated system.
-
user_id: Optional[int]¶ User id number stored in database.
-
username: Optional[str]¶
-
-
class
simulation_api.controller.schemas.SimRequest(*, system: simulation_api.controller.schemas.SimSystem = <SimSystem.HO: 'Harmonic-Oscillator'>, t_span: List[float] = [], t_eval: List[float] = [], t_steps: int = 0, ini_cndtn: List[float] = [], params: Dict[str, float], method: simulation_api.controller.schemas.IntegrationMethods = 'RK45', sim_id: str = None, user_id: int = 0, username: str = 'Pepito Perez')[source]¶ Bases:
pydantic.main.BaseModelSchema needed to request simulations via POST in
/api/request/{sim_system}.For the attributes that do not have a description see
simulation_api.simulation.simulations.Simulation.Note
Most of the attributes in this pydantic class are arguments of the classes defined in the module
simulation_api.simulation.simulations, for more information please refer to It.-
ini_cndtn: List[float]¶
-
method: Optional[simulation_api.controller.schemas.IntegrationMethods]¶
-
params: Dict[str, float]¶
-
sim_id: Optional[str]¶ ID of simulation. This is handled internally, leave it blank when requesting a simulation.
-
t_eval: Optional[List[float]]¶
-
t_span: List[float]¶
-
t_steps: Optional[int]¶
-
user_id: Optional[int]¶ User id number stored in database. This is handled internally, leave it blank when requesting a simulation.
-
username: str¶
-
-
class
simulation_api.controller.schemas.SimResults(*, sim_results: scipy.integrate._ivp.ivp.OdeResult)[source]¶ Bases:
pydantic.main.BaseModelResults of simulation as returned by
scipy.integrate.solve_ivp-
sim_results: scipy.integrate._ivp.ivp.OdeResult¶
-
-
class
simulation_api.controller.schemas.SimStatus(*, sim_id: str, user_id: int, date: datetime.datetime, system: simulation_api.controller.schemas.SimSystem = None, ini_cndtn: List[float] = None, params: Dict[str, float] = None, method: simulation_api.controller.schemas.IntegrationMethods = None, route_pickle: str = None, route_results: str = None, route_plots: str = None, plot_query_values: List[Union[simulation_api.controller.schemas.PlotQueryValues_HO, simulation_api.controller.schemas.PlotQueryValues_ChenLee]] = None, plot_query_receipe: str = "'route_plots' + '?value=' + 'plot_query_value'", success: bool = None, message: str = None)[source]¶ Bases:
pydantic.main.BaseModelSchema of the status of simulations.
This pydantic model is intended to store paths of results of the simulations algong with some metadata. This information can be accessed via GET in
/api/simulate/status/{sim_id}.-
date: datetime.datetime¶ Date of request of simulation.
-
ini_cndtn: Optional[List[float]]¶
-
message: Optional[str]¶ Additional information on status of simulation.
-
method: Optional[simulation_api.controller.schemas.IntegrationMethods]¶
-
params: Optional[Dict[str, float]]¶
-
plot_query_receipe: Optional[str]¶
-
plot_query_values: Optional[List[Union[simulation_api.controller.schemas.PlotQueryValues_HO, simulation_api.controller.schemas.PlotQueryValues_ChenLee]]]¶ Query params values of different automatically generated plots. These values are needed to download the plots in route
/api/results/{sim_id}/plot?value=<plot_query_value>.
-
route_pickle: Optional[str]¶ Route of pickle file generated by the simulation.
-
route_plots: Optional[str]¶ Route of plots generated by the simulation backend.
-
route_results: Optional[str]¶ Route of frontend showing results.
-
sim_id: str¶ ID of simulation.
-
success: Optional[bool]¶ Success status of simulation.
-
system: Optional[simulation_api.controller.schemas.SimSystem]¶ Simulated system.
-
user_id: int¶ User id number stored in database.
-
-
class
simulation_api.controller.schemas.SimSystem(value)[source]¶ Bases:
str,enum.EnumList of available systems for simulation.
Warning
The values of the attributes of this class must coincide with the dictionary keys defined in
simulation_api.simulation.simulations.Simulations, otherwise the system won’t be simulated by the backend.Warning
This class needs update each time a new simulation is added: add an appropiate new attribute.
-
ChenLee: str = 'Chen-Lee-Attractor'¶ Chen-Lee Attractor Enum attribute.
-
HO: str = 'Harmonic-Oscillator'¶ Harmonic Oscillator Enum attribute.
-
-
simulation_api.controller.schemas.SimSystem_to_SimParams= {'Chen-Lee-Attractor': <class 'simulation_api.controller.schemas.ChenLeeParams'>, 'Harmonic-Oscillator': <class 'simulation_api.controller.schemas.HOParams'>}¶ Maps the name of each available system to its parameters model
Warning
Needs update each time a new simulation is added: add a new appropiate item to this dict.
-
class
simulation_api.controller.schemas.SimulationDBSch(*, sim_id: str = None, user_id: int = None, date: str = None, system: str = None, method: str = None, route_pickle: str = None, route_results: str = None, route_plots: str = None, success: bool = None, message: str = None)[source]¶ Bases:
simulation_api.controller.schemas.SimulationDBSchBaseModel for API type checking when reading a row in
simulationstable insimulations.dbdatabase.-
class
Config[source]¶ Bases:
objectThis class is needed for database reading optimization (thanks to Object Relational Mapper –ORM.)
Note
Read more in FastAPI docs.
-
orm_model= True¶
-
-
date: Optional[str]¶
-
message: Optional[str]¶
-
method: Optional[str]¶
-
route_pickle: Optional[str]¶
-
route_plots: Optional[str]¶
-
route_results: Optional[str]¶
-
sim_id: Optional[str]¶
-
success: Optional[bool]¶
-
system: Optional[str]¶
-
user_id: Optional[int]¶
-
class
-
class
simulation_api.controller.schemas.SimulationDBSchBase(*, sim_id: str = None, user_id: int = None, date: str = None, system: str = None, method: str = None, route_pickle: str = None, route_results: str = None, route_plots: str = None, success: bool = None, message: str = None)[source]¶ Bases:
pydantic.main.BaseModelBasemodel for API type checking when querrying
simulationstable insimulations.dbdatabase.-
date: Optional[str]¶
-
message: Optional[str]¶
-
method: Optional[str]¶
-
route_pickle: Optional[str]¶
-
route_plots: Optional[str]¶
-
route_results: Optional[str]¶
-
sim_id: Optional[str]¶
-
success: Optional[bool]¶
-
system: Optional[str]¶
-
user_id: Optional[int]¶
-
-
class
simulation_api.controller.schemas.SimulationDBSchCreate(*, sim_id: str, user_id: int, date: str, system: str, method: str = None, route_pickle: str = None, route_results: str = None, route_plots: str = None, success: bool, message: str = None)[source]¶ Bases:
simulation_api.controller.schemas.SimulationDBSchBaseModel for API type checking when creating a ‘simulation information’ row in
simulationstable insimulations.dbdatabase.-
date: str¶
-
message: Optional[str]¶
-
method: Optional[str]¶
-
route_pickle: Optional[str]¶
-
route_plots: Optional[str]¶
-
route_results: Optional[str]¶
-
sim_id: str¶
-
success: bool¶
-
system: str¶
-
user_id: int¶
-
-
class
simulation_api.controller.schemas.UserDBSch(*, username: str = None, user_id: int = None)[source]¶ Bases:
simulation_api.controller.schemas.UserDBSchBaseModel for API type checking when reading a user information in
userstable insimulations.dbdatabase.-
class
Config[source]¶ Bases:
objectThis class is needed for database reading optimization (thanks to Object Relational Mapper –ORM.)
Note
Read more in FastAPI docs.
-
orm_model= True¶
-
-
user_id: Optional[int]¶
-
username: Optional[str]¶
-
class
-
class
simulation_api.controller.schemas.UserDBSchBase(*, username: str = None)[source]¶ Bases:
pydantic.main.BaseModelBasemodel for API type checking when querrying
userstable insimulations.dbdatabase.-
username: Optional[str]¶
-
-
class
simulation_api.controller.schemas.UserDBSchCreate(*, username: str, hash_value: str = None)[source]¶ Bases:
pydantic.main.BaseModelModel for API type checking when creating a ‘user information’ row in
userstable insimulations.dbdatabase.-
hash_value: Optional[str]¶
-
username: str¶
-
-
simulation_api.controller.schemas.params_mapping_ChenLee= {'param0': 'a', 'param1': 'b', 'param2': 'c'}¶ Maps the name of each Chen-Lee Attractor parameter in frontend form to its name in backend (defined by its corresponding attribute in class
ChenLeeAttractor)
-
simulation_api.controller.schemas.params_mapping_HO= {'param0': 'm', 'param1': 'k'}¶ Maps the name of each Harmonic Oscillator parameter in frontend form to its name in backend (defined by its corresponding attribute in class
HarmonicOsc1D)
-
simulation_api.controller.schemas.system_to_params_dict= {'Chen-Lee-Attractor': {'param0': 'a', 'param1': 'b', 'param2': 'c'}, 'Harmonic-Oscillator': {'param0': 'm', 'param1': 'k'}}¶ Maps the name of each available system to its parameter change-of-convention mapping (e.g.
params_mapping_HOorparams_mapping_ChenLee.)This is used to translate the parameters name convention in frontend simulation request to the parameters name convention in backend simulation request (with appropiate schema given by
SimRequest.)
simulation_api.controller.tasks¶
This file will do background tasks e.g. the simulation
-
simulation_api.controller.tasks._api_simulation_request(sim_system: simulation_api.controller.schemas.SimSystem, sim_params: simulation_api.controller.schemas.SimRequest, background_tasks: starlette.background.BackgroundTasks, db: sqlalchemy.orm.session.Session) → simulation_api.controller.schemas.SimIdResponse[source]¶ Requests simulation to BackgroundTasks.
- Parameters
sim_system (SimSystem) – System to be simulated.
sim_params (SimRequest) – Contains all the information about the simulation request.
background_tasks (
fastapi.BackgroundTasks) – Object needed to request simulation in the background.db (
sqlalchemy.orm.Session) – Needed for interaction with database.
- Returns
sim_id_response – Contains information about simulation request, such as simulation ID and others. See
SimIdResponsefor more information.- Return type
-
simulation_api.controller.tasks._check_chen_lee_params(a: float, b: float, c: float)[source]¶ Checks that the set of Chen-Lee parameters satisfy chaotic conditions, therefore bound solutions.
The conditions are
\[a > 0 \,\text{ and }\, b < 0 \,\text{ and }\, c < 0 \,\text{ and }\, a < - (b + c)\]Note
This conditions are stated in this reference.
- Parameters
a (float) – \(\omega_x\) parameter.
b (float) – \(\omega_y\) parameter.
c (float) – \(\omega_z\) parameter.
-
simulation_api.controller.tasks._create_pickle_path_disk(sim_id: str) → str[source]¶ Creates disk path to simulation results (pickle) by
sim_id.
-
simulation_api.controller.tasks._create_plot_path_disk(sim_id: str, query_param: Union[simulation_api.controller.schemas.PlotQueryValues_HO, simulation_api.controller.schemas.PlotQueryValues_ChenLee], plot_format: str = '.png') → str[source]¶ Creates disk path to plots of simulation results by
sim_id.
-
simulation_api.controller.tasks._pickle(file_name: str, path: str = '', data: Optional[dict] = None) → Any[source]¶ Saves
datato pickle or reads Python object from pickle.Saves
datato pickle ifdata. Otherwise it will try to read a pickle frompath + '/' + file_nameand return the python object stored in it.- Parameters
file_name (str) – Name of file to be read from or to write on.
path (str) – Path of directory in which the file will be saved or read from.
data (dict or None, optional) – If you want to save data to a pickle, provide the data as dictionary. Default is None.
- Returns
loaded_object – Object loaded when no data is provided.
- Return type
Any or None
-
simulation_api.controller.tasks._plot_solution(sim_results: simulation_api.controller.schemas.SimResults, system: simulation_api.controller.schemas.SimSystem, plots_basename: str = '00000') → List[str][source]¶ Generates relevant simulation’s plots and saves them.
- Parameters
sim_results (SimResults) – Simulation results as returned by
simulate().system (SimSystem) – System to be simulated.
plots_basename (str) – Base name of the plots. Actual name of each plot will be
<plotbasename>_<plot_query_value>.png, where<plot_query_value>is a special tag for each type of plot. In this API, baseplot will always be the value ofsim_id.
- Returns
plot_query_values – Names of each type of plot. These are very important since they are needed to access the plots in the API route (these are the possible values for the query param “value” in route
/api/results/{sim_id}/plot).- Return type
List[str]
-
simulation_api.controller.tasks._run_simulation(sim_params: simulation_api.controller.schemas.SimRequest) → None[source]¶ Runs the requested simulation and stores the outcome in a database.
This function runs the simulation, stores the simulation parameters in a database, stores the simulation result in a pickle and creates and saves relevant plots of the simulation.
- Parameters
sim_params (SimRequest) – Contains all the information needed for the simulation.
- Returns
- Return type
None
-
simulation_api.controller.tasks._sim_form_to_sim_request(form: Dict[str, str]) → simulation_api.controller.schemas.SimRequest[source]¶ Translates simulation form –from frontend– to simulation request which is understood by backend in
_api_simulation_request().- Parameters
form (Dict[str, str]) – Simulation request information as obtained by frontend.
- Returns
Simulation request information in a format the backend understands.
- Return type