Archive for Python

Ahead of Teaching

Posted in Biographical, Education, mathematics, Maynooth, Music with tags , , , , on February 3, 2019 by telescoper

It’s 3rd February 2019, which means that today is two days after Imbolc, a Gaelic festival marking the point halfway between the winter solstice and vernal equinox. This either happens 1st or 2nd February, and this year it was former, i.e. last Friday In Ireland this day is sometimes regarded as the first day of spring, as it is roughly the time when the first spring lambs are born. It corresponds to the Welsh Gŵyl Fair y Canhwyllau and is also known as the `Cross Quarter Day’ or (my favourite) `The Quickening of the Year’.

I wrote a post about this time last year, on the day I gave my first ever lecture in Maynooth University, on Computational Physics, in a theatre called Physics Hall. That was on Thursday February 1st 2018. It’s hard to believe that was a full year ago. Time certainly has gone quickly this year.

Owing to the vagaries of the academic calendar we’re a week later getting back to teaching this year than last year so my first Computational Physics lecture won’t be until this Thursday (7th February) at 9am, but sadly it won’t be in Physics Hall, which I rather liked, but in Hall C – a much less atmospheric venue, but one rather closer to my office, which will be handy if I forget anything (which I am prone to do). There are about 25 students taking this module, a few down on last year, which means they should fit comfortably into our computer lab. I’m not surprised they moved the lecture, really. The capacity of Physics Hall is 90, and even last year I only had about 30 students. Still, it did have a piano (which Hall C does not):

