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

Update lightcurve.py #587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
212 changes: 145 additions & 67 deletions fermipy/lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,77 +170,155 @@
(time[0], time[1]))
print(sys.exc_info())
return {'fit_success': False}

gta._lck_params = lck_params
# Recompute source map for source of interest and sources within 3 deg
if gta.config['gtlike']['use_scaled_srcmap']:
names = [s.name for s in
gta.roi.get_sources(distance=3.0, skydir=gta.roi[name].skydir)
if not s.diffuse]
gta.reload_sources(names)

# Write the current model
gta.write_xml(xmlfile)

# Optimize the model
gta.optimize(skip=diff_sources,
shape_ts_threshold=kwargs.get('shape_ts_threshold'),
max_free_sources=kwargs.get('max_free_sources') )

fit_results = _fit_lc(gta, name, **kwargs)
gta.write_xml('fit_model_final.xml')
srcmodel = copy.deepcopy(gta.get_src_model(name))
numfree = gta.get_free_param_vector().count(True)
#Add an additional try structure so the code won't crash due to NaN values, and will simply ignore the corresponding bin
try:
gta._lck_params = lck_params
# Recompute source map for source of interest and sources within 3 deg
if gta.config['gtlike']['use_scaled_srcmap']:
names = [s.name for s in

Check warning on line 178 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L178

Added line #L178 was not covered by tests
gta.roi.get_sources(distance=3.0, skydir=gta.roi[name].skydir)
if not s.diffuse]
gta.reload_sources(names)

Check warning on line 181 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L181

Added line #L181 was not covered by tests

# Write the current model
gta.write_xml(xmlfile)

# Optimize the model
gta.optimize(skip=diff_sources,
shape_ts_threshold=kwargs.get('shape_ts_threshold'),
max_free_sources=kwargs.get('max_free_sources') )

fit_results = _fit_lc(gta, name, **kwargs)
gta.write_xml('fit_model_final.xml')
srcmodel = copy.deepcopy(gta.get_src_model(name))
numfree = gta.get_free_param_vector().count(True)

const_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results = fit_results.copy()
fixed_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results['fit_success'],fixed_srcmodel['fit_success'] = [False,False]
fixed_fit_results['fit_quality'],fixed_srcmodel['fit_quality'] = [0,0]
max_ts_thresholds = [None, 4, 9, 16, 25]
for max_ts in max_ts_thresholds:
if max_ts is not None:
gta.free_sources(minmax_ts=[None, max_ts], free=False, exclude=[name])

# rerun fit using params from full time (constant) fit using same
# param vector as the successful fit to get loglike
specname, spectrum = const_spectrum
gta.set_source_spectrum(name, spectrum_type=specname,
spectrum_pars=spectrum,
update_source=False)
gta.free_source(name, free=False)
const_fit_results = gta.fit()
if not const_fit_results['fit_success']:
continue
const_srcmodel = gta.get_src_model(name)
# rerun using shape fixed to full time fit
# for the fixed-shape lightcurve
gta.free_source(name, pars='norm')
fixed_fit_results = gta.fit()
if not fixed_fit_results['fit_success']:
continue
fixed_srcmodel = gta.get_src_model(name)
break
const_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results = fit_results.copy()
fixed_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results['fit_success'],fixed_srcmodel['fit_success'] = [False,False]
fixed_fit_results['fit_quality'],fixed_srcmodel['fit_quality'] = [0,0]
max_ts_thresholds = [None, 4, 9, 16, 25]
for max_ts in max_ts_thresholds:
if max_ts is not None:
gta.free_sources(minmax_ts=[None, max_ts], free=False, exclude=[name])

Check warning on line 204 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L204

Added line #L204 was not covered by tests

# rerun fit using params from full time (constant) fit using same
# param vector as the successful fit to get loglike
specname, spectrum = const_spectrum
gta.set_source_spectrum(name, spectrum_type=specname,
spectrum_pars=spectrum,
update_source=False)
gta.free_source(name, free=False)
const_fit_results = gta.fit()
if not const_fit_results['fit_success']:
continue

Check warning on line 215 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L215

Added line #L215 was not covered by tests
const_srcmodel = gta.get_src_model(name)
# rerun using shape fixed to full time fit
# for the fixed-shape lightcurve
gta.free_source(name, pars='norm')
fixed_fit_results = gta.fit()
if not fixed_fit_results['fit_success']:
continue

Check warning on line 222 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L222

Added line #L222 was not covered by tests
fixed_srcmodel = gta.get_src_model(name)
break

# special lc output
o = {'flux_const': const_srcmodel['flux'],
'loglike_const': const_fit_results['loglike'],
'fit_success': fit_results['fit_success'],
'fit_success_fixed': fixed_fit_results['fit_success'],
'fit_quality': fit_results['fit_quality'],
'fit_status': fit_results['fit_status'],
'num_free_params': numfree,
'config': config}
# full flux output
if fit_results['fit_success'] == 1:
for k in defaults.source_flux_output.keys():
if not k in srcmodel:
# special lc output
o = {'flux_const': const_srcmodel['flux'],
'loglike_const': const_fit_results['loglike'],
'fit_success': fit_results['fit_success'],
'fit_success_fixed': fixed_fit_results['fit_success'],
'fit_quality': fit_results['fit_quality'],
'fit_status': fit_results['fit_status'],
'num_free_params': numfree,
'config': config}
# full flux output
if fit_results['fit_success'] == 1:
for k in defaults.source_flux_output.keys():
if not k in srcmodel:
continue

