From b58b8121069258c18ecdf43b6d8442a8c1dc9c2e Mon Sep 17 00:00:00 2001 From: Owen Allemang Date: Thu, 29 Sep 2022 10:24:20 +0100 Subject: [PATCH 1/2] typing hints --- pattern.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pattern.py b/pattern.py index c020081..6cc0309 100644 --- a/pattern.py +++ b/pattern.py @@ -6,6 +6,8 @@ from PIL import Image from scipy.ndimage import gaussian_filter +from typing import Optional, Tuple + def update(phi, r0, r1): dt = 0.1 @@ -20,14 +22,19 @@ def update(phi, r0, r1): return phi -def run(): +def arg_parse(): p = argparse.ArgumentParser() p.add_argument("--url", type=str, metavar="FILENAME", help="File name or URL") p.add_argument("-r", nargs=2, metavar=("r0", "r1"), type=float, help="Gaussian radius") args = p.parse_args() print(args.r, args.url) - if args.r: + return args + + +def run(r: Optional[Tuple[float, float]], url: str): + + if r: r0, r1 = args.r else: r0, r1 = 5, 4 @@ -59,4 +66,5 @@ def run(): if __name__ == "__main__": - run() + args = arg_parse() + run(args.r, args.url) From 54ee466ef92bbe07f5cd04a5f168ebd643a231bd Mon Sep 17 00:00:00 2001 From: Owen Allemang Date: Thu, 29 Sep 2022 10:40:14 +0100 Subject: [PATCH 2/2] add docstring, typing hint, and reformat the arg parser in its own function --- pattern.py | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/pattern.py b/pattern.py index 6cc0309..03f67c7 100644 --- a/pattern.py +++ b/pattern.py @@ -1,4 +1,4 @@ -import numpy +import numpy as np import matplotlib.pyplot as plt import requests import argparse @@ -9,7 +9,23 @@ from typing import Optional, Tuple -def update(phi, r0, r1): +def update(phi: np.ndarray, r0: float, r1: float) -> np.ndarray: + """ + Helper function to update the image to create the turing pattern + Parameters + ---------- + phi : np.ndarray, + array of the image to use + r0 : float, + radius for the gaussian filter + r1 : float, + radius for the gaussian filter + + Returns + ------- + phi: np.ndarray, + updated image + """ dt = 0.1 p = gaussian_filter(phi, sigma=r0, mode="wrap") q = gaussian_filter(phi, sigma=r1, mode="wrap") @@ -23,6 +39,7 @@ def update(phi, r0, r1): def arg_parse(): + """function for argument parsing""" p = argparse.ArgumentParser() p.add_argument("--url", type=str, metavar="FILENAME", help="File name or URL") p.add_argument("-r", nargs=2, metavar=("r0", "r1"), @@ -32,23 +49,34 @@ def arg_parse(): return args -def run(r: Optional[Tuple[float, float]], url: str): +def run(r: Optional[Tuple[float, float]], url: Optional[str]) -> None: + """ + Generating turing pattern from an image. + Parameters + ---------- + r : tuple of floats, optional, + tuple of the radii for the gaussian filter + + url : str, optional, + url or path of the image + if None, random image is used + """ if r: - r0, r1 = args.r + r0, r1 = r else: r0, r1 = 5, 4 - if args.url: - url = args.url + + if url: phi = Image.open(requests.get(url, stream=True).raw) w, h = phi.size phi = phi.resize((w * 4, h * 4), Image.ANTIALIAS) else: - phi = numpy.random.rand(400, 400) + phi = np.random.rand(400, 400) - phi = numpy.array(phi, dtype=numpy.float32) + phi = np.array(phi, dtype=np.float32) if len(phi.shape) == 3: - phi = numpy.sum(phi, axis=2) + phi = np.sum(phi, axis=2) plt.imshow(phi, cmap="gray") plt.show()