Ejemplo n.º 1
0
def PCAStream(Nimgs=100, n_components=16):
    '''
        This runs principle component analysis on the last Nimgs

        **Stream Inputs**
            image : 2d np.ndarray
                the nth image

        **Stream Outputs**
            components : 2d np.ndarray
                the components

        Returns
        -------
            sin : Stream instance
                the input stream

            sout : Stream instance
                the output stream
    '''
    sin = sc.Stream(stream_name="PCA Stream")
    sout = sin.sliding_window(Nimgs)
    sout = scs.select(sin, ('image', 'data'))
    sout = scs.squash(sin)
    sout = scs.map(sout, PCA_fit, n_components=n_components)

    return sin, sout
Ejemplo n.º 2
0
def ThumbStream(blur=None, crop=None, resize=None):
    ''' Thumbnail stream

        **Stream Inputs**
            image : 2d np.ndarray
                the image

        **Stream Outputs**
            sin : Stream instance
                the stream input

            sout : Stream instance
                the stream output

        Parameters
        ----------
            blur : float, optional
                the sigma of the Gaussian kernel to convolve image with
                for smoothing
                default is None, no smoothing

            crop : 4 tuple of int, optional
                the boundaries to crop by
                default is None, no cropping

            resize : int, optional
                the factor to resize by
                for example resize=2 performs 2x2 binning of the image

        Stream Inputs
        -------------
            image : 2d np.ndarray
                the image

        Returns
        -------
            sin :
                the stream input

            sout :
                the stream output
    '''
    # TODO add flags to actually process into thumbs
    sin = sc.Stream(stream_name="Thumbnail Stream")
    sout = scs.add_attributes(sin, stream_name="thumb")
    # s1 = sin.add_attributes(stream_name="ThumbStream")
    sout = scs.map(_blur, sout, sigma=blur, remote=True)
    sout = scs.map(_crop, sout, crop=crop, remote=True)
    sout = scs.map(_resize, sout, resize=resize, remote=True)
    # change the key from image to thumb
    sout = scs.select(sout, ('image', 'thumb'))

    return sin, sout
Ejemplo n.º 3
0
def PeakFindingStream(name='peakfind'):
    ''' Find peaks in 1d line data.

        **Stream Inputs**
            sqx : 1d np.ndarray
                the x domain of the curve

            sqy : 1d np.ndarray
                the y domain of the curve

        **Stream Outputs**
            model: lmfit.Model instance
                The model for the fit

            y_origin:

            inds_peak: list
               the peak indices

            xdata:

            ratio:

            ydata:

            wdata:

            bkgd:

            variance:

            variance_mean:

            peaksx:

            peaksy:

        Parameters
        ----------
        name : str, optional
            name of stream
    '''
    sin = sc.Stream(stream_name="Peak Finder")
    # pkfind stream
    sout = scs.map(call_peak, scs.select(sin, 'sqy', 'sqx'))
    sout = scs.add_attributes(sout, stream_name=name)
    return sin, sout
Ejemplo n.º 4
0
def ImageTaggingStream():
    ''' Creates an image tagging stream.

        This stream will take in an image and output a tage as to what the
        image is.

        **Stream Inputs**
            image : 2d np.ndarray
                the image to be tagged

        **Stream Outputs**
            tag_name : str
                the name of the tag for the image
    '''
    sin = sc.Stream(stream_name="Image Tagger")
    sout = scs.map(infer, scs.select(sin, 'image'))
    sout = scs.add_attributes(sout, stream_name="image-tag")
    return sin, sout
Ejemplo n.º 5
0
def AngularCorrelatorStream(bins=(800, 360)):
    ''' Stream to run angular correlations.

        **Stream Inputs**
            image : 2d np.ndarray
                the image to run the angular correltions on

            mask : 2d np.ndarray
                the mask

            origin : 2 tuple
                the beam center of the image

            bins : tuple
                the number of bins in q and phi

            method : string, optional
                the method to use for the angular correlations
                defaults to 'bgest'

            q_map : the q_map to be used

        **Stream Outputs**
            sin : Stream instance
                the stream input

            sout : Stream instance
                the stream output
    '''
    # TODO : Allow optional kwargs in streams
    sin = sc.Stream(stream_name="Angular Correlation")
    sout = scs.select(sin, ('image', 'image'), ('mask', 'mask'),
                      ('origin', 'origin'), ('q_map', 'r_map'))
    sout = scs.map(angular_corr, sout, bins=bins)
    sout = scs.add_attributes(sout, stream_name="angular-corr")
    return sin, sout
Ejemplo n.º 6
0
s_stitch = scs.map(get_stitch, sout_attributes)
# name the stream for proper output

# circular average
sin_circavg, sout_circavg = CircularAverageStream()
s_imgmaskcalib.connect(sin_circavg)
# L_circavg = sout_circavg.sink_to_list()

# peak finding
sin_peakfind, sout_peakfind = PeakFindingStream()
sout_circavg.connect(sin_peakfind)

# merge with sq
sout_sqpeaks = scs.merge(
    sc.zip(sout_circavg,
           scs.select(sout_peakfind, 'inds_peak', 'peaksx', 'peaksy')))

# L_sqpeaks = sout_sqpeaks.sink_to_list()


def normexposure(image, exposure_time):
    return dict(image=image / exposure_time)


