pyosv.ai.data_reduction

 1from sklearn.decomposition import PCA
 2import numpy as np
 3
 4def image_PCA(img : np.ndarray, n_components : int) -> np.ndarray:
 5    '''
 6
 7        Reduce the image dimensionality along the channells axis using PCA
 8
 9        Parameters:  
10        ----------  
11            - img : np.ndarray
12                a WxHxB image, with width W, height H and B bands (channel last)
13            - n_components : int
14                number of bands of the compressed image
15        
16        Returns:
17        --------
18            - reduced : np.ndarray
19                the WxHxn_components compressed image, with width W, height H and n_components bands
20
21        Usage:
22        ------
23
24        ```python
25        import numpy as np  
26
27        img         = np.array(  
28            [[
29                [0.1, 0.2, 0.3],  
30                [0.4, 0.5, 0.6],  
31                [0.7, 0.8, 0.9]
32             ],
33             [
34                [1.1, 1.2, 1.3],  
35                [1.4, 1.5, 1.6],  
36                [1.7, 1.8, 1.9]
37             ]
38            ]  
39        ) 
40
41        # Making channels last
42        img = np.moveaxis(img, 0, -1)
43
44        reduced = image_PCA(img, n_components = 1)
45        ```
46        Output:
47        ------
48        ```
49        array([
50              [[ 0.56568542],
51                [ 0.42426407],
52                [ 0.28284271]],
53              [[0.14142136],
54                [-0.        ],
55                [-0.14142136]],
56              [[-0.28284271],
57                [-0.42426407],
58                [-0.56568542]]])
59       ```
60
61    '''
62
63    if len(img.shape) != 3:
64        raise Exception('Error: lenght of image shape must be 3 -  (space, space, channels)')
65
66    pca = PCA(n_components)
67    reduced = pca.fit_transform(img.reshape((img.shape[0] * img.shape[1], img.shape[2])))
68    reduced = reduced.reshape((img.shape[0], img.shape[1], n_components))
69    reduced = np.array(reduced)
70
71    return reduced
72
73
74def image_stack_PCA(imgs : np.ndarray, n_components : int) -> np.ndarray:
75    '''
76        Reduce a temporal stack of images along the temporal axis
77    '''
78    if len(imgs.shape) != 4:
79        raise Exception('Error: lenght of imgs shape must be 4 - (space, space, channels, time)')
80    
81    pass
def image_PCA(img: numpy.ndarray, n_components: int) -> numpy.ndarray:
 5def image_PCA(img : np.ndarray, n_components : int) -> np.ndarray:
 6    '''
 7
 8        Reduce the image dimensionality along the channells axis using PCA
 9
10        Parameters:  
11        ----------  
12            - img : np.ndarray
13                a WxHxB image, with width W, height H and B bands (channel last)
14            - n_components : int
15                number of bands of the compressed image
16        
17        Returns:
18        --------
19            - reduced : np.ndarray
20                the WxHxn_components compressed image, with width W, height H and n_components bands
21
22        Usage:
23        ------
24
25        ```python
26        import numpy as np  
27
28        img         = np.array(  
29            [[
30                [0.1, 0.2, 0.3],  
31                [0.4, 0.5, 0.6],  
32                [0.7, 0.8, 0.9]
33             ],
34             [
35                [1.1, 1.2, 1.3],  
36                [1.4, 1.5, 1.6],  
37                [1.7, 1.8, 1.9]
38             ]
39            ]  
40        ) 
41
42        # Making channels last
43        img = np.moveaxis(img, 0, -1)
44
45        reduced = image_PCA(img, n_components = 1)
46        ```
47        Output:
48        ------
49        ```
50        array([
51              [[ 0.56568542],
52                [ 0.42426407],
53                [ 0.28284271]],
54              [[0.14142136],
55                [-0.        ],
56                [-0.14142136]],
57              [[-0.28284271],
58                [-0.42426407],
59                [-0.56568542]]])
60       ```
61
62    '''
63
64    if len(img.shape) != 3:
65        raise Exception('Error: lenght of image shape must be 3 -  (space, space, channels)')
66
67    pca = PCA(n_components)
68    reduced = pca.fit_transform(img.reshape((img.shape[0] * img.shape[1], img.shape[2])))
69    reduced = reduced.reshape((img.shape[0], img.shape[1], n_components))
70    reduced = np.array(reduced)
71
72    return reduced

Reduce the image dimensionality along the channells axis using PCA

Parameters:


 - img : np.ndarray
     a WxHxB image, with width W, height H and B bands (channel last)
 - n_components : int
     number of bands of the compressed image

Returns:


 - reduced : np.ndarray
     the WxHxn_components compressed image, with width W, height H and n_components bands

Usage:


import numpy as np 
img = np.array(
[[ [0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9] ], [ [1.1, 1.2, 1.3],
[1.4, 1.5, 1.6],
[1.7, 1.8, 1.9] ] ]
) # Making channels last img = np.moveaxis(img, 0, -1) reduced = image_PCA(img, n_components = 1)

Output:


array([ [[ 0.56568542], [ 0.42426407], [ 0.28284271]], [[0.14142136], [-0. ], [-0.14142136]], [[-0.28284271], [-0.42426407], [-0.56568542]]])

def image_stack_PCA(imgs: numpy.ndarray, n_components: int) -> numpy.ndarray:
75def image_stack_PCA(imgs : np.ndarray, n_components : int) -> np.ndarray:
76    '''
77        Reduce a temporal stack of images along the temporal axis
78    '''
79    if len(imgs.shape) != 4:
80        raise Exception('Error: lenght of imgs shape must be 4 - (space, space, channels, time)')
81    
82    pass

Reduce a temporal stack of images along the temporal axis