Skip to content

Commit

Permalink
Merge pull request #10 from andrewpaulreeves/NewStructure
Browse files Browse the repository at this point in the history
New structure for AOTools
  • Loading branch information
Andrew Reeves committed Feb 13, 2017
2 parents 02025f2 + dc8998f commit ac7adaa
Show file tree
Hide file tree
Showing 18 changed files with 269 additions and 280 deletions.
4 changes: 1 addition & 3 deletions aotools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .aotools import *

from . import wfs, circle, fft, interp, turbulence

phasescreen = turbulence # For compatibility
from . import astronomy, fft, functions, image_processing, interp, turbulence, wfs

from ._version import get_versions
__version__ = get_versions()['version']
Expand Down
1 change: 1 addition & 0 deletions aotools/astronomy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .astronomy import *
39 changes: 1 addition & 38 deletions aotools/aotools.py → aotools/astronomy/astronomy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""
Unspecified tools for AO and astronomy
"""

def photonsPerMag(mag, mask, pxlScale, wvlBand, expTime):
'''
Calculates the photon flux for a given aperture, star magnitude and wavelength band
Expand Down Expand Up @@ -29,37 +25,4 @@ def photonsPerMag(mag, mask, pxlScale, wvlBand, expTime):

photons = photonPerSec * expTime

return photons

def image_contrast(image):
"""
Calculates the 'Michelson' contrast.
Uses a method by Michelson (Michelson, A. (1927). Studies in Optics. U. of Chicago Press.), to calculate the contrast ratio of an image. Uses the formula:
(img_max - img_min)/(img_max + img_min)
Parameters:
image (ndarray): Image array
Returns:
float: Contrast value
"""

contrast = (image.max() - image.min()) / (image.max() + image.min())

return contrast

def rms_contrast(image):
"""
Calculates the RMS contrast - basically the standard deviation of the image
Parameters:
image (ndarray): Image array
Returns:
float: Contrast value
"""

image /= image.max()

return image.std()
return photons
1 change: 0 additions & 1 deletion aotools/centroiders/__init__.py

This file was deleted.

2 changes: 0 additions & 2 deletions aotools/circle/__init__.py

This file was deleted.

152 changes: 0 additions & 152 deletions aotools/circle/circle.py

This file was deleted.

3 changes: 3 additions & 0 deletions aotools/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .functions import *
from .pupil import *
from .zernike import *
61 changes: 61 additions & 0 deletions aotools/functions/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import numpy


def gaussian2d(size, width, amplitude=1., cent=None):
'''
Generates 2D gaussian distribution
Args:
size (tuple, float): Dimensions of Array to place gaussian
width (tuple, float): Width of distribution.
Accepts tuple for x and y values.
amplitude (float): Amplitude of guassian distribution
cent (tuple): Centre of distribution on grid.
'''

try:
xSize = size[0]
ySize = size[1]
except (TypeError, IndexError):
xSize = ySize = size

try:
xWidth = float(width[0])
yWidth = float(width[1])
except (TypeError, IndexError):
xWidth = yWidth = float(width)

if not cent:
xCent = size[0] / 2.
yCent = size[1] / 2.
else:
xCent = cent[0]
yCent = cent[1]

X, Y = numpy.meshgrid(range(0, xSize), range(0, ySize))

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
"""

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

return avg
89 changes: 89 additions & 0 deletions aotools/functions/pupil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
def circle(radius, size, circle_centre=(0, 0), origin="middle"):
"""
Create a 2-D array: elements equal 1 within a circle and 0 outside.
The default centre of the coordinate system is in the middle of the array:
circle_centre=(0,0), origin="middle"
This means:
if size is odd : the centre is in the middle of the central pixel
if size is even : centre is in the corner where the central 4 pixels meet
origin = "corner" is used e.g. by psfAnalysis:radialAvg()
Examples:
circle(1,5) circle(0,5) circle(2,5) circle(0,4) circle(0.8,4) circle(2,4)
00000 00000 00100 0000 0000 0110
00100 00000 01110 0000 0110 1111
01110 00100 11111 0000 0110 1111
00100 00000 01110 0000 0000 0110
00000 00000 00100
circle(1,5,(0.5,0.5)) circle(1,4,(0.5,0.5))
.-->+
| 00000 0000
| 00000 0010
+V 00110 0111
00110 0010
00000
Args:
radius (float) : radius of the circle
size (int) : size of the 2-D array in which the circle lies
circle_centre (tuple): coords of the centre of the circle
origin (str) : where is the origin of the coordinate system
in which circle_centre is given;
allowed values: {"middle", "corner"}
Returns:
ndarray (float64) : the circle array
Raises:
TypeError if input is of wrong type
Exception if a bug in generation of coordinates is detected (see code)
"""
# (2) Generate the output array:
C = numpy.zeros((size, size))

# (3.a) Generate the 1-D coordinates of the pixel's centres:
# coords = numpy.linspace(-size/2.,size/2.,size) # Wrong!!:
# size = 5: coords = array([-2.5 , -1.25, 0. , 1.25, 2.5 ])
# size = 6: coords = array([-3. , -1.8, -0.6, 0.6, 1.8, 3. ])
# (2015 Mar 30; delete this comment after Dec 2015 at the latest.)

# Before 2015 Apr 7 (delete 2015 Dec at the latest):
# coords = numpy.arange(-size/2.+0.5, size/2.-0.4, 1.0)
# size = 5: coords = array([-2., -1., 0., 1., 2.])
# size = 6: coords = array([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])

coords = numpy.arange(0.5, size, 1.0)
# size = 5: coords = [ 0.5 1.5 2.5 3.5 4.5]
# size = 6: coords = [ 0.5 1.5 2.5 3.5 4.5 5.5]

# (3.b) Just an internal sanity check:
if len(coords) != size:
raise exceptions.Bug("len(coords) = {0}, ".format(len(coords)) +
"size = {0}. They must be equal.".format(size) +
"\n Debug the line \"coords = ...\".")

# (3.c) Generate the 2-D coordinates of the pixel's centres:
x, y = numpy.meshgrid(coords, coords)

# (3.d) Move the circle origin to the middle of the grid, if required:
if origin == "middle":
x -= size / 2.
y -= size / 2.

# (3.e) Move the circle centre to the alternative position, if provided:
x -= circle_centre[0]
y -= circle_centre[1]

# (4) Calculate the output:
# if distance(pixel's centre, circle_centre) <= radius:
# output = 1
# else:
# output = 0
mask = x * x + y * y <= radius * radius
C[mask] = 1

# (5) Return:
return C
File renamed without changes.
2 changes: 2 additions & 0 deletions aotools/image_processing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .centroiders import *
from .image_processing import *
File renamed without changes.
Loading

0 comments on commit ac7adaa

Please sign in to comment.