cmutils module

Pre-/Post-Processing of contMech Data

Miscellaneous functions for input, output, manipulation and plotting of contMech config files. Very powerful in combinations with numpy’s and scipy’s standard manipulation and statistics tools.

Examples

Generate, view, export, import and rotate a contMech config file:

x,y = cm.XY((64,64), Lx=2., Ly=2.)
z = x**2 - y**2
cm.plotImg(z)
cm.dumpConfig(z, 'konfig.dat')
z2 = cm.rot90(cm.readConfig('konfig.dat'))
cm.plotImg(z2)

API documentation

cmutils.Ra(array)[source]

Calculate the arithmetic average height Ra.

cmutils.Rt(array, D=1)[source]

Calculate the peak-to-valley height Rt.

cmutils.Rz(array, N=5, D=1, norm='deprecated')[source]

Calculate the average maximum height Rz.

cmutils.XY(shape, Lx=None, Ly=None, **kwargs)[source]
cmutils.bilin_resample(array, new_shape)[source]

resample / change array resolution through bilinear interpolation

Parameters:
  • array (np.ndarray) – array of shape (nx, ny) to be resampled into shape new_shape

  • new_shape (iterable containing two int values) – new target resolution (nx_new, ny_new)

Returns:

result (np.ndarray) – resampled version of array

cmutils.center(array)[source]
cmutils.circularMask(w, h, center=None, rMax=None, rMin=None)[source]
cmutils.config2img(array)[source]
cmutils.contStats(filepath='', nbin=100)[source]
cmutils.corners(array, N=None)[source]

meeting point of array’s corners

constructs the 2 N x 2 N area around the point where the four corners meet if np.ndarray array is periodically repeated in the plane.

Returns:

np.ndarray

cmutils.createBorder(array, nxNew, nyNew)[source]

create border around surface

generates a (nxNew, nyNew) numpy.ndarray that contains the (nx, ny) numpy.ndarray array. nxNew must be larger than nx and nyNew larger than ny.

Returns:

the new numpy.ndarray

cmutils.curvCos1(sigCosine)[source]
cmutils.curvCos2(sigCosine)[source]
cmutils.curvEla(dy)[source]
cmutils.curvEla2D(dx, dy)[source]
cmutils.curvMorse1(sigMorse)[source]
cmutils.curvMorse2(sigMorse)[source]
cmutils.dumpConfig(arrays, filepath='konfig0py.real', Lx=None, Ly=None)[source]

dump arrays as contMech konfig

writes list arrays of numpy.ndarrays to a file in the typical format that can be plotted in gnuplot or imported into a contMech simulation.

cmutils.dumpImg(array, filename)[source]

dump array as image text file

writes 2D (nx, ny) numpy.ndarray array to a text file with ny values per line and nx lines.

cmutils.dumpLines(array, lines, filename='', dim=0)[source]

Dump line scan(s)

exports individual lines along dimension dim from 2D numpy.ndarray array into filename. lines can be an int or a list/tuple of ints with desired line indices to be exported.

cmutils.flatPunch(nx, ny, r, h)[source]
cmutils.flip(array, axis=1, direction=-1)[source]
cmutils.flipMulti(arrays, axis=0)[source]

flips displacements and pressures by rotation around a lateral axis

assumes that arrays is a tuple containing uz, pz(, uy, py, ux, px) in this order. Returns the tuple manipulated as if the simulation was rotated by 180° around the x-axis (axis = 0) or y-axis (axis = 1).

Parameters:
  • arrays (tuple/list of np.ndarrays) – assuming z,y,x ordering, displacement and pressure

  • axis (int (0,1)) – axis, around which the system is rotated. 0: x-axis, 1: y-axis.

Returns:

arrays as represented in the flipped coordinate system

cmutils.img2config(array)[source]
cmutils.int1D(filename, xcol=0, ycol=1)[source]
cmutils.logsmooth(infilename, outfilename='', nbin=100, usecols=(0, 1))[source]
cmutils.plotImg(array, clim='auto', title=False, aspect='true', cmap='turbo', alpha=1.0, axis=None, pad=0.01, cba=0.85, rot=True, figsize=(3.3, 3), **kwargs)[source]

plot array as an image

plots 2D numpy.ndarray array as a colored image, similar to gnuplot’s “splot … with pm3d”. clim must be of form (v_min,v_max), the value range to which colors are mapped.

Returns:

matplotlib.axes.Axes object containing the plot

cmutils.plotLines(array, lines, axis=None, dim=0, ls='-', figsize=None, **kwargs)[source]

plot line scan(s)

plots line(s) along dimension dim from numpy.ndarray array. lines can be an int or a list/tuple of ints with desired line indices. axis, the matplotlib.axes.Axes object on which to plot, can be specified. as well as typical kwargs for plots.

Returns:

matplotlib.axes.Axes object containing the plot

cmutils.plotPSD(array, axis=None, **kwargs)[source]

plot PSD of array

plots the PSD of numpy.ndarray array. axis, the matplotlib.axes.Axes object on which to plot, can be specified.

Returns:

matplotlib.axes.Axes object containing the plot

cmutils.plotSurf(array, kind='color', title=False, clim='auto', axis=None, figsize=None, stride=4, cmap='turbo', lw=0.5, lc='gray', aa=False, alpha=1, label=None, **kwargs)[source]

plot 3D view of surface

plots 2D numpy.ndarray array, using the data as z values, in a 3D view.

for kind = “color”, the color map cmap can be specified, as well as the color range clim in the form (v_min,v_max). stride specifies the discretization increment, lower being higher quality.

for kind = “wire”, the linewidth lw and linecolor lc can be specified.

Returns:

matplotlib.axes.Axes object containing the plot

cmutils.psd(array, output='')[source]

compute Power Spectral Density (PSD) of an array

calculates the PSD of numpy.ndarray array with auto quasi-log q space. writes the PSD to file output if given.

Returns:

numpy.ndarray with 2 columns containing q and C(q) values.

cmutils.psd1D(array, output='', dim=0)[source]
cmutils.readConfig(filepath, usecols=2)[source]

read contMech konfig file

reads column usecols from file filepath, and converts it to an (nx,ny) numpy.ndarray, assuming the first line in the file is of the form “#nx ny”

Returns:

numpy.ndarray

cmutils.readImg(filepath)[source]

read image file (without lateral info)

reads file filepath with ny numbers per line and nx lines into a (nx, ny) np.ndarray.

Returns:

np.ndarray

cmutils.readMulti(filepath, usecols=None)[source]

read contMech konfig file

reads column usecols from file filepath, and converts it to an (nx,ny) numpy.ndarray, assuming the first line in the file is of the form “#nx ny”

Returns:

numpy.ndarray

cmutils.reduce(array, resol)[source]

resample / change array resolution

resamples the (nx,ny) numpy.ndarray array to the new resolution resol. resol can be of form (nx_new, ny_new) or a single int, which is translated to resol = (round(nx/resol), round(ny/resol)).

Returns:

resampled numpy.ndarray

cmutils.resample(array, resol)[source]

resample / change array resolution

Parameters:
  • array (np.ndarray with shape (nx, ny)) – array to be resampled

  • resol (float or 2-tuple/list of int) – either the new target resolution (nx_new, ny_new) or a single float representing the new resolution relative to the original according to (nx_new, ny_new) = (round(resol * nx), round(resol * ny)).

Returns:

result (np.ndarray) – resampled version of array

cmutils.rot180(array)[source]
cmutils.rot180Multi(arrays)[source]
cmutils.rot270(array)[source]
cmutils.rot270Multi(arrays)[source]
cmutils.rot90(array)[source]
cmutils.rot90Multi(arrays)[source]
cmutils.show()[source]
cmutils.sigCos1(dy, uRho)[source]
cmutils.sigCos2(dy, uRho)[source]
cmutils.sigMorse1(dy, uRho)[source]
cmutils.sigMorse2(dy, uRho)[source]
cmutils.slopeX(array, periodic=True)[source]
cmutils.slopeY(array, periodic=True)[source]
cmutils.smoothPBC(array, overlapX=None, overlapY=None)[source]

make non-periodic image smooth at periodic boundaries

Parameters:
  • array (np.ndarray) – array whose edges are manipulated

  • overlapX (int or None) – all points within overlapX from the edges of array along axis 0 are gradually approach the value of the opposite edge.

  • overlapY (int or None) – all points within overlapY from the edges of array along axis 1 are gradually approach the value of the opposite edge.

Returns:

np.ndarray

cmutils.uCos1(dy, sigCosine)[source]
cmutils.uCos2(dy, sigCosine)[source]
cmutils.uMorse1(dy, sigMorse)[source]
cmutils.uMorse2(dy, sigMorse)[source]
cmutils.untilt(array, avg=None)[source]

subtract the tilt from a 2D surface

subtract the macroscopic tilt from np.ndarray array, assuming it to be linear in both directions.

Returns:

the untilted numpy.ndarray