Skip to content

Commit

Permalink
"0.2.7" Optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Warrfie committed Dec 7, 2023
1 parent 997f6d2 commit 594e4de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 58 deletions.
67 changes: 12 additions & 55 deletions combidata/classes/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from combidata.classes.combination import Combination, current_workflow
from combidata.classes.mul_dim_graph import MDG
from combidata.funcs.exeptions import CombinatoricsError
from combidata.funcs.form_and_combine import unlimited_cases


def check_all_names(init_lib):
Expand Down Expand Up @@ -83,53 +84,18 @@ def __init__(self, library: dict,

assert self.combinations, "No combinations for tests" # TODO deep logging needed

if amount is not None:
if amount:
self.extend_cases(amount)

def extend_cases(self, amount):

workflow = copy.deepcopy(self.workflow)
if "ST_COMBINE" in [process.name for process in current_workflow(workflow, True)]:
combi_graph = MDG(self.init_lib, self.types_for_generation) #todo add logger
combinations = list(self.combinations.keys())
random.shuffle(combinations)
for combination_name in combinations:
main_case = self.combinations[combination_name].main_case
if main_case.field_name in combi_graph.neutral_lib.keys() and main_case.field_mode in combi_graph.neutral_lib[main_case.field_name].keys():
if not combi_graph.can_combine(main_case):
del self.combinations[combination_name]
else:
del self.combinations[combination_name]

dict_keys = list(self.combinations.keys())
random.shuffle(dict_keys)
key_count = len(dict_keys)

if key_count == amount:
return
elif key_count > amount:
for i in range(key_count - amount):
del self.combinations[dict_keys[i]]
return

i = 0
added_key_count = 0
shuffle_point = (amount // key_count) * key_count

while added_key_count < amount:
if i % key_count == 0 and i == shuffle_point:
random.shuffle(dict_keys)
key = dict_keys[i % key_count]
value = self.combinations[key]

if i < key_count:
self.combinations[key] = copy.deepcopy(value)
temp = {}
for test_case in unlimited_cases(self.init_lib, self.combinations, self.workflow, self.types_for_generation):
if amount != 0:
temp.update(test_case)
amount -= 1
else:
extended_key = f"{key}[{i // key_count}]"
self.combinations[extended_key] = copy.deepcopy(value)

i += 1
added_key_count += 1
self.combinations = temp
return

def form_combinations(self):
self.combinations = {}
Expand All @@ -146,7 +112,7 @@ def run(self):
for combination_name in combinations_names:
self.combinations[combination_name].run()
if isinstance(self.combinations[combination_name].step_done, type(CombinatoricsError())):
del self.combinations[combination_name]
del self.combinations[combination_name] #todo logs!

def run_one(self):
combinations = list(self.combinations.keys())
Expand All @@ -157,17 +123,8 @@ def run_one(self):
return combinations[combination_name]

def get_one(self):
workflow = copy.deepcopy(self.workflow)
if "ST_COMBINE" in [process.name for process in current_workflow(workflow, True)]:
combi_graph = MDG(self.init_lib, self.types_for_generation)
combinations = list(self.combinations.keys())
random.shuffle(combinations)
for combination in combinations:
if combi_graph.can_combine(self.combinations[combination].main_case):
return self.combinations[combination]
else:
combinations = list(self.combinations.keys())
return self.combinations[random.choice(combinations)]
for test_case in unlimited_cases(self.init_lib, self.combinations, self.workflow, self.types_for_generation):
return test_case[list(test_case.keys())[0]] #todo ugly

# todo def any_passed
@staticmethod
Expand Down
48 changes: 47 additions & 1 deletion combidata/funcs/form_and_combine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import random

from combidata.classes.combination import current_workflow

def should_keep(x):
return len(x) > 1
Expand Down Expand Up @@ -59,3 +59,49 @@ def can_combine(neutral_lib, current_case, types_for_generation):
return result
else:
return False


def unlimited_cases(init_lib, cases_lib, workflow, types_for_generation):
correct_cases = {}
is_full = False
current_keys_list = []
counter = 0

workflow = copy.deepcopy(workflow)
must_prove = "ST_COMBINE" in [process.name for process in current_workflow(workflow, True)]

if not must_prove:
correct_cases = copy.deepcopy(cases_lib)
is_full = True
current_keys_list = list(correct_cases.keys())
random.shuffle(current_keys_list)

while True:
if is_full:
if current_keys_list:
combination_name = current_keys_list.pop()
if counter == 0:
yield {combination_name: cases_lib[combination_name]}
else:
yield {f"{combination_name} [{counter}]": cases_lib[combination_name]}
else:
current_keys_list = list(correct_cases.keys())
random.shuffle(current_keys_list)
counter += 1
combination_name = current_keys_list.pop()
yield {f"{combination_name} [{counter}]": cases_lib[combination_name]}
else:
from combidata.classes.mul_dim_graph import MDG
combi_graph = MDG(init_lib, types_for_generation) # todo add logger
combinations = list(cases_lib.keys())
random.shuffle(combinations)
for combination_name in combinations:
main_case = cases_lib[combination_name].main_case
if main_case.field_name in combi_graph.neutral_lib.keys() and main_case.field_mode in \
combi_graph.neutral_lib[main_case.field_name].keys() and combi_graph.can_combine(main_case):
correct_cases.update({combination_name: main_case})
yield {combination_name: cases_lib[combination_name]}
is_full = True
current_keys_list = list(correct_cases.keys())
random.shuffle(current_keys_list)
counter += 1
6 changes: 6 additions & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ hide:
- navigation
---


# Release Notes: Version 0.2.7

## Optimization:
1. **Case Amount Optimization:** Now it will be faster.

# Release Notes: Version 0.2.6

## Bug Fixes:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ keywords = [
urls = {Homepage = "https://github.com/Warrfie/combidata"}
authors = [{name = "MaximKuklikov(Warrfie)", email = "[email protected]"}]
requires-python = ">=3.10"
version="0.2.6"
version="0.2.7"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="combidata",
version="0.2.6",
version="0.2.7",
description="Package for random data generation and combination different cases",
long_description=open('README.md').read(),
url="https://warrfie.github.io/combidata",
Expand Down

0 comments on commit 594e4de

Please sign in to comment.