Skip to content

Commit

Permalink
astropy update
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhenshaw committed Apr 25, 2024
1 parent d5a4b08 commit f5d9b61
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 180 deletions.
7 changes: 2 additions & 5 deletions scousepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Packages may add whatever they like to this file, but
# should keep this content at the top.
# ----------------------------------------------------------------------------
from ._astropy_init import *
from ._astropy_init import __version__, test
# ----------------------------------------------------------------------------

if not _ASTROPY_SETUP_:
# For egg_info test builds to pass, put package imports here.

from .scouse import scouse
from .scouse import scouse
141 changes: 7 additions & 134 deletions scousepy/_astropy_init.py
Original file line number Diff line number Diff line change
@@ -1,143 +1,16 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

__all__ = ['__version__', '__githash__', 'test']
import os

# this indicates whether or not we are in the package's setup.py
try:
_ASTROPY_SETUP_
except NameError:
from sys import version_info
if version_info[0] >= 3:
import builtins
else:
import __builtin__ as builtins
builtins._ASTROPY_SETUP_ = False
from astropy.tests.runner import TestRunner

__all__ = ['__version__', 'test']

try:
from .version import version as __version__
except ImportError:
__version__ = ''
try:
from .version import githash as __githash__
except ImportError:
__githash__ = ''


# set up the test command
def _get_test_runner():
import os
from astropy.tests.helper import TestRunner
return TestRunner(os.path.dirname(__file__))


def test(package=None, test_path=None, args=None, plugins=None,
verbose=False, pastebin=None, remote_data=False, pep8=False,
pdb=False, coverage=False, open_files=False, **kwargs):
"""
Run the tests using `py.test <http://pytest.org/latest>`__. A proper set
of arguments is constructed and passed to `pytest.main`_.
.. _py.test: http://pytest.org/latest/
.. _pytest.main: http://pytest.org/latest/builtin.html#pytest.main
Parameters
----------
package : str, optional
The name of a specific package to test, e.g. 'io.fits' or 'utils'.
If nothing is specified all default tests are run.
test_path : str, optional
Specify location to test by path. May be a single file or
directory. Must be specified absolutely or relative to the
calling directory.
args : str, optional
Additional arguments to be passed to pytest.main_ in the ``args``
keyword argument.
plugins : list, optional
Plugins to be passed to pytest.main_ in the ``plugins`` keyword
argument.
verbose : bool, optional
Convenience option to turn on verbose output from py.test_. Passing
True is the same as specifying ``'-v'`` in ``args``.
pastebin : {'failed','all',None}, optional
Convenience option for turning on py.test_ pastebin output. Set to
``'failed'`` to upload info for failed tests, or ``'all'`` to upload
info for all tests.
remote_data : bool, optional
Controls whether to run tests marked with @remote_data. These
tests use online data and are not run by default. Set to True to
run these tests.
pep8 : bool, optional
Turn on PEP8 checking via the `pytest-pep8 plugin
<http://pypi.python.org/pypi/pytest-pep8>`_ and disable normal
tests. Same as specifying ``'--pep8 -k pep8'`` in ``args``.
pdb : bool, optional
Turn on PDB post-mortem analysis for failing tests. Same as
specifying ``'--pdb'`` in ``args``.
coverage : bool, optional
Generate a test coverage report. The result will be placed in
the directory htmlcov.
open_files : bool, optional
Fail when any tests leave files open. Off by default, because
this adds extra run time to the test suite. Requires the
`psutil <https://pypi.python.org/pypi/psutil>`_ package.
parallel : int, optional
When provided, run the tests in parallel on the specified
number of CPUs. If parallel is negative, it will use the all
the cores on the machine. Requires the
`pytest-xdist <https://pypi.python.org/pypi/pytest-xdist>`_ plugin
installed. Only available when using Astropy 0.3 or later.
kwargs
Any additional keywords passed into this function will be passed
on to the astropy test runner. This allows use of test-related
functionality implemented in later versions of astropy without
explicitly updating the package template.
"""
test_runner = _get_test_runner()
return test_runner.run_tests(
package=package, test_path=test_path, args=args,
plugins=plugins, verbose=verbose, pastebin=pastebin,
remote_data=remote_data, pep8=pep8, pdb=pdb,
coverage=coverage, open_files=open_files, **kwargs)

if not _ASTROPY_SETUP_: # noqa
import os
from warnings import warn
from astropy.config.configuration import (
update_default_config,
ConfigurationDefaultMissingError,
ConfigurationDefaultMissingWarning)

# add these here so we only need to cleanup the namespace at the end
config_dir = None

