diff --git a/pattern.py b/pattern.py index c020081..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 @@ -6,8 +6,26 @@ from PIL import Image from scipy.ndimage import gaussian_filter +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") @@ -20,28 +38,45 @@ def update(phi, r0, r1): return phi -def run(): +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"), type=float, help="Gaussian radius") args = p.parse_args() print(args.r, args.url) - if args.r: - r0, r1 = args.r + return args + + +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 = 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() @@ -59,4 +94,5 @@ def run(): if __name__ == "__main__": - run() + args = arg_parse() + run(args.r, args.url)