Successfully reported this slideshow.

Python for Scientists

4,674 views

Published on

This is a presentation about using the programming language Python in science and research.

Published in: Technology
  • Please note that this is pretty outdated by now, so just use for 'archeological' purposes ;)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Python for Scientists

  1. 1. Python forScientists<br />Dewes<br />Quantronics Group Seminar<br />
  2. 2. Python: Qu'est-ce que c'est?<br />Multi-paradigm<br />(objectoriented, functional…)<br />print“Easy as…“<br />fori in [1,2,3]:<br />printi<br />ctypes,sockets,files,regex,…<br />Matplotlib,SciPy,NumPy,PyQT,…<br />
  3. 3. Whytouseit: A Scientific Workflow<br />
  4. 4. Software makeslifeeasier…<br />
  5. 5. Using Python…<br />
  6. 6. Data Acquisition<br /> http://pyvisa.sourceforge.net http://docs.python.org/library/ctypes.html <br />
  7. 7. PyVisaExample: MWG & VNA<br />fromvisaimport*<br />#Open a connectionto an Anritsu MWG<br />anritsu = instrument(“GPIB0::12“)<br />#Askfortheoutputfrequency<br />print anritsu.ask(“OF1“)<br />#Set theoutput power<br />anritsu.write(“L15DM“)<br />#Turn on themicrowave<br />anritsu.write(“RF0“)<br />vna = instrument(„GPIB0::15“)<br />x = vna.ask_for_values(“fma;ofv;“)<br />y = vna.ask_for_values(“fma;ofd;“)<br />VNA Frontpanel<br />Keypoints<br /><ul><li>Clear, object-orientedinterface
  8. 8. Directly process & analyze measured data
  9. 9. Fast, flexible programming
  10. 10. Not toomuchworryingabouttechnicaldetails…
  11. 11. VNA exampleusesPyQtfortheuserinterface</li></li></ul><li>ctypesExample: Taming an Acqiriscard<br />Objective: Loadtheexternal DLL, transfersome<br />measurementdataandprocessit on thefly.<br />frompylabimport*;fromctypesimport*<br />vi_session= c_uint32()<br />temperature= c_int32() <br />time_us= c_int32()<br />timestamp= c_int32()<br />nsegments_returned= c_int32()<br />npoints_returned= c_int32()<br />acqiris=windll.LoadLibrary(r‘C:projectspythonpylabinstrumentsdllAcqiris.dll‘)<br />acqiris.FindDevicesV1(byref(vi_session),byref(temperature))<br />#Configuration...<br />acqiris.AcquireV1(vi_session,1,byref(time_us))<br />waveforms = zeros((4,250 *100))<br />acqiris.DMATransferV1(vi_session,15,100,250,0,1,4e-9, byref(timestamp),waveforms[0,:].ctypes.data,waveforms[1,:].ctypes.data,waveforms[2,:].ctypes.data,waveforms[3,:].ctypes.data,byref(time_us),byref(nsegments_returned),byref(npoints_returned))<br />#Waveformdatanowisstored in „waveforms“ arrayandcanbeprocessed, e.g. apply FFT & plot:<br />plot(range(len(waveforms[0,:])),fft(waveforms[0,:]))<br />
  12. 12. Data Analysis<br /> http://www.scipy.org http://numpy.scipy.org<br />
  13. 13. SciPy & NumPyExample<br />importscipy.optimize<br />#Define the model to befitted<br />fitfunc= lambda p, x: p[0]+p[1]*<br />exp(-x*abs(p[2]))*cos(p[3]+p[4]*x)<br />#Define the errorfunction<br />errfunc = lambda p, x, y: fitfunc(p, x)– y<br />#Make a fit by minimizing the errorfunction<br />p1 = scipy.optimize.fmin(lambdap,x,y: <br />norm(errfunc(p,x,y)), p0, args=(data [:,0], data[:,1]))<br />#Print the returnedparameters<br />print p1<br />#Plot the data and the fit…<br />plot(data[:,0], data[:,1], "ro", data[:,0], <br />fitfunc(p1, data[:,0]), "b-")<br />#Add a legend<br />legend(("data","fit"))<br />#Add a title<br />title("Ramsey - $V_{fluxline}= %d mV$, <br /> $T_2 = %d$ ns, $A = %f$ " % (int(voltage) , abs(int(1.0/p1[2])),p1[1]))<br />
  14. 14. Presentation & Visualization<br />http://matplotlib.sourceforge.net/ http://qt.nokia.com/ <br />
  15. 15. MatplotlibExamples<br />grid()#Add gridlinestotheplot<br />#Plot 4 datasets<br />plot(x1,y1,x2,y2,x3,y3,x4,y4)<br />#Add a legend<br />legend((“Amplification…“,“…“))<br />#Set theaxislabels<br />xlabel(“frequency[GHz]“)<br />ylabel(“attenuation[dB]“)<br />#Save thefigureas a PDF<br />savefig(“attenuation.pdf“)<br />jet() #Select a colorsheme<br />#Plot the matrix<br />imshow(dataMatrix,aspect = 'auto',origin = 'lower',extent = (xmin,xmax,ymin,ymax))<br />xlabel("$A$ [V]")<br />ylabel("$f$ [GHz]")<br />#Use Latex for text…<br />title("Qubit 1 – Modulated by $V_{flux2} = A cdot sin{(20 MHz cdot 2pi t)}$")<br />
  16. 16. Somemoreexamples…<br />
  17. 17. PyQTExample: Instrument frontpanels<br />title = QLabel(„Qubit 1")<br />splitter = QSplitter(Qt.Horizontal)<br />SetButton= QPushButton("Set")<br />splitter.addWidget(SetButton)<br />#...<br />grid = QGridLayout(self)<br />grid.addWidget(title,0,0)<br />grid.addWidget(QLabel("Voltage"),1,0)<br />#...<br />self.connect(SetButton,SIGNAL("clicked()"),self.changeVoltage)self.setLayout(grid)<br />Or use<br />Qt Designer:<br />
  18. 18. HowToGetStarted<br />1. Download it…<br />2. Check out thedocumentation & tutorial (online)<br />3. Go &contributetotheWiki<br />4. Askyourcolleagues…<br /> http://www.python.org http://docs.python.org/tutorial/ http://wiki-quantro.extra.cea.fr<br />
  19. 19. That‘s it.<br />
  20. 20. Object HirarchyExample<br />Dispatcher<br />Subject<br />Reloadable<br />ThreadedDispatcher<br />Instrument<br />RemoteInstrument<br />VisaInstrument<br />VNA<br />AgilentMWG<br />Yokogawa<br />Acqiris<br />

×