python
132 posts — page 9
-
ipython and PyTrilinos
start ipcontroller start ipengines: mpiexec -n 4 ipengine --mpi=pytrilinos start ipython 0.11: import PyTrilinos from IPython.kernel import client mec = client.MultiEngineClient() %loadext parallelmagic mec.activate() px import PyTrilinos px comm=PyTrilinos.Epetra.PyComm() px print(comm.NumProc())
-
memory map npy files
Mem-map the stored array, and then access the second row directly from disk: X = np.load('/tmp/123.npy', mmapmode='r')
-
force local install of python module
python setup.py install --prefix FOLDER creates lib/python2.6/site-packages, to force a local install you should use: python setup.py install --install-lib FOLDER
-
switch to interactive backend with ipython -pylab
objective: when running ipython without pylab or executing scripts you want to use an image matplotlib backend like Agg just when calling ipython -pylab you want to use an interactive backend like GTKAgg or TKAgg you need first to setup as default backend on .matplotlib/matplotlibrc Agg : backend : Agg then setup you i
-
numpy dtypes and fits keywords
bool: 'L', uint8: 'B', int16: 'I', int32: 'J', int64: 'K', float32: 'E', float64: 'D', complex64: 'C', complex128: 'M'
-
count hits with numpy
I have an array where I record hits a=np.zeros(5) and an array with the indices of the hits, for example I have 2 hits on index 2 hits=np.array([2,2]) so I want to increase index 2 of a by 2 I tried: a[hits]+=1 but it gives array([ 0., 0., 1., 0., 0.]) does someone have a suggestion?
-
change column name in a fits with pyfits
no way to change it manipulating the dtype of the data array. a=pyfits.open('filename.fits') a[1].header.update('TTYPE1','newname') you need to change the header, using the update method of the right TTYPE and then write again the fits file using a.writeto.
-
parallel computing the python way
Note: The original showmedo.com video tutorial site is no longer available. For parallel computing in Python, consider using multiprocessing, dask, or mpi4py as modern alternatives to MPI.
-
quaternions for python
Update: SciPy now includes scipy.spatial.transform.Rotation which provides excellent quaternion support. See the SciPy documentation.
-
using numpy dtype with loadtxt
Let's say you want to read a text file like this: filename start end fdsafda.fits 23143214 23143214 safdsafafds.fits 21423 23423432 you can use dtype to create a custom array, which is very flexible as you can work by row or columns with strings and floats in the same array: dt=np.dtype({'names':['filename','start','end'],'formats':['S100',np.float,np.float]}) [I tried also using np.str instead of S100 without success, anyone knows why?] then give this as input to loadtxt to load the file and create the array.
-
Stop ipcluster from a script
Ipcluster is easy to start but not trivial to stop from a script, after having finished the processing, here's the solution: from IPython.kernel import client mec = client.MultiEngineClient() mec.kill(controller=True)
-
load arrays from a text file with numpy
space separated text file with 5 arrays in columns: [sourcecode language="python"] ods,rings,gains,offsets,rparams = np.loadtxt(filename,unpack=True) [/sourcecode] quite impressive...