# image stitching
# normalize by exposure time
s_imagenorm = scs.map(normexposure, scs.merge(sc.zip(s_exposure, s_image)))
# use this for image stitch
s_imgmaskoriginstitch = scs.merge(
    sc.zip(s_imagenorm, s_mask, s_origin, s_stitch))
#s_imgmaskoriginstitch.sink(lambda *x, **x2: print("sin: {} {}".format(x, x2)))
Ejemplo n.º 7
0
def ImageStitchingStream(return_intermediate=False):
    '''
        Image stitching

        **Stream Inputs**
            image : 2d np.ndarray
                the image for the stitching

            mask : 2d np.ndarray
                the mask

            origin : 2 tuple
                the beam center

            stitchback : bool
                whether or not to stitchback to previous image

        **Stream Outputs**
            image : 2d np.ndarray
                the stitched image

            mask : 2d np.ndarray
                the mask from the stitch

            origin : 2 tuple
                the beam center

            stitchback : bool
                whether or not to stitchback to previous image

        Returns
        -------
            sin : Stream instance
                the input stream

            sout : Stream instance
                the output stream

        Parameters
        ----------
            return_intermediate : bool, optional
                decide whether to return intermediate results or not
                defaults to False

        Notes
        -----
        Any normalization of images (for ex: by exposure time) should be done
        before inputting to this stream.

        Examples
        --------
        >>> sin, sout = ImageStitchingStream()
        >>> L = sout.sink_to_list()
        >>> mask = np.ones((10, 10), dtype=np.int64)
        >>> img1 = np.ones_like(mask, dtype=float)
        >>> # 3 rows are higher
        >>> img1[2:4] = 2
        >>> # some arb value
        >>> origin1 = [2, 3]
        >>> # roll along zero axis
        >>> img2 = np.roll(img1, 2, axis=0)
        >>> # rolled by two
        >>> origin2 = [2+2, 3]
        >>> # first image, stitchback can be anything
        >>> sdoc1 = StreamDoc(kwargs=dict(mask=mask, image=img1,
        ...                               origin=origin1,
        ...                               stitchback=False))
        >>> sin.emit(sdoc1)
        >>> # emit a second image and it will be stitched
        >>> sdoc2 = StreamDoc(kwargs=dict(mask=mask, image=img2,
        ...                               origin=origin2,
        ...                               stitchback=True))
        >>> sin.emit(sdoc2)
        >>> # A new image with False stitchback will have output
        >>> # stream output a result
        >>> img3 = np.random.random((10,10))
        >>> origin3 = (0,0)
        >>> sdoc3 = StreamDoc(kwargs=dict(mask=mask, image=img3,
        ...                               origin=origin3,
        ...                               stitchback=False))
        >>> sin.emit(sdoc3)
        >>> len(L) == 1
        True
        >>> # the stitched image is here:
        >>> img = L[0]['kwargs']['image']
    '''

    # TODO : add state. When False returned, need a reason why
    def validate(x):
        if not hasattr(x, 'kwargs'):
            raise ValueError("No kwargs")
        kwargs = x['kwargs']
        expected = ['mask', 'origin', 'stitchback', 'image']
        for key in expected:
            if key not in kwargs:
                message = "{} not in kwargs".format(key)
                raise ValueError(message)
        if not isinstance(kwargs['mask'], np.ndarray):
            message = "mask is not array"
            raise ValueError(message)

        if not isinstance(kwargs['image'], np.ndarray):
            message = "image is not array"
            raise ValueError(message)

        if len(kwargs['origin']) != 2:
            message = "origin not length 2"
            raise ValueError(message)
        return x

    # TODO : remove the add_attributes part and just keep stream_name
    sin = sc.Stream(stream_name="Image Stitching Stream")
    # sout = sc.map(sin, validate)
    # sin.map(lambda x : print("Beginning of stream data\n\n\n"))
    # TODO : remove compute requirement
    # TODO : incomplete
    sout = scs.add_attributes(sin, stream_name="stitch")

    sout = scs.select(sout, ('image', None), ('mask', None), ('origin', None),
                      ('stitchback', None))

    # put all args into a tuple
    def pack(*args):
        return args

    sout = scs.map(pack, sout)
    # sout = scs.map(s3.map(psdm(pack))
    sout = scs.accumulate(_xystitch_accumulate, sout)

    sout = scs.map(scs.star(_xystitch_result), sout)

    def stitchbackcomplete(xtuple):
        ''' only plot images whose stitch is complete, and only involved more
        than one image
        NOTE : *Only* the bool "True" will activate a stitch. "1" does not
        count. This is handled by checking 'is True' and 'is not True'
        '''
        # previous must have been true for stitch to have involved more than
        # one image
        prev = xtuple[0]['kwargs']['stitchback']
        # next must be False (or just not True) to be complete
        next = xtuple[1]['kwargs']['stitchback']

        return next is not True and prev is True

    # swin.map(lambda x : print("result : {}".format(x)), raw=True)

    # only get results where stitch is stopped
    # NOTE : need to compute before filtering here

    # now emit some dummy value to swin, before connecting more to stream
    # swin.emit(dict(attributes=dict(stitchback=0)))

    # TODO : figure out how to filter
    if not return_intermediate:
        # keep previous two results
        sout = sout.sliding_window(2)
        sout = sout.filter(stitchbackcomplete)
        sout = sout.map(lambda x: x[0])

    return sin, sout