Numpy has the ability to mask arrays and ignore their values for certain computations, called “masked arrays”. They contain a .mask attribute which is a boolean array, True where the value should be masked and False otherwise.
Numpy also comes with a suite of functions which can handle this masking naturally. Typically for a function in the np. namespace, there is a masked-array-aware version under the np.ma. namespace:
np.median => np.
RTFM! Today I brought down our head node at work, because of a misunderstanding of command line arguments for a linux program.
In fairness, I should have read the man page more carefully for the entry in question!
I was using xargs for some nice command line parallelism and process running. The command I ran was:
ls | grep action119 | grep exposureCycle | xargs -n 1 -I {} find {} -name 'IMAGE*.
I spent some time trying to get timestamps added to C++ printing, e.g .through cout.
I naive approach is to write a function get_current_time() and put it before all printing statements e.g.:
cout << get_current_time() << "Message" << endl; This requires changing all logging statements. Then my googling stumbled upon this question which had an elegant solution incorporating a decorator object.
Further down the page however I came upon a much nicer solution that transcends languages and programs and can be applied to running shell commands.
pymysql Defaults to autocommit=False connection = pymysql.connect(user='user', db='test') cursor = connection.cursor() cursor.execute('insert into test (value) values (10)') connection.close() connection = pymysql.connect(user='user', db='test') cursor = connection.cursor() cursor.execute('select value from test') # => [] To commit changes to the database, #commit() must be called:
connection = pymysql.connect(user='user', db='test') cursor = connection.cursor() cursor.execute('insert into test (value) values (10)') # Call the commit line connection.commit() connection.close() connection = pymysql.connect(user='user', db='test') cursor = connection.
I used to have two simple shell aliases for IPython:
alias ipy=ipython alias pylab='ipython --pylab' These were separated for a couple of reasons:
The pylab mode of IPython was deprecated, for good reason. It “infects” the global namespace with all matplotlib and numpy functions. It breaks two entries in the famous “Zen of Python”:
Explicit is better than implicit. Namespaces are one honking great idea – let’s do more of those!
I see a lot of complaints about git submodules, people suggesting alternatives, complaints about merging or other bits and pieces.
Git submodules have their place. Yes they are not ideal for all situations but they are ideal for the typical use case I’m about to outline.
Example use case In my work I have a master project which contains multiple submodules. Each submodule is also cloned into a separate development repository sitting near by.
So git rebase is a powerful tool, able to change history itself. With this power however requires great care to avoid needing to git push --force.
Git rebase comes with very user friendly ways to cancel out of a rebase if something goes wrong or if you become confused:
git rebase --abort This returns your working tree back to the state before the rebase was started.
One thing that has always made me nervous when using rebase was when I rebased and a conflict occured, so I only kept changes from the HEAD commit which caused the following message:
For interpolation in python, scipy includes the interpolateackage containing (amongst other things) interp1d for simple interpolation.
The function does not however perform extrapolation; if the interpolator is asked for a value outside the original range it will raise an exception. To get around this, the interpolator contains a .x parameter which contains the original x values used to construct itself. A boolean index can then be used to reject inputoints which fall outside of this range:
For printing multiple images, it’s usually handy to tile images so that more than one page fits on a piece of paper at once. This can be achieved with ImageMagick and the montage command.
Building a tiled image For a 2x2 image an example command is:
montage -tile 2x2 -geometry 1600x1200 1.png 2.png 3.png 4.png output.png Be careful with the output specification, as if you forget it it’ll overwrite the last png in the list.
If you’re:
running Fedora using dnf as a package manager get the following error: Failed loading plugin: copr then listen up, your solution is at hand: install python-requests using yum:
sudo yum install -y python-requests I have not tested if this will work installing with pip yet. The linked bug report suggests that this will be registered as a dependency as of 2014-06-05 so this problem should go away.