Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
scottprahl committed May 8, 2024
1 parent a03d0b1 commit fb31e2a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pyspeckle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
pyspeckle.create_Exponential_3D(M, pix_per_speckle)
pyspeckle.create_Rayleigh_3D(M, pix_per_speckle)
"""
__version__ = '0.5.1'
__version__ = '0.6.0'
__author__ = 'Scott Prahl'
__email__ = '[email protected]'
__copyright__ = 'Copyright 2018-23, Scott Prahl'
__copyright__ = '2018-24, Scott Prahl'
__license__ = 'MIT'
__url__ = 'https://github.com/scottprahl/pyspeckle.git'
__url__ = 'https://github.com/scottprahl/pyspeckle'

from .pyspeckle import *
11 changes: 7 additions & 4 deletions pyspeckle/pyspeckle.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def create_gaussian_1D(M, mean, stdev, cl):
Args:
M: dimension of desired array [-]
mean: average value of signal [gray levels]
std: standard deviation of signal [gray levels]
stdev: standard deviation of signal [gray levels]
cl: correlation length [# of pixels]
Returns:
Expand Down Expand Up @@ -405,6 +405,7 @@ def statistics_plot(x, initialize=True):
Args:
x: speckle pattern to be analyzed
initialize: boolean to initialize the plot
Returns:
nothing
Expand Down Expand Up @@ -465,7 +466,7 @@ def statistics_plot(x, initialize=True):

def create_Rayleigh(N, pix_per_speckle, alpha=1, shape='ellipse'):
"""
Generate an M x M unpolarized speckle irradiance pattern.
Generate an N x N unpolarized speckle irradiance pattern.
The speckle pattern will have a Rayleigh distribution and results from
the incoherent sum of two speckle patterns.
Expand All @@ -481,13 +482,13 @@ def create_Rayleigh(N, pix_per_speckle, alpha=1, shape='ellipse'):
they are wide.
Args:
M: dimension of desired square speckle image
N: dimension of desired square speckle image
pix_per_speckle: number of pixels per smallest speckle.
alpha: ratio of horizontal width to vertical width
shape: 'ellipse' or 'rectangle' describing the laser shape
Returns:
M x M speckle image
N x N speckle image
"""
y1 = create_Exponential(N, pix_per_speckle, shape=shape, alpha=alpha)
y2 = create_Exponential(N, pix_per_speckle, shape=shape, alpha=alpha)
Expand Down Expand Up @@ -632,6 +633,8 @@ def slice_plot(data, x, y, z, initialize=True, show_sqrt=True):
x: constant x slice
y: constant y slice
z: constant z slice
initialize: boolean to initialize plot
show_sqrt: take sqrt() of image for better visualization
Returns:
nothing
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Setup for pyspeckle module."""
import re
import os.path
from setuptools import setup
Expand Down
41 changes: 33 additions & 8 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,99 @@
"""Tests of basic functionality of pyspeckle."""
import numpy as np
import pytest
import pyspeckle


# Tests for create_exp_1D
def test_create_exp_1D_output_length():
"""Test length of create_exp_1D."""
arr = pyspeckle.create_exp_1D(100, 10, 2, 5)
assert len(arr) == 100


def test_create_exp_1D_mean_and_std():
"""Test mean and stdev of create_exp_1D."""
arr = pyspeckle.create_exp_1D(1000, 10, 2, 5)
assert abs(np.mean(arr) - 10) < 0.8 # A small tolerance might be needed due to randomness
assert abs(np.std(arr) - 2) < 0.8


@pytest.mark.parametrize("M,mean,stdev,cl", [(0, 10, 2, 5), (100, 10, -2, 5), (100, 10, 2, -5), (100, 10, 2, 51)])
@pytest.mark.parametrize("M,mean,stdev,cl",
[(0, 10, 2, 5),
(100, 10, -2, 5),
(100, 10, 2, -5),
(100, 10, 2, 51)])
def test_create_exp_1D_invalid_args(M, mean, stdev, cl):
with pytest.raises(ValueError): # or another appropriate exception based on behavior
"""Test bad inputs to create_exp_1D."""
with pytest.raises(ValueError):
pyspeckle.create_exp_1D(M, mean, stdev, cl)


# Tests for create_gaussian_1D
def test_create_gaussian_1D_output_length():
"""Test length of create_gaussian_1D."""
arr = pyspeckle.create_gaussian_1D(100, 10, 2, 5)
assert len(arr) == 100


