Skip to content

Commit

Permalink
Merge pull request #128 from carlgogo/master
Browse files Browse the repository at this point in the history
Update to ``svd_wrapper``, pytorch support. Removed ascii logo when importing vip
  • Loading branch information
carlos-gg authored Feb 26, 2018
2 parents 9b1ca92 + b2e5c2c commit 6d3f090
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 53 deletions.
16 changes: 16 additions & 0 deletions docs/source/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ VIP - Vortex Image Processing package
:target: http://vip.readthedocs.io/en/latest/?badge=latest



::

---------------------------------------------------
oooooo oooo ooooo ooooooooo.
`888. .8' `888' `888 `Y88.
`888. .8' 888 888 .d88'
`888. .8' 888 888ooo88P'
`888.8' 888 888
`888' 888 888
`8' o888o o888o
---------------------------------------------------
Vortex Image Processing package
---------------------------------------------------


Introduction
------------

Expand Down
20 changes: 20 additions & 0 deletions docs/source/vip_hci.leastsq.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
vip\_hci\.leastsq package
=========================

.. automodule:: vip_hci.leastsq
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

vip\_hci\.leastsq\.leastsq module
---------------------------------

.. automodule:: vip_hci.leastsq.leastsq
:members:
:undoc-members:
:show-inheritance:


8 changes: 8 additions & 0 deletions docs/source/vip_hci.madi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ vip\_hci\.madi\.adi\_source module
:undoc-members:
:show-inheritance:

vip\_hci\.madi\.adi\_utils module
---------------------------------

.. automodule:: vip_hci.madi.adi_utils
:members:
:undoc-members:
:show-inheritance:


1 change: 1 addition & 0 deletions docs/source/vip_hci.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Subpackages
vip_hci.exlib
vip_hci.fits
vip_hci.frdiff
vip_hci.leastsq
vip_hci.llsg
vip_hci.madi
vip_hci.negfc
Expand Down
16 changes: 16 additions & 0 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ VIP - Vortex Image Processing package
:target: http://vip.readthedocs.io/en/latest/?badge=latest



::

---------------------------------------------------
oooooo oooo ooooo ooooooooo.
`888. .8' `888' `888 `Y88.
`888. .8' 888 888 .d88'
`888. .8' 888 888ooo88P'
`888.8' 888 888
`888' 888 888
`8' o888o o888o
---------------------------------------------------
Vortex Image Processing package
---------------------------------------------------


Introduction
------------

Expand Down
12 changes: 1 addition & 11 deletions vip_hci/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,5 @@
__version__ = "0.9.2"


print("---------------------------------------------------")
print(" oooooo oooo ooooo ooooooooo. ")
print(" `888. .8' `888' `888 `Y88. ")
print(" `888. .8' 888 888 .d88' ")
print(" `888. .8' 888 888ooo88P' ")
print(" `888.8' 888 888 ")
print(" `888' 888 888 ")
print(" `8' o888o o888o ")
print("---------------------------------------------------")
print(" Vortex Image Processing package v"+__version__)
print("---------------------------------------------------")


2 changes: 1 addition & 1 deletion vip_hci/conf/utils_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from __future__ import print_function

__author__ = 'Carlos Alberto Gomez Gonzalez'
__all__ = ['eval_func_tuple', 'redirect_output', 'sep']
__all__ = []

import sys

Expand Down
2 changes: 1 addition & 1 deletion vip_hci/frdiff/framediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ..conf import time_ini, timing
from ..pca.utils_pca import pca_annulus
from ..madi.adi_utils import _compute_pa_thresh, _find_indices, _define_annuli
from ..conf import eval_func_tuple as EFT
from ..conf.utils_conf import eval_func_tuple as EFT


array = None
Expand Down
2 changes: 1 addition & 1 deletion vip_hci/leastsq/leastsq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ..preproc import cube_derotate, cube_collapse, check_pa_vector
from ..conf import time_ini, timing
from ..madi.adi_utils import _find_indices, _define_annuli
from ..conf import eval_func_tuple as EFT
from ..conf.utils_conf import eval_func_tuple as EFT


array = None
Expand Down
2 changes: 1 addition & 1 deletion vip_hci/llsg/llsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ..var import get_annulus_segments, cube_filter_highpass
from ..pca.svd import svd_wrapper, get_eigenvectors
from .thresholding import thresholding
from ..conf import eval_func_tuple as EFT
from ..conf.utils_conf import eval_func_tuple as EFT


cube_init = None
Expand Down
2 changes: 1 addition & 1 deletion vip_hci/madi/adi_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..conf import time_ini, timing
from ..var import get_annulus, mask_circle
from ..preproc import cube_derotate, cube_collapse, check_pa_vector
from ..conf import eval_func_tuple as EFT
from ..conf.utils_conf import eval_func_tuple as EFT
from .adi_utils import _find_indices, _define_annuli


