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

gammaspec.py changes #1046

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open

Conversation

KennellyT
Copy link
Contributor

Functions are now undercase with underscore, added extra line on the bottom of gammaspec.py, and added a test to test spectrometry.py

@@ -91,7 +91,21 @@ def test_gross_counts():
def test_net_count():
nc=sa.net_counts(gspec1, 475, 484, 1)


def test_read_spec_id_file():
Copy link
Member

Choose a reason for hiding this comment

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

How is this working? You don't seem to actually call read_spec_id_file()

Copy link
Contributor

Choose a reason for hiding this comment

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

it's called at global scope

@KennellyT
Copy link
Contributor Author

@scopatz, It seems I forgot to add another gspec variable to call read_spec_id_file(). Ill edit this file to add gspec3 which calls read_spec_id_file().

@scopatz
Copy link
Member

scopatz commented Jun 4, 2018

OK! Just post to this issue when you have it figured out!

@KennellyT
Copy link
Contributor Author

gspec3 variable has been added to test_spectrometry.py and test_read_spec_id_file() has been edited to test the read_spec_id() function. I also added another module called gamma_effect.py that calculates the compton edge energy, backscatter energy ,single escape energy, and double escape energy when given an input energy. A test function has been created for this module and is called test_gamma_effects.py.
I will be drafting a tutorial on how to use gammaspec as well as gamma_effects to analyze .spe files within the week.

"""Returns compton edge energy from a given gamma energy"""
E_compton = E- E/(1+(2*E/mec2))
return E_compton
def backscatter(E):
Copy link
Member

Choose a reason for hiding this comment

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

PEP8: two spaces between functions.


"""

mec2 = 511. #keV # Rest mass energy of an electron #.511 MeV
Copy link
Member

Choose a reason for hiding this comment

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

global variables should be ALL_UPPERCASE

@scopatz
Copy link
Member

scopatz commented Jun 8, 2018

Thanks @KennellyT - just let me know here, when you think this PR is ready to go!

@KennellyT
Copy link
Contributor Author

KennellyT commented Jun 8, 2018

Changed pull request to mirror comments. Also added srim_read.py, a function that reads srim files. Also added writecsv function in gammaspec.py.

@KennellyT
Copy link
Contributor Author

Update of the above pull requests:
gammaspec.py:

read_spec_id_file()- reads and collects data from .spe files of the 'spec_id' format.
write_csv()-takes energy,counts, and channels from spectrum and writes them to csv file to be named by user.

srim_read.py
read_srim_output()-Reads a srim file

spectanalysis.py
net_area()-Calculates the net area under a peak by subtracting background from total
counts under the peak.
end_point_average_area()- Calculates the net area under a peak by end point averaging the
background counts and subtracting the background from total
counts under the peak.

gaussian_fit()-Plots the Gaussian fit of the counts versus channel number.

fwhm()- Calculates the full width half maximum of a peak.

@@ -0,0 +1,35 @@
"""Purpose:
Copy link
Member

Choose a reason for hiding this comment

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

We use numpydoc format. So you don't need "purpose" and the following lines shouldn't be indented. thanks!


def compton_edge(E):
"""Returns compton edge energy from a given gamma energy"""
E_compton = E- E/(1+(2*E/MEC_2))
Copy link
Member

Choose a reason for hiding this comment

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

There should be a single space in front of the minus: E - E/

def single_escape(E):
"""Returns single escape energy from a given gamma energy"""
if (E < 1022): #keV
raise RuntimeError('E needs to be equal to or greater than 1022 keV to be valid')
Copy link
Member

Choose a reason for hiding this comment

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

This should be a ValueError instead of a RuntimeError, I would think.

def double_escape(E):
"""Returns double escape energy from a given gamma energy"""
if (E < 1022): #keV
raise RuntimeError('E needs to be equal to or greater than 1022 keV to be valid')
Copy link
Member

Choose a reason for hiding this comment

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

ValueError again.

long_straggs=None,long_stragg_units=None,lat_straggs=None,
lat_stragg_units=None):
"""Define srim specific variables """
super(srim, self).__init__()
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need to call super, but it doesn't hurt.

warn(__name__ + " is not yet QA compliant.", QAWarning)


class srim(object):
Copy link
Member

Choose a reason for hiding this comment

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

Class name should be CapCase, so this should be Srim or similar.



class srim(object):
"""srim class includes srim specific variables"""
Copy link
Member

Choose a reason for hiding this comment

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

This docstring doesn't help users who don't know what srim is. Could you expand please? Thanks!

ion_units=None,proj_ranges=None,proj_range_units=None,
long_straggs=None,long_stragg_units=None,lat_straggs=None,
lat_stragg_units=None):
"""Define srim specific variables """
Copy link
Member

