| Conditions | 6 |
| Total Lines | 55 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | from pathlib import Path |
||
| 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 | } |
||
| 76 |