Passed
Push — master ( c3d045...3525ae )
by Konstantinos
01:55 queued 43s
created

artificial_artwork._demo.create_algo_runner()   B

Complexity

Conditions 6

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nop 4
dl 0
loc 55
rs 8.0826
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
from pathlib import Path
2
3
from .disk_operations import Disk
4
from .styling_observer import StylingObserver
5
from .algorithm import NSTAlgorithm, AlogirthmParameters
6
from .nst_tf_algorithm import NSTAlgorithmRunner
7
from .termination_condition_adapter_factory import TerminationConditionAdapterFactory
8
from .nst_image import noisy, convert_to_uint8
9
from .production_networks import NetworkDesign
10
from .pretrained_model import ModelHandlerFacility
11
from .utils import load_pretrained_model_functions, read_images
12
13
this_file_directory = Path(__file__).parent
14
source_root_dir = this_file_directory.parent.parent
15
16
17
def create_algo_runner(
18
    iterations=100,
19
    output_folder='gui-output-folder',
20
    content_img_file=None,
21
    style_img_file=None,
22
):
23
    print("[DEBUG] output type: {}".format(type(output_folder)))
24
25
    current_directory = Path.cwd()
26
27
    termination_condition = 'max-iterations'
28
29
    content_img_file = content_img_file if content_img_file else source_root_dir / 'tests' / 'data' / 'canoe_water_w300-h225.jpg'
30
    style_img_file = style_img_file if style_img_file else source_root_dir / 'tests' / 'data' / 'blue-red_w300-h225.jpg'
31
32
    content_image, style_image = read_images(content_img_file, style_img_file)
33
34
    load_pretrained_model_functions()  # ie import VGG ModelHandler implementation (to allow facility creating instances)
35
    model_design = type('ModelDesign', (), {
36
        'pretrained_model': ModelHandlerFacility.create('vgg'),
37
        'network_design': NetworkDesign.from_default_vgg()
38
    })
39
    model_design.pretrained_model.load_model_layers()
40
41
    termination_condition = TerminationConditionAdapterFactory.create(
42
        termination_condition,
43
        iterations,  # maximun number of iterations to run the algorithm
44
    )
45
46
    print(f' -- Termination Condition: {termination_condition.termination_condition}')
47
48
    algorithm_parameters = AlogirthmParameters(
49
        content_image,
50
        style_image,
51
        termination_condition,
52
        output_folder,
53
    )
54
55
    algorithm = NSTAlgorithm(algorithm_parameters)
56
57
    noisy_ratio = 0.6  # ratio
58
59
    algorithm_runner = NSTAlgorithmRunner.default(
60
        lambda matrix: noisy(matrix, noisy_ratio),
61
    )
62
63
    algorithm_runner.progress_subject.add(
64
        termination_condition,
65
    )
66
    algorithm_runner.persistance_subject.add(
67
        StylingObserver(Disk.save_image, convert_to_uint8, iterations)
68
    )
69
    return {
70
        'run': lambda: algorithm_runner.run(algorithm, model_design),
71
        'subscribe': lambda observer: algorithm_runner.progress_subject.add(observer),
72
    }
73
    # return algorithm_runner, lambda: algorithm_runner.run(algorithm, model_design)
74
75
    # algorithm_runner.run(algorithm, model_design)
76