My 404s Don't code and drive

About

Pip Downgrading or Upgrading a Package to a Version Range

I was upgrading all Python packages and it turned out that the Flake8 package used for linting my Python code needed an older version of the PyCodeStyle package. To fix this, instead of downgrading, one can use the range requirement for pip.

pip install 'pycodestyle<2.4.0,>=2.0.0'

That is, install a pycodestyle version lesser than version 2.4.0 but greater or equal to version 2.0.0. Pretty neat.

Useful Matlab Commands

There are number of useful commands that will help you keep sanity in Matlab.

If you want to run Matlab in terminal mode to avoid the very resource and memory taxing Java interface.

	matlab -nodesktop

Then if you want add all paths in the current and subfolders

addpath(genpath(pwd))

And maybe displays all available variables

	who

Sometimes the printing of information will not happen because resources are going somewhere else you can force the printing just like the flush command in C++. To flush the output you can do this, however, the command probably only is relevant for the Java interface.

drawnow(update);

Sometimes or most of the time you need to debug the code. You can then give Matlab the command to stop where the error occurred with all the variables in the scope available for inspection. It will make your life 100% easier if not 200%.

dbstop on error

Another useful debugging command is:

dbstop if nanif

It only looks for nan inf values that appears in your code and stops. Extremly useful for discovering spurios NaN and Inf values that comes up at unexpected places.

Once the script has stopped due to an error you can move up in the stack trace using,

dbup

and to continue running after an error has been raised you can just write,

dbcont

To turn of the error stopping functionality you can use this command,

dbclear all

Terminal Search For Non-Ascii Characters

Sometimes compilers of all sorts gives you trouble for non-ascii characters usually without no feedback as to where in the code they are located. One can then do a directory or file search for the pesky non-ascii characters. I found this discussion on Stack Overflow. What you do is a grep with regular expression that captures all non-ascii characters. To do this over for example your site directory like this,

grep --color='auto' -P -n '[^\x00-\x7F]' _site/*

or on OSX

pcregrep --color='auto' -n "[^\x00-\x7F]" _site/* 

Or you can just do a regular expression search for [^\x00-\x7F] in your favorite editor.

How To Easily Find The Clipping Parameters For A Figure In Latex

You are in Latex setting up a figure and you want only part of it. Then you can use the clipping parameters of the includegraphics command. Like this,

\begin{figure}
% l b r t
\centering
\includegraphics[width=1.\linewidth,clip,trim=280 225 180 190]{mypic.png}
\caption{Beautiful Picture.}
\end{figure}

where trim=280 225 180 190 is the clipping margins as [left,bottom,right,top] or [l,b,r,t].

This can be a tricky endeavor since we have to do trial and error to see how much we should remove to get it right. It is often difficult to see the difference between the figure background and document if they have the same background color. However, if we put a frame around the figure we can better estimate how much space is left to the margin. This can be done by using the package,

\usepackage[export]{adjustbox}

We can then do,

\begin{figure}
% l b r t
\centering
\includegraphics[width=1.\linewidth,clip,trim=280 225 180 190,frame]{mypic.png}
\caption{Beautiful Picture.}
\end{figure}

this gives a black frame around the image and we can see how much space is left. I have found this extremely useful for pdfs that are exported from Keynote since there is no clipping function in the program.

Using Python's Enumerate With Iterators That Returns Multiple Values

Lets say you are using an iterator that returns multiple values which you pass to function, like this,

results = [myFun(val1,val2,...) for val1,val2,... in myIterable]

Now, the function value you return might depend on where you are in the iteration so we would like to pass an index value to the function. In Python we can do this with enumerate, for example, if we are creating a new array by multiplying each value in an array with its position in the array,

result = [val*idx for idx,val in enumerate(myArray)]

To incorporate this into the iterable that returns multiple values we have to create a tuple,

results = [myFun(val1,val2,...,idx) for idx,(val1,val2,...) in enuemrate(myIterable)]

Notice that even though the val1, val2… is inside the tuple we can still refer to them as arguments for the function.