Python library advice

Affiliation
American Association of Variable Star Observers (AAVSO)
Tue, 08/11/2015 - 21:11

Python has become a popular language in the astronomy community, and I'd like to give it a try.  I see there are quite a few astronomy-related Python libraries out there.  I'd appreciate advice about which boat to jump in to.  My immediate need is coordinate transforations (RA Dec <-> alt-az) but over time I might wish to replace my perl and csh processing scripts with Python, so FITS, WCS handling etc are also relevant.  Which library has the most traction, which should I "invest" in.

Thanks for your advice,    Gary Billings

Affiliation
American Association of Variable Star Observers (AAVSO)
Anaconda Scientific Distribution

Hi Gary

I'm not a dyed-in-the-wool Python guy by any means but I do use it for scientific programing.  I use Anaconda's Scientific distibution. The Anaconda distribution has the advantage of being a self contained package that comes with many tools needed for scientific computation like NumPy, SciPY, Matplotlib and others. Anaconda also can use the Spyder IDE.  I'm sure there are many other dristributions that would work as well.

https://store.continuum.io/cshop/anaconda/

https://store.continuum.io/static/img/Anaconda-Quickstart.pdf

You will have a decision to make regarding using  3.3 or 2.7.  This choice is pretty much based on libraries available with each version.  I satyed with 2.7 although I think you could switch between the two with Anaconda.

Jim Jones

Affiliation
American Association of Variable Star Observers (AAVSO)
Anaconda...

I was new to Python when I started developing TG.  I agree with Jim, the Anaconda distribution integrated everything I needed and save a lot of time.  Also, when I found an incompatibility between Anaconda on the Mac and the same version on PC, I emailed the support site and they worked with me finding and incorporating a fix in the next release.  Be sure you also web search "python astronomy".  Many of the packages they refer to are alredy part of Anconda.  Check existing libraries to see if the RA Dec <-> alt-az is already available.

 

 

Affiliation
American Association of Variable Star Observers (AAVSO)
more detail

Thanks for the pointers to the Anaconda Python distro.  I'm following up on that.

Is AstroPy the recommended library/module/package to use?  There is also palpy, PyEphem, astrolib, pySLALIB etc...  I'm thinking astropy is the most inclusive, and hopefuly reliable, but would welcome advice on that.

Gary Billings

 

 

 

 

 

Affiliation
Astronomical Society of South Australia (ASSAU)
astropy

Hi Gary

I have used astropy to good effect for time conversion/manipulation (JD and other) and I/O for FITS header modification, so would recommend it. I could point you to some example code.

It's installed by default with Anaconda which I also use for any scientific Python work, at least under Mac and Windows but Linux at times as well.

I have no experience with the others you've mentioned, only heard of some.

My inclination is to suggest that if it's in Anaconda or available via "conda install (yet another Python installer: pip, easy_install, setup.py, ... wink), then it's considered stable and useful. If there is more than one to do the same job, then it becomes a question of library feature/functionality comparison.

David 

Affiliation
American Association of Variable Star Observers (AAVSO)
Another great hands-on tutorial about astronomy and Python

There is a lot of more content, useful for learning:
http://nbviewer.ipython.org/github/spacetelescope

but to learn how to use Python in astronomy, there are two courses (with lecture materials and homeworks+solutions):

http://nbviewer.ipython.org/github/spacetelescope/scientific-python-training-2012/tree/master/

and

http://nbviewer.ipython.org/github/spacetelescope/scientific-python-training-2015/tree/master/

Specifically about (aperture) photometry in Python, using AstroPy:

http://nbviewer.ipython.org/github/spacetelescope/UserTraining2015/blob/master/photutils.ipynb

Tõnis

Affiliation
American Association of Variable Star Observers (AAVSO)
Other resources/help/training

 

 

I found this article in nature to be helpful in filtering wheat from chaff:

http://www.nature.com/news/programming-pick-up-python-1.16833

 

Also, a recent post about using python to do periodogram analysis on an RR Lyrae star seems relevant to the AAVSO community: 

https://jakevdp.github.io/blog/2015/06/13/lomb-scargle-in-python/&nbsp;

Kev.

 

Affiliation
American Association of Variable Star Observers (AAVSO)
Too many languages

