<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> from numpy import linalg as LA <EOL> import pyfits <EOL> from matplotlib import pyplot as plt <EOL> import matplotlib . cm as cm <EOL> from scipy import ndimage , optimize <EOL> from time import sleep <EOL> import shutil <EOL> from glob import glob <EOL> from re import split <EOL> import cPickle <EOL> from shutil import copy <EOL> import os <EOL> def quadraticFit ( derivative , ext ) : <EOL> '''<STR_LIT>''' <EOL> rangeOfFit = <NUM_LIT:1> <EOL> lenDer = len ( derivative ) / <NUM_LIT:2> <EOL> if ext == "<STR_LIT>" : <EOL> indExtrema = np . argmax ( derivative [ : lenDer ] ) <EOL> elif ext == "<STR_LIT>" : <EOL> indExtrema = np . argmin ( derivative [ lenDer : ] ) + lenDer <EOL> fitPart = derivative [ indExtrema - rangeOfFit : indExtrema + rangeOfFit + <NUM_LIT:1> ] <EOL> if len ( fitPart ) == <NUM_LIT:3> : <EOL> stackPolynomials = np . zeros ( [ <NUM_LIT:3> , <NUM_LIT:3> ] ) <EOL> for i in range ( <NUM_LIT:0> , len ( fitPart ) ) : <EOL> stackPolynomials [ i , : ] = [ i ** <NUM_LIT:2> , i , <NUM_LIT:1.0> ] <EOL> estimatedCoeffs = np . dot ( LA . inv ( stackPolynomials ) , fitPart ) <EOL> d_fit = - estimatedCoeffs [ <NUM_LIT:1> ] / ( <NUM_LIT> * estimatedCoeffs [ <NUM_LIT:0> ] ) <EOL> extremum = d_fit + indExtrema - rangeOfFit <EOL> else : <EOL> extremum = indExtrema <EOL> return extremum <EOL> def trackSmooth ( image , est_x , est_y , smoothingConst , plottingThings , preCropped = False , zoom = <NUM_LIT> , plots = False ) : <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if plots : <EOL> [ fig , subplotsDimensions , photSubplotsOffset ] = plottingThings <EOL> if preCropped : <EOL> zoom = image . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> <EOL> est_x , est_y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> target = image <EOL> else : <EOL> target = image [ est_x - zoom : est_x + zoom , est_y - zoom : est_y + zoom ] <EOL> if plots : <EOL> target_orig = target . copy ( ) <EOL> axisA_orig = np . sum ( target , axis = <NUM_LIT:0> ) <EOL> axisB_orig = np . sum ( target , axis = <NUM_LIT:1> ) <EOL> target = ndimage . gaussian_filter ( target , sigma = smoothingConst , order = <NUM_LIT:0> ) <EOL> axisA = np . sum ( target , axis = <NUM_LIT:0> ) <EOL> axisB = np . sum ( target , axis = <NUM_LIT:1> ) <EOL> axisADeriv = np . diff ( axisA ) <EOL> axisBDeriv = np . diff ( axisB ) <EOL> lenaxisADeriv = len ( axisADeriv ) <EOL> lenaxisADeriv_2 = lenaxisADeriv / <NUM_LIT:2> <EOL> lenaxisBDeriv = len ( axisBDeriv ) <EOL> lenaxisBDeriv_2 = lenaxisBDeriv / <NUM_LIT:2> <EOL> derivMinAind = np . where ( axisADeriv == min ( axisADeriv [ lenaxisADeriv_2 : lenaxisADeriv ] ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> derivMinBind = np . where ( axisBDeriv == min ( axisBDeriv [ lenaxisBDeriv_2 : lenaxisBDeriv ] ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> derivMaxAind = np . where ( axisADeriv == max ( axisADeriv [ <NUM_LIT:0> : lenaxisADeriv_2 ] ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> derivMaxBind = np . where ( axisBDeriv == max ( axisBDeriv [ <NUM_LIT:0> : lenaxisBDeriv_2 ] ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> extremumA = quadraticFit ( axisADeriv , ext = "<STR_LIT>" ) <EOL> extremumB = quadraticFit ( axisADeriv , ext = "<STR_LIT>" ) <EOL> extremumC = quadraticFit ( axisBDeriv , ext = "<STR_LIT>" ) <EOL> extremumD = quadraticFit ( axisBDeriv , ext = "<STR_LIT>" ) <EOL> averageRadius = ( abs ( derivMinAind - derivMaxAind ) + abs ( derivMinBind - derivMaxBind ) ) / <NUM_LIT> <EOL> axisAcenter = ( extremumA + extremumB ) / <NUM_LIT> <EOL> axisBcenter = ( extremumC + extremumD ) / <NUM_LIT> <EOL> xCenter = est_x - zoom + axisBcenter <EOL> yCenter = est_y - zoom + axisAcenter <EOL> if plots : <EOL> plt . clf ( ) <EOL> def format_coord ( x , y ) : <EOL> '''<STR_LIT>''' <EOL> col = int ( x + <NUM_LIT:0.5> ) <EOL> row = int ( y + <NUM_LIT:0.5> ) <EOL> try : <EOL> return '<STR_LIT>' % ( x , y , target [ row , col ] ) <EOL> except : <EOL> return '<STR_LIT>' % ( x , y ) <EOL> dimx , dimy = target . shape <EOL> med = np . median ( target ) <EOL> dsig = np . std ( target ) <EOL> ax = fig . add_subplot ( subplotsDimensions + <NUM_LIT:1> ) <EOL> ax . imshow ( target_orig , cmap = cm . gray , interpolation = "<STR_LIT>" , vmin = med - <NUM_LIT:0.5> * dsig , vmax = med + <NUM_LIT:2> * dsig ) <EOL> ax . set_title ( '<STR_LIT>' ) <EOL> ax . axvline ( ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , x = axisAcenter + <NUM_LIT:0.5> , color = '<STR_LIT:b>' , linewidth = <NUM_LIT:2> ) <EOL> ax . axhline ( xmin = <NUM_LIT:0> , xmax = <NUM_LIT:1> , y = axisBcenter + <NUM_LIT:0.5> , color = '<STR_LIT:r>' , linewidth = <NUM_LIT:2> ) <EOL> ax . set_xlim ( [ - <NUM_LIT> , dimx - <NUM_LIT> ] ) <EOL> ax . set_ylim ( [ - <NUM_LIT> , dimy - <NUM_LIT> ] ) <EOL> ax . set_xlabel ( '<STR_LIT:X>' ) <EOL> ax . set_ylabel ( '<STR_LIT:Y>' ) <EOL> ax . format_coord = format_coord <EOL> ax2 = fig . add_subplot ( subplotsDimensions + <NUM_LIT:2> ) <EOL> ax2 . set_title ( '<STR_LIT>' ) <EOL> ax2 . plot ( axisB , '<STR_LIT>' ) <EOL> ax2 . plot ( axisB_orig , '<STR_LIT>' , alpha = <NUM_LIT> ) <EOL> ax2 . axvline ( x = extremumC , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:r>' , linestyle = '<STR_LIT::>' , linewidth = <NUM_LIT:1> ) <EOL> ax2 . axvline ( x = extremumD , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:r>' , linestyle = '<STR_LIT::>' , linewidth = <NUM_LIT:1> ) <EOL> ax2 . axvline ( x = axisBcenter , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:r>' , linewidth = <NUM_LIT:2> ) <EOL> ax2 . set_xlabel ( '<STR_LIT:X>' ) <EOL> ax2 . set_ylabel ( '<STR_LIT>' ) <EOL> ax3 = fig . add_subplot ( subplotsDimensions + <NUM_LIT:3> ) <EOL> ax3 . plot ( axisA , '<STR_LIT>' ) <EOL> ax3 . plot ( axisA_orig , '<STR_LIT>' , alpha = <NUM_LIT> ) <EOL> ax3 . set_title ( '<STR_LIT>' ) <EOL> ax3 . axvline ( x = extremumA , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:b>' , linestyle = '<STR_LIT::>' , linewidth = <NUM_LIT:1> ) <EOL> ax3 . axvline ( x = extremumB , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:b>' , linestyle = '<STR_LIT::>' , linewidth = <NUM_LIT:1> ) <EOL> ax3 . axvline ( x = axisAcenter , ymin = <NUM_LIT:0> , ymax = <NUM_LIT:1> , color = '<STR_LIT:b>' , linewidth = <NUM_LIT:2> ) <EOL> ax3 . set_xlabel ( '<STR_LIT:Y>' ) <EOL> ax3 . set_ylabel ( '<STR_LIT>' ) <EOL> plt . draw ( ) <EOL> return [ xCenter , yCenter , averageRadius , False ] <EOL> except Exception : <EOL> print "<STR_LIT>" <EOL> return [ est_x , est_y , <NUM_LIT:1.0> , True ] </s>
