torchbox.utils package
Submodules
torchbox.utils.colormaps module
torchbox.utils.colors module
- torchbox.utils.colors.gray2rgb(gray, cmap, drange=[0, 255], fmtstr=False)
Converts gray images to rgb image
converts gray images 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 (torch tensor) – The gray image \(N\times H\times W\), or the gray images \(N\times H\times W\), \(N\) is the number of images.
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)
:param see also
rgb2gray()
.:Examples
import torchbox as tb import matplotlib.pyplot as plt import torch as th cmap = 'jet' # cmap = 'hsv' # cmap = 'hot' # cmap = 'parula' gray = ts.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, th.min(gray), th.max(gray), gray.dtype) print(rgb.shape, th.min(rgb), th.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()
- torchbox.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.see also
gray2rgb()
.- Parameters
rgb (Tensor) – Original RGB tensor.
fmt (str, optional) – Specifies the position of channels in
rgb
tensor, surpported are: -'chnllast'
(default) -'chnlfirst'
torchbox.utils.const module
torchbox.utils.convert module
- torchbox.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
- torchbox.utils.convert.dict2str(ddict, attrtag=': ', indent=' ', linebreak='\n', nindent=0)
dump dict object to str
- Parameters
ddict (dict) – The dict object to be converted
attrtag (str, optional) – The tag of attribution, by default
': '
indent (str, optional) – The dict identifier, by default
' '
linebreak (str, optional) – The line break character, by default ‘n’
nindent (int, optional) – the number of initial indent characters, by default 0
- Returns
The converted string.
- Return type
- torchbox.utils.convert.file2hash(file, hmode='sha256', tohex=True)
convert a string to hash code
- torchbox.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
- torchbox.utils.convert.str2bool(s, truelist=['true', '1', 'y', 'yes'], falselist=['false', '0', 'n', 'no'])
Converts string to bool
- torchbox.utils.convert.str2hash(s, hmode='sha256', enc='utf-8', tohex=True)
convert a string to hash code
- torchbox.utils.convert.str2list(s, sep=' ')
Converts string to list
- Parameters
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)
- torchbox.utils.convert.str2num(s, vfn=None)
Extracts numbers in a string.
- Parameters
s (str) – The string.
vfn (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
- torchbox.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
- torchbox.utils.convert.str2tuple(s, sep=' ')
Converts string to tuple
- Parameters
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)
torchbox.utils.file module
- torchbox.utils.file.copyfile(srcfile, dstfile)
copy file
copy file from srcfile to dstfile.
- torchbox.utils.file.copyfiles(srcdir, dstdir, filenames)
copy files
copy files from source directory to destnation directory
- torchbox.utils.file.data_path(dsname=None)
obtain data source path
- torchbox.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.
- torchbox.utils.file.fopen(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True)
file open
difference to python’s builtin function
open()
is when mode is'w'
, if the file is not empty, this function gives a selection of overwrite or skip.- Parameters
file (str) – file path string.
mode (str, optional) –
'r'
(read),'w'
(overwrite),'a'
(append), …, see alsoopen()
, by default “r”.buffering (int, optional) – see also
open()
, , by default -1encoding (str, optional) – see also
open()
, , by default Nonenewline (str, optional) – see also
open()
, , by default Noneclosefd (bool, optional) – see also
open()
, , by default True
- Return type
TextIOWrapper or None
- torchbox.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
- torchbox.utils.file.pathjoin(*kwargs)
Joint strings to a path.
- Parameters
*kwargs – strings.
- Returns
The joined path string.
- Return type
- torchbox.utils.file.pkg_path()
obtain this package’s path
- Returns
package path string.
- Return type
- torchbox.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
- torchbox.utils.file.readnum(filepath, pmain='Train', psub='loss', vfn=<class 'float'>, nshots=None)
Read a file and extract numbers in it.
- Parameters
filepath (str) – Description
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
- torchbox.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
torchbox.utils.image module
- torchbox.utils.image.histeq(img, nbins=256)
- torchbox.utils.image.imadjust(img, lhin, lhout)
- torchbox.utils.image.imadjustlog(img, lhin=None, lhout=None)
- torchbox.utils.image.imread(imgfile)
- torchbox.utils.image.imresize(img, oshape=None, odtype=None, order=1, mode='constant', cval=0, clip=True, preserve_range=False)
resize image to oshape
see also
skimage.transform.resize()
.- Parameters
img (Tensor) – Input image.
oshape (tuple, 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
Tensor
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)
- torchbox.utils.image.imsave(outfile, img, **kwargs)
torchbox.utils.ios module
- torchbox.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 torchbox 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)
- torchbox.utils.ios.loadh5(filename, keys=None)
load h5 file
- torchbox.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.
- torchbox.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.
- torchbox.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.
- torchbox.utils.ios.mvkeyh5(filepath, ksf, kst, sep='.')
rename keys in
.h5
file
- torchbox.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 torchbox 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)
- torchbox.utils.ios.saveh5(filename, mdict, mode='w')
save data to h5 file
- torchbox.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 byNone
)- 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
- torchbox.utils.ios.saveyaml(filepath, ddict=None, indent='-', mode='w')
Load a yaml file.
torchbox.utils.plot_show module
- class torchbox.utils.plot_show.Plots(plotdir='./', xlabel='x', ylabel='y', title='', figname=None, issave=False, isshow=True)
Bases:
object
- torchbox.utils.plot_show.cplot(ca, lmod=None)
- torchbox.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 also
matplotlib.pyplot.imshow()
:param see also
plot()
: :parammesh()
: :parammshow()
.:- 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)
- torchbox.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 other kwargs, see also
matplotlib.pyplot.plot_surface()
:param see also
imshow()
: :paramplot()
: :parammshow()
.:- 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)
- torchbox.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 also
matplotlib.pyplot.plot_surface()
:param see also
imshow()
: :parammesh()
: :paramplot()
.:- 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)
- torchbox.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.plot()
:param see also
imshow()
: :parammesh()
: :parammshow()
.:- 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()
- torchbox.utils.plot_show.plots(x, ydict, plotdir='./', xlabel='x', ylabel='y', title='', issave=False, isshow=True)