Initial commit: планирование эксперимента и оптимизация смесей
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
# core/fs.py
|
||||
"""
|
||||
Модуль для работы с файлами проекта (сохранение/загрузка в JSON).
|
||||
"""
|
||||
import json
|
||||
from core.classes import Mixture
|
||||
from core.experiment import ExperimentDesign
|
||||
|
||||
PROJECT_FILENAME = "project.json"
|
||||
|
||||
def load_project(filename: str = PROJECT_FILENAME):
|
||||
"""
|
||||
Загружает проект из JSON-файла.
|
||||
Возвращает кортеж (mixture, design, responses).
|
||||
Если файл отсутствует или повреждён, возвращает новые объекты по умолчанию.
|
||||
"""
|
||||
try:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
mixture = Mixture.from_dict(data.get("mixture", {})) if "mixture" in data else Mixture()
|
||||
design = ExperimentDesign.from_dict(data.get("design", {})) if "design" in data else ExperimentDesign()
|
||||
responses = data.get("responses", [])
|
||||
if isinstance(responses, list) and responses and isinstance(responses[0], (int, float)):
|
||||
responses = [{"name": "Отклик 1", "values": responses}]
|
||||
return mixture, design, responses
|
||||
except (FileNotFoundError, json.JSONDecodeError, KeyError):
|
||||
return Mixture(), ExperimentDesign(), []
|
||||
|
||||
def save_project_partial(filename: str = PROJECT_FILENAME, mixture=None, design=None, responses=None):
|
||||
"""
|
||||
Сохраняет только переданные части проекта, остальные загружаются из существующего файла.
|
||||
Используется для точечного сохранения (например, только смеси).
|
||||
"""
|
||||
cur_mixture, cur_design, cur_responses = load_project(filename)
|
||||
if mixture is not None:
|
||||
cur_mixture = mixture
|
||||
if design is not None:
|
||||
cur_design = design
|
||||
if responses is not None:
|
||||
cur_responses = responses
|
||||
data = {
|
||||
"mixture": cur_mixture.to_dict(),
|
||||
"design": cur_design.to_dict(),
|
||||
"responses": cur_responses
|
||||
}
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
Reference in New Issue
Block a user