-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add masking and limits to pstat #35
Open
stscicrawford
wants to merge
8
commits into
spacetelescope:main
Choose a base branch
from
stscicrawford:add_mask
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a222019
Added the option to overplot
stscicrawford 56dc860
update the help information for pstat
stscicrawford 2ca008c
updates to formating
stscicrawford 15beec3
added masking for pstat
stscicrawford 7206c29
white space fix
stscicrawford 285a4a1
include upper and lower limit
stscicrawford 2655c37
update to help for upper and lower limit
stscicrawford 180777a
Update pstat.py
stscicrawford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,77 @@ | |
|
||
plt.ion() | ||
|
||
def pstat(filename, extname="sci", units="counts", stat="midpt", title=None, | ||
xlabel=None, ylabel=None, plot=True): | ||
|
||
def pstat(filename, extname="sci", units="counts", stat="midpt", mask=None, | ||
low_limit = None, high_limit = None, title=None, xlabel=None, | ||
ylabel=None, plot=True, overplot=False): | ||
"""A function to plot the statistics of one or more pixels up an IR ramp. | ||
|
||
Pixel values here are 0 based, not 1 based """ | ||
Parameters | ||
---------- | ||
filename: string | ||
Input MultiAccum image name with optional image section | ||
specification. If no image section is specified, the entire image | ||
is used. This should be either a _raw or _ima file, containing all | ||
the data from multiple readouts. You must specify just the | ||
file name and image section, with no extname designation. | ||
|
||
extname: {"sci", "err", "dq"} | ||
Extension name (EXTNAME keyword value) of data to plot. | ||
|
||
units: {"counts", "rate"} | ||
Plot "sci" or "err" data in units of counts or countrate | ||
("rate"). Input data can be in either unit; conversion will be | ||
performed automatically. Ignored when plotting "dq", "samp", or | ||
"time" data. | ||
|
||
stat: { "mean", "midpt", "mode", "stddev", "min", "max"} | ||
Type of statistic to compute. | ||
|
||
mask: `numpy.ndarray` (bool), optional | ||
A boolean mask with the same shape as ``data``, where a `True` | ||
value indicates the corresponding element of ``data`` is masked. | ||
Masked pixels are excluded when computing the statistics. | ||
|
||
low_limit: float, (optional) | ||
If set, the statistics will be calculated using only pixels that are | ||
above this value. | ||
|
||
high_limit: float, (optional) | ||
If set, the statistics will be calculated using only pixels that are | ||
below this value. | ||
|
||
title: str | ||
Title for the plot. If left blank, the name of the input image, | ||
appended with the extname and image section, is used. | ||
|
||
xlabel: str | ||
Label for the X-axis of the plot. If left blank, a suitable default | ||
is generated. | ||
|
||
ylabel: str | ||
Label for the Y-axis of the plot. If left blank, a suitable default | ||
based on the plot units and the extname of the data is generated. | ||
|
||
plot: Bool | ||
Set plot to false if you only want the data returned | ||
|
||
overplot: Bool | ||
If True, the results will be overplotted on the previous plot | ||
|
||
Returns | ||
------- | ||
xaxis: `numpy.ndarray` | ||
Array of x-axis values that will be plotted | ||
|
||
yaxis: `numpuy.ndarray` | ||
Array of y-axis values that will be plotted as specified by 'units' | ||
|
||
|
||
Notes | ||
----- | ||
Pixel values here are 0 based, not 1 based | ||
""" | ||
|
||
# pull the image extension from the filename string | ||
section_start = filename.find("[") | ||
|
@@ -98,25 +164,36 @@ def pstat(filename, extname="sci", units="counts", stat="midpt", title=None, | |
yend = myfile[1].header["NAXIS2"] # full y size | ||
|
||
for i in range(1, nsamp, 1): | ||
if "midpt" in stat: | ||
yaxis[i-1] = np.median(myfile[extname.upper(), i].data[xstart:xend, ystart:yend]) | ||
|
||
if "mean" in stat: | ||
yaxis[i-1] = np.mean(myfile[extname.upper(), i].data[xstart:xend, ystart:yend]) | ||
data = myfile[extname.upper(), i].data[xstart:xend, ystart:yend] | ||
|
||
if "mode" in stat: | ||
yaxis[i-1] = mode(myfile[extname.upper(), i].data[xstart:xend, ystart:yend], axis=None)[0] | ||
# mask the data and remove outlier values | ||
if mask is not None: | ||
data = data[~mask[xstart:xend, ystart:yend]] | ||
|
||
if "min" in stat: | ||
yaxis[i-1] = np.min(myfile[extname.upper(), i].data[xstart:xend, ystart:yend]) | ||
if high_limit is not None: | ||
data = data[data < high_limit] | ||
|
||
if "max" in stat: | ||
yaxis[i-1] = np.max(myfile[extname.upper(), i].data[xstart:xend, ystart:yend]) | ||
if low_limit is not None: | ||
data = data[data > low_limit] | ||
|
||
if "stddev" in stat: | ||
yaxis[i-1] = np.std(myfile[extname.upper(), i].data[xstart:xend, ystart:yend]) | ||
if data.size == 0: | ||
print("No valid pixels in ext {} of {}".format(i, imagename)) | ||
return xaxis, yaxis | ||
|
||
exptime=myfile["SCI",i ].header['SAMPTIME'] | ||
if "midpt" in stat: | ||
yaxis[i-1] = np.median(data) | ||
elif "mean" in stat: | ||
yaxis[i-1] = np.mean(data) | ||
elif "mode" in stat: | ||
yaxis[i-1] = mode(data, axis=None)[0]= | ||
elif "min" in stat: | ||
yaxis[i-1] = np.min(data) | ||
elif "max" in stat: | ||
yaxis[i-1] = np.max(data) | ||
elif "stddev" in stat: | ||
yaxis[i-1] = np.std(data) | ||
|
||
exptime = myfile["SCI", i].header['SAMPTIME'] | ||
xaxis[i-1] = exptime | ||
|
||
# convert to countrate | ||
|
@@ -127,13 +204,14 @@ def pstat(filename, extname="sci", units="counts", stat="midpt", title=None, | |
yaxis[i-1] *= exptime | ||
|
||
if plot: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silly question, where does the plotting actually happen? |
||
plt.clf() # clear out any current plot | ||
if not overplot: | ||
plt.clf() # clear out any current plot | ||
if not ylabel: | ||
if "rate" in units.lower(): | ||
if "/" in bunit.lower(): | ||
ylabel = bunit | ||
else: | ||
ylabel = bunit+" per second" | ||
ylabel = bunit + " per second" | ||
else: | ||
if "/" in bunit: | ||
stop_index = bunit.find("/") | ||
|
@@ -190,4 +268,5 @@ def help(file=None): | |
f.write(helpstr) | ||
f.close() | ||
|
||
|
||
pstat.__doc__ = getHelpAsString(docstring=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it won't make a lot of difference to the code at this point in time, but I feel like these would be more clear as an
elif
run for all the stat stuff. Since you're editing this file anyway it seems worth doing.