-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsherpa.html
More file actions
796 lines (742 loc) · 33.7 KB
/
sherpa.html
File metadata and controls
796 lines (742 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sherpa Overview — Python4Astronomers 2.0 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap-astropy.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="top" title="Python4Astronomers 2.0 documentation" href="../index.html" />
<link rel="up" title="Fitting and Modeling 1-D and 2-D Data" href="fitting.html" />
<link rel="next" title="1-D data with errors" href="spectrum.html" />
<link rel="prev" title="Sherpa Installation" href="installation.html" />
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,600' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip0").click(function(){
$(".panel0").slideToggle("normal");
});
$(".flip1").click(function(){
$(".panel1").slideToggle("normal");
});
$(".flip2").click(function(){
$(".panel2").slideToggle("normal");
});
$(".flip3").click(function(){
$(".panel3").slideToggle("normal");
});
$(".flip4").click(function(){
$(".panel4").slideToggle("normal");
});
$(".flip5").click(function(){
$(".panel5").slideToggle("normal");
});
$(".flip6").click(function(){
$(".panel6").slideToggle("normal");
});
$(".flip7").click(function(){
$(".panel7").slideToggle("normal");
});
$(".flip8").click(function(){
$(".panel8").slideToggle("normal");
});
$(".flip9").click(function(){
$(".panel9").slideToggle("normal");
});
});
</script>
<style type="text/css">
div.panel0,p.flip0
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip0
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel0
{
display:none;
}
div.panel1,p.flip1
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip1
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel1
{
display:none;
}
div.panel2,p.flip2
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip2
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel2
{
display:none;
}
div.panel3,p.flip3
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip3
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel3
{
display:none;
}
div.panel4,p.flip4
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip4
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel4
{
display:none;
}
div.panel5,p.flip5
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip5
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel5
{
display:none;
}
div.panel6,p.flip6
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip6
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel6
{
display:none;
}
div.panel7,p.flip7
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip7
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel7
{
display:none;
}
div.panel8,p.flip8
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip8
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel8
{
display:none;
}
div.panel9,p.flip9
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip9
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel9
{
display:none;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18709417-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body role="document">
<div class="topbar">
<a class="brand" title="Documentation Home" href="../index.html"><span id="logotext1">python</span><span id="logotext2">4</span><span id="logotext3">astronomers</span></a>
<ul>
<li><a class="homelink" title="Astropy Homepage" href="http://www.astropy.org"></a></li>
</ul>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right">
<a href="spectrum.html" title="1-D data with errors">
next »
</a>
</li>
<li class="right">
<a href="installation.html" title="Sherpa Installation">
« previous
</a>
|
</li>
<li>
<a href="../index.html">Python4Astronomers 2.0 documentation</a>
»
</li>
<li><a href="fitting.html" accesskey="U">Fitting and Modeling 1-D and 2-D Data</a> »</li>
<li>Sherpa Overview</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="sherpa-overview">
<h1>Sherpa Overview<a class="headerlink" href="#sherpa-overview" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="http://cxc.harvard.edu/sherpa">Sherpa</a> is a general purpose modeling and fitting application written in Python.</p>
<ul class="simple">
<li>Uses Python’s interactive capabilities and its Object Oriented Programming
(OOP) approach.</li>
<li>Provides a flexible environment for resolving spectral and image properties,
analyzing time series, and modeling generic types of data.</li>
<li>Implements the forward fitting technique for parametrized data modeling.</li>
<li>Includes functions to calculate goodness-of-fit and parameter confidence
limits.</li>
<li>Data structures are contained in Python modules so users can easily add their
own data structures, models, statistics or optimization methods to Sherpa.</li>
<li>Complex model expressions are supported using a general purpose and compact
definition syntax.</li>
<li>Has a high-level UI that deals with a lot of the data management
and general book-keeping you come across, but the low-level API
can also be used (e.g. as part of a separate application).</li>
</ul>
<p>In this tutorial we will show you how Sherpa can be used to model and
fit 1D data (without and with errors) and 2D images. Higher
dimensionality data is supported (to some extent) but there is no
documentation. The 1D examples are for “unbinned” data, but you can
also handle the case where the model has to be summed (integrated)
across each bin (for the 2D case we treat image data as point/unbinned
for convenience and speed).</p>
<div class="section" id="documentation">
<h2>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://cxc.harvard.edu/sherpa">Sherpa home page</a>: Sherpa for CIAO users</li>
<li><a class="reference external" href="http://cxc.harvard.edu/contrib/sherpa">Sherpa python page</a>: Sherpa for Python users</li>
</ul>
<p>The Sherpa documentation collection includes a gallery of examples, fitting
threads, and AHELP pages that describe each Sherpa function:</p>
<ul class="simple">
<li><a class="reference external" href="http://cxc.cfa.harvard.edu/sherpa/gallery/thumbnails.py.html">Sherpa gallery</a>: Examples by plot</li>
<li><a class="reference external" href="http://cxc.cfa.harvard.edu/sherpa/threads/index.html">Sherpa fitting threads</a>: Example scripts</li>
<li><a class="reference external" href="http://cxc.cfa.harvard.edu/sherpa/ahelp/index_alphabet.html">Sherpa AHELP pages</a>: Function information</li>
</ul>
</div>
<div class="section" id="load-data-into-sherpa">
<h2>Load data into Sherpa<a class="headerlink" href="#load-data-into-sherpa" title="Permalink to this headline">¶</a></h2>
<p>If you still have the 3C120 data from the
<a class="reference external" href="../core/numpy_scipy.html#setup">NumPy introduction</a>
then go to the py4ast/core directory, otherwise</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>$ ipython --matplotlib
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/core/core_examples.tar'
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd py4ast/core
</pre></div>
</div>
<p>Now we load the Sherpa UI module and other requirements:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sherpa.astro.ui</span> <span class="kn">as</span> <span class="nn">ui</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">astropy.io</span> <span class="kn">import</span> <span class="n">fits</span>
<span class="c1"># import pycrates</span>
<span class="c1"># import pychips</span>
</pre></div>
</div>
<p>and then the data, using the <code class="docutils literal"><span class="pre">load_arrays</span></code> command</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">img</span> <span class="o">=</span> <span class="n">fits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'3c120_stis.fits.gz'</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">data</span>
<span class="c1"># cr = pycrates.read_file('3c120_stis.fits.gz')</span>
<span class="c1"># img = pycrates.get_piximgvals(cr)</span>
<span class="n">profile</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">xaxis</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">profile</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
<span class="n">ui</span><span class="o">.</span><span class="n">load_arrays</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">xaxis</span><span class="p">,</span> <span class="n">profile</span><span class="p">)</span>
<span class="n">ui</span><span class="o">.</span><span class="n">plot_data</span><span class="p">()</span>
<span class="c1"># pychips.log_scale(pychips.Y_AXIS)</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/3c120_data.png"><img alt="../_images/3c120_data.png" src="../_images/3c120_data.png" style="width: 432.0px; height: 324.0px;" /></a>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">I will be using the CIAO version of Sherpa for the demonstation, but
feel free to use the standalone version. Here we load the data into
dataset number <code class="docutils literal"><span class="pre">1</span></code> (which is the default); data set ids can be
integers or strings (for example “src” and “bgnd”). Some routines
work on a single dataset and some on all; for some commands
the id value can be left out to use the default (<code class="docutils literal"><span class="pre">plot_data</span></code>
is and <code class="docutils literal"><span class="pre">load_arrays</span></code> isn’t).</p>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">Try out <code class="docutils literal"><span class="pre">d1</span> <span class="pre">=</span> <span class="pre">ui.get_data()</span></code> and <code class="docutils literal"><span class="pre">dir(d1)</span></code>.</p>
</div>
</div>
<div class="section" id="set-up-the-model">
<h2>Set up the model<a class="headerlink" href="#set-up-the-model" title="Permalink to this headline">¶</a></h2>
<p>The aim is to determine the approximate spatial extent of the profile,
so we start with a gaussian:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.set_source(ui.gauss1d.g1)
print(g1)
gauss1d.g1
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 10 1.17549e-38 3.40282e+38
g1.pos thawed 0 -3.40282e+38 3.40282e+38
g1.ampl thawed 1 -3.40282e+38 3.40282e+38
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The Sherpa UI uses fancy Python magic to create a variable with
the name of the model component - in this case <code class="docutils literal"><span class="pre">g1</span></code> - which is
then used to read and modify the model parameters. Parameters
have a value, range, and can be thawed (can be adjusted during
a fit) or frozen (will not be fixed).</p>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">Try out <code class="docutils literal"><span class="pre">dir(g1)</span></code>. As shown below, the source expression
can be retrieved with <code class="docutils literal"><span class="pre">ui.get_source</span></code>.</p>
</div>
<p>It would be nice if the optimizer were guaranteed to find the
best fit no matter where you start (and the quality of the data),
but it often helps to try and give the system a helping hand.
One way to do this is via the <code class="docutils literal"><span class="pre">guess</span></code> command, which
uses simple heuristics to initialize some of the
parameter values and ranges (the algorithm used depends on
the model).</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.freeze(g1.fwhm)
ui.guess(g1)
ui.thaw(g1.fwhm)
print(g1)
gauss1d.g1
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 10 1.17549e-38 3.40282e+38
g1.pos thawed 254 0 511
g1.ampl thawed 3.11272e+06 3112.72 3.11272e+09
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The reason for freezing the <code class="docutils literal"><span class="pre">fwhm</span></code> parameter before the <code class="docutils literal"><span class="pre">guess</span></code>
is to avoid a strange error message
(<code class="docutils literal"><span class="pre">ParameterErr:</span> <span class="pre">parameter</span> <span class="pre">g1.fwhm</span>
<span class="pre">has</span> <span class="pre">a</span> <span class="pre">hard</span> <span class="pre">minimum</span> <span class="pre">of</span> <span class="pre">1.17549e-38</span></code>) that is specific to the
<code class="docutils literal"><span class="pre">gauss1d</span></code> model.</p>
</div>
</div>
<div class="section" id="selecting-a-statistic-and-optimizer">
<h2>Selecting a statistic and optimizer<a class="headerlink" href="#selecting-a-statistic-and-optimizer" title="Permalink to this headline">¶</a></h2>
<p>For this dataset we have no errors so use the least-squared statistic,
and the default optimizer (the Levenberg-Marquardt method).
Other choices for the statistic are gaussian - with a range of error
estimates - or Cash, and optimizers are Simplex and a Monte-Carlo
based method. Some situations require a particular choice, but
it can be useful to change values to check that you
are at the best-fit location (or, to avoid the wrath of any
Statistician, the local minimum).</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">ui</span><span class="o">.</span><span class="n">set_stat</span><span class="p">(</span><span class="s1">'leastsq'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">get_method</span><span class="p">())</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">levmar</span>
<span class="n">ftol</span> <span class="o">=</span> <span class="mf">1.19209289551e-07</span>
<span class="n">xtol</span> <span class="o">=</span> <span class="mf">1.19209289551e-07</span>
<span class="n">gtol</span> <span class="o">=</span> <span class="mf">1.19209289551e-07</span>
<span class="n">maxfev</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">epsfcn</span> <span class="o">=</span> <span class="mf">1.19209289551e-07</span>
<span class="n">factor</span> <span class="o">=</span> <span class="mf">100.0</span>
<span class="n">verbose</span> <span class="o">=</span> <span class="mi">0</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The parameters for the optimizers (e.g. <code class="docutils literal"><span class="pre">ftol</span></code> for <code class="docutils literal"><span class="pre">levmar</span></code>)
should be left alone unless you get <em>really</em> stuck <strong>and</strong> know
what you are doing.</p>
</div>
</div>
<div class="section" id="now-the-fit">
<h2>Now the fit<a class="headerlink" href="#now-the-fit" title="Permalink to this headline">¶</a></h2>
<p>For this example, the fit is quick (it does not take many iterations):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.fit()
Dataset = 1
Method = levmar
Statistic = leastsq
Initial fit statistic = 5.46696e+13
Final fit statistic = 9.55741e+10 at function evaluation 34
Data points = 512
Degrees of freedom = 509
Change in statistic = 5.4574e+13
g1.fwhm 1.28959
g1.pos 254.075
g1.ampl 3.14129e+06
</pre></div>
</div>
<p>and we repeat just to make sure:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.fit()
Dataset = 1
Method = levmar
Statistic = leastsq
Initial fit statistic = 9.55741e+10
Final fit statistic = 9.55741e+10 at function evaluation 5
Data points = 512
Degrees of freedom = 509
Change in statistic = 0
g1.fwhm 1.28959
g1.pos 254.075
g1.ampl 3.14129e+06
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The exact values you get depend on both the OS and build type
(32 vs 64 bit).</p>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">The <code class="docutils literal"><span class="pre">fit</span></code> command will fit all loaded datasets when called
with no id; use <code class="docutils literal"><span class="pre">fit(1)</span></code> to fit a single dataset.
The screen output from the <code class="docutils literal"><span class="pre">fit</span></code> command can also be
retrieved as a structure (a Python object) using the
<code class="docutils literal"><span class="pre">ui.get_fit_results()</span></code> command.</p>
</div>
</div>
<div class="section" id="view-the-fit">
<h2>View the fit<a class="headerlink" href="#view-the-fit" title="Permalink to this headline">¶</a></h2>
<p>The fit can be viewed graphically (the warnings can be ignored):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.plot_fit()
WARNING: unable to calculate errors using current statistic: leastsq
ui.plot_fit_resid()
WARNING: unable to calculate errors using current statistic: leastsq
WARNING: unable to calculate errors using current statistic: leastsq
# pychips.limits(pychips.X_AXIS, 245, 265)
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/3c120_fit_resid1.png"><img alt="../_images/3c120_fit_resid1.png" src="../_images/3c120_fit_resid1.png" style="width: 432.0px; height: 324.0px;" /></a>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">The level of screen output created by Sherpa can be controlled
using the <a class="reference external" href="http://docs.python.org/library/logging.html">Python logging module</a>.
Unless you
have used a similar library in another language, it will appear
needlessly complex (as it does a lot) and we unfortunately don’t have time to discuss it here.</p>
</div>
</div>
<div class="section" id="adding-a-component">
<h2>Adding a component<a class="headerlink" href="#adding-a-component" title="Permalink to this headline">¶</a></h2>
<p>We can re-use existing components in a source expression:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.set_source(g1 + ui.const1d.bgnd)
print(ui.get_source())
(gauss1d.g1 + const1d.bgnd)
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 1.28959 1.17549e-38 3.40282e+38
g1.pos thawed 254.075 0 511
g1.ampl thawed 3.14129e+06 3112.72 3.11272e+09
bgnd.c0 thawed 1 0 3.40282e+38
</pre></div>
</div>
<p>Rather than using <code class="docutils literal"><span class="pre">guess</span></code>, let’s see how well the optimizer does:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ui.fit()
Dataset = 1
Method = levmar
Statistic = leastsq
Initial fit statistic = 9.55644e+10
Final fit statistic = 4.96699e+10 at function evaluation 16
Data points = 512
Degrees of freedom = 508
Change in statistic = 4.58945e+10
g1.fwhm 1.28402
g1.pos 254.076
g1.ampl 3.1326e+06
bgnd.c0 9497.67
ui.fit()
Dataset = 1
Method = levmar
Statistic = leastsq
Initial fit statistic = 4.96699e+10
Final fit statistic = 4.96699e+10 at function evaluation 6
Data points = 512
Degrees of freedom = 508
Change in statistic = 0
g1.fwhm 1.28402
g1.pos 254.076
g1.ampl 3.1326e+06
bgnd.c0 9497.67
ui.plot_fit_resid()
# pychips.limits(pychips.X_AXIS, 245, 265)
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/3c120_fit_resid2.png"><img alt="../_images/3c120_fit_resid2.png" src="../_images/3c120_fit_resid2.png" style="width: 432.0px; height: 324.0px;" /></a>
</div>
<div class="section" id="evaluating-the-model-expression-directly">
<h2>Evaluating the model expression directly<a class="headerlink" href="#evaluating-the-model-expression-directly" title="Permalink to this headline">¶</a></h2>
<p>Model components and source expressions can be evaluated directly,
although this approach only works for simple models; that is those
without convolution (either explicitly via <code class="docutils literal"><span class="pre">ui.set_psf</span></code> or implictly
as happens with the handling of the response information in X-ray
data).:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">xi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">250</span><span class="p">,</span> <span class="mi">260</span><span class="p">)</span>
<span class="n">src</span> <span class="o">=</span> <span class="n">ui</span><span class="o">.</span><span class="n">get_source</span><span class="p">()</span>
<span class="n">yi</span> <span class="o">=</span> <span class="n">src</span><span class="p">(</span><span class="n">xi</span><span class="p">)</span>
<span class="nb">zip</span><span class="p">(</span><span class="n">xi</span><span class="p">,</span> <span class="n">yi</span><span class="p">)</span>
<span class="p">[(</span><span class="mi">250</span><span class="p">,</span> <span class="mf">9497.6705120244224</span><span class="p">),</span>
<span class="p">(</span><span class="mi">251</span><span class="p">,</span> <span class="mf">9498.0568224326398</span><span class="p">),</span>
<span class="p">(</span><span class="mi">252</span><span class="p">,</span> <span class="mf">11732.300774634092</span><span class="p">),</span>
<span class="p">(</span><span class="mi">253</span><span class="p">,</span> <span class="mf">457003.64642740792</span><span class="p">),</span>
<span class="p">(</span><span class="mi">254</span><span class="p">,</span> <span class="mf">3112045.5828799075</span><span class="p">),</span>
<span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mf">754169.02805867838</span><span class="p">),</span>
<span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="mf">15685.485177760009</span><span class="p">),</span>
<span class="p">(</span><span class="mi">257</span><span class="p">,</span> <span class="mf">9499.4505770869582</span><span class="p">),</span>
<span class="p">(</span><span class="mi">258</span><span class="p">,</span> <span class="mf">9497.6705274404576</span><span class="p">),</span>
<span class="p">(</span><span class="mi">259</span><span class="p">,</span> <span class="mf">9497.6705097123686</span><span class="p">)]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">zip</span></code> command is one of those utility functions that
comes in really handy.</p>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">There are a family of commands, such as <code class="docutils literal"><span class="pre">ui.get_data_plot</span></code>,
<code class="docutils literal"><span class="pre">ui.get_model_plot</span></code>, and <code class="docutils literal"><span class="pre">ui.get_fit_plot</span></code> which provide
access to the data used to create the corresponding plot command.
This is one way to handle those models which include a convolution
component.</p>
</div>
<p>I want to find those columns that are significantly higher than
the background, so let’s try <code class="docutils literal"><span class="pre">bgnd.c0</span> <span class="pre">+</span> <span class="pre">5</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="n">yi</span> <span class="o">></span> <span class="n">bgnd</span><span class="o">.</span><span class="n">c0</span> <span class="o">+</span> <span class="mi">5</span><span class="p">])</span>
<span class="p">[]</span>
</pre></div>
</div>
<p>Well, that was unexpected! So what went wrong?:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>bgnd.c0 + 5
<BinaryOpParameter '(bgnd.c0 + 5)'>
</pre></div>
</div>
<p>In order to support linked parameters
(demonstrated in the <cite>next section <spectrum.html></cite>), and a
bunch of other sparkly goodness, the
value <cite>bgnd.c0</cite> is actually a Python object. To get at its value
you have to use the <code class="docutils literal"><span class="pre">val</span></code> field:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>bgnd.c0
<Parameter 'c0' of model 'bgnd'>
bgnd.c0.val
9497.6705097123631
bgnd.c0.val + 5
9502.6705097123631
print(xi[yi>bgnd.c0.val + 5])
[252 253 254 255 256]
</pre></div>
</div>
</div>
<div class="section" id="saving-the-session">
<h2>Saving the session<a class="headerlink" href="#saving-the-session" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">save</span></code> command can be used to store the
current session as a single file.:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">ui</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">"3c120.sherpa"</span><span class="p">)</span>
</pre></div>
</div>
<p>This file can then be
loaded into a new session with the <code class="docutils literal"><span class="pre">restore</span></code> command.:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>$ ipython --matplotlib
In [1]: import sherpa.astro.ui as ui
In [2]: ui.restore("simple1.sherpa")
Solar Abundance Vector set to angr: Anders E. & Grevesse N. Geochimica et Cosmochimica Acta 53, 197 (1989)
Cross Section Table set to bcmc: Balucinska-Church and McCammon, 1998
In [3]: ui.show_all()
Data Set: 1
Filter: 0.0000-511.0000 x
name =
x = Int64[512]
y = Float32[512]
staterror = None
syserror = None
Model: 1
(gauss1d.g1 + const1d.bgnd)
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 1.28402 1.17549e-38 3.40282e+38
g1.pos thawed 254.076 0 511
g1.ampl thawed 3.1326e+06 3112.72 3.11272e+09
bgnd.c0 thawed 9497.67 0 3.40282e+38
Optimization Method: LevMar
name = levmar
ftol = 1.19209289551e-07
xtol = 1.19209289551e-07
gtol = 1.19209289551e-07
maxfev = None
epsfcn = 1.19209289551e-07
factor = 100.0
verbose = 0
Statistic: LeastSq
Least Squared
Fit:Dataset = 1
Method = levmar
Statistic = leastsq
Initial fit statistic = 4.96699e+10
Final fit statistic = 4.96699e+10 at function evaluation 6
Data points = 512
Degrees of freedom = 508
Change in statistic = 0
g1.fwhm 1.28402
g1.pos 254.076
g1.ampl 3.1326e+06
bgnd.c0 9497.67
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">save</span></code> command takes advantage of Python’s pickling
capabilities. The result is a binary file that can be shared between
machines, even on a different OS or - I believe - 32 and 64 bit
variants. This makes sharing fits with colleagues very easy
- e.g. via DropBox - but has some downsides: it is not guaranteed
that the files can be used with different versions of Sherpa;
you can’t manually inspect the file to see what was done;
and those people implementing advanced features
(e.g. user models or statistics) may not
support this functionality. The <code class="docutils literal"><span class="pre">ui.save_all</span></code> command
writes out a Python script, but it is aimed mainly at users who
load in data from files rather than with the <code class="docutils literal"><span class="pre">load_arrays</span></code>
command.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><h3>Page Contents</h3>
<ul>
<li><a class="reference internal" href="#">Sherpa Overview</a><ul>
<li><a class="reference internal" href="#documentation">Documentation</a></li>
<li><a class="reference internal" href="#load-data-into-sherpa">Load data into Sherpa</a></li>
<li><a class="reference internal" href="#set-up-the-model">Set up the model</a></li>
<li><a class="reference internal" href="#selecting-a-statistic-and-optimizer">Selecting a statistic and optimizer</a></li>
<li><a class="reference internal" href="#now-the-fit">Now the fit</a></li>
<li><a class="reference internal" href="#view-the-fit">View the fit</a></li>
<li><a class="reference internal" href="#adding-a-component">Adding a component</a></li>
<li><a class="reference internal" href="#evaluating-the-model-expression-directly">Evaluating the model expression directly</a></li>
<li><a class="reference internal" href="#saving-the-session">Saving the session</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Sherpa Installation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="spectrum.html"
title="next chapter">1-D data with errors</a></p>
</div>
</div>
<div class="clearer"></div>
</div>
<footer class="footer">
<p class="pull-right">
<a href="../_sources/fitting/sherpa.txt"
rel="nofollow">Page Source</a>
<a href="#">Back to Top</a></p>
<p>
© Copyright 2011-2015, Smithsonian Astrophysical Observatory under
terms of <a rel="license"
href="http://creativecommons.org/licenses/by/3.0/">CC
Attribution 3.0</a>.<br/>
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.1.
</footer>
</body>
</html>