Readability of Scientific Python Code (Line Continuations, Variable Names,
Imports)
Do Python's stylistic best practices apply to scientific coding?
I am finding it difficult to keep scientific Python code readable.
For example, it is suggested to use meaningful names for variables and to
keep the namespace ordered by avoiding import *. Thus, e.g. :
import numpy as np
normbar = np.random.normal(mean, std, np.shape(foo))
But these suggestions can lead to some difficult-to-read code, especially
given the 79-character line width. For example, I just wrote the following
operation:
net["weights"][ix1][ix2] += lrate * (CD / nCases -
opts["weightcost_pretrain"].dot(net["weights"][ix1][ix2]))
I can span the expression across lines:
net["weights"][ix1][ix2] += lrate * (CD / nCases -
opts["weightcost_pretrain"].dot(net["weights"][ix1][ix2]))
but this does not seem much better, and I am not sure how deep to indent
the second line. These kinds of line continuations become even trickier
when one is double-indented into a nested loop, and there are only 50
characters available on a line.
Should I accept that scientific Python looks clunky, or are there ways to
avoid lines like the example above?
Some potential approaches:
using shorter variable names
using shorter dictionary key names
importing numpy functions directly and assigning them short names
defining helper functions for combinations of arithmetic operations
breaking operations into smaller pieces, and placing one on each line
Any wisdom is much appreciated.
No comments:
Post a Comment