Python equivalent of interp2

I wrote python version of interp2(z, xi, yi,’linear’) from matlab https://github.com/serge-m/pyinterp2

September 16, 2014 · SergeM

Simple occlusion filling for depth maps

As an example I use images from middleburry. Solution is very dirty and slow. [https://github.com/serge-m/depth_map_occlusion](https://github.com/serge-m/depth_map_occlusion" target="_blank) # In[1]: import numpy import scipy import matplotlib.pyplot as plt In[2]: from scipy import ndimage import numpy as np kernels for shift k = np.array([ [[0,0,0], [0,0,1], [0,0,0],], [[0,1,0], [0,0,0], [0,0,0],], [[0,0,0], [1,0,0], [0,0,0],], [[0,0,0], [0,0,0], [0,1,0],], [[1,0,0], [0,0,0], [0,0,0],], [[0,0,1], [0,0,0], [0,0,0],], [[0,0,0], [0,0,0], [0,0,1],], [[0,0,0], [0,0,0], [1,0,0],], ]) In[8]: from scipy import misc...

September 1, 2014 · SergeM

Use Matlab function from python

There are several solutions: Source: http://stackoverflow.com/questions/2883189/calling-matlab-functions-from-python pymatA low level interface to Matlab using the matlab engine (libeng) for communication (basically a library that comes with matlab). The module has to be compiled and linked with libeng. [http://pymat.sourceforge.net](http://pymat.sourceforge.net/" rel="nofollow) Last updated: 2003 pymat2A somewhat short lived continuation of the pymat development. Seems to work on windows (including 64bit), linux and mac (with some changes). [https://code.google.com/p/pymat2/](https://code.google.com/p/pymat2/" rel="nofollow) Last updated: 2012 mlabwrapA high level interface that also comes as a module which needs compilation and linking against libeng....

August 31, 2014 · SergeM

displaying multiple grayscale figures in python's matplotlib

from matplotlib import pyplot as plt import matplotlib.cm as cm plt.figure() # without this it display one after another plt.imshow(image_one, cmap=cm.gray) # without cm.gray it displays grayscale images in colormap plt.imshow(image_two, cmap=cm.gray) # plt.show() use %matplotlib inline in ipython notebook to display image inplace Shorter version: %matplotlib inline import matplotlib.pyplot as plt plt.axis(‘off’) plt.imshow(dpt, cmap=plt.cm.gray, interpolation=‘nearest’)

June 6, 2014 · SM!

Import Sihlouette roto masks into several Nuke shapes

Today kids, I tell you how to split imported Sihlouette shapes into Nuke RotoPaint nodes. In SihlouetteFX select several nodes. Go File->Export->nuke 6.2+ Shapes and save Nuke project I tried to use also File->Export->Nuke Shapes but in Nuke 7.0 it wasn’t opened properly Open saved project in Nuke. There is a single RotoPaint node. It contains several layers with several shapes in each. I need separate layers into many nodes to use them separately....

December 10, 2013 · SergeM

Add backslash before all escape characters in python

Useful for using in regular expressions: import re re.escape( str ) Python detects all escape characters that can be used by regex  and add slash before them [http://docs.python.org/2/library/re.html#re.escape](http://docs.python.org/2/library/re.html#re.escape)

November 12, 2013 · SergeM

Convert all *.avi files in current directory to png sequences

Using python and ffmpeg: 1 2 3 4 5 6 7 8 9 10 11 12 13 #!/usr/bin/python import glob import os t=glob.glob("*.avi" ) # search all AVI files for v in t: vv = os.path.splitext(v)[0]; os.makedirs( vv ) # make a directory for each input file pathDst = os.path.join( vv, "%05d.png" ) # deststination path os.system("ffmpeg -i {0} {1}".format( v, pathDst ) )

October 22, 2013 · SergeM

Torch-Lightning library (draft)

How to visualize gradients with torch-lightning and tensorboard in your model class define a optimizer_step. class Model(pl.LightningModule): # ... def optimizer_step( self, epoch: int, batch_idx: int, optimizer, optimizer_idx: int, second_order_closure = None, ) -> None: if self.trainer.use_tpu and XLA_AVAILABLE: xm.optimizer_step(optimizer) elif isinstance(optimizer, torch.optim.LBFGS): optimizer.step(second_order_closure) else: optimizer.step() #### Gradient reporting start ### if batch_idx % 500 == 0: for tag, param in self.model.named_parameters(): self.logger.experiment.add_histogram('{}_grad'.format(tag), param.grad.cpu().detach()) #### Gradient reporting end ### # clear gradients optimizer....

April 29, 2000 · SergeM

Conda cheat sheet

I usually prefer to keep (ana)conda deactivate in my system by default. One can chose such and option during conda installation. In order to activate base conda environment I run: source ~/anaconda3/etc/profile.d/conda.sh or source ~/miniconda3/etc/profile.d/conda.sh

December 7, 1020 · SergeM

Mixin pattern in Python

Design pattern Mixin is often used in python. There are two main situations where mixins are used 1: You want to provide a lot of optional features for a class. You want to use one particular feature in a lot of different classes. Order of mixins definition Order in which you use mixins defines the behaviour. Quote from 2: in Python the class hierarchy is defined right to left, so in this case the Mixin2 class is the base class, extended by Mixin1 and finally by BaseClass....

SergeM