2024-11-05 20:57:07 +04:00
|
|
|
|
import pandas as pd
|
2024-10-25 02:02:31 +04:00
|
|
|
|
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
2024-11-05 20:57:07 +04:00
|
|
|
|
from pyDOE3 import pbdesign, lhs
|
2024-10-28 13:30:02 +04:00
|
|
|
|
|
2024-10-15 16:34:09 +04:00
|
|
|
|
from db.csv_to_db import csv_to_db
|
2024-10-27 23:29:52 +04:00
|
|
|
|
from network.routes import (ch_experimentdb_experiment_data_router, experiment_data_router,
|
|
|
|
|
experiment_parameters_router, experiment_category_router)
|
2024-10-22 16:46:39 +04:00
|
|
|
|
from network.routes import load_parameters_router, recycling_parameters_router
|
|
|
|
|
from network.schemas import *
|
2024-10-25 02:02:31 +04:00
|
|
|
|
from new_experiment_planner import run_experiment
|
2024-11-05 20:57:07 +04:00
|
|
|
|
from new_experiment_planner_pyDOE3 import scale_design, scale_design_lhs, round_by_index
|
2024-10-08 21:21:05 +04:00
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
2024-10-22 16:46:39 +04:00
|
|
|
|
app.include_router(ch_experimentdb_experiment_data_router.router,
|
|
|
|
|
prefix="/ch_experimentdb_experiment_data",
|
|
|
|
|
tags=["ch_experimentdb_experiment_data"])
|
2024-10-27 23:29:52 +04:00
|
|
|
|
app.include_router(experiment_category_router.router,
|
|
|
|
|
prefix="/experiment_category",
|
|
|
|
|
tags=["experiment_category"])
|
2024-10-22 16:46:39 +04:00
|
|
|
|
app.include_router(experiment_data_router.router,
|
|
|
|
|
prefix="/experiment_data",
|
|
|
|
|
tags=["experiment_data"])
|
|
|
|
|
app.include_router(experiment_parameters_router.router,
|
|
|
|
|
prefix="/experiment_parameters",
|
|
|
|
|
tags=["experiment_parameters"])
|
|
|
|
|
app.include_router(load_parameters_router.router,
|
|
|
|
|
prefix="/load_parameters",
|
|
|
|
|
tags=["load_parameters"])
|
|
|
|
|
app.include_router(recycling_parameters_router.router,
|
|
|
|
|
prefix="/recycling_parameters",
|
|
|
|
|
tags=["recycling_parameters"])
|
2024-10-17 17:13:21 +04:00
|
|
|
|
|
|
|
|
|
|
2024-10-08 21:21:05 +04:00
|
|
|
|
# Эндпоинт для запуска эксперимента
|
|
|
|
|
@app.post("/run_experiment/")
|
|
|
|
|
def run_experiment_api(params: ExperimentParameters):
|
|
|
|
|
try:
|
|
|
|
|
# Вызываем функцию run_experiment с параметрами
|
|
|
|
|
run_experiment(
|
|
|
|
|
params.outer_blades_count,
|
|
|
|
|
params.outer_blades_length,
|
|
|
|
|
params.outer_blades_angle,
|
|
|
|
|
params.middle_blades_count,
|
|
|
|
|
params.load,
|
|
|
|
|
params.recycling
|
|
|
|
|
)
|
|
|
|
|
return {"status": "success", "message": "Experiment started successfully."}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
2024-10-15 00:10:09 +04:00
|
|
|
|
|
|
|
|
|
|
2024-10-22 16:46:39 +04:00
|
|
|
|
# эндпоинт инициализации бд из csv файлов
|
2024-10-17 13:51:33 +04:00
|
|
|
|
@app.get('/init_db_data')
|
2024-10-25 02:02:31 +04:00
|
|
|
|
async def init_db_data(background_tasks: BackgroundTasks):
|
2024-10-15 00:10:09 +04:00
|
|
|
|
try:
|
2024-10-25 02:02:31 +04:00
|
|
|
|
background_tasks.add_task(csv_to_db)
|
|
|
|
|
return {"status": "success", "message": "Инициализация БД запущена в фоне"}
|
2024-10-15 00:10:09 +04:00
|
|
|
|
except Exception as e:
|
2024-10-25 02:02:31 +04:00
|
|
|
|
print(str(e.with_traceback()))
|
2024-10-15 00:10:09 +04:00
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
2024-11-05 20:57:07 +04:00
|
|
|
|
|
|
|
|
|
# Пример запроса
|
|
|
|
|
# {
|
|
|
|
|
# "param_ranges": {
|
|
|
|
|
# "outer_blades_count": [12, 48],
|
|
|
|
|
# "outer_blades_length": [44, 107.5],
|
|
|
|
|
# "outer_blades_angle": [30, 75],
|
|
|
|
|
# "middle_blades_count": [9, 36],
|
|
|
|
|
# "load": [315, 465],
|
|
|
|
|
# "recycling_level": [0, 20],
|
|
|
|
|
# "oxidizer_temp": [471, 493]
|
|
|
|
|
# },
|
|
|
|
|
# "count_exp": 1440,
|
|
|
|
|
# "round_rules": [0, 1, 1, 0, 1, 1, 1]
|
|
|
|
|
# }
|
|
|
|
|
|
|
|
|
|
@app.post("/pyDOE3_screening_design")
|
|
|
|
|
def generate_screening_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]:
|
|
|
|
|
param_ranges = request.param_ranges
|
|
|
|
|
|
|
|
|
|
# Создаем screening design и масштабируем его
|
|
|
|
|
num_factors = len(param_ranges)
|
|
|
|
|
screening_design = pbdesign(num_factors)
|
|
|
|
|
scaled_screening_design = scale_design(screening_design, param_ranges)
|
|
|
|
|
|
|
|
|
|
# Преобразуем в DataFrame и возвращаем результат
|
|
|
|
|
df_screening = pd.DataFrame(scaled_screening_design, columns=param_ranges.keys())
|
|
|
|
|
return df_screening.to_dict(orient="records")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/pyDOE3_lhs_design")
|
|
|
|
|
def generate_lhs_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]:
|
|
|
|
|
param_ranges = request.param_ranges
|
|
|
|
|
count_exp = request.count_exp
|
|
|
|
|
round_rules = request.round_rules
|
|
|
|
|
|
|
|
|
|
# Создаем lhs design и масштабируем его
|
|
|
|
|
num_factors = len(param_ranges)
|
|
|
|
|
lhs_samples = lhs(num_factors, samples=count_exp)
|
|
|
|
|
scaled_lhs_samples = scale_design_lhs(lhs_samples, param_ranges)
|
|
|
|
|
|
|
|
|
|
# Округляем значения
|
|
|
|
|
round_scaled_lhs_samples = round_by_index(scaled_lhs_samples, round_rules)
|
|
|
|
|
|
|
|
|
|
# Преобразуем в DataFrame и возвращаем результат
|
|
|
|
|
df_lhs = pd.DataFrame(round_scaled_lhs_samples, columns=param_ranges.keys())
|
|
|
|
|
return df_lhs.to_dict(orient="records")
|