2024-12-09 23:32:11 +04:00
|
|
|
|
from random import uniform, random
|
|
|
|
|
from traceback import format_exc
|
|
|
|
|
|
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-12-11 13:41:58 +04:00
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
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-12-09 23:32:11 +04:00
|
|
|
|
from db.models import ChExperimentDBExperimentData
|
2024-11-18 23:16:24 +04:00
|
|
|
|
from db.repositories import save_experiment_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
|
2024-12-09 23:32:11 +04:00
|
|
|
|
from network.routes.ch_experimentdb_experiment_data_router import create_ch_experimentdb_experiment_data
|
|
|
|
|
from network.routes.experiment_data_router import create_experiment_data
|
|
|
|
|
from network.routes.experiment_parameters_router import get_all_experiment_parameters
|
2024-10-22 16:46:39 +04:00
|
|
|
|
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-12-11 13:41:58 +04:00
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=['*'],
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
2024-12-09 23:32:11 +04:00
|
|
|
|
@app.get('/init_data_plot')
|
|
|
|
|
async def init_data_plot():
|
|
|
|
|
try:
|
|
|
|
|
parameters = await get_all_experiment_parameters()
|
|
|
|
|
|
|
|
|
|
async def generate_data_for_file_id(file_id):
|
|
|
|
|
for _ in range(100):
|
|
|
|
|
await create_ch_experimentdb_experiment_data(
|
|
|
|
|
ChExperimentDBExperimentData(
|
|
|
|
|
volume=uniform(1e-8, 6.3e-6),
|
|
|
|
|
nitrogen_oxide_emission=uniform(2.7e-10, 8.92e-10),
|
|
|
|
|
temperature=uniform(1543.0, 2432.0),
|
|
|
|
|
co_fraction=uniform(0.0875, 0.4567),
|
|
|
|
|
co2_fraction=uniform(0.0824, 0.5678),
|
|
|
|
|
x=uniform(0.0002, 0.9849),
|
|
|
|
|
y=uniform(4.25, 10.31),
|
|
|
|
|
z=uniform(4.20, 10.26),
|
|
|
|
|
file_id=file_id
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for param in parameters:
|
|
|
|
|
print(param)
|
|
|
|
|
file_id = param.experiment_hash
|
|
|
|
|
if file_id:
|
|
|
|
|
await generate_data_for_file_id(file_id)
|
|
|
|
|
|
|
|
|
|
return {"status": "success", "message": "Добавление данных в БД успешно завершено"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(format_exc())
|
|
|
|
|
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,
|
2024-11-20 00:11:58 +04:00
|
|
|
|
# "round_rules": [0, 1, 1, 0, 0, 0, 0]
|
2024-11-05 20:57:07 +04:00
|
|
|
|
# }
|
|
|
|
|
|
|
|
|
|
@app.post("/pyDOE3_screening_design")
|
2024-11-18 23:16:24 +04:00
|
|
|
|
async def generate_screening_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]:
|
2024-11-05 20:57:07 +04:00
|
|
|
|
param_ranges = request.param_ranges
|
2024-12-11 13:41:58 +04:00
|
|
|
|
category = request.category
|
2024-11-05 20:57:07 +04:00
|
|
|
|
|
|
|
|
|
# Создаем screening design и масштабируем его
|
|
|
|
|
num_factors = len(param_ranges)
|
|
|
|
|
screening_design = pbdesign(num_factors)
|
|
|
|
|
scaled_screening_design = scale_design(screening_design, param_ranges)
|
|
|
|
|
|
2024-11-18 23:16:24 +04:00
|
|
|
|
# Преобразуем в DataFrame
|
2024-11-05 20:57:07 +04:00
|
|
|
|
df_screening = pd.DataFrame(scaled_screening_design, columns=param_ranges.keys())
|
2024-11-18 23:16:24 +04:00
|
|
|
|
|
|
|
|
|
# Сохраняем результаты в базу данных
|
2024-12-11 13:41:58 +04:00
|
|
|
|
await save_experiment_to_db(df_screening, category)
|
2024-11-18 23:16:24 +04:00
|
|
|
|
|
2024-11-05 20:57:07 +04:00
|
|
|
|
return df_screening.to_dict(orient="records")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/pyDOE3_lhs_design")
|
2024-11-18 23:16:24 +04:00
|
|
|
|
async def generate_lhs_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]:
|
2024-11-05 20:57:07 +04:00
|
|
|
|
param_ranges = request.param_ranges
|
|
|
|
|
count_exp = request.count_exp
|
|
|
|
|
round_rules = request.round_rules
|
2024-12-11 13:41:58 +04:00
|
|
|
|
category = request.category
|
2024-11-05 20:57:07 +04:00
|
|
|
|
|
|
|
|
|
# Создаем 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)
|
|
|
|
|
|
2024-11-18 23:16:24 +04:00
|
|
|
|
# Преобразуем в DataFrame
|
2024-11-05 20:57:07 +04:00
|
|
|
|
df_lhs = pd.DataFrame(round_scaled_lhs_samples, columns=param_ranges.keys())
|
2024-11-18 23:16:24 +04:00
|
|
|
|
|
|
|
|
|
# Сохраняем результаты в базу данных
|
2024-12-11 13:41:58 +04:00
|
|
|
|
await save_experiment_to_db(df_lhs, category)
|
2024-11-18 23:16:24 +04:00
|
|
|
|
|
2024-11-05 20:57:07 +04:00
|
|
|
|
return df_lhs.to_dict(orient="records")
|