March 9th, 2015 - neuro-code-review/meetings GitHub Wiki
Maxwell presented Python code for running graph community analysis on fMRI data.
Some issues/ideas that were brought up:
-
When working with NiBabel, keep in mind that
get_data()
will load data asint16
unless there is a scaling factor in the NIfTI header. As such, it's good to get in the habit of adding.astype('float64')
(orfloat32
if memory usage is a concern). This will help avoid things like arithmetic overflows. -
Docstrings should follow the NumPy/SciPy conventions as much as possible.
-
The
glob
module is very useful when working with unix file paths. -
Using a linter plugin can help you catch errors as you code. Chris suggested Pyflakes, which is available as a plugin for most editors.
-
Here's a guide to "Transforming Code into Beautiful, Idiomatic Python"
-
Be mindful of changes coming in Python 3.
-
Values that do not change (constants) should be defined at the top of your code and be written in
ALL_CAPS
. Where possible, include a comment explaining what these values are and where they come from.
# Frequencies contributing to functional connectivity
# (Cordes et. al. 2001, ANJR)
BANDPASS = (0.01, 0.1)
-
When naming iteration variables,
i
should only be used when the object is an index, and even then, a name likei_scan
can often be more informative. -
In Python,
is
and==
are not interchangeable. -
Using
continue
can help avoid deeply nested loops that make code difficult to read.