def pearson_r(data_1, data_2):
"""
Compute the Pearson correlation coefficient between two samples.
Parameters
----------
data_1 : array_like
One-dimensional array of data.
data_2 : array_like
One-dimensional array of data.
Returns
-------
output : float
The Pearson correlation coefficient between `data_1`
and `data_2`.
Notes
-----
.. Only entries where both `data_1` and `data_2` are not NaN are
used.
.. If the variance of `data_1` or `data_2` is zero, return NaN.
"""
x, y = _convert_two_data(data_1, data_2, inf_ok=False, min_len=2)
return _pearson_r(x, y)
python类float()的实例源码
def swap_random(a, b):
"""
Randomly swap entries in two arrays.
Parameters
----------
a : array_like
1D array of entries to be swapped.
b : array_like
1D array of entries to be swapped. Must have the same lengths
as `a`.
Returns
-------
a_out : ndarray, dtype float
Array with random entries swapped.
b_out : ndarray, dtype float
Array with random entries swapped.
"""
a, b = _convert_two_data(a, b)
return _swap_random(a, b)
def frac_yay_dems(dems, reps):
"""
Compute fraction of yay votes from Democrats. This function is
specific to exercises in Statistical Thinking in Python Part I.
It is only included here for completeness.
Parameters
----------
dems : array_like, dtype bool
Votes for democrats, True for yay vote, False for nay.
reps : ignored
Ignored; was only needed to specific application in permutation
test in Statistical Thinking I.
Returns
-------
output : float
Fraction of Democrates who voted yay.
"""
if dems.dtype != bool:
raise RuntimeError('`dems` must be array of bools.')
return np.sum(dems) / len(dems)
def heritability(parents, offspring):
"""
Compute the heritability from parent and offspring samples.
Parameters
----------
parents : array_like
Array of data for trait of parents.
offspring : array_like
Array of data for trait of offspring.
Returns
-------
output : float
Heritability of trait.
"""
par, off = _convert_two_data(parents, offspring)
covariance_matrix = np.cov(par, off)
return covariance_matrix[0,1] / covariance_matrix[0,0]
generate_melody.py 文件源码
项目:SourceFilterContoursMelody
作者: juanjobosch
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def normalize(x, axis=None):
"""Normalize the values of an ndarray to sum to 1 along the given axis.
Parameters
----------
x : np.ndarray
Input multidimensional array to normalize.
axis : int, default=None
Axis to normalize along, otherwise performed over the full array.
Returns
-------
z : np.ndarray, shape=x.shape
Normalized array.
"""
if not axis is None:
shape = list(x.shape)
shape[axis] = 1
scalar = x.astype(float).sum(axis=axis).reshape(shape)
scalar[scalar == 0] = 1.0
else:
scalar = x.sum()
scalar = 1 if scalar == 0 else scalar
return x / scalar
def bbox_overlaps(boxes, query_boxes):
"""
determine overlaps between boxes and query_boxes
:param boxes: n * 4 bounding boxes
:param query_boxes: k * 4 bounding boxes
:return: overlaps: n * k overlaps
"""
n_ = boxes.shape[0]
k_ = query_boxes.shape[0]
overlaps = np.zeros((n_, k_), dtype=np.float)
for k in range(k_):
query_box_area = (query_boxes[k, 2] - query_boxes[k, 0] + 1) * (query_boxes[k, 3] - query_boxes[k, 1] + 1)
for n in range(n_):
iw = min(boxes[n, 2], query_boxes[k, 2]) - max(boxes[n, 0], query_boxes[k, 0]) + 1
if iw > 0:
ih = min(boxes[n, 3], query_boxes[k, 3]) - max(boxes[n, 1], query_boxes[k, 1]) + 1
if ih > 0:
box_area = (boxes[n, 2] - boxes[n, 0] + 1) * (boxes[n, 3] - boxes[n, 1] + 1)
all_area = float(box_area + query_box_area - iw * ih)
overlaps[n, k] = iw * ih / all_area
return overlaps
def _coco_results_one_category(self, boxes, cat_id):
results = []
for im_ind, index in enumerate(self.image_index):
dets = boxes[im_ind].astype(np.float)
if dets == []:
continue
scores = dets[:, -1]
xs = dets[:, 0]
ys = dets[:, 1]
ws = dets[:, 2] - xs + 1
hs = dets[:, 3] - ys + 1
results.extend(
[{'image_id' : index,
'category_id' : cat_id,
'bbox' : [xs[k], ys[k], ws[k], hs[k]],
'score' : scores[k]} for k in xrange(dets.shape[0])])
return results
def SExtractorCat2fits(sextractorfiles,stringcols=[1],header=73,verbose=True):
"""
Converting an ascii catalog with columns defined in header in the SExtractor format, i.e. one column
name per row preceeded by a "#" and a column numner, and followed by a description (or any ascii file
with the given setup) to a fits binary table
--- INPUT ---
sextractorfiles List of ascii files to convert to fits
stringcols Columns to use a string format for (all other columns will be set to double float)
header Header containing the column names of the catalogs following the "SExtractor notation"
verbose Toggle verbosity
--- EXAMPLE OF USE ---
import glob
import tdose_utilities as tu
catalogs = glob.glob('/Volumes/DATABCKUP2/MUSE-Wide/catalogs_photometry/catalog_photometry_candels-cdfs-*.cat')
tu.SExtractorCat2fits(catalogs,stringcols=[1],header=73,verbose=True)
"""
for sexcat_ascii in sextractorfiles:
asciiinfo = open(sexcat_ascii,'r')
photcols = []
for line in asciiinfo:
if line.startswith('#'):
colname = line.split()[2]
photcols.append(colname)
photfmt = ['D']*len(photcols)
for stringcol in stringcols:
photfmt[stringcol] = 'A60'
sexcat_fits = tu.ascii2fits(sexcat_ascii,asciinames=photcols,skip_header=header,fitsformat=photfmt,verbose=verbose)
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def galfit_getheadervalue(compnumber,key,headerinfo):
"""
Return the paramters of a GALFIT model header
--- INPUT ---
compnumber A string containing the component number to extract info for (number after "COMP_" in header)
key The key to extract (keyword after "COMPNUMBER_" in header)
headerinfo Header to extract info from.
"""
hdrinfo = headerinfo[compnumber+'_'+key]
if '*' in hdrinfo: # handling parameters fixed in GALFIT run
hdrinfo = hdrinfo.replace('*','')
if '+/-' in hdrinfo:
value = float(hdrinfo.split('+/-')[0])
error = float(hdrinfo.split('+/-')[1])
else:
value = float(hdrinfo[1:-1])
error = None
if (key == 'XC') or (key == 'YC'):
xrange, yrange = headerinfo['FITSECT'][1:-1].split(',')
xrange = np.asarray(xrange.split(':')).astype(float)
yrange = np.asarray(yrange.split(':')).astype(float)
if key == 'XC':
value = value - xrange[0] + 1.0
if key == 'YC':
value = value - yrange[0] + 1.0
return value, error
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def galfit_getcentralcoordinate(modelfile,coordorigin=1,verbose=True):
"""
Return the central coordinates of a GALFIT model extracted using the reference image WCS and the FITSECT keyword
--- INPUT ---
modelfile Path and name to GALFIT model fits file to retrieve central coordinates for
coordorigin Origin of coordinates in reference image to use when converting pixels to degrees (skycoord)
verbose Toggle verbosity
--- EXAMPLE OF USE ---
fileG = '/Volumes/DATABCKUP2/TDOSEextractions/models_cutouts/model8685multicomponent/model_acs_814w_candels-cdfs-02_cut_v1.0_id8685_cutout7p0x7p0arcsec.fits' # Gauss components
fileS = '/Volumes/DATABCKUP2/TDOSEextractions/models_cutouts/model8685multicomponent/model_acs_814w_candels-cdfs-02_cut_v1.0_id9262_cutout2p0x2p0arcsec.fits' # Sersic components
xpix, ypix, ra_model, dec_model = tu.galfit_getcentralcoordinate(fileG,coordorigin=1)
"""
if verbose: print ' - Will extract central coordinates from '+modelfile
refimg_hdr = pyfits.open(modelfile)[1].header
model_hdr = pyfits.open(modelfile)[2].header
imgwcs = wcs.WCS(tu.strip_header(refimg_hdr.copy()))
fit_region = model_hdr['FITSECT']
cutrange_low_x = int(float(fit_region.split(':')[0].split('[')[-1]))
cutrange_low_y = int(float(fit_region.split(',')[-1].split(':')[0]))
xsize = model_hdr['NAXIS1']
ysize = model_hdr['NAXIS2']
xpix = cutrange_low_x + int(xsize/2.)
ypix = cutrange_low_y + int(ysize/2.)
if verbose: print ' - Converting pixel position to coordinates using a pixel origin='+str(coordorigin)
skycoord = wcs.utils.pixel_to_skycoord(xpix,ypix,imgwcs,origin=coordorigin)
ra_model = skycoord.ra.value
dec_model = skycoord.dec.value
return xpix,ypix,ra_model,dec_model
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def get_user(self, username):
leaderboard, status_code = self.get_leaderboard()
if status_code!=200:
return (None, None, None, None, status_code)
for user in leaderboard[0]['leaderboard']:
if user['username']==username:
return (user['username'], np.float(user['logloss']['public']), user['rank']['public'], user['earned'], status_code)
return (None, None, None, None, status_code)
def load_data(ds_name, use_node_labels):
node2graph = {}
Gs = []
with open("../datasets/%s/%s_graph_indicator.txt"%(ds_name,ds_name), "r") as f:
c = 1
for line in f:
node2graph[c] = int(line[:-1])
if not node2graph[c] == len(Gs):
Gs.append(nx.Graph())
Gs[-1].add_node(c)
c += 1
with open("../datasets/%s/%s_A.txt"%(ds_name,ds_name), "r") as f:
for line in f:
edge = line[:-1].split(",")
edge[1] = edge[1].replace(" ", "")
Gs[node2graph[int(edge[0])]-1].add_edge(int(edge[0]), int(edge[1]))
if use_node_labels:
with open("../datasets/%s/%s_node_labels.txt"%(ds_name,ds_name), "r") as f:
c = 1
for line in f:
node_label = int(line[:-1])
Gs[node2graph[c]-1].node[c]['label'] = node_label
c += 1
labels = []
with open("../datasets/%s/%s_graph_labels.txt"%(ds_name,ds_name), "r") as f:
for line in f:
labels.append(int(line[:-1]))
labels = np.array(labels, dtype = np.float)
return Gs, labels
def empty(N, dtype=np.float, bytes=16):
return pyfftw.empty_aligned(N, dtype=dtype, n=bytes)
def zeros(N, dtype=np.float, bytes=16):
return pyfftw.zeros_aligned(N, dtype=dtype, n=bytes)
def empty(N, dtype=np.float, bytes=None):
return Empty(N, dtype=dtype)
def zeros(N, dtype=np.float, bytes=None):
return Zeros(N, dtype=dtype)
def _read_configuration(self):
"""Initialize configuration from ROS parameters.
"""
self.uwb_multi_range_topic = rospy.get_param('~multi_range_raw_topic', '/uwb/multi_range_with_offsets')
self.uwb_tracker_topic = rospy.get_param('~tracker_topic', '/uwb/tracker')
self.tracker_frame = rospy.get_param('~tracker_frame', 'uwb')
self.target_frame = rospy.get_param('~target_frame', 'target')
# Get parameters for covariance matrices
self.initial_position_covariance = rospy.get_param('~initial_position_covariance', 10)
self.process_covariance_position = rospy.get_param('~process_covariance_position', 0)
self.process_covariance_velocity = rospy.get_param('~process_covariance_velocity', 1)
self.measurement_covariance = rospy.get_param('~measurement_covariance', 0.1 ** 2)
# Get parameters for filter update and initial gauss-newton estimation
self.ignore_z_position = rospy.get_param('~ignore_z_position', True)
# The default value of 7.779 represents the 0.9 quantile of a Chi-Square distribution
# with 4 degrees of freedom (4 UWB measurements).
self.outlier_threshold_quantile = rospy.get_param('~outlier_threshold_quantile', 0.1)
self.ikf_iterations = rospy.get_param('~ikf_iterations', 2)
self.initial_guess_position = np.empty((3, 1), dtype=np.float)
self.initial_guess_position[0] = rospy.get_param('~initial_guess_position_x', 0)
self.initial_guess_position[1] = rospy.get_param('~initial_guess_position_y', 0)
self.initial_guess_position[2] = rospy.get_param('~initial_guess_position_z', 0)
self.initial_guess_iterations = rospy.get_param('~initial_guess_iterations', 200)
self.initial_guess_tolerance = rospy.get_param('~initial_guess_tolerance', 1e-5)
self.initial_guess_residuals_threshold = rospy.get_param('~initial_guess_residuals_threshold', 0.1)
self.ikf_max_outlier_count = rospy.get_param('~ikf_max_outlier_count', 200)
def update_filter(self, timestep, estimate, ranges):
"""Update position filter.
Args:
timestep (float): Time elapsed since last update.
estimate (StateEstimate): Position estimate to update.
ranges (list of floats): Range measurements.
Returns:
new_estimate (StateEstimate): Updated position estimate.
outlier_flag (bool): Flag indicating whether the measurement was rejected as an outlier.
"""
num_of_units = len(ranges)
x = estimate.state
P = estimate.covariance
# Compute process matrix and covariance matrices
F, Q, R = self._compute_process_and_covariance_matrices(timestep)
# rospy.logdebug('F: {}'.format(F))
# rospy.logdebug('Q: {}'.format(Q))
# rospy.logdebug('R: {}'.format(R))
# Prediction
x = np.dot(F, x)
P = np.dot(F, np.dot(P, F.T)) + Q
# Update
n = np.copy(x)
H = np.zeros((num_of_units, x.size))
z = np.zeros((num_of_units, 1))
h = np.zeros((num_of_units, 1))
for i in xrange(self.ikf_iterations):
n, K, outlier_flag = self._ikf_iteration(x, n, ranges, h, H, z, estimate, R)
if outlier_flag:
new_estimate = estimate
else:
new_state = n
new_covariance = np.dot((np.eye(6) - np.dot(K, H)), P)
new_estimate = UWBTracker.StateEstimate(new_state, new_covariance)
return new_estimate, outlier_flag
def arrangement_sort(x, y):
return int(100000000*(abs(float(min(x['width'],x['height'])) / float(max(x['width'],x['height']))) - abs(float(min(y['width'],y['height'])) / float(max(y['width'],y['height'])))))
def proportion_sort(x, y):
return int(100000000*(abs(float(min(x[0],x[1])) / float(max(x[0],x[1]))) - abs(float(min(y[0],y[1])) / float(max(y[0],y[1])))))