<jupyter_start><jupyter_text>Manipulating FileFunction Spectra This tutorial demonstrates some of the methods that can be used to manipulate FileFunction sources in fermipy.  For this example we'll use the draco analysis.<jupyter_code>import os
if os.path.isfile('../data/draco.tar.gz'):
    !tar xzf ../data/draco.tar.gz
else:
    !curl -OL https://raw.githubusercontent.com/fermiPy/fermipy-extras/master/data/draco.tar.gz
    !tar xzf draco.tar.gz

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from fermipy.gtanalysis import GTAnalysis
gta = GTAnalysis('draco/config.yaml')
gta.setup()<jupyter_output>2016-04-20 08:00:56 INFO     GTAnalysis.__init__(): 
--------------------------------------------------------------------------------
This is fermipy version 0.6.8+57.gbeca.dirty.
2016-04-20 08:00:58 INFO     GTAnalysis.setup(): Running setup
2016-04-20 08:00:58 INFO     GTBinnedAnalysis.setup(): Running setup for Analysis Component: 00
2016-04-20 08:01:11 INFO     GTBinnedAnalysis.setup(): Finished setup for Analysis Component: 00
2016-04-20 08:01:11 INFO     GTAnalysis.setup(): Initializing source properties
2016-04-20 08:01:11 INFO     GTAnalysis.setup(): Finished setup<jupyter_text>By default all sources are initialized with parametric spectral models (PowerLaw, etc.).  The spectral model of a source an be updated by calling ``set_source_spectrum()``.<jupyter_code>print gta.roi['3FGL J1725.3+5853']<jupyter_output>Name           : 3FGL J1725.3+5853
Associations   : ['3FGL J1725.3+5853', '7C 1724+5854', '2FGL J1725.2+5853']
RA/DEC         :    261.332/    58.887
GLON/GLAT      :     87.497/    33.997
TS             : nan
Npred          : 278.15
Flux           : 9.942e-10 +/-      nan
EnergyFlux     : 2.323e-06 +/-      nan
SpatialModel   : PointSource
SpectrumType   : PowerLaw
Spectral Parameters
Index          :     -2.179 +/-        nan
Scale          :       1701 +/-        nan
Prefactor      :  1.627e-13 +/-        nan<jupyter_text>Running ``set_source_spectrum()`` with no additional arguments will substitute the source spectrum with a FileFunction with the same distribution in differential flux.  The normalization parameter is defined such that 1.0 corresponds to the normalization of the original source spectrum.<jupyter_code>gta.set_source_spectrum('3FGL J1725.3+5853','FileFunction')
print gta.roi['3FGL J1725.3+5853']<jupyter_output>Name           : 3FGL J1725.3+5853
Associations   : ['3FGL J1725.3+5853', '7C 1724+5854', '2FGL J1725.2+5853']
RA/DEC         :    261.332/    58.887
GLON/GLAT      :     87.497/    33.997
TS             : 283.34
Npred          : 278.15
Flux           : 9.942e-10 +/-      nan
EnergyFlux     : 2.323e-06 +/-      nan
SpatialModel   : PointSource
SpectrumType   : FileFunction
Spectral Parameters
Normalization  :          1 +/-          0<jupyter_text>The differential flux of a FileFunction source can be accessed or modified at runtime by calling the ``get_source_dfde()`` and ``set_source_dfde()`` methods:<jupyter_code>x, y = gta.get_source_dfde('3FGL J1725.3+5853')
y1 = 1E-12*10**(-2.0*(x-3.0))*np.exp(-10**(x-3.0))
plt.figure()
plt.plot(x,y)
plt.plot(x,y1)
plt.gca().set_yscale('log')
plt.gca().set_ylim(1E-17,1E-5)
print gta.like()
gta.set_source_dfde('3FGL J1725.3+5853',y1)
print gta.like()<jupyter_output>60891.8571503
60979.3497856<jupyter_text>Calling ``set_source_spectrum()`` with the optional dictionary argument can be used to explicitly set the parameters of the new spectral model.<jupyter_code>gta.set_source_spectrum('3FGL J1725.3+5853','PowerLaw',{'Index' : 2.179, 'Scale' : 1701, 'Prefactor' : 1.627e-13})
gta.like()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13927500681384575, 'fraction_numerical': 0.1060234396293268, 'mean_word_length': 3.19908466819222, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '432971', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1269, 'n_words': 308, 'path': 'notebooks/file_function_examples.ipynb', 'repo_name': 'XanAstia/fermipy-extra', 'size': '3669'}