Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pixel variance import #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,11 @@ def _calcCovarianceMatrix(self, method='focal', nthreads=None):
self.config.adaptive_subsampling)\
.reshape(nleaves, nleaves)

if self.quadtree.leaf_mean_px_var is not None:
if self.quadtree.leaf_mat_mean_px_var is not None:
self._log.debug(
'Adding variance from scene.displacement_px_var')
cov_matrix[num.diag_indices_from(cov_matrix)] +=\
self.quadtree.leaf_mean_px_var
self.quadtree.leaf_mat_mean_px_var

else:
raise TypeError('Covariance calculation %s method not defined!'
Expand Down
32 changes: 31 additions & 1 deletion src/quadtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,32 @@ def var(self):

@property_cached
def mean_px_var(self):
""" Variance of displacement
""" Mean pixel variance of displacement in leaf
:type: float
"""
if self.displacement_px_var is not None:
return float(num.nanmean(self.displacement_px_var))

return None

@property_cached
def mat_mean_px_var(self):
""" Mean matrix pixel variance in leaf

Mean value of Matrix with pixel variance in
:type: float
"""
if self.displacement_px_var is not None:
nvalid_leaf = num.sum(num.isfinite(self.displacement_px_var))
nmat = nvalid_leaf * nvalid_leaf
if nmat>0.:
return float(num.nansum(self.displacement_px_var)/nmat)
else:
return float(0.)

return None


@property_cached
def corr_median(self):
""" Standard deviation of node's displacement corrected for median
Expand Down Expand Up @@ -760,6 +779,17 @@ def leaf_mean_px_var(self):
if self.scene.displacement_px_var is not None:
return num.array([lf.mean_px_var for lf in self.leaves])
return None

@property
def leaf_mat_mean_px_var(self):
"""
:getter: Mean pixel variance in each quadtree,
if :attr:`kite.Scene.displacement_px_var` is set.
:type: :class:`numpy.ndarray`, size ``N``.
"""
if self.scene.displacement_px_var is not None:
return num.array([lf.mat_mean_px_var for lf in self.leaves])
return None

@property_cached
def leaf_means(self):
Expand Down
24 changes: 18 additions & 6 deletions src/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ def save(self, filename=None):
_file, ext = op.splitext(filename)
filename = _file if ext in ['.yml', '.npz'] else filename

components = ['_displacement', 'theta', 'phi']
components = ['_displacement', 'theta', 'phi', 'displacement_px_var']
self._log.debug('Saving scene data to %s.npz' % filename)

num.savez('%s.npz' % (filename),
Expand Down Expand Up @@ -956,6 +956,9 @@ def load(cls, filename):
displacement = data['arr_0']
theta = data['arr_1']
phi = data['arr_2']
if num.shape(data)[0]>3:
displacement_px_var = data['arr_3']

except IOError:
raise UserIOWarning('Could not load data from %s.npz' % basename)

Expand All @@ -965,11 +968,20 @@ def load(cls, filename):
except IOError:
raise UserIOWarning('Could not load %s.yml' % basename)

scene = cls(
displacement=displacement,
theta=theta,
phi=phi,
config=config)
if num.shape(data)[0]>3:
scene = cls(
displacement=displacement,
theta=theta,
phi=phi,
displacement_px_var = displacement_px_var,
config=config)
else:
scene = cls(
displacement=displacement,
theta=theta,
phi=phi,
config=config)

scene._log.debug('Loading from %s[.npz,.yml]', basename)

scene.meta.filename = filename
Expand Down