Skip to content

Commit

Permalink
Merge pull request #50 from matthewtownson/master
Browse files Browse the repository at this point in the history
Tidying function names ready for v1.0
  • Loading branch information
andrewpaulreeves authored Nov 5, 2019
2 parents f55ac77 + 0a39916 commit a317c05
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 200 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ branches:
- v*

python:
- 2.7
# - 3.4 Not in travis repositories
- 3.5
#- 3.5 Not in travis repositories
- 3.6
- 3.7

Expand Down
84 changes: 1 addition & 83 deletions aotools/functions/_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import numpy
from . import pupil
import warnings


def gaussian2d(size, width, amplitude=1., cent=None):
Expand Down Expand Up @@ -40,84 +38,4 @@ def gaussian2d(size, width, amplitude=1., cent=None):
image = amplitude * numpy.exp(
-(((xCent - X) / xWidth) ** 2 + ((yCent - Y) / yWidth) ** 2) / 2)

return image


def aziAvg(data):
"""
Measure the azimuthal average of a 2d array
Args:
data (ndarray): A 2-d array of data
Returns:
ndarray: A 1-d vector of the azimuthal average
"""
warnings.warn("This function will be removed in version 0.5, instead use aotools.image_processing.azimuthal_average",
DeprecationWarning)

size = data.shape[0]
avg = numpy.empty(int(size / 2), dtype="float")
for i in range(int(size / 2)):
ring = pupil.circle(i + 1, size) - pupil.circle(i, size)
avg[i] = (ring * data).sum() / (ring.sum())

return avg


def encircledEnergy(data,
fraction=0.5, center=None,
eeDiameter=True):
"""
Return the encircled energy diameter for a given fraction
(default is ee50d).
Can also return the encircled energy function.
Translated and extended from YAO.
Parameters:
data : 2-d array
fraction : energy fraction for diameter calculation
default = 0.5
center : default = center of image
eeDiameter : toggle option for return.
If False returns two vectors: (x, ee(x))
Default = True
Returns:
Encircled energy diameter
or
2 vectors: diameters and encircled energies
"""
warnings.warn(
"This function will be removed in version 0.5, instead use aotools.image_processing.encircled_energy",
DeprecationWarning)

dim = data.shape[0] // 2
if center is None:
center = [dim, dim]
xc = center[0]
yc = center[1]
e = 1.9
npt = 20
rad = numpy.linspace(0, dim**(1. / e), npt)**e
ee = numpy.empty(rad.shape)

for i in range(npt):
pup = pupil.circle(rad[i],
int(dim) * 2,
circle_centre=(xc, yc),
origin='corner')
rad[i] = numpy.sqrt(numpy.sum(pup) * 4 / numpy.pi) # diameter
ee[i] = numpy.sum(pup * data)

rad = numpy.append(0, rad)
ee = numpy.append(0, ee)
ee /= numpy.sum(data)
xi = numpy.linspace(0, dim, int(4 * dim))
yi = numpy.interp(xi, rad, ee)

if eeDiameter is False:
return xi, yi
else:
ee50d = float(xi[numpy.argmin(numpy.abs(yi - fraction))])
return ee50d
return image
6 changes: 3 additions & 3 deletions aotools/functions/zernike.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def phaseFromZernikes(zCoeffs, size, norm="noll"):
return phase


def zernike(j, N):
def zernike_noll(j, N):
"""
Creates the Zernike polynomial with mode index j,
where j = 1 corresponds to piston.
Expand Down Expand Up @@ -143,7 +143,7 @@ def zernikeArray(J, N, norm="noll"):
nJ = len(J)
Zs = numpy.empty((nJ, N, N))
for i in xrange(nJ):
Zs[i] = zernike(J[i], N)
Zs[i] = zernike_noll(J[i], N)

# Else, cast to int and create up to that number
except TypeError:
Expand All @@ -154,7 +154,7 @@ def zernikeArray(J, N, norm="noll"):
Zs = numpy.empty((maxJ, N, N))

for j in xrange(1, maxJ+1):
Zs[j-1] = zernike(j, N)
Zs[j-1] = zernike_noll(j, N)


if norm=="p2v":
Expand Down
1 change: 0 additions & 1 deletion aotools/image_processing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .centroiders import *
from ._image_processing import *
from .contrast import *
from .psf import *
43 changes: 0 additions & 43 deletions aotools/image_processing/_image_processing.py

This file was deleted.

16 changes: 6 additions & 10 deletions aotools/image_processing/centroiders.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def correlation_centroid(im, ref, threshold=0., padding=1):
# Correlate frame with reference image
corr = cross_correlate(im[frame], ref, padding=padding)

cx, cy = centreOfGravity(corr, threshold=threshold)
cx, cy = centre_of_gravity(corr, threshold=threshold)

cy -= float(ny) / 2. * (float(padding) - 1)
cx -= float(nx) / 2. * (float(padding) - 1)
Expand All @@ -51,20 +51,16 @@ def correlation_centroid(im, ref, threshold=0., padding=1):
return centroids


def centreOfGravity(img, threshold=0, minThreshold=0, **kwargs):
def centre_of_gravity(img, threshold=0, min_threshold=0, **kwargs):
"""
Centroids an image, or an array of images.
Centroids over the last 2 dimensions.
Sets all values under "threshold*max_value" to zero before centroiding
Origin at 0,0 index of img.
The value under which pixels are set to 0
is max(threshold*max_value, minThreshold)
Parameters:
img (ndarray): ([n, ]y, x) 2d or greater rank array of imgs to centroid
threshold (float): Percentage of max value under which pixels set to 0
minThreshold (float): Absolute max value under which pixels set to 0
Returns:
ndarray: Array of centroid values (2[, n])
Expand All @@ -73,10 +69,10 @@ def centreOfGravity(img, threshold=0, minThreshold=0, **kwargs):