if not os.environ.get('ASTROPY_SKIP_CONFIG_UPDATE', False):
config_dir = os.path.dirname(__file__)
config_template = os.path.join(config_dir, __package__ + ".cfg")
if os.path.isfile(config_template):
try:
update_default_config(
__package__, config_dir, version=__version__)
except TypeError as orig_error:
try:
update_default_config(__package__, config_dir)
except ConfigurationDefaultMissingError as e:
wmsg = (e.args[0] +
" Cannot install default profile. If you are "
"importing from source, this is expected.")
warn(ConfigurationDefaultMissingWarning(wmsg))
del e
except Exception:
raise orig_error
# Create the test function for self test
test = TestRunner.make_test_runner_in(os.path.dirname(__file__))
test.__test__ = False
12 changes: 8 additions & 4 deletions scousepy/scouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ def chunk_saas(self, nchunks):

return saa_dict_chunks

def stage_2(config='', refit=False, verbose=None, s1file=None, s2file=None):
def stage_2(config='', refit=False, verbose=None, s1file=None, s2file=None,
interactive=True, SNR=3, alpha=5):
"""
Fitting of the SAAs
Expand Down Expand Up @@ -609,8 +610,11 @@ def stage_2(config='', refit=False, verbose=None, s1file=None, s2file=None):
fit_dict=self.saa_dict,
parent=saa_list[:,1],
fitcount=self.fitcount,
refit=refit)
fitterobject.show()
refit=refit,
interactive=interactive,
SNR=SNR, alpha=alpha, verbose=self.verbose)
if interactive:
fitterobject.show()

if np.all(self.fitcount):
# Now we want to go through and add the model solutions to the SAAs
Expand Down Expand Up @@ -931,7 +935,7 @@ def stage_4(config='', bitesize=False, verbose=None, nocheck=False,
progress_bar = print_to_terminal(stage='s4', step='start')

# Interactive coverage generator
fitcheckerobject=ScouseFitChecker(scouseobject=self, selected_spectra=self.check_spec_indices, scouseobjectalt=scouseobjectalt)
fitcheckerobject=ScouseFitChecker(scouseobject=self, selected_spectra=self.check_spec_indices, scouseobjectalt=scouseobjectalt, verbose=self.verbose)
if not nocheck:
fitcheckerobject.show()
else:
Expand Down
3 changes: 3 additions & 0 deletions scousepy/scousefitchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ def click(self, event):
# get the flattened indices of the pixel and its neighbours
self.keys=get_neighbours(self)

print('')
print(self.xpos, self.ypos)
print('')
# if the map is selected then we are going to plot
# some spectra
plot_spectra(self, self.scouseobject, color='limegreen')
Expand Down
109 changes: 74 additions & 35 deletions scousepy/scousefitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def __init__(self, modelstore,
SNR=3,minSNR=1,maxSNR=30,
alpha=5,minalpha=0.1,maxalpha=30,
outputfile=None,
xarrkwargs={},unit='',refit=False):
xarrkwargs={},unit='',refit=False,
interactive=True,
verbose=True):

"""
Expand Down Expand Up @@ -123,6 +125,8 @@ def __init__(self, modelstore,
self.maxalpha=maxalpha
self.outputfile=outputfile
self.models={}
self.interactive=interactive
self.verbose=verbose

# Prepare the fitter according to method selection
if method=='scouse':
Expand All @@ -147,9 +151,10 @@ def __init__(self, modelstore,
# Check to see if all of the spectra have been fitted.
if np.all(self.fitcount):
if not refit:
print('')
print(colors.fg._lightgreen_+"All spectra have solutions. Fitting complete. "+colors._endc_)
print('')
if self.verbose:
print('')
print(colors.fg._lightgreen_+"All spectra have solutions. Fitting complete. "+colors._endc_)
print('')
# exit if fitting has been completed
return
else:
Expand Down Expand Up @@ -369,6 +374,9 @@ def __init__(self, modelstore,
self.slider_alpha_ax=self.fig.add_axes([0.4875, 0.91, 0.3, 0.02])
self.slider_alpha=make_slider(self.slider_alpha_ax,"alpha",self.minalpha,self.maxalpha,self.update_alpha,valinit=self.alpha, valfmt="%i")#, valfmt="%.2f")

if not self.interactive:
self.dspec_apply_to_all('')

def show(self):
"""
Show the plot
Expand Down Expand Up @@ -443,47 +451,77 @@ def dspec_apply_to_all(self,event):
# this feature will apply dspec settings to all spectra without
# solutions and exit the fitter - first we want to make sure we save
# the current solution.
print("Fitting all remaining spectra using derivative spectroscopy... ")
print('')
if self.verbose:
print("Fitting all remaining spectra using derivative spectroscopy... ")
print('')

self.modelstore[self.index]=self.modeldict
self.fitcount[self.index]=True
# identify all spectra that do not currently have best-fitting solutions
id = np.where(self.fitcount==False)[0]
# loop through and fit
for i in tqdm(id):
# retrieve the new index
index=int(i)
# check against modelstore to see if there is a solution
if not index in self.modelstore.keys():
if self.method=='scouse':
# get the relevant spectrum
self.my_spectrum=retrieve_spectrum(self,self.spectra,index)
else:
self.index=index
# get the spectral information
get_spectral_info(self)
# initiate the decomposer
self.initiate_decomposer()
#compute new dspec
self.dsp = compute_dsp(self)
# Fit the spectrum according to dspec guesses
if self.dsp.ncomps != 0:
Decomposer.fit_spectrum_with_guesses(self.decomposer,self.guesses,fittype=self.fittype)
# Add the best-fitting solution and useful parameters to a dictionary
self.modeldict=get_model_info(self)
# add model to model store
self.modelstore[index]=self.modeldict
# update fit status
self.fitcount[index]=True

if self.verbose:
for i in tqdm(id):
# retrieve the new index
index=int(i)
# check against modelstore to see if there is a solution
if not index in self.modelstore.keys():
if self.method=='scouse':
# get the relevant spectrum
self.my_spectrum=retrieve_spectrum(self,self.spectra,index)
else:
self.index=index
# get the spectral information
get_spectral_info(self)
# initiate the decomposer
self.initiate_decomposer()
#compute new dspec
self.dsp = compute_dsp(self)
# Fit the spectrum according to dspec guesses
if self.dsp.ncomps != 0:
Decomposer.fit_spectrum_with_guesses(self.decomposer,self.guesses,fittype=self.fittype)
# Add the best-fitting solution and useful parameters to a dictionary
self.modeldict=get_model_info(self)
# add model to model store
self.modelstore[index]=self.modeldict
# update fit status
self.fitcount[index]=True
else:
for i in id:
# retrieve the new index
index=int(i)
# check against modelstore to see if there is a solution
if not index in self.modelstore.keys():
if self.method=='scouse':
# get the relevant spectrum
self.my_spectrum=retrieve_spectrum(self,self.spectra,index)
else:
self.index=index
# get the spectral information
get_spectral_info(self)
# initiate the decomposer
self.initiate_decomposer()
#compute new dspec
self.dsp = compute_dsp(self)
# Fit the spectrum according to dspec guesses
if self.dsp.ncomps != 0:
Decomposer.fit_spectrum_with_guesses(self.decomposer,self.guesses,fittype=self.fittype)
# Add the best-fitting solution and useful parameters to a dictionary
self.modeldict=get_model_info(self)
# add model to model store
self.modelstore[index]=self.modeldict
# update fit status
self.fitcount[index]=True

# return model dict to none
self.modeldict=None
# close the fitter
self.close_window()
# print completion statement
print('')
print(colors.fg._lightgreen_+"All spectra have solutions. Fitting complete. "+colors._endc_)
if self.verbose:
print('')
print(colors.fg._lightgreen_+"All spectra have solutions. Fitting complete. "+colors._endc_)

return

Expand Down Expand Up @@ -982,8 +1020,9 @@ def compute_dsp(self):
if np.any(noisy.mask):
dsp = DSpec(self.specx,spectrum_masked,self.specrms,SNR=self.SNR,alpha=self.alpha)
else:
print('')
print(colors.fg._yellow_+"Warning: No noise free channels detected, proceed with caution. "+colors._endc_)
if self.verbose:
print('')
print(colors.fg._yellow_+"Warning: No noise free channels detected, proceed with caution. "+colors._endc_)
dsp = DSpec(self.specx,self.specy,self.specrms,SNR=self.SNR,alpha=self.alpha)

self.ysmooth = dsp.ysmooth
Expand Down
2 changes: 1 addition & 1 deletion scousepy/stage_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def generate_SAAs(scouseobject, coverageobject):
if scouseobject.njobs > 1:
# if __name__ == 'scousepy.stage_1':
# print(__name__)
results = list(parallel_map(create_saa, inputlist, numcores=scouseobject.njobs))
results = list(parallel_map(create_saa, inputlist, numcores=scouseobject.njobs, verbose=scouseobject.verbose))
else:
if scouseobject.verbose:
results=[create_saa(input) for input in tqdm(inputlist)]
Expand Down
2 changes: 1 addition & 1 deletion scousepy/stage_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def autonomous_decomposition(scouseobject, indivspec_list):

# if njobs > 1 run in parallel else in series
if scouseobject.njobs > 1:
results = parallel_map(decomposition_method, inputlist, numcores=scouseobject.njobs)
results = parallel_map(decomposition_method, inputlist, numcores=scouseobject.njobs, verbose=scouseobject.verbose)
else:
if scouseobject.verbose:
results=[decomposition_method(input) for input in tqdm(inputlist)]
Expand Down

0 comments on commit f5d9b61

Please sign in to comment.