deeptexture package
Submodules
deeptexture.cbir module
- class deeptexture.cbir.CBIR(dtr_obj: Any, project: str = 'DB', working_dir: str = '.')[source]
Bases:
object- create_db(df_attr: Any, dtrs: None | ndarray = None, img_attr: str = 'imgfile', case_attr: str = 'patient', type_attr: str = 'tissue', save: bool = True) None[source]
Create CBIR database.
- Parameters:
df_attr (Any) – Pandas dataframe containing at least image files and case IDs.
dtrs (np.ndarray, optional) – pre-computed DTRs for the image files. The order should be the same as the img_attr. Default to None.
img_attr (str, optional) – Column name of image files in df_attr. Defaults to “imgfile”.
case_attr (str, optional) – Column name of case ID in df_attr. Defaults to “patient”.
type_attr (str, optional) – Column name of additional attribute to show in df_attr. Defaults to “tissue”.
save (bool, optional) – Saves database in the project direcotry if True. Defaults to True.
- search(qimgfile: str, n: int = 50, show_query: bool = True, show: bool = True, scale: None | int = None, fkey: None | str = None, fval: str | List[str] | None = None, dpi: int = 320, save: bool = False, outfile: str = '') Tuple[ndarray, DataFrame][source]
Search and show images similar to the query image using DTR
- Parameters:
qimgfile (str) – Query image file.
show_query (bool, optional) – Show query image. Defaults to True.
show (bool, optional) – Show retrieved images. Defaults to True.
n (int, optional) – The number of retrieved images. Defaults to 50.
scale (Union[None, int], optional) – Query image is rescaled. Default to None.
fkey (Union[None, str]) – Key for filter in df_attr. Default to None.
fval (Union[str, List[str], None]) – Value(s) for filter. Default to None.
dpi (int, optional) – Dots per inch (DPI) of output image. Defaults to 320.
save (bool, optional) – Save the output image to outfile if True. Defaults to False.
outfile (str, optional) – Output image file. Defaults to “”.
- Returns:
Retrieved image file and the corresponding info(case, similarity, and attribute).
- Return type:
Tuple[np.ndarray, pd.DataFrame]
- search_multi(qimgfiles: List[str], strategy: str = 'max', n: int = 50, show_query: bool = True, show: bool = True, scale: None | int = None, fkey: None | str = None, fval: str | List[str] | None = None, dpi: int = 320, save: bool = False, outfile: str = '') DataFrame[source]
Search and show images similar to the query image using DTR
- Parameters:
qimgfiles (List[str]) – Query image files.
strategy (str, optional) – Search strategy. In ‘max’, the similarity for the case is cacluated based on the maximum similarity among queries. In ‘mean’, the similarity for the case is cacluated based on the average similarity among queries.
show_query (bool, optional) – Show query images. Defaults to True.
show (bool, optional) – Show retrieved images. Defaults to True.
n (int, optional) – The number of retrieved images. Defaults to 50.
scale (Union[None, int], optional) – Query images are rescaled. Default to None.
fkey (Union[None, str]) – Key for filter in df_attr. Default to None.
fval (Union[str, List[str], None]) – Value(s) for filter. Default to None.
dpi (int, optional) – Dots per inch (DPI) of output image. Defaults to 320.
save (bool, optional) – Save the output image to outfile if True. Defaults to False.
outfile (str, optional) – Output image file. Defaults to “”.
- Returns:
Results
- Return type:
pd.DataFrame
- show_db(n: int = 50, cases: List[str] | None = None, attrs: List[str] | None = None)[source]
Show database images.
- Parameters:
n (int, optional) – The number of images shown. Defaults to 50.
cases (List[str], optional) – Cases shown. Defaults to None.
attrs (List[str], optional) – Attributes shown. Defaults to None.
deeptexture.dtr module
- class deeptexture.dtr.DTR(arch: str = 'vgg', layer: str = 'block3_conv3', dim: int = 1024)[source]
Bases:
object- get_dtr(img: Any, angle: None | int | List[int] = None, size: None | int = None, scale: None | float = None, multi_scale: bool = False) ndarray[source]
Calculates DTR for an image object or file.
- Parameters:
img (Any) – Image file or image object (numpy array or PIL Image object)
angle (Union[None, int, List[int]], optional) – Rotation angle(s) (0-360). If list is given, mean DTRs of the rotated image return. Defaults to None.
size (Union[None, int], optional) – Image is resized to the given size. Default to None.
scale (Union[None, int], optional) – Image is rescaled. Active only size is not specified. Default to None.
multi_scale (bool, optional) – DTR for 1/4 sized image is concatenated. The dimension of the DTR will be 2*dim. Default to False.
- Returns:
DTR for the image
- Return type:
np.ndarray
- get_dtr_multifiles(imgfiles: List[str], angle: None | int | List[int] = None, size: None | int = None, scale: None | float = None) ndarray[source]
Calculates DTRs for multiple images.
- Parameters:
imgfiles (List[str]) – List of image files.
angle (Union[None, int, List[int]], optional) – Rotation angle(s) (0-360). If list is given, mean DTRs of the rotated image return. Defaults to None.
size (Union[None, int], optional) – Image is resized to the given size. Default to None.
scale (Union[None, int], optional) – Image is rescaled. Active only size is not specified. Default to None.
- Returns:
DTRs
- Return type:
np.ndarray
- get_mean_dtrs(dtrs: ndarray, cases: List[str], df: None | DataFrame | List[str] = None) Tuple[ndarray, List[str], None | DataFrame | List[str]][source]
Calculate mean dtrs.
- Parameters:
dtrs (np.ndarray) – M-dimensional DTRs for N images (NxM array).
cases (List[str]) – List of case IDs for N images.
df – Union[None, pd.DataFrame, List[str]]: List or dataframe containing attributes of N images. The order should be the same as dtrs and cases. Default to None.
- Returns:
mean DTRs, List of image file path of the representative images (medoid for each case), and case IDs.
- Return type:
Tuple[np.ndarray, List[str], List[str]]
deeptexture.ml module
- class deeptexture.ml.ML(dtrs: ndarray, imgfiles: List[str])[source]
Bases:
object- clustering(method: str = 'bayes_gmm', n_components: int = 10, show: bool = False) List[int][source]
Clustering of dtrs.
- Parameters:
method (str, optional) – Clustering algorithm. Defaults to ‘bayes_gmm’.
n_components (int, optional) – Number of (maximum) clusters. Defaults to 10.
show (bool, optional) – Show representative images. Defaults to False.
- Returns:
Cluster labels.
- Return type:
List[int]
- fit_eval(y: list | ndarray, cases: list | ndarray, additional_features: ndarray | None = None, min_samples: int = 5, show: bool = True, test_size: float | int = 0.25) Any[source]
Logistic regression analysis.
- Parameters:
y (Union[list, np.ndarray]) – Target variable.
cases (Union[list, np.ndarray]) – Case IDs (used as group).
additional_features (np.ndarray, optional) – Additional features used for the classification. It MUST be the numerical arrays. If it is a categorical variable, please use categorical encoders. Defaults to None.
min_samples (int, optional) – Minimum number of cases analyzed in a target. Targets below the value will be removed. Defaults to 5.
show (bool, optional) – Show confusion matrix or ROC curve. Defaults to True.
test_size (Union[int, float], optional) – If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. Defaults to 0.25.
- Returns:
AUROC (for binary classification) or confusion matrix (for multiclass classification).
- Return type:
Any
deeptexture.plt_dtr module
- deeptexture.plt_dtr.plt_dtr_attr(X: ndarray, attr: List[str], cases: None | List[str] = None, method: None | str = None, s: int = 10, outfile: str = '', palette: str = 'colorblind', dpi: int = 320, save: bool = False, axis: bool = False, text: None | List[str] = None, use_plotly: bool = False, show_medoid: bool = False, **kwargs) DataFrame[source]
Plot DTRs in two-dimensional space given or calculated by the specified dimensionality reduction method. User specified attributes are plotted at the position in the space.
- Parameters:
X (np.ndarray) – M-dimensional DTRs for N images (NxM array) or two-dimensional coordicates of DTRs for N images (Nx2 array).
attr (List[str]) – List of attribute string.
cases (Union[None, str], optional) – If not None, the case ID is shown or used for medoid calculation. Activated only when use_plotly is True or show_medoid is True. Defaults to None.
method (Union[None, str], optional) – If not None, the specified dimensionality reduction method (“tsne”, “pca”, “umap”, “lle”, “isomap”, “se”) is used. Defaults to None.
s (int, optional) – Point size in the plot. Defaults to 10.
outfile (str, optional) – Output image file. Defaults to “”.
palette (str, optional) – Color palette. Defaults to ‘colorblind’.
dpi (int, optional) – Dots per inch (DPI) of output image. Defaults to 320.
save (bool, optional) – Save the output image to outfile if True. Defaults to False.
axis (bool, optional) – Show axis if True. Defaults to False.
use_plotly (bool, optional) – Use plotly. Defaults to False.
text (Union[None, List[str]], optional) – Show text if not None. Defaults to Union[None, List[str]]. Defaults to None.
show_medoid (bool, optional) – only show medoid. Active if DTRs are given as X. Defaults to False.
- Returns:
Dataframe with ‘attr’, ‘x1’, and ‘x2’ columns.
- Return type:
pd.DataFrame
- deeptexture.plt_dtr.plt_dtr_image(X: ndarray, files: List[str], method: None | str = None, dpi: int = 320, scale: float = 1.0, outfile: str = '', save: bool = False, axis: bool = False, text: None | List[str] = None, show_medoid: bool = False, cases: None | str = None, **kwargs) ndarray[source]
Plot DTRs in two-dimensional space given or calculated by the specified dimensionality reduction method. Images are plotted at the position in the space.
- Parameters:
X (np.ndarray) – M-dimensional DTRs for N images (NxM array) or two-dimensional coordicates of DTRs for N images (Nx2 array).
files (List[str]) – list of image file.
method (Union[None, str], optional) – If not None, the specified dimensionality reduction method (“tsne”, “pca”, “umap”, “lle”, “isomap”, “se”) is used. Defaults to None.
dpi (int, optional) – Dots per inch (DPI) of output image. Defaults to 320.
scale (float, optional) – Image scale in the plot. Defaults to 1.0.
outfile (str, optional) – Output image file. Defaults to “”.
save (bool, optional) – Save the output image to outfile if True. Defaults to False.
axis (bool, optional) – Show axis if True. Defaults to False.
text (Union[None, List[str]], optional) – Show text if not None. Defaults to Union[None, List[str]]. Defaults to None.
show_medoid (bool, optional) – only show medoid. Active if DTRs are given as X. Defaults to False.
cases (Union[None, str], optional) – Cases for each image. Valid only if show_medoid is True. Defaults to None.
- Returns:
Two-dimensional coordicates of DTRs for N images (Nx2 array)
- Return type:
np.ndarray