magni.imaging.dictionaries.analysis module¶
Module providing functionality for the analysis of dictionaries.
Routine listings¶
 get_reconstructions(img, transform, fractions)
 Function to get a set of transform reconstructions.
 show_coefficient_histogram(img, transforms, bins=None, range=None,
 output_path=None, fig_ext=’pdf’) Function to show a transform coefficient histogram.
 show_psnr_energy_rolloff(img, reconstructions, fractions, return_vals=False,
 output_path=None, fig_ext=’pdf’) Function to show PSNR and retained energy rolloff of reconstructions.
 show_reconstructions(coefficients, reconstructions, transform, fractions,
 output_path=None, fig_ext=’pdf’) Function to show transforms reconstructions and coefficients.
 show_sorted_coefficients(img, transforms, output_path=None, fig_ext=’pdf’)
 Function to show a plot of transform coefficients in sorted order.
 show_transform_coefficients(img, transforms, output_path=None, fig_ext=’pdf’)
 Function to show transform coefficients.
 show_transform_quantiles(img, transform, fraction=1.0, area_mask=None)
 Function to show quantiles of transform coefficients.

magni.imaging.dictionaries.analysis.
get_reconstructions
(img, transform, fractions)[source]¶ Return transform reconstructions with different fractions of coefficents.
The image img is transform coded using the specified transform. Reconstructions for the
fractions
of transform coefficients kept are returned along with the coefficients used in the reconstructions.Parameters:  img (ndarray) – The image to get reconstructions of.
 transform (str) – The transform to use to obtain the reconstructions.
 fractions (list or tuple) – The fractions of coefficents to be used in the reconstructions.
Returns:  coefficients (list) – The list of coefficients (each an ndarray) used in the reconstructions.
 reconstructions (list) – The list of reconstructions.
Examples
Get reconstructions from DCT based on 20% and 40% of the coefficients:
>>> import numpy as np >>> from magni.imaging.dictionaries.analysis import get_reconstructions >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transform = 'DCT' >>> fractions = (0.2, 0.4) >>> coefs, recons = get_reconstructions(img, transform, fractions) >>> len(recons) 2 >>> tuple(int(s) for s in coefs[0].shape) (8, 8) >>> tuple(int(s) for s in recons[0].shape) (8, 8)

magni.imaging.dictionaries.analysis.
show_coefficient_histogram
(img, transforms, bins=None, range=None, output_path=None, fig_ext='pdf')[source]¶ Show a histogram of coefficient values for different transforms.
A histogram of the transform coefficient values for img using the different transforms are shown. If output_path is not None, the resulting figure and data used in the figure are saved.
Parameters:  img (ndarray) – The image to get transform coefficients for.
 transforms (list or tuple) – The names as strings of the transforms to use.
 bins (int) – The number of bins to use in the histogram (the default is None, which implies that the number of bins is determined based on the size of img).
 range (tuple) – The lower and upper range of the bins to use in the histogram (the default is None, which implies that the min and max values of img are used).
 output_path (str) – The output path (see notes below) to save the figure and data to (the default is None, which implies that the figure and data are not saved).
 fig_ext (str) – The figure extension determining the format of the saved figure (the default is ‘pdf’ which implies that the figure is saved as a PDF).
See also
matplotlib.pyplot.hist()
 The underlying histogram plot function.
Notes
The output_path is specified as a path to a folder + an optional prefix to the file name. The remaining file name is fixed. If e.g, the fixed part of the file name was ‘plot’, then:
 output_path = ‘/home/user/’ would save the figure under /home/user/ with the name plot.pdf.
 output_path = ‘/home/user/best’ would save the figure under /home/user with the name best_plot.pdf.
In addition to the saved figures, an annotated and chased HDF database with the data used to create the figures are also saved. The name of the HDF database is the same as for the figure with the exception that the file extension is ‘.hdf5’.
Examples
Save a coefficient histogram using the DCT and the DFT as transforms
>>> import os, numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transforms = ('DCT', 'DFT') >>> output_path = './histogram_test' >>> _a.show_coefficient_histogram(img, transforms, output_path=output_path) >>> current_dir = os.listdir('./') >>> for file in sorted(current_dir): ... if 'histogram_test' in file: ... print(file) histogram_test_coefficient_histogram.hdf5 histogram_test_coefficient_histogram.pdf

magni.imaging.dictionaries.analysis.
show_psnr_energy_rolloff
(img, reconstructions, fractions, return_vals=False, output_path=None, fig_ext='pdf')[source]¶ Show the PSNR and energy rolloff for the reconstructions.
A plot of the Peak Signal to Noise Ratio (PSNR) and retained energy in the recontructions versus the
fractions
of coefficients used in the reconstructions is shown. If return_vals is True, the data used in the plot is returned. If output_path is not None, the resulting figure and data used in the figure are saved.Parameters:  img (ndarray) – The image which the reconstructions are based on.
 reconstructions (list or tuple) – The reconstructions (each an ndarray) to show rolloff for.
 fractions (list or tuple) – The fractions of coefficents used in the reconstructions.
 return_vals (bool) – The flag indicating wheter or not to return the PSNR and energy values (the default is False, which indicate that the values are not returned).
 output_path (str) – The output path (see notes below) to save the figure and data to (the default is None, which implies that the figure and data are not saved).
 fig_ext (str) – The figure extension determining the format of the saved figure (the default is ‘pdf’ which implies that the figure is saved as a PDF).
