Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move notebook functions into .py file and add tests (which will fail) #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions shifty.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@
},
"outputs": [],
"source": [
"\n",
"import numpy as np\n",
"import matplotlib.mlab as mlab\n",
"\n",
Expand All @@ -276,7 +275,7 @@
" fig = plt.plot(x_gauss,y_gauss)\n",
" \n",
" print('mean = ', mean, 'sigma = ', sigma)\n",
" return fig\n"
" return fig"
]
},
{
Expand Down Expand Up @@ -396,7 +395,7 @@
" df['RMS'] = RMSs\n",
" df['sigma'] = sigmas\n",
"\n",
" df.to_csv(str(data_tar[0][1])+'_RMS_bad_removed.txt',sep='\\t')\n"
" df.to_csv(str(data_tar[0][1])+'_RMS_bad_removed.txt',sep='\\t')"
]
},
{
Expand Down Expand Up @@ -861,8 +860,7 @@
" plt.plot(w_tar_plot,f_tar_plot,color='black')\n",
" plt.plot(w_tar_plot,f_comp_plot,color='red')\n",
" \n",
" plt.show()\n",
" "
" plt.show()"
]
},
{
Expand Down Expand Up @@ -904,7 +902,9 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
""
]
}
],
"metadata": {
Expand All @@ -916,7 +916,7 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 3.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
Expand All @@ -927,5 +927,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
}
"nbformat_minor": 0
}
18 changes: 7 additions & 11 deletions shifty.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import matplotlib.mlab as mlab
from scipy.stats import norm
# import BDdb
import astrodb
from astrodbkit import astrodb
import pandas as pd

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring to the code

db = astrodb.Database('/Users/victoriaditomasso/Desktop/BDNYCdeprecated.db')
Expand All @@ -27,7 +27,7 @@ def mc_rms(y_array1, y_array2, unc1, unc2, n):
y_array1_rand = y_array1 + (np.random.randn(len(y_array1)) * unc1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line by line comments would be helpful, describing what the individual line achieves. For example, this line 'wiggles' within the uncertainty.

y_array2_rand = y_array2 + (np.random.randn(len(y_array2)) * unc2)
diff = y_array1_rand - y_array2_rand
rms = (np.sum(diff ** 2)) / (len(diff)) ** (0.5)
rms = np.sum(diff ** 2) / len(diff) ** 0.5
RMS.append(rms)

avg = np.mean(RMS)
Expand Down Expand Up @@ -92,8 +92,7 @@ def get_comp_data(spec_id):
Arguments: spec_id
Outputs: w_comp,f_comp,rv_comp,unc_comp"""

data_comp = db.query(
"select spectra.wavelength, spectra.flux, spectra.unc from spectra where spectra.id={}".format(row['spec_id']))
data_comp = db.query("select spectra.wavelength, spectra.flux, spectra.unc from spectra where spectra.id={}").format(spec_id)

w_comp = np.asarray(data_comp[0][0])

Expand All @@ -115,7 +114,7 @@ def rv_shift(wavelength, rv):
Arguments: wavelength array, rv
Outputs: shifted_wavelength"""

shifted_wavelength = (wavelength) * (1. - (rv / 2.99792458e5))
shifted_wavelength = wavelength * (1. - (rv / 2.99792458e5))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is that number? Comment - also if it is a known constant try using astropy.constants, if the attached units give trouble, at end of variable put variable.value()


return shifted_wavelength

Expand All @@ -135,9 +134,6 @@ def normalize_spectra(f1, unc1, f2, unc2):
return f2_norm


import numpy as np
import matplotlib.mlab as mlab


def mc_output_hist(MC_calculations, bins=25):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document this function

plt.hist(MC_calculations, bins, normed=True)
Expand Down Expand Up @@ -177,7 +173,7 @@ def yfcq(tar_source_id, spec_order, path_to_comp_sample_dataframe):
unc_tar = data_tar[0][4]

# Adjusting wavelength array of the target according to its RV
w_tar = w_tar * (1. - (rv_tar / 2.99792458e5))
w_tar *= 1. - (rv_tar / 2.99792458e5)

# Reads in the dataframe
df = pd.read_csv(path_to_comp_sample_dataframe, sep='\t')
Expand Down Expand Up @@ -209,7 +205,7 @@ def yfcq(tar_source_id, spec_order, path_to_comp_sample_dataframe):
unc_comp = snr_to_unc(unc_comp)

# Shifts spectra based on their RVs
w_comp = w_comp * (1. - (rv_comp / 2.99792458e5))
w_comp *= 1. - (rv_comp / 2.99792458e5)

# Interpolates comparison spectrum and comparison unc
# Remember: once you interpolate, you need to plot the w_tar vs the interpolated flux NOT w_comp vs interpolated flux
Expand Down Expand Up @@ -257,7 +253,7 @@ def vis_rms_results(tar_source_id, spec_order, path_to_sorted_dataframe):
w_tar, f_tar, rv_tar, unc_tar = get_tar_data(tar_source_id, spec_order)

# Shift target wavelength according to its RV
w_tar = (w_tar) * (1. - (rv_tar / 2.99792458e5))
w_tar = w_tar * (1. - (rv_tar / 2.99792458e5))

# Read in the dataframe
df = pd.read_csv(path_to_sorted_dataframe, sep='\t')
Expand Down