Expand Down
3 changes: 2 additions & 1 deletion vip_hci/negfc/mcmc_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from scipy.stats import norm
from ..fits import open_adicube, open_fits
from ..phot import cube_inject_companions
from ..conf import time_ini, timing, sep
from ..conf import time_ini, timing
from ..conf.utils_conf import sep
from .simplex_fmerit import get_values_optimize
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand Down
4 changes: 3 additions & 1 deletion vip_hci/negfc/simplex_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from scipy.optimize import minimize
from .simplex_fmerit import chisquare
from ..var import frame_center
from ..conf import time_ini, timing, sep
from ..conf import time_ini, timing
from ..conf.utils_conf import sep


__all__ = ['firstguess']

Expand Down
2 changes: 1 addition & 1 deletion vip_hci/pca/pca_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from multiprocessing import Pool, cpu_count
from ..preproc import cube_derotate, cube_collapse, check_pa_vector
from ..conf import time_ini, timing
from ..conf import eval_func_tuple as EFT
from ..conf.utils_conf import eval_func_tuple as EFT
from ..var import get_annulus_segments, matrix_scaling, get_annulus
from ..madi.adi_utils import _find_indices, _define_annuli
from ..stats import descriptive_stats
Expand Down
84 changes: 56 additions & 28 deletions vip_hci/pca/svd.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