Returns:  psnrs (ndarray) – The PSNR values shown in the figure (only returned if return_vals=True).
 energy (ndarray) – The retained energy values shown in the figure (only returned if return_vals=True).
Notes
The output_path is specified as a path to a folder + an optional prefix to the file name. The remaining file name is fixed. If e.g, the fixed part of the file name was ‘plot’, then:
 output_path = ‘/home/user/’ would save the figure under /home/user/ with the name plot.pdf.
 output_path = ‘/home/user/best’ would save the figure under /home/user with the name best_plot.pdf.
In addition to the saved figures, an annotated and chased HDF database with the data used to create the figures are also saved. The name of the HDF database is the same as for the figure with the exception that the file extension is ‘.hdf5’.
Examples
Save a PSNR and energy rolloff plot for reconstructions bases on the DCT:
>>> import os, numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transform = 'DCT' >>> fractions = (0.2, 0.4) >>> coefs, recons = _a.get_reconstructions(img, transform, fractions) >>> o_p = './rolloff_test' >>> _a.show_psnr_energy_rolloff(img, recons, fractions, output_path=o_p) >>> current_dir = os.listdir('./') >>> for file in sorted(current_dir): ... if 'rolloff_test' in file: ... print(file) rolloff_test_psnr_energy_rolloff.hdf5 rolloff_test_psnr_energy_rolloff.pdf

magni.imaging.dictionaries.analysis.
show_reconstructions
(coefficients, reconstructions, transform, fractions, output_path=None, fig_ext='pdf')[source]¶ Show reconstructions and corresponding coefficients.
Parameters:  coefficients (list or tuple) – The coefficients (each an ndarray) used in the reconstructions.
 reconstructions (list or tuple) – The reconstructions (each an ndarray) to show.
 transform (str) – The transform used to obtain the reconstructions.
 fractions (list or tuple) – The fractions of coefficents used in the reconstructions.
 output_path (str) – The output path (see notes below) to save the figure and data to (the default is None, which implies that the figure and data are not saved).
 fig_ext (str) – The figure extension determining the format of the saved figure (the default is ‘pdf’ which implies that the figure is saved as a PDF).
Notes
The output_path is specified as a path to a folder + an optional prefix to the file name. The remaining file name is fixed. If e.g, the fixed part of the file name was ‘plot’, then:
 output_path = ‘/home/user/’ would save the figure under /home/user/ with the name plot.pdf.
 output_path = ‘/home/user/best’ would save the figure under /home/user with the name best_plot.pdf.
In addition to the saved figures, an annotated and chased HDF database with the data used to create the figures are also saved. The name of the HDF database is the same as for the figure with the exception that the file extension is ‘.hdf5’.
Examples
Save images of coefficients and reconstructions based on the DCT:
>>> import os, numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> trans = 'DCT' >>> fracs = (0.2, 0.4) >>> coefs, recons = _a.get_reconstructions(img, trans, fracs) >>> o_p = './reconstruction_test' >>> _a.show_reconstructions(coefs, recons, trans, fracs, output_path=o_p) >>> current_dir = os.listdir('./') >>> for file in sorted(current_dir): ... if 'reconstruction_test' in file: ... print(file) reconstruction_test_reconstruction_coefficients.hdf5 reconstruction_test_reconstruction_coefficients.pdf reconstruction_test_reconstructions.hdf5 reconstruction_test_reconstructions.pdf

magni.imaging.dictionaries.analysis.
show_sorted_coefficients
(img, transforms, output_path=None, fig_ext='pdf')[source]¶ Show the transform coefficient values in sorted order.
A plot of the sorted coefficient values vs array index number is shown. If output_path is not None, the resulting figure and data used in the figure are saved.
Parameters:  img (ndarray) – The image to show the sorted transform coefficients values for.
 transforms (list or tuple) – The names as strings of the transforms to use.
 output_path (str) – The output path (see notes below) to save the figure and data to (the default is None, which implies that the figure and data are not saved).
 fig_ext (str) – The figure extension determining the format of the saved figure (the default is ‘pdf’ which implies that the figure is saved as a PDF).
Notes
The output_path is specified as a path to a folder + an optional prefix to the file name. The remaining file name is fixed. If e.g, the fixed part of the file name was ‘plot’, then:
 output_path = ‘/home/user/’ would save the figure under /home/user/ with the name plot.pdf.
 output_path = ‘/home/user/best’ would save the figure under /home/user with the name best_plot.pdf.
