In [1]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
In [2]:
# Import packages
import os.path
import time
import numpy as np
np.set_printoptions(threshold=np.nan) # print entire matrices
import sys
import inspect
import matplotlib.pyplot as plt
import scipy.io as sio
from tqdm import *
import pandas as pd
In [4]:
import seaborn as sns
sns.set_palette('hls')
sns.set_style(style='white')
In [11]:
sys.path.append('../scripts/')
import bicorr as bicorr
import bicorr_plot as bicorr_plot
In [6]:
%load_ext autoreload
%autoreload 2
In [7]:
# Load det_df
det_df = bicorr.load_det_df(plot_flag=True)
In [9]:
det_df.head()
Out[9]:
In [8]:
plt.scatter(det_df.index,det_df['angle'].values,color='k')
plt.xlabel('DataFrame index')
plt.ylabel('Angle (degrees)')
plt.title('Detector pair angles (degrees)')
plt.xlim([-5,1000])
plt.ylim([0,185])
plt.grid()
plt.savefig('../fig/angle_vs_pair.png')
plt.show()
In [14]:
plt.hist(det_df['angle'],20,color='gray')
plt.xlabel('Angle between detector pairs (degrees)')
plt.ylabel('Number of detector pairs')
plt.title('Distribution of detector pair angles')
bicorr_plot.save_fig_to_folder(fig_filename='hist_angles',fig_folder='../fig')
plt.show()
In [12]:
bin_edges = np.array([14.0,20.0,30.0,40.0,50.0,59.0,61.0,65.0,75.0,85.0,95.0, 105.0,115.0,125.0, 133.0,139.0,145.0,149.0,153.0,160.0,170.0,180.0])
bin_centers = (bin_edges[1:] + bin_edges[:-1])/2
In [13]:
hist = np.histogram(det_df['angle'].values, bins = bin_edges)[0]
In [14]:
plt.plot(bin_centers, hist,'.k')
plt.xlabel('Bin center, detector pair angle (degrees)')
plt.ylabel('Number of pairs')
plt.title('Non-uniform bin widths')
plt.show()
How many unique angles are there?
In [15]:
[unique, unique_counts] = np.unique(det_df['angle'].values,return_counts=True)
print(unique.shape)
In [16]:
plt.plot(unique,unique_counts,'.k')
plt.xlabel('Detector pair angle')
plt.ylabel('Number of pairs')
plt.title('Unique detector pair angles')
plt.savefig('../fig/angles_unique.png')
plt.show()
Round to the nearest .1 degrees. This collapses 6 bins... not very many.
In [17]:
[unique, unique_counts] = np.unique(np.round(det_df['angle'].values,1),return_counts=True)
print(unique.shape)
plt.plot(unique,unique_counts,'.k')
plt.xlabel('Detector pair angle')
plt.ylabel('Number of pairs')
plt.title('Unique detector pair angles')
plt.show()
Try rounding it to the nearest degree. This collapses the data a lot to only 90 unique angles.
In [18]:
[unique, unique_counts] = np.unique(np.round(det_df['angle'].values,0),return_counts=True)
print(unique.shape)
plt.plot(unique,unique_counts,'.k')
plt.xlabel('Detector pair angle')
plt.ylabel('Number of pairs')
plt.title('Unique detector pair angles')
plt.savefig('../fig/angles_unique_round.png')
plt.show()
Into which bin in the histogram does each detector pair fall?
Make use of the numpy function np.digitize
, https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.digitize.html.
In [19]:
angle_hist, angle_bin_edges = np.histogram(det_df['angle'].values,20)
print(angle_bin_edges)
In [20]:
for pair in det_df.index:
det_df.loc[pair,'bin'] = np.digitize(det_df.loc[pair,'angle'],angle_bin_edges)
In [21]:
det_df.head()
Out[21]:
In [22]:
i_bin = 19
det_df[det_df['bin']==i_bin]
pair_is = det_df[det_df['bin']==i_bin].index.values
print(len(pair_is), 'pairs bin', i_bin, 'at indices', pair_is)
det_df.loc[pair_is]
Out[22]:
In [23]:
mean_angle = np.mean(det_df.loc[pair_is]['angle'].values)
print(mean_angle)
In [24]:
[unique_bin, unique_counts_bin] = np.unique(det_df.loc[pair_is]['angle'],return_counts=True)
print(unique_bin)
print(unique_counts_bin)
In [25]:
th_min = 20.0
th_max = 25.0
ind_mask = (det_df['angle'] > th_min) & (det_df['angle'] <= th_max) # Includes upper bin edge
ind = det_df.index[ind_mask].values
print(ind)
In [26]:
bicorr.generate_pair_is(det_df,th_min = 0,th_max=20)
Out[26]:
In [27]:
det_df.loc[bicorr.generate_pair_is(det_df,i_bin = 18)]
Out[27]:
In [28]:
# Load det_df
det_df = bicorr.load_det_df()
In [29]:
det_df.head()
Out[29]:
In [30]:
angle_hist, angle_bin_edges = plt.hist(det_df['angle'].values,bins=np.arange(0,181,30))[0:2]
plt.xlabel('Angle (degrees)')
plt.ylabel('Number of pairs')
plt.show()
In [31]:
angle_bin_edges
Out[31]:
Fill bin
column
In [32]:
for pair in det_df.index:
det_df.loc[pair,'bin'] = np.digitize(det_df.loc[pair,'angle'],angle_bin_edges,right=True)
In [33]:
det_df.head()
Out[33]:
Loop through each bin and generate, plot pair_is
In [34]:
count = 0
for i_bin in np.arange(1,len(angle_bin_edges)):
print('Bin ', i_bin)
pair_is = bicorr.generate_pair_is(det_df, i_bin = i_bin)
count += len(pair_is)
plt.scatter(det_df.loc[pair_is]['d1'],det_df.loc[pair_is]['d2'],c=list(det_df.loc[pair_is]['angle']),s=20,marker='s',edgecolor='none',cmap='jet')
plt.colorbar()
plt.xlim([0,50])
plt.ylim([0,50])
plt.xlabel('Detector 1')
plt.ylabel('Detector 2')
plt.title('Detector pairs between {} and {} degrees'.format(angle_bin_edges[i_bin-1],angle_bin_edges[i_bin]))
plt.show()
print(count)
In [ ]: