Skip to content

Commit

Permalink
Fix 5
Browse files Browse the repository at this point in the history
  • Loading branch information
nicl-nno committed Mar 15, 2024
1 parent 0063f8c commit 7abad85
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
7 changes: 5 additions & 2 deletions app/api/composer/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .history_convert_utils import history_to_graph
from .schema import ComposingHistoryGraphSchema, ComposingStartSchema
from .service import composer_history_for_case
from .service import composer_history_for_case, clean_case_id
from ..pipelines.service import pipeline_by_uid
from ..showcase.service import create_new_case_async
from ..showcase.showcase_utils import showcase_item_from_db
Expand All @@ -24,7 +24,9 @@ class ComposerHistoryResource(Resource):
@responds(schema=ComposingHistoryGraphSchema, many=False)
def get(self, case_id: str) -> Dict[str, Any]:
"""Get history of the composer for the specific dataset"""
graph_dict = history_to_graph(composer_history_for_case(case_id))
show_full = '_full' in case_id
case_id = clean_case_id(case_id)
graph_dict = history_to_graph(composer_history_for_case(case_id), show_full)
return graph_dict


Expand All @@ -43,6 +45,7 @@ def post(self):
async def start_async():
data = request.get_json()
case_id = data['case_id']
case_id = clean_case_id(case_id)
initial_uid = data['initial_uid']
gen_index = data.get('gen_index', None)
original_uid = data.get('original_uid', None)
Expand Down
14 changes: 7 additions & 7 deletions app/api/composer/history_convert_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from matplotlib import pyplot as plt


def history_to_graph(history: OptHistory) -> Dict[str, Any]:
all_nodes = _create_operators_and_nodes(history)
def history_to_graph(history: OptHistory, show_all: bool = True) -> Dict[str, Any]:
all_nodes = _create_operators_and_nodes(history, show_all)

all_nodes, edges = _create_edges(all_nodes)
all_nodes = _clear_tmp_fields(all_nodes)
Expand Down Expand Up @@ -61,10 +61,10 @@ def _process_operator(all_nodes, operator, individual, o_id, gen_id, prev_operat
return all_nodes


def _create_all_individuals_for_population(history, all_nodes, gen_id, order_id, uids_to_show):
def _create_all_individuals_for_population(history, all_nodes, gen_id, order_id, uids_to_show, show_all):
for ind_id in range(len(history.individuals[gen_id])):
individual = history.individuals[gen_id][ind_id]
if individual.uid not in uids_to_show or gen_id > uids_to_show[individual.uid]:
if not show_all and (individual.uid not in uids_to_show or gen_id > uids_to_show[individual.uid]):
continue

# add pipelines as node
Expand All @@ -90,7 +90,7 @@ def _create_all_individuals_for_population(history, all_nodes, gen_id, order_id,
return all_nodes, order_id


def _create_operators_and_nodes(history):
def _create_operators_and_nodes(history, show_all):
all_nodes = []
current_order_id = 0
if hasattr(history, 'final_choices') and history.final_choices:
Expand Down Expand Up @@ -121,13 +121,13 @@ def _create_operators_and_nodes(history):
o_id = 0
all_nodes, current_order_id = _create_all_individuals_for_population(history, all_nodes, gen_id,
current_order_id,
uid_to_last_generation_map)
uid_to_last_generation_map, show_all)
if gen_id == 0:
continue
for ind_id, individual in enumerate(generation):
if individual.native_generation != gen_id:
continue
if individual.uid not in uid_to_last_generation_map or gen_id > uid_to_last_generation_map[individual.uid]:
if not show_all and (individual.uid not in uid_to_last_generation_map or gen_id > uid_to_last_generation_map[individual.uid]):
continue

# add evo operators as nodes
Expand Down
14 changes: 8 additions & 6 deletions app/api/composer/service.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import datetime
import itertools
import json
import multiprocessing
import sys
from functools import lru_cache
from os import PathLike
from pathlib import Path
from typing import Optional

from bson import json_util
from fedot.api.main import Fedot
from fedot.core.pipelines.adapters import PipelineAdapter
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.pipelines.template import PipelineTemplate
from fedot.core.repository.tasks import TsForecastingParams
from flask import current_app
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory

from app.api.data.service import get_input_data
from app.api.pipelines.service import create_pipeline, is_pipeline_exists
from app.api.showcase.showcase_utils import showcase_item_from_db
from app.singletons.db_service import DBServiceSingleton
from utils import project_root, threading_lock
from utils import project_root


def clean_case_id(case_id: str):
case_id = case_id.replace('_full', '')
return case_id


# @threading_lock
# @lru_cache(maxsize=128) #CHEC
def composer_history_for_case(case_id: str, validate_history: bool = False) -> OptHistory:
case_id = clean_case_id(case_id)
case = showcase_item_from_db(case_id)
if case is None:
raise ValueError(f'Showcase item for case_id={case_id} is None but should exist')
Expand Down
5 changes: 5 additions & 0 deletions app/api/showcase/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
from .models import ShowcaseItem, Metadata
from .showcase_utils import prepare_icon_path, showcase_item_from_db
from ..analytics.pipeline_analytics import get_metrics_for_pipeline, get_metrics_for_golem_individual
from ..composer.service import clean_case_id


def showcase_full_item_by_uid(case_id: str) -> Optional[ShowcaseItem]:
case_id = clean_case_id(case_id)

dumped_item: Optional[Dict[str, Any]] = DBServiceSingleton().try_find_one('cases', {'case_id': case_id})
if dumped_item is None:
return None
Expand Down Expand Up @@ -83,6 +86,8 @@ def all_showcase_items_ids(with_custom: bool = False) -> List[str]:
def create_new_case(case_id, case_meta_json, opt_history_json, initial_pipeline: Pipeline = None, original_history=None,
modifed_generation_index=None, original_uid=None, is_golem_history=False):
from init.init_history import _init_composer_history_for_case
case_id = clean_case_id(case_id)

case = ShowcaseItem(
case_id=case_id,
title=case_id,
Expand Down
3 changes: 3 additions & 0 deletions app/api/showcase/showcase_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from flask import url_for

from .models import ShowcaseItem
from ..composer.service import clean_case_id


def showcase_item_from_db(case_id: str) -> Optional[ShowcaseItem]:
case_id = clean_case_id(case_id)

dumped_item = DBServiceSingleton().try_find_one('cases', {'case_id': case_id})
if dumped_item is None:
return None
Expand Down

0 comments on commit 7abad85

Please sign in to comment.