Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, data, segment_img, label, filtered_data=None,
error=None, mask=None, background=None, wcs=None):
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('segment_img and data must have the same shape.')
inputs = (data, filtered_data, error, background)
has_unit = [hasattr(x, 'unit') for x in inputs if x is not None]
use_units = all(has_unit)
if any(has_unit) and not use_units:
raise ValueError('If any of data, filtered_data, error, or '
'background has units, then they all must have '
'the same units.')
if use_units:
self._data_unit = data.unit
else:
def __init__(self, data, segment_img, label, filtered_data=None,
error=None, mask=None, background=None, wcs=None):
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('segment_img and data must have the same shape.')
inputs = (data, filtered_data, error, background)
has_unit = [hasattr(x, 'unit') for x in inputs if x is not None]
use_units = all(has_unit)
if any(has_unit) and not use_units:
raise ValueError('If any of data, filtered_data, error, or '
'background has units, then they all must have '
'the same units.')
if use_units:
self._data_unit = data.unit
else:
self._data_unit = 1
relabel = False
# if the are more sources at the upper level, then
# remove the parent source(s) from the lower level,
# but keep any sources in the lower level that do not have
# multiple children in the upper level
for label in segments[i].labels:
mask = (segm_lower == label)
# checks for 1-to-1 label mapping n -> m (where m >= 0)
upper_labels = segm_upper[mask]
upper_labels = np.unique(upper_labels[upper_labels != 0])
if upper_labels.size >= 2:
relabel = True
segm_lower[mask] = segm_upper[mask]
if relabel:
segm_new = object.__new__(SegmentationImage)
segm_new._data = ndilabel(segm_lower, structure=selem)[0]
segments[i + 1] = segm_new
else:
segments[i + 1] = segments[i]
# Deblend using watershed. If any sources do not meet the
# contrast criterion, then remove the faintest such source and
# repeat until all sources meet the contrast criterion.
markers = segments[-1].data
mask = segment_img.data.astype(bool)
remove_marker = True
while remove_marker:
markers = watershed(-data, markers, mask=mask, connectivity=selem)
labels = np.unique(markers[markers != 0])
flux_frac = np.array([np.sum(data[markers == label])
Print some properties of the second object (labeled with ``2`` in
the segmentation image):
>>> props[1].id # id corresponds to segment label number
2
>>> props[1].centroid # doctest: +FLOAT_CMP
>>> props[1].perimeter # doctest: +FLOAT_CMP
>>> props[1].orientation # doctest: +FLOAT_CMP
"""
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('segment_img and data must have the same shape.')
# filter the data once, instead of repeating for each source
if filter_kernel is not None:
filtered_data = _filter_data(data, filter_kernel, mode='constant',
fill_value=0.0, check_normalization=True)
else:
filtered_data = None
if labels is None:
labels = segment_img.labels
labels = np.atleast_1d(labels)
sources_props = []
4.0
Print some properties of the second object (labeled with ``2`` in
the segmentation image):
>>> props[1].id # id corresponds to segment label number
2
>>> props[1].centroid # doctest: +FLOAT_CMP
>>> props[1].perimeter # doctest: +FLOAT_CMP
>>> props[1].orientation # doctest: +FLOAT_CMP
"""
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('segment_img and data must have the same shape.')
# filter the data once, instead of repeating for each source
if filter_kernel is not None:
filtered_data = _filter_data(data, filter_kernel, mode='constant',
fill_value=0.0, check_normalization=True)
else:
filtered_data = None
if labels is None:
labels = segment_img.labels
labels = np.atleast_1d(labels)
relabeled such that the labels are in consecutive order starting
from 1.
Returns
-------
segment_image : `~photutils.segmentation.SegmentationImage`
A segmentation image, with the same shape as ``data``, where
sources are marked by different positive integer values. A
value of zero is reserved for the background.
See Also
--------
:func:`photutils.segmentation.detect_sources`
"""
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('The data and segmentation image must have '
'the same shape')
if labels is None:
labels = segment_img.labels
labels = np.atleast_1d(labels)
segment_img.check_labels(labels)
if filter_kernel is not None:
data = _filter_data(data, filter_kernel, mode='constant',
fill_value=0.0)
last_label = segment_img.max_label
from 1.
Returns
-------
segment_image : `~photutils.segmentation.SegmentationImage`
A segmentation image, with the same shape as ``data``, where
sources are marked by different positive integer values. A
value of zero is reserved for the background.
See Also
--------
:func:`photutils.segmentation.detect_sources`
"""
if not isinstance(segment_img, SegmentationImage):
segment_img = SegmentationImage(segment_img)
if segment_img.shape != data.shape:
raise ValueError('The data and segmentation image must have '
'the same shape')
if labels is None:
labels = segment_img.labels
labels = np.atleast_1d(labels)
segment_img.check_labels(labels)
if filter_kernel is not None:
data = _filter_data(data, filter_kernel, mode='constant',
fill_value=0.0)
last_label = segment_img.max_label
segm_deblended = object.__new__(SegmentationImage)
segm_slices = ndimage.find_objects(segm_img)
for i, slices in enumerate(segm_slices):
cutout = segm_img[slices]
segment_mask = (cutout == (i+1))
if np.count_nonzero(segment_mask) < npixels:
cutout[segment_mask] = 0
if np.count_nonzero(segm_img) == 0:
warnings.warn('No sources were found.', NoDetectionsWarning)
if deblend_skip:
continue
else:
segms.append(None)
continue
segm = object.__new__(SegmentationImage)
segm._data = segm_img
if deblend_skip and segm.nlabels == 1:
continue
else:
segm.relabel_consecutive()
segms.append(segm)
return segms