I did a quick estimate from wikipedia's list of programming languages, and there's over 500 listed! When I started out eons ago, there were just a handful - like COBOL, FORTRAN, BASIC, C, ALGOL and such. I wondered back then, why there were so many ;)

Personally, I have settled in on C/C++ for 30+ years and found no reason to learn a new one, they all basically will do the same thing, right?

And of course, C/C++ is a universal standard under GNU, is all free and works on any computer system/OS. If you don't mind an interpretive environment with its overhead, Java does it much the same way with less worries about pointers going awry ;)

So why Python of the other 500+?

Mike

 

 

Affiliation
None
Python

Here's my two cents worth:

I started with FORTRAN, then later moved up to C and C++, with various assembly languages, and scientific programming environments like Maple, Mathcad and Matlab.  Recently I noticed that many of the technical courses offered on edX and Coursera required some knowledge of Python, so I decided to learn it.  Someone asked "Why Phthon?"  (and not C/C++), and for me the answer is very easy:  for someone who does not spent an appreciable amount of time writing code (in other words, needs to debug pretty much everything) there is nothing to beat an interpreted language.  With Python, your code is up and running in a flash, because the feedback is instantaneous.   

As to the environment, I am not familiar with all the distributions, having installed Canopy.  But I find that the native GUI, Idle, is just as good as Canopy and loads much faster.  The fancier distros tend to consume a fair amount of overhead.  Now it is true that with Canopy many of the most popular packages such as numpy and matplotlib are included, but downloading and installing them is trivially easy.  (Especially if you are on a linux platform; I mean, how hard is it to open a command window and type sudo apt-get install python-numpy?)

Finally, since I had already taken formal courses in C, C++ and FORTRAN I might have tried to learn Python from any of the myriad of tutorials out there, but after looking at the mountain of documentation, I concluded that a course would be the best and quickest way for me.  I surveyed the four or five Python courses offered on edX and Coursera, and came to the conclusion that the MIT 6.00.1x and 6.00.2x courses were right for me.  I recommend them heartily to anyone who has prior programming experience.  They are advertized as no prior knowledge required, but if you have not already been introduced to object oriented programming the learning curve will be very steep!

Affiliation
Astronomical Society of South Australia (ASSAU)
Programming Language Tower of Babel

Hi Mike

The Programming Language Tower of Babel (Communications of the ACM, 1961) is even taller today and most of the languages we use now aren't even depicted on it. frown

It's an interesting question and one worth exploring in detail elsewhere, but here are a few specific thoughts with a focus on Python.

I think the popularity of Python (vs Perl, Ruby, or other scripting languages) can be accounted for in terms of the following:

  • Low barrier to entry. 
    • Its dynamically typed nature and non-verbose syntax help a lot. 
    • It encourages exploratory programming and a rapid edit/run cycle.
    • It's also often said that Python looks somewhat like pseudocode.
    • Despite the low entry cost, it's a powerful and capable language.
  • Availability of many high quality libraries across a range of domains from science to web development.
    • Especially where high performance science libraries are concerned, there is a dependency upon C/C++ libraries wrapped and invoked from Python, but that turns out to be a strength in this case.

Python is increasingly being taught in schools/universities where Scheme might once have been; that may account in part for its popularity.

There are things I don't like about Python (e.g. I prefer whitespace to have no meaning) and every other programming language I've ever used. It's also true that hype has accompanied many programming languages, and continues to do so.

For a software engineer, languages like C/C++ and Java are sometimes be more desirable. When a language needs to be learned or used, that's really just part of the professional's job description and is helped by a formal study of programming paradigms.

When I'm building a big system (more than a couple of 1000 of lines of code) I prefer as much static typing in a language as possible in order to eliminate as many classes of bugs as possible and also to provide tools (e.g. Integrated Development Environments) with richer hints. For small programs/scripts, this is far less important.

When I learned Perl it changed my view of what a programming language could be, but Perl has suffered in its maturity. However, I don't think much really differentiates Perl or Python from other similar dynamically typed scripting languages except the amount of tool, library, and other support. To be fair though, Perl has quirks that made it rather less appealing than Python and others but many of us loved it anyway.