def svd_wrapper(matrix, mode, ncomp, debug, verbose, usv=False,
random_state=None):
random_state=None, to_numpy=True):
""" Wrapper for different SVD libraries (CPU and GPU).
Parameters
Expand Down Expand Up @@ -76,9 +76,17 @@ def svd_wrapper(matrix, mode, ncomp, debug, verbose, usv=False,
If True the explained variance ratio is computed and displayed.
verbose: bool
If True intermediate information is printed out.
usv : {False, True}, bool optional
usv : bool optional
If True the 3 terms of the SVD factorization are returned.
random_state : int, RandomState instance or None, optional
If int, random_state is the seed used by the random number generator.
If RandomState instance, random_state is the random number generator.
If None, the random number generator is the RandomState instance used
by np.random. Used for ``randsvd`` mode.
to_numpy : bool, optional
If True (by default) the arrays computed in GPU are transferred from
VRAM and converted to numpy ndarrays.
Returns
-------
The right singular vectors of the input matrix. If ``usv`` is True it
Expand Down Expand Up @@ -250,22 +258,26 @@ def reconstruction(ncomp, U, S, V, var=1):
u_gpu, s_gpu, vh_gpu = cupy.linalg.svd(a_gpu, full_matrices=True,
compute_uv=True)
V = vh_gpu[:ncomp]
V = cupy.asnumpy(V)
if to_numpy:
V = cupy.asnumpy(V)
if usv:
S = s_gpu[:ncomp]
S = cupy.asnumpy(S)
if to_numpy:
S = cupy.asnumpy(S)
U = u_gpu[:, :ncomp]
U = cupy.asnumpy(U)
if to_numpy:
U = cupy.asnumpy(U)
if verbose:
print('Done SVD/PCA with cupy (GPU)')

elif mode == 'randcupy':
if no_cupy:
raise RuntimeError('Cupy is not installed')
U, S, V = randomized_svd_gpu(matrix, ncomp, n_iter=2, lib='cupy')
V = cupy.asnumpy(V)
S = cupy.asnumpy(S)
U = cupy.asnumpy(U)
if to_numpy:
V = cupy.asnumpy(V)
S = cupy.asnumpy(S)
U = cupy.asnumpy(U)
if debug:
reconstruction(ncomp, U, S, V)
if verbose:
Expand All @@ -286,43 +298,52 @@ def reconstruction(ncomp, U, S, V, var=1):
for i in range(V.shape[1]):
V[:, i] /= S # scaling by the square root of eigenvalues
V = V[:ncomp]
V = cupy.asnumpy(V)
S = cupy.asnumpy(S)
if to_numpy:
V = cupy.asnumpy(V)
if verbose:
print('Done PCA with cupy eigh function (GPU)')

elif mode == 'pytorch':
if no_torch:
raise RuntimeError('Pytorch is not installed')
a_gpu = torch.Tensor.cuda(torch.from_numpy(matrix.T))
a_gpu = torch.Tensor.cuda(torch.from_numpy(matrix.astype('float32').T))
u_gpu, s_gpu, vh_gpu = torch.svd(a_gpu)
V = np.array(vh_gpu)[:ncomp]
S = np.array(s_gpu)[:ncomp]
U = np.array(u_gpu)[:, :ncomp]
V = vh_gpu[:ncomp]
S = s_gpu[:ncomp]
U = torch.transpose(u_gpu, 0, 1)[:ncomp]
if to_numpy:
V = np.array(V)
S = np.array(S)
U = np.array(U)
if verbose:
print('Done SVD/PCA with pytorch (GPU)')

elif mode == 'eigenpytorch':
if no_torch:
raise RuntimeError('Pytorch is not installed')
a_gpu = torch.Tensor.cuda(torch.from_numpy(matrix))
a_gpu = torch.Tensor.cuda(torch.from_numpy(matrix.astype('float32')))
C = torch.mm(a_gpu, torch.transpose(a_gpu, 0, 1))
e, EV = torch.eig(C, eigenvectors=True)
pc = torch.mm(torch.transpose(EV, 0, 1), a_gpu)
V = np.array(pc)
S = np.array(torch.sqrt(e[:, 0]))
V = torch.mm(torch.transpose(EV, 0, 1), a_gpu)
S = torch.sqrt(e[:, 0])
if debug:
reconstruction(ncomp, None, S, None)
for i in range(V.shape[1]):
V[:, i] /= S
V = np.array(V)[:ncomp]
V = V[:ncomp]
if to_numpy:
V = np.array(V)
if verbose:
print('Done PCA with pytorch eig function')

elif mode == 'randpytorch':
if no_torch:
raise RuntimeError('Pytorch is not installed')
U, S, V = randomized_svd_gpu(matrix, ncomp, n_iter=2, lib='pytorch')
if to_numpy:
V = np.array(V)
S = np.array(S)
U = np.array(U)
if debug:
reconstruction(ncomp, U, S, V)
if verbose:
Expand All @@ -332,13 +353,20 @@ def reconstruction(ncomp, U, S, V, var=1):
raise ValueError('The SVD mode is not available')

if usv:
if mode == 'lapack' or mode=='pytorch':
if mode == 'lapack':
return V.T, S, U.T
elif mode == 'pytorch':
if to_numpy:
return V.T, S, U.T
else:
return torch.transpose(V, 0, 1), S, torch.transpose(U, 0, 1)
else:
return U, S, V
else:
if mode == 'lapack' or mode=='pytorch':
if mode == 'lapack':
return U.T
elif mode == 'pytorch':
return U
else:
return V

Expand Down Expand Up @@ -512,22 +540,22 @@ def randomized_svd_gpu(M, n_components, n_oversamples=10, n_iter='auto',
return U[:, :n_components], s[:n_components], V[:n_components, :]

elif lib == 'pytorch':
M = torch.Tensor.cuda(torch.from_numpy(M.astype('float32')))
M_gpu = torch.Tensor.cuda(torch.from_numpy(M.astype('float32')))

# Generating normal random vectors with shape: (M.shape[1], n_random)
Q = torch.cuda.FloatTensor(M.shape[1], n_random).normal_()
Q = torch.cuda.FloatTensor(M_gpu.shape[1], n_random).normal_()

# Perform power iterations with Q to further 'imprint' the top
# singular vectors of M in Q
for i in range(n_iter):
Q = torch.mm(M, Q)
Q = torch.mm(torch.transpose(M, 0, 1), Q)
Q = torch.mm(M_gpu, Q)
Q = torch.mm(torch.transpose(M_gpu, 0, 1), Q)

# Sample the range of M using by linear projection of Q. Extract an orthonormal basis
Q, _ = torch.qr(torch.mm(M, Q))
Q, _ = torch.qr(torch.mm(M_gpu, Q))

# project M to the (k + p) dimensional space using the basis vectors
B = torch.mm(torch.transpose(Q, 0, 1), M)
B = torch.mm(torch.transpose(Q, 0, 1), M_gpu)

# compute the SVD on the thin matrix: (k + p) wide
Uhat, s, V = torch.svd(B)
Expand Down
3 changes: 2 additions & 1 deletion vip_hci/phot/contrcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from skimage.draw import circle
from matplotlib import pyplot as plt
from .fakecomp import cube_inject_companions, frame_inject_companion, psf_norm
from ..conf import time_ini, timing, sep
from ..conf import time_ini, timing
from ..conf.utils_conf import sep
from ..var import frame_center, dist


Expand Down
2 changes: 1 addition & 1 deletion vip_hci/phot/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from skimage.feature import peak_local_max
from ..var import (mask_circle, pp_subplots, get_square, frame_center,
frame_filter_gaussian2d, fit_2dgaussian)
from ..conf import sep
from ..conf.utils_conf import sep
from .snr import snr_ss
from .frame_analysis import frame_quick_report

Expand Down
2 changes: 1 addition & 1 deletion vip_hci/phot/frame_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import photutils
from skimage import draw
from .snr import snr_ss
from ..conf import sep
from ..conf.utils_conf import sep


def frame_quick_report(array, fwhm, source_xy=None, verbose=True):
Expand Down
3 changes: 2 additions & 1 deletion vip_hci/phot/snr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from astropy.convolution import convolve, Tophat2DKernel
from astropy.stats import median_absolute_deviation as mad
from multiprocessing import Pool, cpu_count
from ..conf import eval_func_tuple, time_ini, timing
from ..conf.utils_conf import eval_func_tuple
from ..conf import time_ini, timing
from ..var import get_annulus, frame_center, dist, pp_subplots


Expand Down
Loading

0 comments on commit 6d3f090

Please sign in to comment.