Check warning on line 239 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L239

Added line #L239 was not covered by tests
o[k] = srcmodel[k]
o[k+'_fixed'] = fixed_srcmodel[k]

gta.logger.info('Finished time range %i %i' % (time[0], time[1]))
return o
##
except:
print('Analysis failed in time range %i %i' %

Check warning on line 247 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L246-L247

Added lines #L246 - L247 were not covered by tests
(time[0], time[1]))
print(sys.exc_info())
return {'fit_success': False}

Check warning on line 250 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L249-L250

Added lines #L249 - L250 were not covered by tests

gta._lck_params = lck_params
# Recompute source map for source of interest and sources within 3 deg
if gta.config['gtlike']['use_scaled_srcmap']:
names = [s.name for s in

Check warning on line 255 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L255

Added line #L255 was not covered by tests
gta.roi.get_sources(distance=3.0, skydir=gta.roi[name].skydir)
if not s.diffuse]
gta.reload_sources(names)

# Write the current model
gta.write_xml(xmlfile)

Check warning on line 261 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L261

Added line #L261 was not covered by tests

# Optimize the model
gta.optimize(skip=diff_sources,

Check warning on line 264 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L264

Added line #L264 was not covered by tests
shape_ts_threshold=kwargs.get('shape_ts_threshold'),
max_free_sources=kwargs.get('max_free_sources') )

fit_results = _fit_lc(gta, name, **kwargs)
gta.write_xml('fit_model_final.xml')
srcmodel = copy.deepcopy(gta.get_src_model(name))
numfree = gta.get_free_param_vector().count(True)

Check warning on line 271 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L268-L271

Added lines #L268 - L271 were not covered by tests

const_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results = fit_results.copy()
fixed_srcmodel = gta.get_src_model(name).copy()
fixed_fit_results['fit_success'],fixed_srcmodel['fit_success'] = [False,False]
fixed_fit_results['fit_quality'],fixed_srcmodel['fit_quality'] = [0,0]
max_ts_thresholds = [None, 4, 9, 16, 25]
for max_ts in max_ts_thresholds:
if max_ts is not None:
gta.free_sources(minmax_ts=[None, max_ts], free=False, exclude=[name])

Check warning on line 281 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L273-L281

Added lines #L273 - L281 were not covered by tests

# rerun fit using params from full time (constant) fit using same
# param vector as the successful fit to get loglike
specname, spectrum = const_spectrum
gta.set_source_spectrum(name, spectrum_type=specname,

Check warning on line 286 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L285-L286

Added lines #L285 - L286 were not covered by tests
spectrum_pars=spectrum,
update_source=False)
gta.free_source(name, free=False)
const_fit_results = gta.fit()
if not const_fit_results['fit_success']:
continue
const_srcmodel = gta.get_src_model(name)

Check warning on line 293 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L289-L293

Added lines #L289 - L293 were not covered by tests
# rerun using shape fixed to full time fit
# for the fixed-shape lightcurve
gta.free_source(name, pars='norm')
fixed_fit_results = gta.fit()
if not fixed_fit_results['fit_success']:

Check warning on line 298 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L296-L298

Added lines #L296 - L298 were not covered by tests
continue
o[k] = srcmodel[k]
o[k+'_fixed'] = fixed_srcmodel[k]
fixed_srcmodel = gta.get_src_model(name)
break

Check warning on line 301 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L300-L301

Added lines #L300 - L301 were not covered by tests

# special lc output
o = {'flux_const': const_srcmodel['flux'],

Check warning on line 304 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L304

Added line #L304 was not covered by tests
'loglike_const': const_fit_results['loglike'],
'fit_success': fit_results['fit_success'],
'fit_success_fixed': fixed_fit_results['fit_success'],
'fit_quality': fit_results['fit_quality'],
'fit_status': fit_results['fit_status'],
'num_free_params': numfree,
'config': config}
# full flux output
if fit_results['fit_success'] == 1:
for k in defaults.source_flux_output.keys():
if not k in srcmodel:
continue
o[k] = srcmodel[k]
o[k+'_fixed'] = fixed_srcmodel[k]

Check warning on line 318 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L313-L318

Added lines #L313 - L318 were not covered by tests

gta.logger.info('Finished time range %i %i' % (time[0], time[1]))
return o
gta.logger.info('Finished time range %i %i' % (time[0], time[1]))
return o

Check warning on line 321 in fermipy/lightcurve.py

View check run for this annotation

Codecov / codecov/patch

fermipy/lightcurve.py#L320-L321

Added lines #L320 - L321 were not covered by tests


def calcTS_var(loglike, loglike_const, flux_err, flux_const, systematic, fit_success):
Expand Down
Loading