if threshold != 0:
if len(img.shape) == 2:
thres = numpy.max((threshold*img.max(), minThreshold))
thres = numpy.max((threshold*img.max(), min_threshold))
img = numpy.where(img > thres, img - thres, 0)
else:
thres = numpy.maximum(threshold*img.max(-1).max(-1), [minThreshold]*img.shape[0])
thres = numpy.maximum(threshold*img.max(-1).max(-1), [min_threshold]*img.shape[0])
img_temp = (img.T - thres).T
zero_coords = numpy.where(img_temp < 0)
img[zero_coords] = 0
Expand All @@ -94,7 +90,7 @@ def centreOfGravity(img, threshold=0, minThreshold=0, **kwargs):
return numpy.array([x_centroid, y_centroid])


def brightestPxl(img, threshold, **kwargs):
def brightest_pixel(img, threshold, **kwargs):
"""
Centroids using brightest Pixel Algorithm
(A. G. Basden et al, MNRAS, 2011)
Expand Down Expand Up @@ -124,7 +120,7 @@ def brightestPxl(img, threshold, **kwargs):
img[:] = (img.T - pxlValues).T
img = img.clip(0, img.max(), out=img)

return centreOfGravity(img)
return centre_of_gravity(img)


def cross_correlate(x, y, padding=1):
Expand Down
2 changes: 0 additions & 2 deletions aotools/turbulence/slopecovariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"""

import time
import multiprocessing

import numpy
import scipy.special

from ..functions import circle

class CovarianceMatrix(object):
"""
Expand Down
9 changes: 6 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ build: false

environment:
matrix:
- PYTHON_VERSION: 2.7
MINICONDA: C:\Miniconda-x64

- PYTHON_VERSION: 3.5
MINICONDA: C:\Miniconda35-x64

- PYTHON_VERSION: 3.6
MINICONDA: C:\Miniconda36-x64

- PYTHON_VERSION: 3.7
MINICONDA: C:\Miniconda37-x64


branches:
only:
Expand Down
11 changes: 7 additions & 4 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ def __getattr__(cls, name):

# General information about the project.
project = u'AOtools'
copyright = u'2016, Centre for Advanced Instrumentation'
author = u'Centre for Advanced Instrumentation'
copyright = u'2019, The Authors'
author = u'AOtools'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u'0.1'
full_version = aotools.__version__
version_parts = full_version.split('.')
print(version_parts)
version = u'0.5'
# The full version, including alpha/beta/rc tags.
release = u'0.1.0'
release = aotools.__version__ # '0.1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
18 changes: 9 additions & 9 deletions test/test_centroiders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
from nose.tools import raises


def test_centreOfGravity_single():
def test_centre_of_gravity_single():
img = numpy.random.random((10, 10))
com = image_processing.centreOfGravity(img, 0.1)
com = image_processing.centre_of_gravity(img, 0.1)
assert(com.shape[0]) == 2


def test_centreOfGravity_many():
def test_centre_of_gravity_many():
img = numpy.random.random((5, 10, 10))
com = image_processing.centreOfGravity(img, 0.1)
com = image_processing.centre_of_gravity(img, 0.1)
assert(com.shape[0] == 2)
assert(com.shape[1] == 5)

def test_centreOfGravity_value():
def test_centre_of_gravity_value():
img = numpy.zeros((1, 5, 5))
img[0, 1:3, 2:4] = 1.
centroid = image_processing.centreOfGravity(img)
centroid = image_processing.centre_of_gravity(img)
numpy.testing.assert_almost_equal(centroid, numpy.array([[2.5], [1.5]]))

def test_brightestPxl_single():
def test_brightest_pixel_single():
img = numpy.random.random((10, 10))
com = image_processing.brightestPxl(img, 0.3)
com = image_processing.brightest_pixel(img, 0.3)
assert(com.shape[0] == 2)


def test_brightestPxl_many():
img = numpy.random.random((5, 10, 10))
com = image_processing.brightestPxl(img, 0.1)
com = image_processing.brightest_pixel(img, 0.1)
assert(com.shape[0] == 2)
assert(com.shape[1] == 5)

Expand Down
23 changes: 0 additions & 23 deletions test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,3 @@ def test_gaussian2d_2d():
gaussian = functions.gaussian2d((10, 8), (3, 2), 10., (4, 3))
print(gaussian.shape)
assert gaussian.shape == (10, 8)


def test_encircledEnergy():
data = numpy.random.rand(32, 32)
ee50d = functions.encircledEnergy(data)
print(ee50d)
assert type(ee50d) == float


def test_encircledEnergy_func():
data = numpy.random.rand(32, 32)
x, y = functions.encircledEnergy(data, eeDiameter=False)
print(y.min(), y.max())
assert len(x) == len(y)
assert numpy.max(y) <= 1
assert numpy.min(y) >= 0


def test_aziAvg():
data = numpy.random.rand(32, 32)
azi = functions.aziAvg(data)
print(azi.shape)
assert azi.shape == (16,)
Loading

0 comments on commit a317c05

Please sign in to comment.