def test_create_gaussian_1D_mean_and_std():
"""Test mean and stdev of create_gaussian_1D output."""
arr = pyspeckle.create_gaussian_1D(1000, 10, 2, 5)
assert abs(np.mean(arr) - 10) < 0.5
assert abs(np.std(arr) - 2) < 0.5


@pytest.mark.parametrize("M,mean,stdev,cl", [(0, 10, 2, 5), (100, 10, -2, 5), (100, 10, 2, -5), (100, 10, 2, 51)]) # M/cl < 2
@pytest.mark.parametrize("M,mean,stdev,cl",
[(0, 10, 2, 5),
(100, 10, -2, 5),
(100, 10, 2, -5),
(100, 10, 2, 51)]) # M/cl < 2
def test_create_gaussian_1D_invalid_args(M, mean, stdev, cl):
"""Test bad inputs to create_gaussian_1D."""
with pytest.raises(ValueError): # or another appropriate exception based on behavior
pyspeckle.create_gaussian_1D(M, mean, stdev, cl)


# Tests for autocorrelation
def test_autocorrelation_length():
"""Test length of autocorrelation."""
arr = np.array([1, 2, 3, 4, 5])
assert len(pyspeckle.autocorrelation(arr)) == len(arr)


def test_autocorrelation_value():
"""Test max value of autocorrelation."""
arr = np.array([1, 2, 3, 4, 5])
autocorr = pyspeckle.autocorrelation(arr)
assert autocorr[0] == 1 # It's normalized to have a max of 1


def test_autocorrelation_value2():
"""Test autocorrelation with zeros."""
arr = np.array([0, 0, 0])
autocorr = pyspeckle.autocorrelation(arr)
assert autocorr[0] == 0 # It's normalized to have a max of 1


# Test for create_Exponential
def test_Exponential_shape_of_output():
"""Test shape of create_Exponential."""
result = pyspeckle.create_Exponential(10, 2)
assert result.shape == (10, 10)


def test_create_Exponential_shape():
"""Test shape of create_Exponential with params."""
speckle = pyspeckle.create_Exponential(50, 2, alpha=1, shape='ellipse', polarization=1)
assert speckle.shape == (50, 50)


def test_Exponential_maximum_value():
"""Test max value of create_Exponential."""
result = pyspeckle.create_Exponential(10, 2)
assert np.max(result) <= 1.0


def test_Exponential_non_circular_shapes():
"""Verify that other shapes work with create_Exponential."""
shapes = ['ellipse', 'rectangle', 'annulus', 'ELLIPSE', 'Rectangle', 'ANNULus']
for shape in shapes:
result = pyspeckle.create_Exponential(10, 2, shape=shape)
Expand All @@ -82,24 +102,27 @@ def test_Exponential_non_circular_shapes():


def test_create_Exponential_invalid_pol1():
with pytest.raises(ValueError): # or another appropriate exception based on behavior
"""Test invalid polarization."""
with pytest.raises(ValueError):
pyspeckle.create_Exponential(10, 2, polarization=-1)


def test_create_Exponential_invalid_pol2():
with pytest.raises(ValueError): # or another appropriate exception based on behavior
"""Test2 invalid polarization."""
with pytest.raises(ValueError):
pyspeckle.create_Exponential(10, 2, polarization=2)


def test_Exponential_polarization_values():
"""Test valid polarizations."""
for polarization in [0, 0.5, 1]:
result = pyspeckle.create_Exponential(10, 2, polarization=polarization)
assert result.shape == (10, 10)
assert np.max(result) <= 1.0


# Test for _create_mask
def test_ellipse_mask():
"""Basic functionality for ellipse mask."""
mask = pyspeckle.pyspeckle._create_mask(10, 3, 4)
assert mask.shape == (10, 10)
assert mask[5, 5]
Expand All @@ -108,6 +131,7 @@ def test_ellipse_mask():


def test_rectangle_mask():
"""Basic functionality for rect mask."""
mask = pyspeckle.pyspeckle._create_mask(10, 3, 4, shape='rectangle')
assert mask.shape == (10, 10)
assert mask[0, 0]
Expand All @@ -117,6 +141,7 @@ def test_rectangle_mask():


def test_annulus_mask():
"""Basic functionality for annular mask."""
mask = pyspeckle.pyspeckle._create_mask(10, 3, 4, shape='annulus')
assert mask.shape == (10, 10)
assert mask[0, 0] == 0
Expand Down

0 comments on commit fb31e2a

Please sign in to comment.