Other languages such as R occupy a niche, statistics in R's case, but with the right set of libraries, Python is quite capable in that area also.

To sum up re: Python, it's a capable language with great tool and library support (both important for a non-trivial language) from the laptop to high performance compute clusters and I probably "think faster" in Python (once I would have said that of Perl) than most other languages except Java. I've used both for around the same amount of time (Java since 1996, Python since around 2000). At the end of the day, every programming language is just a tool of course.

In any given week, I code in some combination of Python, R, C++, Java, JavaScript, Fortran (depending upon project), and other languages. I enjoy thinking about one language's strengths over another. At work I recently gave a talk about R and one of its frameworks/libraries: Shiny. For some classes of data-centric applications I can write a small amount of code and achieve a great deal that would require an order of magnitude or two in another language/framework. The choice of such things can substantially affect productivity depending upon the scenario.

I the 90s I developed compilers and interpreters, including one for the Amiga and one for the Newton PDA. These are among the programming languages that were useful in a particular context over a certain time frame, but have largely fallen into disuse. Some of the "500+" you refer to would fall into this category. Others would be niche languages (even moreso than something like R, e.g. Matlab, Mathematica). Still others are confined to research or academia or idle curiosity. A small number emerge into the light.

Every so often, a disruptive language comes along. Java was one of those in 1996. Likewise for Perl several years earlier and C in the early 70s. Plenty of other examples exist. But for each "important" and long lived language, many come and go, and there's certainly a merit-based, survival of the fittest process at work. That's not to say that the "best" (by some criteria) survive, just those that are most "successful" (by some criteria).

I like what Bjarne Stroustrup, creator of C++, had to say in his book, The Design and Evolution of C++:

I consider it the obligation of scientists and intellectuals to ensure that their ideas are made accessible and thus useful to society instead of being mere playthings for specialists.

David

Affiliation
American Association of Variable Star Observers (AAVSO)
C is everywhere (more than Java ;)

[quote=David Benn]

Every so often, a disruptive language comes along. Java was one of those in 1996. Likewise for Perl several years earlier and C in the early 70s. Plenty of other examples exist. But for each "important" and long lived language, many come and go, and there's certainly a merit-based, survival of the fittest process at work. That's not to say that the "best" (by some criteria) survive, just those that are most "successful" (by some criteria).

I like what Bjarne Stroustrup, creator of C++, had to say in his book, The Design and Evolution of C++:

I consider it the obligation of scientists and intellectuals to ensure that their ideas are made accessible and thus useful to society instead of being mere playthings for specialists.

[/quote]

Well, Python (at least its most common implementation) is written in C ! As are device drivers, most operating systems and other high level scripting type languages. So, since C/C++ can do everything from low level device interfacing up to high level modular programming, I am happy I stuck with this one as my principal programming language ;)

And, probably too lazy to take the time to learn how to program in Python as efficiently as C. I would likely end up with cross-language confusion in my brain which would slow me down in both.

Mike

 

Affiliation
None
C is everywhere

While to be honest I agree with your choice of C/C++ as the preferred programming languages, I also agree with the assertion that knowing multiple languages is a benefit.  My own experience is that just as with spoken languages, being conversant in multiple programming languages deepens one's understanding of all of them, even if one occasionally erroneously interjects something from one into another.  That said, I cannot judge another person's motivation or capacity.

Another python tool that

Another python tool that might be of use for FITS is pyraf which gives you a python interface to IRAF. That may suit your FITS & WCS needs particularly if you are already familiar with IRAF.

pyephem (based on the c library libastro from xephem) can handle a lot of the basic coordinate transformations http://rhodesmill.org/pyephem/index.html

For the ra/dec >  alt/az you can do something like:

