-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathimage.html
More file actions
721 lines (674 loc) · 34.4 KB
/
image.html
File metadata and controls
721 lines (674 loc) · 34.4 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
<!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>2-D Fitting in Sherpa — 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="The low-level Sherpa API" href="low-level.html" />
<link rel="prev" title="1-D data with errors" href="spectrum.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="low-level.html" title="The low-level Sherpa API">
next »
</a>
</li>
<li class="right">
<a href="spectrum.html" title="1-D data with errors">
« 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>2-D Fitting in Sherpa</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="d-fitting-in-sherpa">
<h1>2-D Fitting in Sherpa<a class="headerlink" href="#d-fitting-in-sherpa" title="Permalink to this headline">¶</a></h1>
<p>Fit image data of a supernova remnant G21.5-0.9 using a 2-D multi-component
source model. First, download the FITS image of <a class="reference download internal" href="../_downloads/image2.fits"><code class="xref download docutils literal"><span class="pre">G21.5-0.9</span></code></a>
and start IPython:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>$ ipython --matplotlib
</pre></div>
</div>
<p>Do the usual imports of numpy and matplotlib:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
</pre></div>
</div>
<p>If you have trouble accessing the image you can download it straight away using
Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">astropy.extern.six.moves.urllib</span> <span class="kn">import</span> <span class="n">request</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">"http://python4astronomers.github.com/_downloads/image2.fits"</span>
<span class="nb">open</span><span class="p">(</span><span class="s2">"image2.fits"</span><span class="p">,</span> <span class="s2">"wb"</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="n">ls</span>
</pre></div>
</div>
<p>Here we eschew the advice of keeping modules separate and load the
high-level API into the default namespace:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sherpa.astro.ui</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>As with the <a class="reference external" href="spectrum.html">1D spectrum</a>, we can load in the data with <code class="docutils literal"><span class="pre">load_data</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>load_data("image2.fits")
print(get_data())
name = image2.fits
x0 = Float64[56376]
x1 = Float64[56376]
y = Float64[56376]
shape = (216, 261)
staterror = None
syserror = None
sky = physical
crval = [ 3798.5 4019.5]
crpix = [ 0.5 0.5]
cdelt = [ 2. 2.]
eqpos = world
crval = [ 278.386 -10.5899]
crpix = [ 4096.5 4096.5]
cdelt = [-0.0001 0.0001]
crota = 0
epoch = 2000
equinox = 2000
coord = logical
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The screen output from the <code class="docutils literal"><span class="pre">get_data</span></code> command will depend on
whether you are using the standalone Sherpa (PyFITS) or the CIAO
version (pyCrates).</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">image_data</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">image_data</span></code> command uses ds9 to view the data rather than
matplotlib (or ChIPS).</p>
</div>
<a class="reference internal image-reference" href="../_images/g21.png"><img alt="../_images/g21.png" src="../_images/g21.png" style="width: 406.5px; height: 567.0px;" /></a>
<p>Calculate the number of source counts in the full FOV:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">calc_data_sum2d</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">32300</p>
</div>
<p>Typically, X-ray data from Chandra contain low counts, so we will switch over
to a maximum likelihood statistic such as <cite>Cash</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_stat</span><span class="p">(</span><span class="s2">"cash"</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">list_stats</span></code> routine returns a list of available statistic methods.</p>
</div>
<p>The default fitting method, least squares, is not suitable for fitting low-count
data such as X-ray images, so we will choose <cite>Simplex</cite> as the optimization
method:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_method</span><span class="p">(</span><span class="s2">"simplex"</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">list_methods</span></code> routine returns the names of optimisation
methods that Sherpa provides. I also will sometimes use the
<code class="docutils literal"><span class="pre">LevMar</span></code> optimiser to start a fit using Cash statistics and then
switch to <code class="docutils literal"><span class="pre">Simplex</span></code> to check; this is not guaranteed to be any
faster or reliable!</p>
</div>
<p>Set the coordinate system for this image to use <cite>physical</cite> coordinates with
<code class="docutils literal"><span class="pre">set_coord</span></code> (Chandra chip coordinates). Valid coordinate systems include
<cite>image</cite>, <cite>physical</cite>, and <cite>wcs</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_coord</span><span class="p">(</span><span class="s2">"physical"</span><span class="p">)</span>
</pre></div>
</div>
<p>Next, we will setup a 2-D region to filter the image. Here we define a 2-D
<cite>CIRCLE</cite> with <cite>x</cite> and <cite>y</cite> defined in physical coordinates and the <cite>radius</cite>
defined in pixels:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">notice2d</span><span class="p">(</span><span class="s2">"CIRCLE(4072.46,4249.34,108)"</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">get_filter</span><span class="p">())</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">Circle(4072.46,4249.34,108)</p>
</div>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">The coordinate system of the data must match the coordinate system used in
the 2-D region definition. Typically, a call to <code class="docutils literal"><span class="pre">set_coord</span></code> is made before
using <code class="docutils literal"><span class="pre">notice2d</span></code> or <code class="docutils literal"><span class="pre">ignore2d</span></code>.</p>
</div>
<div class="admonition-sherpa-also-supports-2-d-regions-from-file-either-ascii-or-fits admonition">
<p class="first admonition-title">Sherpa also supports 2-D regions from file (either ASCII or FITS).</p>
<p>Sherpa supports CIAO region files to define 2-D noticed regions:</p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="n">ignore2d</span><span class="p">()</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">file</span><span class="p">(</span><span class="s2">"circle.reg"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"CIRCLE(4072.46,4249.34,108)</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">notice2d</span><span class="p">(</span><span class="s2">"circle.reg"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>View the filtered image data in DS9:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">image_data</span><span class="p">()</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/g21_filter.png"><img alt="../_images/g21_filter.png" src="../_images/g21_filter.png" style="width: 406.5px; height: 567.0px;" /></a>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">If you know you are not going to be using most of the image, then
filter the data before reading it into Sherpa (in CIAO you can add a
filter to the filename in the <code class="docutils literal"><span class="pre">load_data</span></code> command). The less data
read in <em>can</em> lead to quicker fits (this is generally only relevant
when you include a convolution component).</p>
</div>
<p>Calculate the source counts inside the noticed 2-D region:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">calc_data_sum2d</span><span class="p">(</span><span class="s2">"CIRCLE(4072.46,4249.34,108)"</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">24658.0</p>
</div>
<p>Define a 2-D Gaussian as the source model. This example is simply an
illustration for describing the source emission. Initialize the parameter
values according to coordinate system. The <cite>xpos</cite> and <cite>ypos</cite> parameters are in
<cite>physical</cite> coordinates:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_source</span><span class="p">(</span><span class="n">gauss2d</span><span class="o">.</span><span class="n">g1</span><span class="p">)</span>
<span class="n">g1</span><span class="o">.</span><span class="n">ampl</span> <span class="o">=</span> <span class="mi">20</span>
<span class="n">g1</span><span class="o">.</span><span class="n">fwhm</span> <span class="o">=</span> <span class="mi">20</span>
<span class="n">g1</span><span class="o">.</span><span class="n">xpos</span> <span class="o">=</span> <span class="mf">4065.5</span>
<span class="n">g1</span><span class="o">.</span><span class="n">ypos</span> <span class="o">=</span> <span class="mf">4250.5</span>
</pre></div>
</div>
<p>Next, constraint the parameter limits to roughly the size of the image:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">g1</span><span class="o">.</span><span class="n">fwhm</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="mi">4300</span>
<span class="n">g1</span><span class="o">.</span><span class="n">xpos</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="mi">4300</span>
<span class="n">g1</span><span class="o">.</span><span class="n">ypos</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="mi">4300</span>
<span class="n">g1</span><span class="o">.</span><span class="n">ampl</span><span class="o">.</span><span class="n">min</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">g1</span><span class="o">.</span><span class="n">ampl</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="mi">1000</span>
</pre></div>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">In this case the <code class="docutils literal"><span class="pre">guess</span></code> command is quicker, although the limits
are not exactly the same.</p>
</div>
<p>View the current model definition and view the 2-D Gaussian in DS9:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>print(get_source())
gauss2d.g1
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 20 1.17549e-38 4300
g1.xpos thawed 4065.5 -3.40282e+38 4300
g1.ypos thawed 4250.5 -3.40282e+38 4300
g1.ellip frozen 0 0 0.999
g1.theta frozen 0 0 6.28319 radians
g1.ampl thawed 20 1 1000
image_model()
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/g21_model.png"><img alt="../_images/g21_model.png" src="../_images/g21_model.png" style="width: 406.5px; height: 567.0px;" /></a>
<p>Calculate the Gaussian model counts inside the noticed 2-D region:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">calc_model_sum2d</span><span class="p">(</span><span class="s2">"CIRCLE(4072.46,4249.34,108)"</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">2266.1800709135932</p>
</div>
<p>Now, include a background component to the source model. In this case, an
estimate of (0.2) is made from a radial profile (not shown here):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_source</span><span class="p">(</span><span class="n">g1</span><span class="o">+</span><span class="n">const2d</span><span class="o">.</span><span class="n">bgnd</span><span class="p">)</span>
<span class="n">bgnd</span><span class="o">.</span><span class="n">c0</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">bgnd</span><span class="o">.</span><span class="n">c0</span><span class="o">.</span><span class="n">max</span> <span class="o">=</span> <span class="mi">100</span>
</pre></div>
</div>
<p>View the updated model expression:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>print(get_source())
(gauss2d.g1 + const2d.bgnd)
Param Type Value Min Max Units
----- ---- ----- --- --- -----
g1.fwhm thawed 20 1.17549e-38 4300
g1.xpos thawed 4065.5 -3.40282e+38 4300
g1.ypos thawed 4250.5 -3.40282e+38 4300
g1.ellip frozen 0 0 0.999
g1.theta frozen 0 0 6.28319 radians
g1.ampl thawed 20 1 1000
bgnd.c0 thawed 0.2 0 100
</pre></div>
</div>
<p><strong>NOTE:</strong> The function <code class="docutils literal"><span class="pre">get_model</span></code> is <strong>not</strong> synonymous to <code class="docutils literal"><span class="pre">get_source</span></code>.
Typically, Sherpa functions that end in <code class="docutils literal"><span class="pre">_source</span></code> refer to unconvolved model
components (e.g. components to be convolved with a Point Spread Function
(PSF)). Sherpa functions that end in <code class="docutils literal"><span class="pre">_model</span></code> access the complete convolved
model expression including any convolution components (e.g. PSF).</p>
<p>Fit with <code class="docutils literal"><span class="pre">fit</span></code> and display the data, model, and residuals in DS9 with
<code class="docutils literal"><span class="pre">image_fit</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>fit()
Dataset = 1
Method = neldermead
Statistic = cash
Initial fit statistic = 20661.5
Final fit statistic = -48907.8 at function evaluation 525
Data points = 9171
Degrees of freedom = 9166
Change in statistic = 69569.3
g1.fwhm 57.9477
g1.xpos 4070.4
g1.ypos 4251.11
g1.ampl 23.3562
bgnd.c0 0.266365
image_fit()
</pre></div>
</div>
<div class="admonition-fit-result admonition">
<p class="first admonition-title">Fit Result</p>
<p class="last">Sherpa fit results include the statistic and method used during fitting,
goodness-of-fit indicators, the number of function evaluations computed, and
the list of best-fit parameter values. NOTE: only the thawed parameters are
shown.</p>
</div>
<a class="reference internal image-reference" href="../_images/g21_fit.png"><img alt="../_images/g21_fit.png" src="../_images/g21_fit.png" style="width: 405.75px; height: 567.0px;" /></a>
<p>Calculate the model counts inside the noticed 2-D region using the best-fit
parameter values:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">calc_model_sum2d</span><span class="p">(</span><span class="s2">"CIRCLE(4072.46,4249.34,108)"</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">24658.000000637512</p>
</div>
<p>Calculate the FWHM in arcseconds using the ACIS conversion factor by accessing
the parameter value (remembering to use the the <code class="docutils literal"><span class="pre">val</span></code> attribute):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">g1</span><span class="o">.</span><span class="n">fwhm</span><span class="o">.</span><span class="n">val</span> <span class="o">*</span> <span class="mf">0.492</span>
</pre></div>
</div>
<div class="admonition-result admonition">
<p class="first admonition-title">Result</p>
<p class="last">28.510281281152213</p>
</div>
<p>Save the fitted model to a FITS image using <code class="docutils literal"><span class="pre">save_model</span></code> and save the fit
residuals using <code class="docutils literal"><span class="pre">save_resid</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">save_model</span><span class="p">(</span><span class="s2">"model.fits"</span><span class="p">,</span> <span class="n">clobber</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">save_resid</span><span class="p">(</span><span class="s2">"resid.fits"</span><span class="p">,</span> <span class="n">clobber</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Calculate the parameter confidence limits on thawed parameter values using the
Sherpa method <code class="docutils literal"><span class="pre">conf</span></code>, changing the range from 1 sigma (the default)
to 90 % (1.64 sigma for one interesting parameter):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">set_conf_opt</span><span class="p">(</span><span class="s2">"sigma"</span><span class="p">,</span> <span class="mf">1.6448536269514722</span><span class="p">)</span>
<span class="n">conf</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition-confidence-result admonition">
<p class="first admonition-title">Confidence Result</p>
<p class="last">Sherpa confidence results include the statistic and method used during, a list
of best-fit parameter values, and their associated confidence limits. NOTE:
only the thawed parameters or specified parameters are shown.</p>
</div>
<pre><p>g1.ampl lower bound: -0.399122
g1.fwhm lower bound: -0.465406
g1.ampl upper bound: 0.405767
g1.fwhm upper bound: 0.467569
bgnd.c0 lower bound: -0.0152336
g1.xpos lower bound: -0.297327
g1.xpos upper bound: 0.297382
g1.ypos lower bound: -0.294267
g1.ypos upper bound: 0.294294
bgnd.c0 upper bound: 0.0156245
Dataset = 1
Confidence Method = confidence
Iterative Fit Method = None
Fitting Method = neldermead
Statistic = cash
confidence 1.64485-sigma (90%) bounds:
Param Best-Fit Lower Bound Upper Bound
===== ======== =========== ===========
g1.fwhm 57.9477 -0.465406 0.467569
g1.xpos 4070.4 -0.297327 0.297382
g1.ypos 4251.11 -0.294267 0.294294
g1.ampl 23.3562 -0.399122 0.405767
bgnd.c0 0.266365 -0.0152336 0.0156245</p>
</pre><div class="admonition-exercise-for-the-interested-reader-scipy-special-functions admonition">
<p class="first admonition-title">Exercise (for the interested reader): SciPy special functions</p>
<p>Sherpa does not yet support the feature to indicate the confidence as a
percentage. How can we convert the desired percentage to the sigma that
Sherpa supports?</p>
<p class="last">(Hint): Try <code class="docutils literal"><span class="pre">scipy.special.erfinv</span></code></p>
</div>
<p class="flip0">Click to Show/Hide Solution</p> <div class="panel0"><p>Typically, the confidence is calculated from sigma using the error-function,
ERF(sigma/SQRT(2)). We can invert this operation using the inverse
error-function found in SciPy in the special functions module:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">scipy.special</span>
<span class="k">print</span><span class="p">(</span><span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">erfinv</span><span class="p">(</span><span class="mf">0.90</span><span class="p">)</span> <span class="o">*</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
</div><p>Notice how the parameter confidence limits are displayed as soon as they are
known. The parameter confidence limits are accessed using <code class="docutils literal"><span class="pre">get_conf_results</span></code>.
Save the 90% calculated parameter limits:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">get_conf_results</span><span class="p">()</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">file</span><span class="p">(</span><span class="s2">"fit_results.out"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"NAME VALUE MIN MAX</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">minval</span><span class="p">,</span> <span class="n">maxval</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">parnames</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parvals</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parmins</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parmaxes</span><span class="p">):</span>
<span class="n">line</span> <span class="o">=</span> <span class="p">[</span><span class="n">name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="o">+</span><span class="n">minval</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="o">+</span><span class="n">maxval</span><span class="p">)]</span>
<span class="k">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">line</span><span class="p">)</span><span class="o">+</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>View the new output file:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>!cat fit_results.out
NAME VALUE MIN MAX
g1.fwhm 57.9477261812 57.4823204857 58.4152947363
g1.xpos 4070.40148441 4070.10415775 4070.69886665
g1.ypos 4251.10566089 4250.811394 4251.39995481
g1.ampl 23.3562056688 22.9570833968 23.761972516
bgnd.c0 0.266364723807 0.25113117078 0.281989200833
</pre></div>
</div>
<p>Notice how the function <code class="docutils literal"><span class="pre">zip</span></code> rotates the list of tuples into rows of names,
values, min values, and max values:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">tbl</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">parnames</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parvals</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parmins</span><span class="p">,</span><span class="n">results</span><span class="o">.</span><span class="n">parmaxes</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">tbl</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="p">(</span><span class="s1">'g1.fwhm'</span><span class="p">,</span> <span class="mf">57.947726181203684</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.46540569551049771</span><span class="p">,</span> <span class="mf">0.46756855511208073</span><span class="p">)</span>
<span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">minval</span><span class="p">,</span> <span class="n">maxval</span> <span class="o">=</span> <span class="n">tbl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">minval</span>
<span class="o">-</span><span class="mf">0.46540569551049771</span>
</pre></div>
</div>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">You could use <code class="docutils literal"><span class="pre">min</span></code> and <code class="docutils literal"><span class="pre">max</span></code> as the variable names, but then
you overwrite the Python functions which can cause surprising
results later on in your session.</p>
</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="#">2-D Fitting in Sherpa</a></li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="spectrum.html"
title="previous chapter">1-D data with errors</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="low-level.html"
title="next chapter">The low-level Sherpa API</a></p>
</div>
</div>
<div class="clearer"></div>
</div>
<footer class="footer">
<p class="pull-right">
<a href="../_sources/fitting/image.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>