Choose a reason for hiding this comment

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

This docstring should include the meaning and type of all of the arguments and keyword arguments (except self, which doesn't need to be documented.)

return print_string

def read_srim_output(filepath):
#def read_srim_output(file_path):
Copy link
Member

Choose a reason for hiding this comment

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

Please remove commented out code line

import warnings
from pyne.utils import QAWarning
warnings.simplefilter("ignore", QAWarning)

from pyne import gammaspec
#from pyne import gammaspec
Copy link
Member

Choose a reason for hiding this comment

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

please remove commented out lines like this.


def test_end_point_average_area():
nc = sa.end_point_average_area(gspec1,475,484,var=5)
#assert_equal(nc,20355.5)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the tests here have an assert? Is the problem that you are doing a float comparison? You should probably use assert_almost_equals in this case

@scopatz
Copy link
Member

scopatz commented Jun 14, 2018

Thanks for the update @KennellyT!

@KennellyT
Copy link
Contributor Author

KennellyT commented Jun 15, 2018

@scopatz- I took your comments and advice and implemented them into the pull request. readme.rst file had an issue where it states:

conda install -c conda-forge pyne

where VERSION should be replaced with the version number to be installed.

but version is not in the conda install -c statement nor is it needed to install pyne via conda via https://anaconda.org/conda-forge/pyne

I have updated srim_read and class by adding the meaning of the variables.
Thank you

@py1sl
Copy link
Contributor

py1sl commented Jul 18, 2018

@KennellyT pleased to see this get some attention, its been on my todo list since I first wrote the initial read functions, when I was first learning python
It would be good to add peak find methods as well, i believe i wrote a mariscotti peak find routine for spectanalysis but didn't test it, not needed for this PR

@KennellyT
Copy link
Contributor Author

@py1sl - I'll look at your mariscotti peak find routine/code as I have developed a peak finder function using the second derivative test and Savitzky Golay filter but I have not fully tested the function on a wide variety of gamma spectra.

@katyhuff
Copy link
Member

@KennellyT ... this seems ready to merge to me.

I see that @py1sl has suggested a peak finding method as well. Perhaps we can merge this and add a future issue for peak finding. What do you think?

Copy link
Contributor

@gonuke gonuke left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution @KennellyT - sorry that it has been sitting untended for so long...

I have a few little comments, but will otherwise look to merge this soon.

y = spec.counts[c1:c2]
n = len(x)
mean = sum(x*y)/n
sigma = np.sqrt(sum(y*(x-mean)**2)/n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this sigma ever used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the sigma variable goes into the gauss function (line 240) def gauss(x,amp,x_center,sigma):but in hindsight, this operation can be done in a much cleaner and more efficient manner. I'll take out the gauss function (line 240) and replace it with either its own variable or make the function separate from the gaussian_fit function.

Copy link
Contributor

Choose a reason for hiding this comment

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

But this sigma does NOT get used in the gauss() function.... that function has an argument named sigma that gets populated automatically by the curve_fit method. The initial guess for this value appears to be n/2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I now see the sigma issue. I'll replace the n/2 with sigma. Thank you!

return end_point_average

def gaussian_fit(spec,c1,c2):
"""Plots the gaussian fit to the raw data.
Copy link
Contributor

Choose a reason for hiding this comment

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

It might make sense to separate the curve fitting from the plotting in case users want to fit without plotting. This would let you reuse it in the fwhm() function below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I concur and I will separate this function into two

net_area = p - ((n/2.0)*(count1+count2))
return net_area

def end_point_average_area(spec,c1,c2,var=5):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be equivalent to net_area if var = 1. I don’t think it is, thanks to python indexing. If it should be, then net_area should just Call this with var=1

@KennellyT
Copy link
Contributor Author

Appears that my additions are failing in test_decay.py when trying to download http://data.pyne.io/origen-benchmark-hdf5-1.8.14.h5: E. It seems that the link is down: urllib.error.HTTPError: HTTP Error 404: Not Found or Unable to open/create file 'origen-benchmark-hdf5-1.8.14.h5'

@gonuke
Copy link
Contributor

gonuke commented Jan 10, 2019

You might need to rebase your code against develop. We've recently moved that file to a different location because data.pyne.io has gone down. The default branch now gets this code from the github pyne/data repository

@gonuke
Copy link
Contributor

gonuke commented Mar 21, 2019

I'd like to push for a merge on this @KennellyT - let me know if you want any assistance in rebasing and moving this foward

@gonuke gonuke added this to the Deferred milestone Aug 16, 2020
@ahnaf-tahmid-chowdhury
Copy link
Member

Hi @KennellyT, are you still working on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants