pyaibox.utils package
Submodules
pyaibox.utils.colormaps module
pyaibox.utils.colors module
- pyaibox.utils.colors.gray2rgb(gray, cmap, drange=[0, 255], fmtstr=False)
Converts gray image values to rgb image
converts gray image values to rgb image according to the specified colormap string ‘cmap’, supported are:
'parula'
,'jet'
,'hsv'
,'hot'
,'cool'
,'spring'
,'summer'
,'autumn'
,'winter'
,'gray'
,'bone'
,'copper'
,'pink'
,'lines'
,'colorcube'
,'prism'
,'flag'
,'white'
.- Parameters
gray (numpy array) – The gray image.
cmap (str or colormap) – colormap string
drange (list or tuple) – The low and high value, default is
[0, 255]
fmtstr (bool or str) – Specifies the format (
'int'
,'uint8'
,'uint16'
,'uint32'
,'float16'
,'float32'
, ,'float64'
) for the output rgb matrix. (default isFalse
, don’t change)
Examples
import pyaibox as pl import matplotlib.pyplot as plt import numpy as np cmap = 'jet' # cmap = 'hsv' # cmap = 'hot' # cmap = 'parula' gray = pb.imread('../../data/images/LenaGRAY256.png') print(gray.shape) rgb = gray2rgb(gray, cmap, [0, 1], False) # rgb --> double, [0, 1] # rgb = gray2rgb(gray, cmap, [0, 255], False) # rgb --> double, [0., 255.] # rgb = gray2rgb(gray, cmap, [0, 255], True) # rgb --> uint8, [0, 255] print(gray.shape, np.min(gray), np.max(gray), gray.dtype) print(rgb.shape, np.min(rgb), np.max(rgb), rgb.dtype) plt.figure() plt.subplot(121) plt.imshow(gray, cmap=pb.parula if cmap == 'parula' else cmap) plt.subplot(122) plt.imshow(rgb) plt.show()
- pyaibox.utils.colors.rgb2gray(rgb, fmt='chnllast')
Converts RGB image to GRAY image
Converts RGB image to GRAY image according to
\[G = 0.2989R + 0.5870G + 0.1140B \]see matlab’s
rgb2gray
function for details.- Parameters
rgb (numpy array) – Original RGB tensor.
fmt (str, optional) – Specifies the position of channels in
rgb
tensor, surpported are: -'chnllast'
(default) -'chnlfirst'
pyaibox.utils.const module
pyaibox.utils.convert module
- pyaibox.utils.convert.bstr2int(b, endian='<', signed=True)
convert binary string data to integer
- Parameters
- Returns
The integer in decimal.
- Return type
Examples
n = -123 bs = int2bstr(n, 4, '<', signed=True) print(bs) print(hex(n)) print(bstr2int(bs, '<')) bs = int2bstr(n, 4, '>', signed=True) print(bs) print(hex(n)) print(bstr2int(bs, '>')) # ---output b'\x85\xff\xff\xff' -0x7b -123 b'\xff\xff\xff\x85' -0x7b -123
- pyaibox.utils.convert.dict2str(ddict, indent=' ', linebreak='\n', nindent=0)
dump dict object to str
- Parameters
- Returns
The converted string.
- Return type
- pyaibox.utils.convert.file2hash(file, hmode='sha256', tohex=True)
convert contents of a file to hash code
- Parameters
- Returns
hash code hex string or hash object
- Return type
- pyaibox.utils.convert.int2bstr(n, nbytes, endian='<', signed=True)
converts integer to bytes string
- Parameters
- Returns
The integer in binary string format.
- Return type
bstr
Examples
n = -123 bs = int2bstr(n, 4, '<', signed=True) print(bs) print(hex(n)) print(bstr2int(bs, '<')) bs = int2bstr(n, 4, '>', signed=True) print(bs) print(hex(n)) print(bstr2int(bs, '>')) # ---output b'\x85\xff\xff\xff' -0x7b -123 b'\xff\xff\xff\x85' -0x7b -123
- pyaibox.utils.convert.str2hash(s, hmode='sha256', enc='utf-8', tohex=True)
convert a string to hash code
- Parameters
s (str) – the input.
hmode (str or hash function, optional) –
- must either be a hash algorithm name as a str, a hash constructor, or a callable that returns a hash object.
'sha1'
,'sha224'
,'sha256'
,'sha384'
,'sha512'
,'md5'
, …,
see hashlib , by default ‘sha256’
enc (str, optional) – encoding type, by default ‘utf-8’.
- Returns
hash code hex string or hash object.
- Return type
- pyaibox.utils.convert.str2list(s)
Converts string with
[
and]
to list- Parameters
s (str) – The string.
Examples
s = '[0, [[[[1], 2.], 33], 4], [5, [6, 2.E-3]], 7, [8]], 1e-3' print(str2list(s)) # ---output ([0, [[[[1], 2.0], 33], 4], [5, [6, 0.002]], 7, [8]], 0.001)
- pyaibox.utils.convert.str2num(s, tfunc=None)
Extracts numbers in a string.
- Parameters
s (str) – The string.
tfunc (None, optional) – formating function, such as
int
,float
or'auto'
.
- Returns
The number list.
- Return type
Examples
print(str2num(s, int)) print(str2num(s, float)) print(str2num(s, 'auto')) print(2**(str2num('int8', int)[0])) print(str2num('int', int) == []) # ---output [0, 1, 2, 33, 4, 5, 6, 0, 7, 8, 0] [0.0, 1.0, 2.0, 33.0, 4.0, 5.0, 6.0, 0.002, 7.0, 8.0, 0.001] [0, 1, 2.0, 33, 4, 5, 6, 0.002, 7, 8, 0.001] 256 True
- pyaibox.utils.convert.str2sec(x, sep=':')
Extracts second in a time string.
hh:mm:ss
–>hh*3600 + mm*60 + ss
- Parameters
- Returns
y – The seconds.
- Return type
Examples
print(str2sec('1:00:0')) print(str2sec('1:10:0')) print(str2sec('1:10:6')) print(str2sec('1:10:30')) # ---output 3600 4200 4206 4230
pyaibox.utils.docstr module
- pyaibox.utils.docstr.gpyi(pkgdir, autoskip=True)
generates
pyi
files
pyaibox.utils.file module
- pyaibox.utils.file.copyfile(srcfile, dstfile)
copy file
copy file from srcfile to dstfile.
- pyaibox.utils.file.copyfiles(srcdir, dstdir, filenames)
copy files
copy files from source directory to destnation directory
- pyaibox.utils.file.data_path(dsname=None)
obtain data source path
- pyaibox.utils.file.fileparts(file)
Filename parts
Returns the path, file name, and file name extension for the specified
file
. Thefile
input is the name of a file or folder, and can include a path and file name extension.- Parameters
file (str) – The name string of a file or folder.
- Returns
filepath (str) – The path string of the file or folder.
name (str) – The name string of the file or folder.
ext (str) – The extension string of the file.
- pyaibox.utils.file.listxfile(listdir=None, exts=None, recursive=False)
List the files in a directory.
- Parameters
listdir (None, optional) – The directory for listing. The default is None, which means return
filelist
directly.exts (str, list or None, optional) – File extension string or extension string list, such as
'.png'
or['.png', 'jpg']
. The default is None, which means any extension.recursive (bool, optional) – Recursive search? The default is False, which means only list the root directory (
listdir
).
- Returns
The list of file path with extension
exts
. Sometimes you need to sort the list usingsorted
.- Return type
- pyaibox.utils.file.pathjoin(*kwargs)
Joint strings to a path.
- Parameters
*kwargs – strings.
- Returns
The joined path string.
- Return type
- pyaibox.utils.file.pkg_path()
obtain this package’s path
- Returns
package path string.
- Return type
- pyaibox.utils.file.readcsv(filepath, sep=None, vfn=None, nlines=None)
Read a csv file and extract numbers in it.
- Parameters
filepath (str) – The path string of the file.
sep (str, optional) – The separation character. Such as
','
or' '
. If None (default) or''
(empty) return a list of all the lines.vfn (function or None, optional) – The function for formating the numbers.
float
–> convert to float number;int
–> convert to integer number…, The default isNone
, which means won’t converted, string format.nlines (None, optional) – The number of lines for reading, the default is None, which means all the lines.
- Returns
The list of numbers or strings.
- Return type
- pyaibox.utils.file.readnum(filepath, pmain='Train', psub='loss: ', vfn=<class 'float'>, nshots=None)
Read a file and extract numbers in it.
- Parameters
filepath (str) – The path string of the file.
pmain (str, optional) – The matching pattern string, such as ‘—>Train’.
psub (str, optional) – The sub-matching pattern string, such as ‘loss’.
vfn (function, optional) – The function for formating the numbers.
float
–> convert to float number;int
–> convert to integer number…, The default isfloat
.nshots (None, optional) – The number of shots of sub-matching pattern.
- Returns
The list of numbers.
- Return type
- pyaibox.utils.file.readsec(filepath, pmain='Train', psub='time: ', vfn=<class 'int'>, nshots=None)
Read a file and extract seconds in it.
hh:mm:ss
–>hh*3600 + mm*60 + ss
- Parameters
filepath (str) – The path string of the file.
pmain (str, optional) – The matching pattern string, such as ‘—>Train’.
psub (str, optional) – The sub-matching pattern string, such as ‘loss’.
vfn (function or None, optional) – The function for formating the numbers.
float
–> convert to float number;int
–> convert to integer number.nshots (None, optional) – The number of shots of sub-matching pattern.
- Returns
The list of seconds.
- Return type
pyaibox.utils.image module
- pyaibox.utils.image.histeq(img, nbins=256)
- pyaibox.utils.image.imread(imgfile)
- pyaibox.utils.image.imresize(img, oshape=None, odtype=None, order=1, mode='constant', cval=0, clip=True, preserve_range=False)
resize image to oshape
see
skimage.transform.resize()
.- Parameters
img (ndarray) – Input image.
oshape (tulpe, optional) – output shape (the default is None, which is the same as the input)
odtype (str, optional) – output data type,
'uint8', 'uint16', 'int8', ...
(the default is None, float)order (int, optional) – The order of the spline interpolation, default is 1. The order has to be in the range 0-5. See skimage.transform.warp for detail.
mode (str, optional) – Points outside the boundaries of the input are filled according to the given mode. {
'constant'
,'edge'
,'symmetric'
,'reflect'
,'wrap'
}, Modes match the behaviour of numpy.pad. The default mode is ‘constant’.cval (float, optional) – Used in conjunction with mode ‘constant’, the value outside the image boundaries.
clip (bool, optional) – Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range.
preserve_range (bool, optional) – Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of img_as_float.
- Returns
resized – Resized version of the input.
- Return type
ndarray
Notes
Modes ‘reflect’ and ‘symmetric’ are similar, but differ in whether the edge pixels are duplicated during the reflection. As an example, if an array has values [0, 1, 2] and was padded to the right by four values using symmetric, the result would be [0, 1, 2, 2, 1, 0, 0], while for reflect it would be [0, 1, 2, 1, 0, 1, 2].
Examples
>>> from skimage import data >>> from skimage.transform import resize >>> image = data.camera() >>> resize(image, (100, 100), mode='reflect').shape (100, 100)
- pyaibox.utils.image.imsave(outfile, img)
pyaibox.utils.ios module
- pyaibox.utils.ios.loadbin(file, dbsize=4, dtype='i', endian='little', offsets=0, nshots=None)
load binary file
load data from binary file
- Parameters
file (str) – the binary file path
dbsize (int, optional) – number pf bits of each number, by default 4
dtype (str, optional) – the type of data. -
'c'
:char,'b'
:schar,'B'
:uchar,'s'
:char[],'p'
:char[],'P'
:void* -'h'
:short,'H'
:ushort,'i'
:int,'I'
:uint,'l'
:long,'L'
:ulong,'q'
:longlong,'Q'
:ulonglong -'f'
:float,'d'
:double, by default'i'
endian (str, optional) – byte order
'little'
/'l'
,'big'
/'b'
, by default ‘little’offsets (int, optional) – start reading offsets index, by default 0
nshots (int, optional) – the number of data points to be read.
- Returns
loaded data.
- Return type
Examples
import pyaibox as pb datafile = 'data/data.bin' x = [1, 3, 6, 111] pb.savebin('./data.bin', x, dtype='i', endian='L', mode='o') y = pb.loadbin('./data.bin', dbsize=4, dtype='i', endian='L') print(y) x = (1.3, 3.6) pb.savebin('./data.bin', x, dtype='f', endian='B', offsets=16, mode='a') y = pb.loadbin('./data.bin', dbsize=4, dtype='f', endian='B', offsets=16) print(y)
- pyaibox.utils.ios.loadh5(filename, keys=None)
load h5 file
- pyaibox.utils.ios.loadjson(filepath, field=None)
load a json file
- Parameters
filepath (str) – The file path string.
field (None, optional) – The string of field that want to be loaded.
- pyaibox.utils.ios.loadmat(filepath)
load data from an
.mat
fileload data from an
.mat
file (None
will be replaced byNone
)see https://stackoverflow.com/questions/7008608/scipy-io-loadmat-nested-structures-i-e-dictionaries
- Parameters
filepath (str) – The file path string.
- pyaibox.utils.ios.loadyaml(filepath, field=None)
Load a yaml file.
- Parameters
filepath (str) – The file path string.
field (None, optional) – The string of field that want to be loaded.
- pyaibox.utils.ios.mvkeyh5(filepath, ksf, kst, sep='.')
rename keys in
.h5
file
- pyaibox.utils.ios.savebin(file, x, dtype='i', endian='little', offsets=0, mode='o')
load binary file
load data from binary file
- Parameters
file (str) – the binary file path
x (any) – data to be written (iterable)
dtype (str, optional) – the type of data. -
'c'
:char,'b'
:schar,'B'
:uchar,'s'
:char[],'p'
:char[],'P'
:void* -'h'
:short,'H'
:ushort,'i'
:int,'I'
:uint,'l'
:long,'L'
:ulong,'q'
:longlong,'Q'
:ulonglong -'f'
:float,'d'
:double, by default'i'
endian (str, optional) – byte order
'little'
/'l'
,'big'
/'b'
, by default ‘little’offsets (int, optional) – start reading offsets index, by default 0
mode (int, optional) –
'append'
/'a'
–> append data to the end of the file'overwrite'
/'o'
–> overwrite the file. (default)
Examples
import pyaibox as pb datafile = 'data/data.bin' x = [1, 3, 6, 111] pb.savebin('./data.bin', x, dtype='i', endian='L', mode='o') y = pb.loadbin('./data.bin', dbsize=4, dtype='i', endian='L') print(y) x = (1.3, 3.6) pb.savebin('./data.bin', x, dtype='f', endian='B', offsets=16, mode='a') y = pb.loadbin('./data.bin', dbsize=4, dtype='f', endian='B', offsets=16) print(y)
- pyaibox.utils.ios.saveh5(filename, mdict, mode='w')
save data to h5 file
- pyaibox.utils.ios.savemat(filepath, mdict, fmt='5', long_field_names=True, do_compression=True, oned_as='row')
save data to an
.mat
filesave data to
.mat
file (None
will be replaced by'None'
)- Parameters
filepath (str) – savefile path
mdict (dict) – data in dict formation.
fmt (str, optional) – mat formation, by default ‘5’
long_field_names (bool, optional) – False (the default) - maximum field name length in a structure is 31 characters which is the documented maximum length. True - maximum field name length in a structure is 63 characters which works for MATLAB 7.6+.
do_compression (bool, optional) – Whether or not to compress matrices on write. Default is False.
oned_as ({'row', 'column'}, optional) – If ‘column’, write 1-D NumPy arrays as column vectors. If ‘row’, write 1-D NumPy arrays as row vectors.
- Returns
all is ok!
- Return type
0
- pyaibox.utils.ios.saveyaml(filepath, ddict=None, indent='-', mode='w')
Load a yaml file.
pyaibox.utils.plot_show module
- class pyaibox.utils.plot_show.Plots(plotdir='./', xlabel='x', ylabel='y', title='', figname=None, issave=False, isshow=True)
Bases:
object
- pyaibox.utils.plot_show.cplot(ca, lmod=None)
- pyaibox.utils.plot_show.imshow(Xs, nrows=None, ncols=None, xlabels=None, ylabels=None, titles=None, figsize=None, outfile=None, **kwargs)
show images
This function create an figure and show images in \(a\) rows and \(b\) columns.
- Parameters
Xs (array, list or tuple) – list/tuple of image arrays, if the type is not list or tuple, wrap it.
nrows (int, optional) – show in
nrows
rows, by default None (auto computed).ncols (int, optional) – show in
ncols
columns, by default None (auto computed).xlabels (str, optional) – labels of x-axis
ylabels (str, optional) – labels of y-axis
titles (str, optional) – titles
figsize (tuple, optional) – figure size, by default None
outfile (str, optional) – save image to file, by default None (do not save).
kwargs –
- figfigure handle
sunch as
fig = plt.figure()
see
matplotlib.pyplot.imshow()
- Returns
plot handle
- Return type
plt
Examples
x = np.random.rand(3, 100, 100) plt = imshow([xi for xi in x]) plt.show() # ---animation x = np.random.rand(10, 128, 128) y = np.random.rand(10, 128, 128) fig = plt.figure() for n in range(10): fig.clf() plt = imshow([x[n], y[n]], 1, 2, fig=fig) plt.pause(0.5)
- pyaibox.utils.plot_show.mesh(Zs, nrows=None, ncols=None, xlabels=None, ylabels=None, zlabels=None, titles=None, figsize=None, outfile=None, **kwargs)
This function create an figure and show some 2d-arrays in \(a\) rows and \(b\) columns with 3d projection.
- Parameters
Zs (array, list or tuple) – list/tuple of image arrays, if the type is not list or tuple, wrap it.
nrows (int, optional) – show in
nrows
rows, by default None (auto computed).ncols (int, optional) – show in
ncols
columns, by default None (auto computed).xlabels (str, optional) – labels of x-axis
ylabels (str, optional) – labels of y-axis
zlabels (str, optional) – labels of z-axis
titles (str, optional) – titles
figsize (tuple, optional) – figure size, by default None
outfile (str, optional) – save image to file, by default None (do not save).
kwargs –
- Xslist or tuple
X-axis values
- Yslist or tuple
Y-axis values
- figfigure handle
sunch as
fig = plt.figure()
for others, see
matplotlib.pyplot.plot_surface()
- Returns
plot handle
- Return type
plt
Examples
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 20)) z = np.random.rand(20, 10) plt = mesh(z, 1, 2) plt.show() plt = mesh(z, 1, 2, Xs=[np.arange(30, 40)]) plt.show() # ---animation x = np.random.rand(10, 128, 128) y = np.random.rand(10, 128, 128) fig = plt.figure() for n in range(10): fig.clf() plt = mesh([x[n], y[n]], 1, 2, fig=fig) plt.pause(0.5)
- pyaibox.utils.plot_show.mshow(Zs, nrows=None, ncols=None, xlabels=None, ylabels=None, zlabels=None, titles=None, projections=None, figsize=None, outfile=None, **kwargs)
show tensors
This function create an figure and show some 2d-arrays in \(a\) rows and \(b\) columns with 2d/3d projection.
- Parameters
Zs (array, list or tuple) – list/tuple of image arrays, if the type is not list or tuple, wrap it.
nrows (int, optional) – show in
nrows
rows, by default None (auto computed).ncols (int, optional) – show in
ncols
columns, by default None (auto computed).xlabels (str, optional) – labels of x-axis
ylabels (str, optional) – labels of y-axis
zlabels (str, optional) – labels of z-axis
titles (str, optional) – titles
figsize (tuple, optional) – figure size, by default None
outfile (str, optional) – save image to file, by default None (do not save).
kwargs –
Xs : list or tuple Ys : list or tuple fig : figure handle
sunch as
fig = plt.figure()
for others, see
matplotlib.pyplot.plot_surface()
- Returns
plot handle
- Return type
plt
Examples
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 20)) z1 = np.random.rand(20, 10) z2 = np.random.randn(60, 60) plt = mshow([z1, z2], 1, 2, Xs=[np.arange(30, 40)], projections=['3d', '2d']) plt.show() # ---animation x = np.random.rand(10, 128, 128) y = np.random.rand(10, 128, 128) fig = plt.figure() for n in range(10): fig.clf() plt = mshow([x[n], y[n]], 1, 2, fig=plt) plt.pause(0.5)
- pyaibox.utils.plot_show.plot(Ys, nrows=None, ncols=None, styles=None, legends=None, grids=False, xlabels=None, ylabels=None, titles=None, figsize=None, outfile=None, **kwargs)
show images
This function create an figure and show images in \(a\) rows and \(b\) columns.
- Parameters
Ys (array, list or tuple) – list/tuple of image arrays, if the type is not list or tuple, wrap it.
nrows (int, optional) – show in
nrows
rows, by default None (auto computed).ncols (int, optional) – show in
ncols
columns, by default None (auto computed).xlabels (str, optional) – labels of x-axis
ylabels (str, optional) – labels of y-axis
titles (str, optional) – titles
figsize (tuple, optional) – figure size, by default None
outfile (str, optional) – save image to file, by default None (do not save).
kwargs –
- figfigure handle
sunch as
fig = plt.figure()
- Xslist or tuple
Y-axis values
see
matplotlib.pyplot.imshow()
- Returns
plot handle
- Return type
plt
Examples
x1 = np.random.rand(2, 100) x2 = np.random.rand(2, 100) plt = plot([[xi for xi in x1], [xi for xi in x2]]) plt.show() x1 = np.random.rand(2, 100) x2 = np.random.rand(2, 100) plt = plot([[xi for xi in x1], [xi for xi in x2]], styles=[['-b', '-r'], ['-b', '-r']], legends=[['real', 'imag'], ['real', 'imag']], grids=True) plt.show()
- pyaibox.utils.plot_show.plots(x, ydict, plotdir='./', xlabel='x', ylabel='y', title='', issave=False, isshow=True)