예제 #1
0
    def from_file(path, scale):
        ''' Reads a file into a new RGBImage instance, must be 24bpp/8bpc

        Args:
            path (`string`): path to a file.

            scale (`float`): pixel scale, in microns.

        Returns:
            `RGBImage`: a new image object.

        Notes:
            TODO: proper handling of images with more than 8bpp.
        '''
        # img is an mxnx3 array of unit8s
        img = imread(path).astype(config.precision) / 255

        img = np.flip(img, axis=0)

        # crop the image if it has an odd dimension.
        # TODO: change this an understand why it is an issue
        # fftshift vs ifftshift?
        if is_odd(img.shape[0]):
            img = img[0:-1, :, :]
        if is_odd(img.shape[1]):
            img = img[:, 0:-1, :]
        return RGBImage(r=img[:, :, 0],
                        g=img[:, :, 1],
                        b=img[:, :, 2],
                        sample_spacing=scale,
                        synthetic=False)
예제 #2
0
def test_is_odd_even_numbers(num):
    assert not util.is_odd(num)
예제 #3
0
def test_is_odd_odd_numbers(num):
    assert util.is_odd(num)
예제 #4
0
파일: detector.py 프로젝트: fossabot/prysm
def bindown(array, nsamples_x, nsamples_y=None, mode='avg'):
    ''' Uses summation to bindown (resample) an array.

    Args:
        nsamples_x (`int`): number of samples in x axis to bin by.

        nsamples_y (`int`): number of samples in y axis to bin by.  If None,
            duplicates value from nsamples_x.

        mode (`str`): sum or avg, how to adjust the output signal.

    Returns:
        `numpy.ndarray`: ndarray binned by given number of samples.

    Notes:
        Array should be 2D.  TODO: patch to allow 3D data.

        If the size of `array` is not evenly divisible by the number of samples,
        the algorithm will trim around the border of the array.  If the trim
        length is odd, one extra sample will be lost on the left side as opposed
        to the right side.

    '''
    if nsamples_y is None:
        nsamples_y = nsamples_x

    if nsamples_x == 1 and nsamples_y == 1:
        return array

    # determine amount we need to trim the array
    samples_x, samples_y = array.shape
    total_samples_x = samples_x // nsamples_x
    total_samples_y = samples_y // nsamples_y
    final_idx_x = total_samples_x * nsamples_x
    final_idx_y = total_samples_y * nsamples_y

    residual_x = int(samples_x - final_idx_x)
    residual_y = int(samples_y - final_idx_y)

    # if the amount to trim is symmetric, trim symmetrically.
    if not is_odd(residual_x) and not is_odd(residual_y):
        samples_to_trim_x = residual_x // 2
        samples_to_trim_y = residual_y // 2
        trimmed_data = array[samples_to_trim_x:final_idx_x + samples_to_trim_x,
                             samples_to_trim_y:final_idx_y + samples_to_trim_y]
    # if not, trim more on the left.
    else:
        samples_tmp_x = (samples_x - final_idx_x) // 2
        samples_tmp_y = (samples_y - final_idx_y) // 2
        samples_top = int(floor(samples_tmp_y))
        samples_bottom = int(ceil(samples_tmp_y))
        samples_left = int(ceil(samples_tmp_x))
        samples_right = int(floor(samples_tmp_x))
        trimmed_data = array[samples_left:final_idx_x + samples_right,
                             samples_bottom:final_idx_y + samples_top]

    intermediate_view = trimmed_data.reshape(total_samples_x, nsamples_x,
                                             total_samples_y, nsamples_y)

    if mode.lower() in ('avg', 'average', 'mean'):
        output_data = intermediate_view.mean(axis=(1, 3))
    elif mode.lower() == 'sum':
        output_data = intermediate_view.sum(axis=(1, 3))
    else:
        raise ValueError('mode must be average of sum.')

    # trim as needed to make even number of samples.
    # TODO: allow work with images that are of odd dimensions
    px_x, px_y = output_data.shape
    trim_x, trim_y = 0, 0
    if is_odd(px_x):
        trim_x = 1
    if is_odd(px_y):
        trim_y = 1

    return output_data[:px_y - trim_y, :px_x - trim_x]