obs = ephem.Observer()
obs.lon = "-6.0"
obs.lat = "53.0"
obs.date = ephem.Date('2015/09/10 12:41:16)
obs.epoch = "2000"

t = ephem.FixedBody()
t._ra = "0:40:04.72"
t._dec = "18:47:48.7"

t.compute(obs)
# You then have t.az and t.alt to play with (though you may need to check the epoch!)

 

I use pyephem quite a bit for my tools. Mind you I'm not doing any FITS processing with it yet.

Others have mentioned anaconda. Another nice tool that comes with that is 'ipython notebook' which allows you to code in python in your browser and allow you to make changes to code without needing to rerun your entire script. Very useful for debugging/development.

 

Affiliation
American Association of Variable Star Observers (AAVSO)
pyephem

Thanks for this, Albert.  I did take a look at pyephem, and found it would very conveniently solve my immediate problem, but it seemed limited in scope for some of my future problems.  So, I went with astropy.

My immediate problem was to compute how much to offset the azimuth of my dome, from the "regular" alt-az of a star, to allow for the fact that my scope is on a rather large German equatorial mount, so the OTA can be quite a ways from the centre of the dome.  A solid afternoon staring at the mount, some high-school trig, and some astropy calls, and I seem to have what I need.  Next step is to use pyserial to get the code to send the info to the dome controller directly, instead of me reading the adjusted az output by my python code, and typing it in to a terminal that talks to the dome controller.  One step at a time!

Cheers,  Gary Billings

 

 

 

 

 

Thats interesting to know.

Thats interesting to know. I'll look into astropy if I ever get around to processing images in python. I'm a long way from that at the moment - I'm still getting to grips with the basics of variable star observing! Astrometry was so much easier :)

pyserial is pretty easy from what I've done to interface with an arduino. Well, provided you just need the basics to connect to a serial port, and send and recieve lines of data.

Good luck with your project!

Affiliation
American Association of Variable Star Observers (AAVSO)
Aligning dome slit with equatorial telescope

Hi Gary,

years ago I found really nice webpage that covers coordinate transformations and one example is given as dome slit synchronization with (equatorial) telescope. If you havent seen that, maybe it is interesting to read:

http://www.geocities.jp/toshimi_taki/matrix/matrix.htm

Maybe a successor of this could be rich information and history given in http://www.dppobservatory.net/DomeAutomation/dome_synchronisation.pdf I personally find the first approach to be mathematically beautyful :-) So I thought, that with Python and Numpy, you may like to use matrix formalism...

Best wishes,
Tõnis

Affiliation
American Association of Variable Star Observers (AAVSO)
INDI Forums

Folks that have an interest in the technical aspects of observatory, CCD, mount, etc control might be interested in the INDI Open Astronomy Instrumentation Forums. 

http://indilib.org/forum/general.html

It includes the development and relaese of an observatory control program and library that runs on a Raspberry.

On another topic, the MIT 600.1x course on EdX looks really good.  Typical high quality MIT professor.  Starts slowly and gains steam rapidly.  Thanks for whoever posted the info about it.

Jim Jones

 

 

Affiliation
American Association of Variable Star Observers (AAVSO)
Arduino at its limits

Hi Jim, Thanks for your post/link re INDI on Raspberry Pi.  I'm using the Arduino Uno right now, and it is maxed out on available memory and processor speed.  It is monitoring an azimuth encoder and reference sensor, doing SoftSerial i/o to a user terminal (which will be replaced by a computer-hosted control program in the future), i2c output to an in-the-dome status display, and control of direction and motor "relays".  In the future, as I add functionality, I'll need to use a more capable Arudino-type board, or something like the Pi.  So, note to others:  start out with something a bit beefier!

Gary Billings

 

Affiliation
American Association of Variable Star Observers (AAVSO)
Raspberry

Hi Gary

You are way ahead of me with your Arcuino.  I haven't tried to apply the Raspberry to any control problems yet.  Hopefully this winter.

Interestingly, the Raspberry comes with Python and surprisingly a free copy of Mathematica 10.  Free is a very good price for Mathematica.  And of course Raspberry PI 2 will run Windows 10 (also free).  I haven't used Python on the Raspberry but have made use of Mathematica quite a bit.  Not bad for a $35 computer the size of a credit card.

Jim Jones

Affiliation
None
Microcontroller vs Raspberry

Just wondering if you had given any thought to using a microcontroller instead of the Raspberry pi.  One can buy a Launchpad from TI that plugs into a USB port and is programmable in C using the free Code Composer Studio software (among other free IDEs).  There is a large variety of TI microcontrollers, and the cost for a complete package starts at under $5.  Of course, if you insist on having a separate power supply, that will cost you a couple of bucks more.