This repository was archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.ipynb
2199 lines (2199 loc) · 158 KB
/
.ipynb
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"run_control": {
"frozen": false,
"read_only": false
},
"toc": "true"
},
"source": [
"# Table of Contents\n",
" <p><div class=\"lev1 toc-item\"><a href=\"#Title\" data-toc-modified-id=\"Title-1\"><span class=\"toc-item-num\">1 </span>Title</a></div><div class=\"lev2 toc-item\"><a href=\"#Goals\" data-toc-modified-id=\"Goals-1.1\"><span class=\"toc-item-num\">1.1 </span>Goals</a></div><div class=\"lev1 toc-item\"><a href=\"#Setup\" data-toc-modified-id=\"Setup-2\"><span class=\"toc-item-num\">2 </span>Setup</a></div><div class=\"lev2 toc-item\"><a href=\"#Imports-and-Settings\" data-toc-modified-id=\"Imports-and-Settings-2.1\"><span class=\"toc-item-num\">2.1 </span>Imports and Settings</a></div><div class=\"lev3 toc-item\"><a href=\"#Miscellaneous-Imports\" data-toc-modified-id=\"Miscellaneous-Imports-2.1.1\"><span class=\"toc-item-num\">2.1.1 </span>Miscellaneous Imports</a></div><div class=\"lev3 toc-item\"><a href=\"#Matplotlib-Settings\" data-toc-modified-id=\"Matplotlib-Settings-2.1.2\"><span class=\"toc-item-num\">2.1.2 </span>Matplotlib Settings</a></div><div class=\"lev3 toc-item\"><a href=\"#SymPy\" data-toc-modified-id=\"SymPy-2.1.3\"><span class=\"toc-item-num\">2.1.3 </span>SymPy</a></div><div class=\"lev3 toc-item\"><a href=\"#Javascript-Settings\" data-toc-modified-id=\"Javascript-Settings-2.1.4\"><span class=\"toc-item-num\">2.1.4 </span>Javascript Settings</a></div><div class=\"lev2 toc-item\"><a href=\"#Constants\" data-toc-modified-id=\"Constants-2.2\"><span class=\"toc-item-num\">2.2 </span>Constants</a></div><div class=\"lev3 toc-item\"><a href=\"#Physics-&-Math-Constants\" data-toc-modified-id=\"Physics-&-Math-Constants-2.2.1\"><span class=\"toc-item-num\">2.2.1 </span>Physics & Math Constants</a></div><div class=\"lev3 toc-item\"><a href=\"#Lab-Constants\" data-toc-modified-id=\"Lab-Constants-2.2.2\"><span class=\"toc-item-num\">2.2.2 </span>Lab Constants</a></div><div class=\"lev3 toc-item\"><a href=\"#Lab-Volatile-Constants\" data-toc-modified-id=\"Lab-Volatile-Constants-2.2.3\"><span class=\"toc-item-num\">2.2.3 </span>Lab Volatile Constants</a></div><div class=\"lev3 toc-item\"><a href=\"#Sympy-Constants\" data-toc-modified-id=\"Sympy-Constants-2.2.4\"><span class=\"toc-item-num\">2.2.4 </span>Sympy Constants</a></div><div class=\"lev2 toc-item\"><a href=\"#Miscellaneous-Small-Functions\" data-toc-modified-id=\"Miscellaneous-Small-Functions-2.3\"><span class=\"toc-item-num\">2.3 </span>Miscellaneous Small Functions</a></div><div class=\"lev3 toc-item\"><a href=\"#Loading-Functions\" data-toc-modified-id=\"Loading-Functions-2.3.1\"><span class=\"toc-item-num\">2.3.1 </span>Loading Functions</a></div><div class=\"lev3 toc-item\"><a href=\"#Random-Useful-Functions\" data-toc-modified-id=\"Random-Useful-Functions-2.3.2\"><span class=\"toc-item-num\">2.3.2 </span>Random Useful Functions</a></div><div class=\"lev3 toc-item\"><a href=\"#Generic-functions-for-fits\" data-toc-modified-id=\"Generic-functions-for-fits-2.3.3\"><span class=\"toc-item-num\">2.3.3 </span>Generic functions for fits</a></div><div class=\"lev3 toc-item\"><a href=\"#Specialty-functions-for-fits\" data-toc-modified-id=\"Specialty-functions-for-fits-2.3.4\"><span class=\"toc-item-num\">2.3.4 </span>Specialty functions for fits</a></div><div class=\"lev3 toc-item\"><a href=\"#Fitting-Functions\" data-toc-modified-id=\"Fitting-Functions-2.3.5\"><span class=\"toc-item-num\">2.3.5 </span>Fitting Functions</a></div><div class=\"lev2 toc-item\"><a href=\"#Analysis-&-Plotting\" data-toc-modified-id=\"Analysis-&-Plotting-2.4\"><span class=\"toc-item-num\">2.4 </span>Analysis & Plotting</a></div><div class=\"lev3 toc-item\"><a href=\"#Plotters\" data-toc-modified-id=\"Plotters-2.4.1\"><span class=\"toc-item-num\">2.4.1 </span>Plotters</a></div><div class=\"lev3 toc-item\"><a href=\"#Crunchers\" data-toc-modified-id=\"Crunchers-2.4.2\"><span class=\"toc-item-num\">2.4.2 </span>Crunchers</a></div><div class=\"lev3 toc-item\"><a href=\"#Picture-Handling\" data-toc-modified-id=\"Picture-Handling-2.4.3\"><span class=\"toc-item-num\">2.4.3 </span>Picture Handling</a></div><div class=\"lev3 toc-item\"><a href=\"#MOT-Analysis\" data-toc-modified-id=\"MOT-Analysis-2.4.4\"><span class=\"toc-item-num\">2.4.4 </span>MOT Analysis</a></div><div class=\"lev2 toc-item\"><a href=\"#Packages\" data-toc-modified-id=\"Packages-2.5\"><span class=\"toc-item-num\">2.5 </span>Packages</a></div><div class=\"lev3 toc-item\"><a href=\"#Standard-Basler\" data-toc-modified-id=\"Standard-Basler-2.5.1\"><span class=\"toc-item-num\">2.5.1 </span>Standard Basler</a></div><div class=\"lev3 toc-item\"><a href=\"#Basler-Temperature\" data-toc-modified-id=\"Basler-Temperature-2.5.2\"><span class=\"toc-item-num\">2.5.2 </span>Basler Temperature</a></div><div class=\"lev3 toc-item\"><a href=\"#Standard-Fits\" data-toc-modified-id=\"Standard-Fits-2.5.3\"><span class=\"toc-item-num\">2.5.3 </span>Standard Fits</a></div><div class=\"lev3 toc-item\"><a href=\"#Histogram\" data-toc-modified-id=\"Histogram-2.5.4\"><span class=\"toc-item-num\">2.5.4 </span>Histogram</a></div><div class=\"lev1 toc-item\"><a href=\"#Work\" data-toc-modified-id=\"Work-3\"><span class=\"toc-item-num\">3 </span>Work</a></div>"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"# Title"
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"Mark Brown \n",
"Notebook Base Version 1.0"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:36:21.601040",
"start_time": "2017-04-28T09:36:21.593043"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"date = \"17????\""
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"## Goals"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"# Setup"
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"## Imports and Settings"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Miscellaneous Imports"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:36:50.868691",
"start_time": "2017-04-28T09:36:22.409098"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy import array as arr\n",
"import pandas as pd\n",
"import collections\n",
"from mpl_toolkits.mplot3d import axes3d\n",
"from scipy.optimize import curve_fit as fit\n",
"from astropy.io import fits\n",
"import math as m\n",
"import sys"
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Matplotlib Settings"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T13:02:24.150277",
"start_time": "2017-04-28T13:02:24.135247"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"import matplotlib as mpl\n",
"from matplotlib.pyplot import *\n",
"### set matplotlib plot defaults :D\n",
"%matplotlib inline\n",
"# Style controls many default colors in matplotlib plots.\n",
"# Change the following if you don't like dark backgrounds. Many other options.\n",
"style.use(['dark_background'])\n",
"mpl.rcParams['axes.facecolor'] = '#0a0a0a'\n",
"# the default cycling of colors in this mode isn't very good.\n",
"mpl.rcParams['axes.prop_cycle'] = cycler('color', ['r','c','g','#FFFFFF','y','m','b'])\n",
"mpl.rcParams['figure.figsize'] = (18.0, 8.0)\n",
"mpl.rcParams['axes.grid'] = True\n",
"mpl.rcParams['axes.formatter.useoffset'] = False\n",
"mpl.rcParams['grid.alpha'] = 0.3\n",
"mpl.rcParams['axes.formatter.limits'] = (0,1)\n",
"# jet is awful.\n",
"mpl.rcParams['image.cmap'] = 'inferno'\n",
"# to see all available options, decomment this line.\n",
"#print(mpl.rcParams)\n",
"mpl.rcParams['font.size'] = 14"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### SymPy"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:05.857173",
"start_time": "2017-04-28T09:36:53.269674"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"import sympy as sp\n",
"sp.init_printing(use_latex=True)\n",
"# see the constants section for some constants set in sympy"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"hide_input": false,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Javascript Settings"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:05.867116",
"start_time": "2017-04-28T09:37:05.857173"
},
"collapsed": false,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [
{
"data": {
"application/javascript": [
"// the above line makes this entire cell run javascript commands.\n",
"// this gets rid of scroll bars on the output by default. It's in javascript because javascript is used \n",
"// by Jupyter to actually render the notebook display.\n",
"IPython.OutputArea.auto_scroll_threshold = 9999;"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" %%javascript\n",
" // the above line makes this entire cell run javascript commands.\n",
" // this gets rid of scroll bars on the output by default. It's in javascript because javascript is used \n",
" // by Jupyter to actually render the notebook display.\n",
" IPython.OutputArea.auto_scroll_threshold = 9999;"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"## Constants"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Physics & Math Constants"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.017149",
"start_time": "2017-04-28T09:37:05.867116"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"# all in mks\n",
"H = 6.6260700e-34\n",
"# reduced planck's constant\n",
"HBAR = 1.0545718e-34\n",
"# boltzman's constant\n",
"K_B = 1.380649e-23\n",
"# speed of light (exact)\n",
"c = 299792458\n",
"# Stephan-Boltzman constant\n",
"sigma = 5.6704e-8\n",
"# atomic mass unit\n",
"amu = 1.6605390e-27\n",
"# rubidium 87 mass\n",
"m_Rb87 = 86.909180527 * amu\n",
"# use numpy\n",
"pi = np.pi\n",
"# gravity\n",
"g = 9.80665\n",
"\n",
"rb87Gamma = 38.1*10**6\n",
"rb87I_Sat = 3.576\n",
"# should refine...\n",
"rbD2LineWavelength = 780*10**-9\n",
"rbD1LineWavelength = 795*10**-9"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Lab Constants"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.207208",
"start_time": "2017-04-28T09:37:06.017149"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"andorDataRepository = \"\\\\\\\\andor\\\\share\\\\Data and documents\\\\Data repository\\\\\"\n",
"rawDataLoc = date + \"\\\\Raw Data\\\\\"\n",
"opBeamDacToVoltageConversion = [8.5, -22.532, -1.9323, -0.35142]\n",
"# 7.4 x 7.4 micron mixel size\n",
"baslerCcdPixelSize = 7.4e-6\n",
"# 16 micron pixels\n",
"andorPixelSize = 16e-6\n",
"# basler conversion... joules per greyscale count.\n",
"# number from theory of camera operation\n",
"C = 117*10**-18 \n",
"#number from measurement. I suspect this is a little high because I think I underestimated the attenuation \n",
"# of the signal by the 780nm filter.\n",
"# C = 161*10**-18 "
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Lab Volatile Constants"
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"Constants that can easily change day to day depending on drifts, etc."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.337494",
"start_time": "2017-04-28T09:37:06.207208"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"# this changes when the imaging system for the basler changes. there's a notebook for calculating this. \n",
"baslerMetersPer4x4Pixel = 61.7818944758e-6\n",
"baslerMetersPerPixel = baslerMetersPer4x4Pixel / 4\n",
"# in mW\n",
"sidemotPower = 0.75\n",
"# in mW\n",
"diagonalMPower = 9.3\n",
"# in cm\n",
"motRadius = 5 * baslerMetersPer4x4Pixel * 100\n",
"# in hertz\n",
"imagingDetuning = 10*10**6\n",
"baslerRawGain = 260\n",
"# in cm\n",
"axialImagingLensDiameter = 2.54\n",
"# in cm\n",
"axialImagingLensFocalLength = 10"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Sympy Constants"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.504425",
"start_time": "2017-04-28T09:37:06.339510"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"# Pauli Matrices\n",
"X = sigma_x = sp.Matrix([[0,1],[1,0]])\n",
"Y = sigma_y = sp.Matrix([[0,-1j],[1j,0]])\n",
"Z = sigma_z = sp.Matrix([[1,0],[0,-1]])\n",
"# Hadamard\n",
"H = hadamard = sp.Matrix([[1,1],[1,-1]])\n",
"# Phase Gate\n",
"S = phaseGate = sp.Matrix([[1,0],[0,1j]])\n",
"# Phase Shift gate\n",
"def phaseShiftGate(phi):\n",
" return sp.Matrix([[1,0],[[0,sp.exp(1j*phi)]]])\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"## Miscellaneous Small Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-31T10:55:52.589000Z",
"start_time": "2017-03-31T10:55:52.579474"
},
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Loading Functions"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.688986",
"start_time": "2017-04-28T09:37:06.504425"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def loadBasler(num):\n",
" path = andorDataRepository + rawDataLoc + \"Run\" + str(num) + \".txt\"\n",
" return pd.read_csv(path, sep='\\t', header=None).as_matrix()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:06.868634",
"start_time": "2017-04-28T09:37:06.688986"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def loadFits(num):\n",
" # Get the array from the fits file. That's all I care about.\n",
" path = andorDataRepository + rawDataLoc + \"data_\" + str(num) + \".fits\"\n",
" with fits.open(path, \"append\") as fitsInfo:\n",
" rawData = np.array(fitsInfo[0].data);\n",
" return rawData"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.008602",
"start_time": "2017-04-28T09:37:06.868634"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def loadKey(num):\n",
" key = np.array([]);\n",
" path = andorDataRepository + rawDataLoc + \"key_\" + str(num) + \".txt\"\n",
" with open(path) as keyFile:\n",
" for line in keyFile:\n",
" key = np.append(key, float(line.strip('\\n')))\n",
" keyFile.close() \n",
" return key"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Random Useful Functions"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.156271",
"start_time": "2017-04-28T09:37:07.008602"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def round_sig(x, sig=3):\n",
" return round(x, sig-int(m.floor(m.log10(abs(x)+np.finfo(float).eps)))-1)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.296267",
"start_time": "2017-04-28T09:37:07.156271"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def orderData(data, key):\n",
" key, data = zip(*sorted(zip(key, data)))\n",
" return arr(data), arr(key)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.419934",
"start_time": "2017-04-28T09:37:07.296267"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def getLabels(plotType):\n",
" if plotType == \"Detuning\":\n",
" xlabel = \"Detuning (dac value)\"\n",
" title = \"Detuning Scan\"\n",
" elif plotType == \"Field\":\n",
" xlabel = \"Differential Field Change / 2 (dac value)\"\n",
" title = \"Differential Magnetic Field Scan\"\n",
" elif plotType == \"Time(ms)\":\n",
" xlabel =\"Time(ms)\"\n",
" title = \"Time Scan\"\n",
" elif plotType == \"Power\":\n",
" xlabel =\"Power (dac units)\"\n",
" title = \"Power Scan\"\n",
" else:\n",
" xlabel =\"Key Value\"\n",
" title = \"\"\n",
" return xlabel, title"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.555261",
"start_time": "2017-04-28T09:37:07.419934"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def combineData(data, key):\n",
" \"\"\"\n",
" combines similar key value data entries. data will be in order that unique key items appear in key. \n",
" For example, if key = [1,3,5,3,7,1], returned key and corresponding data will be newKey = [1, 3, 5, 7]\n",
" \"\"\"\n",
" items = {}\n",
" newKey = []\n",
" newData = []\n",
" for elem in key:\n",
" #print(elem)\n",
" if str(elem) not in items:\n",
" indexes = [i for i, x in enumerate(key) if x == elem]\n",
" # don't get it again\n",
" items[str(elem)] = \"!\"\n",
" newKey.append(elem)\n",
" newItem = np.zeros((data.shape[1], data.shape[2]))\n",
" # average together the corresponding data.\n",
" for index in indexes:\n",
" newItem += data[index]\n",
" newItem /= len(indexes)\n",
" newData.append(newItem)\n",
" return arr(newData), arr(newKey)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Generic functions for fits"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.689339",
"start_time": "2017-04-28T09:37:07.557523"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"# Fits \n",
"def quadratic(x,a,b,x0):\n",
" # This assumes downward facing. Best to write another function for upward facing if need be, I think.\n",
" if a < 0:\n",
" return 10**10\n",
" if b > 0:\n",
" return 10**10\n",
" return a + b*(x-x0)**2\n",
"\n",
"\n",
"def gaussian(x, A1, x01, sig1, offset):\n",
" if (offset < 0):\n",
" return 10**10\n",
" return offset + A1 * np.exp(-(x-x01)**2/(2*sig1**2))\n",
"\n",
"\n",
"def doubleGaussian(x, A1, x01, sig1, A2, x02, sig2, offset):\n",
" if (A1 < 0 or A2 < 0):\n",
" # Penalize negative fits.\n",
" return 10**10\n",
" if (offset < 0):\n",
" return 10**10\n",
" return offset + A1 * np.exp(-(x-x01)**2/(2*sig1**2)) + A2 * np.exp(-(x-x02)**2/(2*sig2**2))\n",
"\n",
"\n",
"def tripleGaussian(x, A1, x01, sig1, A2, x02, sig2, A3, x03, sig3, offset ):\n",
" if (A1 < 0 or A2 < 0 or A3 < 0):\n",
" # Penalize negative fits.\n",
" return 10**10\n",
" if (offset < 0):\n",
" return 10**10\n",
" return (offset + A1 * np.exp(-(x-x01)**2/(2*sig1**2)) + A2 * np.exp(-(x-x02)**2/(2*sig2**2)) \n",
" + A3 * np.exp(-(x-x03)**2/(2*sig3**2)))\n",
"# Stolen from http://stackoverflow.com/questions/21566379/fitting-a-2d-gaussian-function-using-scipy-optimize-curve-fit-valueerror-and-m\n",
"def gaussian_2D(coordinates, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):\n",
" x = coordinates[0]\n",
" y = coordinates[1]\n",
" xo = float(xo)\n",
" yo = float(yo) \n",
" a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)\n",
" b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)\n",
" c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)\n",
" g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) + c*((y-yo)**2)))\n",
" return g.ravel()\n",
"\n",
"def decayingCos(x, A, tau, f, phi, offset):\n",
" # Just for sanity. Keep some numbers positive.\n",
" if (A < 0):\n",
" return x * 10**10\n",
" if (phi < 0):\n",
" return x * 10**10\n",
" if (offset < 0):\n",
" return x * 10**10\n",
" # no growing fits.\n",
" if (tau > 0):\n",
" return x * 10**10\n",
" return offset + (1 - A/2 * np.exp(-x/tau) * np.cos(2 * np.pi * f * x + phi))\n",
"\n",
"def sinc2(x, A, center, scale, offset):\n",
" \"\"\"\n",
" The 2 here referes to squared!\n",
" \"\"\"\n",
" if (offset < 0):\n",
" return x * 10**10\n",
" if (A < 0):\n",
" return x * 10**10\n",
" return (A * np.sinc((x - center)/scale)**2 + offset)\n",
"\n",
"\n",
"def lorentzian(x, A, center, width, offset):\n",
" if (offset < 0):\n",
" return x * 10**10\n",
" if (A < 0):\n",
" return x * 10**10\n",
" return (A /((x - center)**2 + (width/2)**2))\n",
"\n",
"\n",
"def poissonian(x, k, weight): \n",
" \"\"\"\n",
" This function calculates p_k{x} = weight * e^(-k) * k^x / x!.\n",
" :param x: argument of the poissonian\n",
" :param k: order or (approximate) mean of the poissonian.\n",
" :param weight: a weight factor, related to the maximum data this is supposed to be fitted to, but typically over-\n",
" weighted for the purposes of this function.\n",
" :return: the poissonian evaluated at x given the parametes.\n",
" \"\"\"\n",
" import numpy as np\n",
" term = 1\n",
" # calculate the term k^x / x!. Can't do this directly, x! is too large.\n",
" for n in range(0, int(x)):\n",
" term *= k / (x - n)\n",
" return np.exp(-k) * term * weight"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Specialty functions for fits"
]
},
{
"cell_type": "markdown",
"metadata": {
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"Coming from various theoretical calculations"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:07.879012",
"start_time": "2017-04-28T09:37:07.689339"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def ballisticMotExpansion(t, sigma_y0, sigma_vy, sigma_I):\n",
" \"\"\"\n",
" You can see a derivation of this in a different notebook for temperature calculations. I don't know why, but\n",
" this function seems mildly unstable as a fitting function. It doesn't always work very sensibly.\n",
" \"\"\"\n",
" return sigma_I*np.sqrt((sigma_y0**2 + sigma_vy**2 * t**2)/(sigma_y0**2+sigma_vy**2*t**2+sigma_I**2))\n",
"\n",
"def simpleMotExpansion(t, sigma_y0, sigma_vy):\n",
" \"\"\"\n",
" this simpler version ignores the size of the beam waist of the atoms. \n",
" It should generally behave better with noisy data.\n",
" \"\"\"\n",
" return sigma_y0 + sigma_vy * t\n",
"\n",
"\n",
"def beamWaistExpansion(z, w0, wavelength):\n",
" \"\"\" assuming gaussian intensity profile of I~exp{-2z^2/w{z}^2} \"\"\"\n",
" return w0 * np.sqrt(1+(wavelength*z/(Pi*w0**3))**2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"source": [
"### Fitting Functions"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2017-04-28T09:37:08.033124",
"start_time": "2017-04-28T09:37:07.879012"
},
"collapsed": true,
"hidden": true,
"run_control": {
"frozen": false,
"read_only": false
}
},
"outputs": [],
"source": [
"def fitPic(picture, show=True):\n",
" pos = np.unravel_index(np.argmax(picture), picture.shape)\n",
" pic = picture.flatten()\n",
" x = np.linspace(1, picture.shape[1], picture.shape[1])\n",
" y = np.linspace(1, picture.shape[0], picture.shape[0])\n",
" x, y = np.meshgrid(x, y)\n",
" initial_guess = (np.max(pic) - np.min(pic),pos[1], pos[0],5,5,0,np.min(pic))\n",
" try: \n",
" popt, pcov = fit(gaussian_2D, (x, y), pic, p0=initial_guess)\n",
" except RuntimeError:\n",
" popt = np.zeros(len(initial_guess))\n",
" pcov = np.zeros((len(initial_guess), len(initial_guess)))\n",
" raise RuntimeError('Fit Failed!')\n",
" if show:\n",
" data_fitted = gaussian_2D((x, y), *popt)\n",
" fig, ax = subplots(1, 1)\n",
" im = ax.imshow(picture, origin='bottom', extent=(x.min(), x.max(), y.min(), y.max()))\n",
" ax.contour(x, y, data_fitted.reshape(picture.shape[0],picture.shape[1]), 8, colors='w')\n",
" fig.colorbar(im)\n",
" return popt, np.sqrt(np.diag(pcov))\n",
"\n",
"\n",
"def fitPictures(pictures, dataRange, show=True):\n",
" fitParameters = []\n",
" fitErrors = []\n",
" count = 0\n",
" warningHasBeenThrown = False\n",
" for picture in pictures:\n",
" if count not in dataRange:\n",