jagomart
digital resources
picture1_Python Pdf 182475 | Lecture 01 Python


 212x       Filetype PDF       File size 0.08 MB       Source: ethz.ch


File: Python Pdf 182475 | Lecture 01 Python
python notes statistical methods in cosmology and astrophysics as part of this statistics course we will rely heavily on hands on practical applications of the statistical ideas that we discuss ...

icon picture PDF Filetype PDF | Posted on 31 Jan 2023 | 2 years ago
Partial capture of text on file.
       Python Notes!
       Statistical Methods in Cosmology and Astrophysics!
       !
       As part of this statistics course, we will rely heavily on hands on practical applications of the 
       statistical ideas that we discuss in class. To do this we will need to work in a common way so that 
       we can learn and work together. The default language for this course will by Python. As well as this 
       the notes for the class will be distributed as iPython Notebooks (see below). !
       !
       Installing Python and associated packages!
       !
       There are many ways of installing a python interpreter and the packages that make python a good 
       environment for scientific programming. The notes here show an example of how to install these 
       on a mac using mac ports. The notes below where are selection of instruction collected from: !
       http://astrofrog.github.io/macports-python!
       http://michigancomputes.wordpress.com/2012/10/18/installing-ipython-notebook-for-mac/!
       !
       !     Installing MacPorts!
       • Ensure that XCode is installed (https://developer.apple.com/xcode/). To check if XCode is 
        installed properly, you can type gcc in the command-line. If you get gcc: command not found, 
        then XCode is not properly installed. !
       • Download the DMG image for MacPorts (https://www.macports.org/install.php) - be sure to pick 
       ! the correct one for your MacOS X version. Mount the disk image and run the installer.  !
       !   Installing Packages!
       !
       Go to the terminal and update the package index: sudo port selfupdate !
       !
       • To install the basic Python packages, run: !
           sudo port install py27-matplotlib py27-numpy py27-scipy 
       Note that this will probably take several hours, and is best done overnight. !
       !
       • Install the packages you will need for ipython notebook:!
           sudo port install py27-zmq 
           sudo port install py27-tornado 
       !   sudo port install py27-nose 
       • install ipython:!
       !   sudo port install py27-ipython 
       There are more packages that you can install, but for now we will focus on these main ones.!
       !
       !    Configuration!
       !
       • Make this Python installation the default: !
            sudo port select --set python python27

       !    sudo port select --set ipython ipython27  
       • You should now be able to check that your notebook works:!
       !    ipython notebook 
       !
       Important packages!
       !
       Python has a large set of packages that contain useful functions for scientific computing. The main 
       three that we will focus on in this course are numpy, scipy and pylab. If you’ve installed everything 
       properly you should be able to import these packages in ipython:!
       !
            import numpy as np 
            import scipy as sp 
       !    import pylab as pl 
       Once you do this you can easily access useful functions. For instance if you want to calculate the 
       sin(0.5), you can use a numpy function,!
       !
            print(np.sin(0.5)) 
       0.479425538604 
       !
       !      numpy!
       numpy is vital for scientific computing with python. There is a lot of documentation online. You can 
       begin with the main site (http://www.numpy.org). For users coming from other programming 
       languages there are also a lot of useful look-up tables to help you find the operations you want.  
       For instance I moved from IDL  and I found the following table very useful: !
       http://www.astro.umd.edu/~mbk/idl-numpy.html!
       One of the key things to remember about using numpy is to try and take advantage of the way it 
       handles operations. For example you should avoid loops where possible and instead use vector 
       operations. For instance to calculate the exponent at a number of x values it is better to do 
       something like !
            x = np.array([0.5,1.0,1.5,2.0]) 
            print(np.exp(x)) 
       [ 1.64872127  2.71828183  4.48168907  7.3890561 ] 
       !
       The first line creates what is called a numpy array from a list of numbers. These arrays can then be 
       manipulated in many ways. Another useful thing about using numpy arrays and vectorised 
       calculations is that it allows you to write code in a way that is similar to how you would write the 
       equations.!
       !
       !    scipy!
       scipy contains many useful functions. At the moment the distinctions between scipy and numpy is 
       becoming a bit blurred (I think) http://www.scipy.org !
       Some of the most important sub packages that you might use in this class are stats, interpolate, 
       fftpack, linalg, integration, optimisation and signal.!
       !
       !    pylon!
       Matplotlib is a very nice plotting library: http://matplotlib.org!
       One useful thing is to set:!
       !   pl.ion() 
       This will make sure that the plots will appear in an interactive way. Personally I add this right after I 
       import pylab. !
       For example to make a plot of sin(x) between 0 and 2pi, you can do something like this;!
       !
           x = np.linspace(0.0,2.0*np.pi) 
           pl.plot(x,np.sin(x)) 
           pl.xlabel(‘Angle') 
       !   pl.ylabel('f(x)') 
       If you haven’t set pl.ion() then you might find that you will need to type pl.show() to see the plot. To 
       save a figure:!
       !
       pl.savefig(‘filename.pdf’) 
       !
       !
       Work environment!
       !
       There are a lot of ways that you can work with python and ultimately the right answer for you will 
       be a matter of personal preference. Below are some of my thoughts. !
       I have decided to divide my work into two parts. If I want to work with python in an interactive 
       mode, for instance I want to plot some data in many different ways while I am trying to explore it, 
       then I use the ipython notebook. When I want to write some code that I want to call later multiple 
       times, for example to make functions that I can import in the same I import numpy functions, I like 
       to use an IDE (integrated development environment). A good IDE will help you write better code. 
       An analogy that I like to use is that coding with a very simple editor is like writing a document on a 
       typewriter - perfectly valid, but maybe not optimal. Going to an IDE is like using a work processor 
       that has things like spell check. !
       My recommendation is to use PyCharm (http://www.jetbrains.com/pycharm), which I know certainly 
       works well on macs. There is free community edition that has all the features that you are likely to 
       need. PyCharm will also help you with your version control and has plugging for bitbucket(https://
       bitbucket.org) and GitHub (https://github.com) - see below.!
       !
       Version control!
       !
       It is a very good idea to keep track of your code in a version controlled repository. There are many 
       standard ways of doing this. The main two are SVN and git. ISG in D-Phys has a number of useful 
       pages: !
       http://wiki.phys.ethz.ch/readme/how_to_start_with_subversion!
       http://wiki.phys.ethz.ch/readme/git_basics!
       There are also a number of commercial options that are available. Two popular ones are bitbucket 
       and Github. Another advantage of using repositories, beyond keeping your previous versions safe, 
       is that it allows for multiple developers. This means that you and others can work jointly on a 
       project.!
       !
The words contained in this file might help you see if this file matches what you are looking for:

...Python notes statistical methods in cosmology and astrophysics as part of this statistics course we will rely heavily on hands practical applications the ideas that discuss class to do need work a common way so can learn together default language for by well be distributed ipython notebooks see below installing associated packages there are many ways interpreter make good environment scientic programming here show an example how install these mac using ports where selection instruction collected from http astrofrog github io macports michigancomputes wordpress com notebook ensure xcode is installed https developer apple check if properly you type gcc command line get not found then download dmg image www org php sure pick correct one your macos x version mount disk run installer go terminal update package index sudo port selfupdate basic py matplotlib numpy scipy note probably take several hours best done overnight zmq tornado nose more but now focus main ones conguration installation ...

no reviews yet
Please Login to review.