Computational Physics doesn’t start until Thursday. Before that I have to start my other module: Engineering Mathematics II. This (what you would probably call a `service course’) covers a mixture of things, mainly Linear Algebra but with some other bits thrown in for fun, such as Laplace transforms. Interestingly I find the Mathematical Physics students do not encounter Laplace Transforms in the first year, but perhaps engineers use them more often than physicists do? I think I’ve written only one paper that made use of a Laplace transform. Anyway, I have to start with this topic as the students need some knowledge of it for some other module they’re taking this semester. I reckon six lectures will be enough to give them what they need. That’s two weeks of lectures, there being three lectures a week for this module.

By coincidence rather than good planning, the timetable for this module is quite nice. I have lectures on Monday, Tuesday and Wednesday and then the students have a choice of tutorial (on either Thursday or Friday). That means I can get through a decent amount of material each week before each tutorial. I don’t do the tutorials, by the way: that’s left to one of our PhD students, who gets paid for doing that and correcting the weekly coursework. There are about 50 students on this module, divided into two courses: Electronic Engineering and Robotics and Intelligent Devices. We don’t have Civil or Mechanical or Chemical Engineering, etc at Maynooth.

Campus has been very quiet for the last week or so. The exam period finished in late January but lectures don’t start until tomorrow morning (Monday 4th February) so there have been few students around. No doubt it will be a different story tomorrow. I’ve done my first week’s notes and compiled my first problem set so I’m more-or-less ready to go. First lecture at 2pm tomorrow in Hall H, which is one of the rooms I taught in last term so at least I know where it is!

 

Advertisements

25 Years of Python!

Posted in History with tags , , on February 1, 2019 by telescoper

Not a lot of people know* that it is 25 years to the day since Guido van Rossum announced the release of Python 1.0.0:

The latest version of Python is 3.7.2.

It’s not quite correct to say that Python is 25 years old today, though. There were versions available before the official Version 1. For a full history see here.

*H/T to Tom Crick, whose tweet alerted me to this.

Other People’s Code

Posted in Education, mathematics with tags , , , on March 16, 2018 by telescoper

I don’t know if this is just me being useless, but one of the things I’ve always found difficult is debugging or rewriting computer programs written by other people. This is not a complaint about people who fail to document their code sufficiently to see what’s going on, it’s that even when the code is documented it seems much more difficult to spot errors in code written by other people than it is when you’ve written the program yourself.

I’ve been thinking a lot since I’ve been teaching Computational Physics here in Maynooth University. One of the standard elements of the assessment for this module is a task wherein the students are given a Python script intended to perform a given task (e.g. a numerical integral) but which contains a number of errors and asked to identify and correct the errors. This is actually a pretty tough challenge, though it is likely to be one that a graduate might have to meet if they get a job in any environment that involves programming.

Another context in which this arises is our twice-weekly computing laboratory sessions. Twice in the last couple of weeks I’ve been asked for a bit of help by students with code that wasn’t working, only to stare at the offending script for ages and fiddling with a number of things that made no difference, without seeing what turned out to be an obvious mistake. Last week it was an incorrect indent in Python (always a hazard if you’ve been brought up on Fortran). This week it was even simpler, a sign error in a line that was just supposed to calculate the mid-point of an interval. I should have been able to spot these very quickly, but I couldn’t.

What makes this so difficult? When given a mathematical calculation to mark I can usually spot errors reasonably easily (unless the working is illegible), but with code it’s different (at least for me). If I’d been given it on a piece of paper as part of a formula, I reckon I would have spotted that minus sign almost immediately.

One possibility is just that I’m getting old. While that may well be true, it doesn’t explain why I found debugging other people’s code difficult even when I was working on software at British Gas when I was 18. In that context I quite often gave up trying to edit and correct software, and instead just deleted it all and wrote my own version from scratch. That’s fine if the task is quite small, but not practicable for large suites written by teams of programmers.

I think one problem is that other people rarely approach a programming task exactly the same way as one would oneself. I have written programs myself to do the tasks given to students in the computing lab, and I’m always conscious of the method I’ve used. That may make it harder to follow what others have tried to do. Perhaps I’d be better off not prejudicing my mind doing the exercises myself?

Anyway, I’d be interested to know if anyone else has the same with other people’s code and if they have any tips that might improve my ability to deal with it. The comments box is at your disposal…

Learning Technology

Posted in Biographical, Cardiff, Education, Maynooth with tags , , , , , , , , on February 20, 2018 by telescoper

I’m just taking a tea break in the Data Innovation Research Institute. Today has been a very day as I have to finish off a lot of things by tomorrow, for reasons that I’ll make clear in my next post…

It struck me when I was putting on the brew how much more technology we use for teaching now than when I was a student. I think many of my colleagues make far more effective use of the available technology than I do, but I do my best to overcome my Luddite tendencies. Reflecting on today’s teaching makes me feel just a little less like a dinosaur.

This morning I gave a two-hour lecture on my Cardiff module Physics of the Early Universe which, as usual, I recorded using our Panopto system. Although there was a problem with some of the University’s central file storage this morning, which made me a bit nervous about whether the lecture recording would work, it did. Predictably I couldn’t access the network drives from the PC in the lecture theatre, but I had anticipated that and took everything I needed on a memory stick.

After a short break for lunch I checked the lecture recording and made it available for registered students via the Virtual Learning Environment (VLE), known to its friends as Learning Central. I use this as a sort of repository of stuff connected with the module: notes, list of textbooks, problem sets, model answers, instructions and, of course, recorded lectures. The students also submit their coursework assignment (an essay) through this system, through the plagiarism detection software Turnitin.

This afternoon the students on my Computational Physics course in Maynooth University had a lab test, the first of four such tests, this one consisting of a short coding exercise. There are two lab sessions per week for this class, one on Thursdays (when I am normally in Maynooth to help supervise) and another on Tuesdays (when I am normally in Cardiff). I have a number of exercises, which are similar in scope but different in detail (to prevent copying) and the Tuesday lab has a completely different set of exercises from the Thursday one. In each exercise the students have to write a simple Python script to plot graphs of a function and its derivative (computed numerically) using matplotlib. The students upload their script and pictures of the plot to the VLE used in Maynooth, which is called Moodle.

In the manner of a TV chef announcing `here’s one I did earlier’, this a sample output produced by my `model’ code:

I wonder if you can guess of what function this is the derivative? By the way in this context `model’ does not mean `a standard of excellence’ but `an imitation of something’ (me being an imitation of a computational physicist). Anyway, students will get marks for producing plots that look right, but also for presenting a nice (commented!) bit of code

This afternoon I’m on Cardiff time but I was able to keep an eye on the submissions coming in to Moodle in case something went wrong. It seemed to work out OK, but the main problem now is that I’ve got 20-odd bits of code to mark! That will have to wait until I’m properly on Maynooth time!

Now, back to the grind…

The Quickening of the Year

Posted in Education, Maynooth, Music, The Universe and Stuff with tags , , , , on February 1, 2018 by telescoper

It’s 1st February 2018, which means that today is Imbolc, a Gaelic festival marking the point halfway between the winter solstice and vernal equinox. This either happens 1st or 2nd February, and this year it is the former. In this part of the world – I’m in Ireland as I write- this day is sometimes regarded as the first day of spring, as it is roughly the time when the first spring lambs are born. It corresponds to the Welsh Gŵyl Fair y Canhwyllau and is also known as the `Cross Quarter Day’ or (my favourite) `The Quickening of the Year’.

So, talking of quickening, the pace of things is increasing for me now too. This morning at 9am I gave my first ever lecture in Maynooth University in a lecture theatre called Physics Hall, which is in the old (South) part of campus as opposed to the newer North Campus where the Science Building that contains my office is situated.

After that it was back to the Department for some frantic behind-the-scenes activity setting up accounts for the students for the afternoon lab session, which is in a computer room near to my office. Students attend one two-hour lab session in addition to the lecture, on either Thursday or Tuesday. The first lecture being this morning (Thursday) the first lab session was this afternoon, with the same material being covered next Tuesday.

I was far more nervous about this afternoon’s lab session than I was about this morning’s lecture as there seemed to be many things that could go wrong in getting the students up and running on our Linux cluster and getting them started on Python. Quite a few things did go wrong, in fact, but they were fewer in number and less drastic in outcome that I had feared.

So there we are, my first full day teaching in Maynooth. I think it went reasonably well and it was certainly nice to meet my first group of Maynooth students who, being physics students, are definitely la crème de la crème. I’ve got another 6 weeks like this (teaching on Tuesday in Cardiff and on Thursday in Maynooth) before the Easter break so it’s going to be a hectic period. Just for tonight, however, I’ve got time to relax with a glass or several of wine.

Incidentally, I was impressed that Physics Hall (where I did this morning’s lecture) is equipped with an electric piano:

I wonder if anyone can suggest appropriate musical numbers to perform for a class of computational physicists? Suggestions are hereby invited via the Comments Box!

A Python Toolkit for Cosmology

Posted in The Universe and Stuff with tags , , , , on December 14, 2017 by telescoper

The programming language Python has established itself as the industry standard for researchers in physics and astronomy (as well as the many other fields, including most of those covered by the Data Innovation Research Institute which employs me part-time). It has also become the standard vehicle for teaching coding skills to undergraduates in many disciplines. In fact it looks like the first module I will be teaching in Maynooth next term is in Computational Physics, and that will be delivered using Python too. It’s been a while since I last did any significant hands-on programming, so this will provide me with a good refresher. The best way to learn something well is to have to teach it to others!

But I digress. This morning I noticed a paper by Benedikt Diemer on the arXiv with the title COLOSSUS: A python toolkit for cosmology, large-scale structure, and dark matter halos. Here is the abstract:

This paper introduces Colossus, a public, open-source python package for calculations related to cosmology, the large-scale structure of matter in the universe, and the properties of dark matter halos. The code is designed to be fast and easy to use, with a coherent, well-documented user interface. The cosmology module implements FLRW cosmologies including curvature, relativistic species, and different dark energy equations of state, and provides fast computations of the linear matter power spectrum, variance, and correlation function. The large-scale structure module is concerned with the properties of peaks in Gaussian random fields and halos in a statistical sense, including their peak height, peak curvature, halo bias, and mass function. The halo module deals with spherical overdensity radii and masses, density profiles, concentration, and the splashback radius. To facilitate the rapid exploration of these quantities, Colossus implements about 40 different fitting functions from the literature. I discuss the core routines in detail, with a particular emphasis on their accuracy. Colossus is available at bitbucket.org/bdiemer/colossus.

The software can be downloaded here. It looks a very useful package that includes code to calculate many of the bits and pieces used by cosmologists working on the theory of large-scale structure and galaxy evolution. It is also, I hope, an example of a trend towards greater use of open-source software, for which I congratulate the author! I think this is an important part of the campaign to create truly open science, as I blogged about here.

An important aspect of the way science works is that when a given individual or group publishes a result, it should be possible for others to reproduce it (or not, as the case may be). At present, this can’t always be done. In my own field of astrophysics/cosmology, for example, results in traditional scientific papers are often based on very complicated analyses of large data sets. This is increasingly the case in other fields too. A basic problem obviously arises when data are not made public. Fortunately in astrophysics these days researchers are pretty good at sharing their data, although this hasn’t always been the case.

However, even allowing open access to data doesn’t always solve the reproducibility problem. Often extensive numerical codes are needed to process the measurements and extract meaningful output. Without access to these pipeline codes it is impossible for a third party to check the path from input to output without writing their own version assuming that there is sufficient information to do that in the first place. That researchers should publish their software as well as their results is quite a controversial suggestion, but I think it’s the best practice for science. There isn’t a uniform policy in astrophysics and cosmology, but I sense that quite a few people out there agree with me. Cosmological numerical simulations, for example, can be performed by anyone with a sufficiently big computer using GADGET the source codes of which are freely available. Likewise, for CMB analysis, there is the excellent CAMB code, which can be downloaded at will; this is in a long tradition of openly available numerical codes, including CMBFAST and HealPix.

I suspect some researchers might be reluctant to share the codes they have written because they feel they won’t get sufficient credit for work done using them. I don’t think this is true, as researchers are generally very appreciative of such openness and publications describing the corresponding codes are generously cited. In any case I don’t think it’s appropriate to withhold such programs from the wider community, which prevents them being either scrutinized or extended as well as being used to further scientific research. In other words excessively proprietorial attitudes to data analysis software are detrimental to the spirit of open science.

Anyway, my views aren’t guaranteed to be representative of the community, so I’d like to ask for a quick show of hands via a poll…

…and you are of course welcome to comment via the usual box.

Straw Poll on Statistical Computing

Posted in Bad Statistics, The Universe and Stuff with tags , , on December 20, 2016 by telescoper

The abstract of my previous (reblogged) post claims that R is “the premier language of statistical computing”. That may be true for the wider world of statistics, and I like R very much, but in my experience astronomers and cosmologists are much more likely to do their coding in Python.  It’s certainly the case that astronomers and physicists are much more likely to be taught Python than R. There may well even be some oldies out there still using other languages like Fortran, or perhaps  relying on books of statistical tables!

Out of interest therefore I’ve decided to run the following totally biased and statistically meaningless poll of my immense readership:

 

If you choose “something else”, please let me know through the comments box what your alternative is. I can then add additional options.