In addition to the saved figures, an annotated and chased HDF database with the data used to create the figures are also saved. The name of the HDF database is the same as for the figure with the exception that the file extension is ‘.hdf5’.
Examples
Save a sorted transform coefficient plot for the DCT and DFT transforms:
>>> import os, numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transforms = ('DCT', 'DFT') >>> output_path = './sorted_test' >>> _a.show_sorted_coefficients(img, transforms, output_path=output_path) >>> current_dir = os.listdir('./') >>> for file in sorted(current_dir): ... if 'sorted_test' in file: ... print(file) sorted_test_sorted_coefficients.hdf5 sorted_test_sorted_coefficients.pdf

magni.imaging.dictionaries.analysis.
show_transform_coefficients
(img, transforms, output_path=None, fig_ext='pdf')[source]¶ Show the transform coefficients.
The transform coefficient of img are shown for the transforms. If output_path is not None, the resulting figure and data used in the figure are saved.
Parameters:  img (ndarray) – The image to show the transform coefficients for.
 transforms (list or tuple) – The names as strings of the transforms to use.
 output_path (str) – The output path (see notes below) to save the figure and data to (the default is None, which implies that the figure and data are not saved).
 fig_ext (str) – The figure extension determining the format of the saved figure (the default is ‘pdf’ which implies that the figure is saved as a PDF).
Notes
The output_path is specified as a path to a folder + an optional prefix to the file name. The remaining file name is fixed. If e.g, the fixed part of the file name was ‘plot’, then:
 output_path = ‘/home/user/’ would save the figure under /home/user/ with the name plot.pdf.
 output_path = ‘/home/user/best’ would save the figure under /home/user with the name best_plot.pdf.
In addition to the saved figures, an annotated and chased HDF database with the data used to create the figures are also saved. The name of the HDF database is the same as for the figure with the exception that the file extension is ‘.hdf5’.
Examples
Save a figure showing the coefficients for the DCT and DFT transforms:
>>> import os, numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transforms = ('DCT', 'DFT') >>> o_p = './coefficient_test' >>> _a.show_transform_coefficients(img, transforms, output_path=o_p) >>> current_dir = os.listdir('./') >>> for file in sorted(current_dir): ... if 'coefficient_test' in file: ... print(file) coefficient_test_transform_coefficients.hdf5 coefficient_test_transform_coefficients.pdf

magni.imaging.dictionaries.analysis.
show_transform_quantiles
(img, transform, fraction=1.0, area_mask=None, ax=None)[source]¶ Show a plot of the quantiles of the transform coefficients.
The fraction of transform coefficients holding the most energy for the image img is considered. The four quantiles within this fraction of coefficients are illustrated in the transform domain by showing coefficients between the different quantiles in different colours. If an area_mask is specified, only this area in the plot is higlighted whereas the rest is darkened.
Parameters:  img (ndarray) – The image to show the transform quantiles for.
 transform (str) – The transform to use.
 fraction (float) – The fraction of coefficients used in the quantiles calculation (the default value is 1.0, which implies that all coefficients are used).
 area_mask (ndarray) – Bool array of the same shape as img which indicates the area of the image to highlight (the default value is None, which implies that no particular part of the image is highlighted).
 ax (matplotlib.axes.Axes) – The axes on which the image is displayed (the default is None, which implies that a separate figure is created).
Returns: coef_count (dict) – Different counts of coeffcients within the area_mask (only returned if area_mask is not None).
Notes
The ticks on the colorbar shown below the figure are the percentiles of the entire set of coefficients corresponding to the quantiles with fraction of coefficients. For instance, if fraction is 0.10, then the percentiles are 92.5, 95.0, 97.5, 100.0, corresponding to the four quantiles within the 10 percent coefficients holding the most energy.
The coef_count dictionary holds the following keys:
 C_total : Total number of considered coefficients.
 Q_potential : Number of potential coefficients within mask_area.
 P_fraction : The fraction of Q_potential to the pixel count in img.
 Q_total : Total number of (considered) coefficients within mask_area.
 Q_fraction : The fraction of Q_total to Q_potential
 QC_fraction : The fraction of Q_total to C_total.
 Q0Q1 : Number of coefficients smaller than the first quantile.
 Q1Q2 : Number of coefficients between the first and second quantile.
 Q2Q3 : Number of coefficients between the second and third quantile.
 Q3Q4 : Number of coefficients between the third and fourth quantile.
Each of the QXQY holds a tuple containing two values:
 The number of coefficients.
 The fraction of the number of coefficients to Q_total.
Examples
For example, show quantiles for a fraction of 0.2 of the DCT coefficients:
>>> import numpy as np >>> from magni.imaging.dictionaries import analysis as _a >>> img = np.arange(64).astype(np.float).reshape(8, 8) >>> transforms = 'DCT' >>> fraction = 0.2 >>> _a.show_transform_quantiles(img, transform, fraction=fraction)

magni.imaging.dictionaries.analysis.
_save_output
(output_path, name, fig, fig_ext, datasets)[source]¶ Save figure and data output.
Parameters:  output_path (str) – The output_path to save to.
 name (str) – The ‘fixed’ part of the file name saved to.
 fig (matplotlib.figure.Figure) – The figure instance to save.
 fig_ext (str) – The file extension to use for the saved figure.
 datasets (dict) – The dict of dicts for datasets to save in a HDF database.