<jupyter_start><jupyter_text>wtlike tutorial> A step by step guide  Install `wtlike` This assumes that you already have a Jupyter Notebook Server configured on your machine. Either run this [notebook](https://github.com/tburnett/wtlike/blob/master/nbs/99_tutorial.ipynb), or copy the cells into a new notebook.`wtlike` is on PyPI--to install it,```pip install wtlike```Next we generate a simulated on-the-fly dataset in lieu of downloading the 3-GB of so of the *fermi* data.  Define a source flux function. Note that we measure time in day units, and will pick a 30-day interval for this demo.The following function has a flare at 15 days.<jupyter_code>def src_flare(t, tzero=15, width=1, amp=5):
    return 1e-6*(1 + amp*np.exp(-(t-tzero)**2/2/width))<jupyter_output><empty_output><jupyter_text>We use it to tell the simulation that the flux has this behavior. Note defaults:- background -- 1e-6 /s- effective area -- 3000 cm^2 Here we create a `Simulation` object, then pass it to the primary wtlike entry point, the class `WtLike`.We tell it to immediately bin all the photons into daily time-bins, called "cells". (The time binning can be easily redone.)<jupyter_code>config=Config(datapath=None)
test_sim = Simulation('flare_sim', src_flux=src_flare, tstart=0, tstop=30, )
daily = WtLike(test_sim, config=config, time_bins=(0,0,1))<jupyter_output>generated 18943 photons
SourceData: Source flare_sim with:
	 data:        18,943 simulated photons over 30.0 days.
	 exposure:     8,640 intervals,  average effective area 3000 cm^2 for 2.6 Ms
	 rates:  source 1.42e-06/s, background 1.01e-06/s, S/N ratio 1.41e+00
CellData: Bin photon data into 30 1-day bins from 0.0 to 30.0
LightCurve: select 30 cells for fitting with e>5 & n>2<jupyter_text>Now make a light curve!<jupyter_code>daily.plot();<jupyter_output><empty_output><jupyter_text>The cells have each been analyzed to create a likelihood function representation. The first few cells are:<jupyter_code>daily.cells.head(2)<jupyter_output><empty_output><jupyter_text>The Kerr likelihood function for a cell is a function of $w$, the list of weights, and $S$, an estimate for the total signal counts in the cell$$ \displaystyle\log\mathcal{L}(\alpha\ |\ w)\ = \sum_{w}  \log \big( 1 + \alpha\ w \big) - \alpha\ S  $$where $\alpha$ is the variation from the nominal zero. The background is assumed to be constant here, as it usually is the region surrounding sources of interest. The relative flux is $1+\alpha$. We generate an approximate representation of this function by fitting it to a 3-parameter Poisson-like function, which easily provides the values of interest.<jupyter_code>daily.fluxes.head(2)<jupyter_output><empty_output><jupyter_text>About DataFramesThe properties `photons`, `exposure`, `cells`, `fits`, and `fluxes` are [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) objects.For those unfamiliar with pandas, note that one can create "csv" files with the method `to_csv(`*filename*`)`.The function `query` is very useful: To select the cells with flux>3,<jupyter_code>daily.fluxes.query('flux>3')<jupyter_output><empty_output><jupyter_text>ViewsA `WtLike` object provides a function `view`, which returns a copy but with a different binning. So if we want a detailed look at the flare, we can choose an interval. The parameters specify an interval 10 days after the start, and 10 days before the end, with 1/day or 6-hour bins.<jupyter_code>qday = daily.view(10, -10, 0.25)
qday.plot();<jupyter_output>CellData: Bin photon data into 40 6-hour bins from 10.0 to 20.0
LightCurve: select 40 cells for fitting with e>1.25 & n>2<jupyter_text>Bayesian-Block viewThe WtLike object also has a special `bb_view` function, which does a Bayesian Block analysis and returns a view of it.<jupyter_code>bb = qday.bb_view();bb.plot();<jupyter_output>Bayesian Blocks: partitioning 40 cells using LikelihoodFitness with penalty 5%
	found 10 / 40 blocks.
LightCurve: Loaded 10 / 10 cells for fitting<jupyter_text>This creates variable-sized cells corresponding to the Bayesian Block analysis, the fits for which are shown here:<jupyter_code>bb.fluxes<jupyter_output><empty_output><jupyter_text>Getting helpThis jupyterlab environment allows easy exploration of help for any class or function.The two classes so far:<jupyter_code>Simulation = Simulation
Simulation?
WtLike?<jupyter_output><empty_output><jupyter_text>The three WtLike methods have (preliminary) help as well:<jupyter_code>WtLike.plot?
WtLike.view?<jupyter_output><empty_output><jupyter_text>Using *Fermi* data.The only change from the above code is that one substitues `PointSource` with the designation of a source, for `Simulation`. Also, the data files must be available, the config parameter `datapath` set to the relevant folder. Currently it is set by a line in `~/.config/wtlike/config.yaml`.  See the [Config help](https://tburnett.github.io/wtlike/configConfig).This requires access to a packaged form of the photon data, a table allowing generation of weights, and the effective area table.The source must have been analyzed with `gtlike` or `pointlike` to produce the weight table.All are available in a 2-GB zip file.The photon and spacecraft data can be checked with `check_data`The configuration must be valid, with path to data existing<jupyter_code>from wtlike import *
config = Config()
if config.valid:
    print(check_data())
    source = PointSource('3C 279'); 
    print(source)
    wtl = WtLike(source, exp_min=50)<jupyter_output>Weekly folder "/home/burnett/wtlike_data/data_files" contains 684 weeks.
	 Last week, # 693, has 2.410 days, ends at UTC 2021-09-11 11:10
None
Source "4FGL J1256.1-0547" at: (l,b)=(305.100,57.062), nickname P88Y3243
SourceData:  4FGL J1256.1-0547: Restoring from cache with key "P88Y3243_data"
SourceData: Source 4FGL J1256.1-0547 with:
	 data:       182,684 photons from 2008-08-04 to 2021-09-11
	 exposure: 2,896,631 intervals,  average effective area 2847 cm^2 for 86.5 Ms
	 rates:  source 3.95e-07/s, background 3.47e-07/s, S/N ratio 1.14e+00
CellData: Bin photon data into 683 1-week bins from 54683.0 to 59464.0
LightCurve: select 318 cells for fitting with e>350 & n>2<jupyter_text>If the data for this source has not been accessed on this machine before, it must be extracted to a cache, a process that takes some 10 min. Otherwise it is a few seconds to retrieve the cache.Generate the (default weekly) light curve:<jupyter_code>if config.valid: wtl.plot(log=True, UTC=True);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09252179232298517, 'fraction_numerical': 0.043584645970331855, 'mean_word_length': 5.204933586337761, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 58, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '287479', 'license': 'Apache-2.0', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2030, 'n_words': 784, 'path': 'nbs/99_tutorial.ipynb', 'repo_name': 'tburnett/wtlike', 'size': '6539'}