<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> from collections import deque <EOL> import numpy as np <EOL> import scipy . signal <EOL> from . constants import calibration_standards <EOL> from . feature import ( filter_peak_height , peak_refinement , <EOL> refine_log_quadratic ) <EOL> from . utils import ( angle_grid , radial_grid , <EOL> pairwise , bin_edges_to_centers , bin_1D ) <EOL> def estimate_d_blind ( name , wavelength , bin_centers , ring_average , <EOL> window_size , max_peak_count , thresh ) : <EOL> """<STR_LIT>""" <EOL> cal = calibration_standards [ name ] <EOL> cands = scipy . signal . argrelmax ( ring_average , order = window_size ) [ <NUM_LIT:0> ] <EOL> cands = filter_peak_height ( ring_average , cands , <EOL> thresh * np . max ( ring_average ) , window = window_size ) <EOL> peaks_x , peaks_y = peak_refinement ( bin_centers , ring_average , cands , <EOL> window_size , refine_log_quadratic ) <EOL> tan2theta = np . tan ( cal . convert_2theta ( wavelength ) ) <EOL> slc = slice ( <NUM_LIT:0> , np . min ( [ len ( tan2theta ) , len ( peaks_x ) , max_peak_count ] ) ) <EOL> d_array = ( peaks_x [ slc ] / tan2theta [ slc ] ) <EOL> return np . mean ( d_array ) , np . std ( d_array ) <EOL> estimate_d_blind . name = list ( calibration_standards ) <EOL> def refine_center ( image , calibrated_center , pixel_size , phi_steps , max_peaks , <EOL> thresh , window_size , <EOL> nx = None , min_x = None , max_x = None ) : <EOL> """<STR_LIT>""" <EOL> if nx is None : <EOL> nx = int ( np . mean ( image . shape ) * <NUM_LIT:2> ) <EOL> phi = angle_grid ( calibrated_center , image . shape , pixel_size ) . ravel ( ) <EOL> r = radial_grid ( calibrated_center , image . shape , pixel_size ) . ravel ( ) <EOL> I = image . ravel ( ) <EOL> phi_steps = np . linspace ( - np . pi , np . pi , phi_steps , endpoint = True ) <EOL> out = deque ( ) <EOL> for phi_start , phi_end in pairwise ( phi_steps ) : <EOL> mask = ( phi <= phi_end ) * ( phi > phi_start ) <EOL> out . append ( bin_1D ( r [ mask ] , I [ mask ] , <EOL> nx = nx , min_x = min_x , max_x = max_x ) ) <EOL> out = list ( out ) <EOL> ring_trace = [ ] <EOL> for bins , b_sum , b_count in out : <EOL> mask = b_sum > <NUM_LIT:10> <EOL> avg = b_sum [ mask ] / b_count [ mask ] <EOL> bin_centers = bin_edges_to_centers ( bins ) [ mask ] <EOL> cands = scipy . signal . argrelmax ( avg , order = window_size ) [ <NUM_LIT:0> ] <EOL> cands = filter_peak_height ( avg , cands , thresh * np . max ( avg ) , <EOL> window = window_size ) <EOL> ring_trace . append ( bin_centers [ cands [ : max_peaks ] ] ) <EOL> tr_len = [ len ( rt ) for rt in ring_trace ] <EOL> mm = np . min ( tr_len ) <EOL> ring_trace = np . vstack ( [ rt [ : mm ] for rt in ring_trace ] ) . T <EOL> mean_dr = np . mean ( ring_trace - np . mean ( ring_trace , axis = <NUM_LIT:1> , keepdims = True ) , <EOL> axis = <NUM_LIT:0> ) <EOL> phi_centers = bin_edges_to_centers ( phi_steps ) <EOL> delta = np . mean ( np . diff ( phi_centers ) ) <EOL> col_shift = ( np . sum ( np . sin ( phi_centers ) * mean_dr ) * <EOL> delta / ( np . pi * pixel_size [ <NUM_LIT:1> ] ) ) <EOL> row_shift = ( np . sum ( np . cos ( phi_centers ) * mean_dr ) * <EOL> delta / ( np . pi * pixel_size [ <NUM_LIT:0> ] ) ) <EOL> return tuple ( np . array ( calibrated_center ) + <EOL> np . array ( [ row_shift , col_shift ] ) ) </s>
