Exemple #1
0
        def findTargets(self, imdata, targetlist):
                imdata_id = imdata.dbid
                self.logger.info(imdata_id)

                # convert to float
                image2 = imdata['image']
                image = numpy.asarray(image2,dtype=numpy.float)
                
                self.setImage(image, 'Image')

                if self.handle is None:
                        self.handle = pymat.open()

                pymat.put(self.handle, 'image', image)
                pymat.put(self.handle, 'image_id',imdata_id)

                self.matlabFindTargets()

                if self.settings['user check']:
                        # user now clicks on targets
                        self.notifyUserSubmit()
                        self.userpause.clear()
                        self.setStatus('user input')
                        self.userpause.wait()

                self.setStatus('processing')

                pymat.put(self.handle, 'image', [])
                pymat.put(self.handle, 'focus', [])
                pymat.put(self.handle, 'acquisition', [])

                self.publishTargets(imdata, 'focus', targetlist)
                self.publishTargets(imdata, 'acquisition', targetlist)

                self.logger.info('Targets have been submitted')
Exemple #2
0
        def targetTestImage(self):
                usercheck = self.settings['user check']
                self.settings['user check'] = False
                filename = self.settings['test image']
                
                try:
                        image2 = mrc.read(filename)             
                        image = numpy.asarray(image2,dtype=numpy.float)
                        
                except:
                        self.logger.error('Failed to load test image')
                        raise
                        return
                self.setImage(image, 'Image')

                if self.handle is None:
                        self.handle = pymat.open()
                pymat.put(self.handle, 'image', image)

                imdata_id = 0

                pymat.put(self.handle, 'image_id',imdata_id)
        
                self.matlabFindTargets()

                self.settings['user check'] = usercheck
Exemple #3
0
    def matlabFindTargets(self):
        pymat.put(self.handle, 'focus', [])
        pymat.put(self.handle, 'acquisition', [])

        d, f = os.path.split(self.settings['module path'])

        if d:
            pymat.eval(self.handle, 'path(path, \'%s\')' % d)

        if not f[:-2]:
            raise RuntimeError

        pymat.eval(self.handle,
                   '[acquisition, focus] = %s(image,image_id)' % f[:-2])

        focus = pymat.get(self.handle, 'focus')
        acquisition = pymat.get(self.handle, 'acquisition')

        self.setTargets(acquisition, 'acquisition')
        self.setTargets(focus, 'focus')
        import time
        time.sleep(1)

        if self.settings['user check']:
            self.panel.foundTargets()
Exemple #4
0
    def __call__(self):

        print 'loading model'

        if self.chunk_size is not None:
            dataset_family = self.dataset_family
            which_set = self.which_set
            dataset_descriptor = self.dataset_family[which_set][size]

            d = serial.load(self.dict_file)
            self.W = d['dictionary'].T

            M.put(s, 'dictionary', d['dictionary'])
            M.put(s, 'lambda', self.lamda)

            self.size = 6

            num_examples = dataset_descriptor.num_examples
            assert num_examples % self.chunk_size == 0

            self.chunk_id = 0
            for i in xrange(0,num_examples, self.chunk_size):
                self.restrict = (i, i + self.chunk_size)

                self._execute()

                self.chunk_id += 1
        else:
            self._execute()
 def transform_points(self, points):
     
     mlabraw.put(self.handle, "points", points)
     mlabraw.eval(self.handle,"""
     points_result = tps_eval(points, params);        
     """)
     points_result = mlabraw.get(self.handle, "points_result")
     return points_result
Exemple #6
0
    def _set(self, name, value):
        r"""Directly set a variable `name` in matlab space to `value`.

        This should normally not be used in user code."""
        if isinstance(value, MlabObjectProxy):
            mlabraw.eval(self._session, "%s = %s;" % (name, value._name))
        else:
            ##             mlabraw.put(self._session, name, self._as_mlabable_type(value))
            mlabraw.put(self._session, name, value)
Exemple #7
0
    def _set(self, name, value):
        r"""Directly set a variable `name` in matlab space to `value`.
        
        This should normally not be used in user code."""
        if isinstance(value, MlabObjectProxy):
            mlabraw.eval(self._session, "%s = %s;" % (name, value._name))
        else:
##             mlabraw.put(self._session, name, self._as_mlabable_type(value))
            mlabraw.put(self._session, name, value)
def branch_points(bw):
    initialize()
    mlabraw.put(MATLAB, "bw",bw)
    mlabraw.eval(MATLAB, """
    bp = bwmorph(bw,'branchpoints')
    bp_d = double(bp);
    """)
    bp_d = mlabraw.get(MATLAB, "bp_d")
    bp =  bp_d.astype('uint8')
    return bp
 def fit_transformation(self, points0, points1):
     assert len(points0) == len(points1)
     mlabraw.put(self.handle, "points0", points0)
     mlabraw.put(self.handle, "points1", points1)
     
     mlabraw.eval(self.handle, """
     opts = opts_fit;
     opts.reg = .01;
     params = tps_fit(points0, points1, opts);
     save('/tmp/after_fitting.mat');
     """)                
def branch_points(bw):
    initialize()
    mlabraw.put(MATLAB, "bw", bw)
    mlabraw.eval(
        MATLAB, """
    bp = bwmorph(bw,'branchpoints')
    bp_d = double(bp);
    """)
    bp_d = mlabraw.get(MATLAB, "bp_d")
    bp = bp_d.astype('uint8')
    return bp
Exemple #11
0
    def _set(self, name, value):
        r"""Directly set a variable `name` in matlab space to `value`.
        
        This should normally not be used in user code."""
        if isinstance(value, MlabObjectProxy):
            mlabraw.eval(self._session, "%s = %s;" % (name, value._name))
        else:
##             mlabraw.put(self._session, name, self._as_mlabable_type(value))

            if isinstance(value, numpy.ndarray):
                value = value.copy() # XXX: mlabraw seems to badly handle strides.
            mlabraw.put(self._session, name, value)
    def transform_poses(self, points, rots):
        mlabraw.put(self.handle, "points", points)                
        put3d(self.handle, "rots", rots)
        
        mlabraw.eval(self.handle,"""
        [points_result, rots_result] = tps_eval_frames(points, rots, params);
        """)
        points_result = mlabraw.get(self.handle,"points_result")
        rots_result = get3d(self.handle, "rots_result")
        
        return points_result, rots_result


        
 def fit_transformation_icp(self, points0, points1):
     mlabraw.put(self.handle, "points0", points0)
     mlabraw.put(self.handle, "points1", points1)
     
     mlabraw.eval(self.handle, """
     opts = opts_icp;
     opts.n_iter=6;
     opts.lines_from_orig=1;
     opts.corr_opts.method='bipartite';
     opts.fit_opts.reg = .01;
     %opts.plot_grid = 1;
     opts.corr_opts.bipartite_opts.deficient_col_penalty=1;
     opts.corr_opts.bipartite_opts.deficient_row_penalty=1;
     params = tps_rpm(points0, points1, opts);
     save('/tmp/after_fitting.mat');
     """)
def remove_holes(labels,min_size):
    initialize()
    mlabraw.put(MATLAB, "L",labels)
    mlabraw.put(MATLAB, "min_size",min_size)
    mlabraw.eval(MATLAB, """
    max_label = max(L(:));
    good_pix = L==0;
    for label = 1:max_label
        good_pix = good_pix | bwareaopen(L==label,min_size,4);    
    end
    bad_pix = ~logical(good_pix);
    
    [~,I] = bwdist(good_pix,'Chessboard');
    NewL = L;
    NewL(bad_pix) = L(I(bad_pix));
    NewL_d = double(NewL);
    """)
    NewL_d = mlabraw.get(MATLAB, "NewL_d")
    return NewL_d.astype('uint8')
def remove_holes(labels, min_size):
    initialize()
    mlabraw.put(MATLAB, "L", labels)
    mlabraw.put(MATLAB, "min_size", min_size)
    mlabraw.eval(
        MATLAB, """
    max_label = max(L(:));
    good_pix = L==0;
    for label = 1:max_label
        good_pix = good_pix | bwareaopen(L==label,min_size,4);    
    end
    bad_pix = ~logical(good_pix);
    
    [~,I] = bwdist(good_pix,'Chessboard');
    NewL = L;
    NewL(bad_pix) = L(I(bad_pix));
    NewL_d = double(NewL);
    """)
    NewL_d = mlabraw.get(MATLAB, "NewL_d")
    return NewL_d.astype('uint8')
        def matlabFindTargets(self):
                pymat.put(self.handle, 'focus', [])
                pymat.put(self.handle, 'acquisition', [])

                d, f = os.path.split(self.settings['module path'])

                if d:
                        pymat.eval(self.handle, 'path(path, \'%s\')' % d)

                if not f[:-2]:
                        raise RuntimeError

                pymat.eval(self.handle, '[acquisition, focus] = %s(image,image_id)' % f[:-2])

                focus = pymat.get(self.handle, 'focus')
                acquisition = pymat.get(self.handle, 'acquisition')

                self.setTargets(acquisition, 'acquisition')
                self.setTargets(focus, 'focus')
                import time
                time.sleep(1)

                if self.settings['user check']:
                        self.panel.foundTargets()
Exemple #17
0
    def _set(self, name, value):
        if isinstance(value, MlabObjectProxy):
            mlabraw.eval(self._session, "%s = %s;" % (name, value._name))
        else:
##             mlabraw.put(self._session, name, self._as_mlabable_type(value))
            mlabraw.put(self._session, name, value)
def put(name, array):
    mlabraw.put(MATLAB, name, array)
def put(name,array):
    mlabraw.put(MATLAB, name, array)
Exemple #20
0
 def set(self, name, value):
     if DEBUG: print "setting '%s' to %s"%(name, `value`)
     mlabraw.put(self._engine, name, value)
Exemple #21
0
    def _do(self, cmd, *args, **kwargs):
        """Semi-raw execution of a matlab command.

        Smartly handle calls to matlab, figure out what to do with `args`,
        and when to use function call syntax and not.

        If no `args` are specified, the ``cmd`` not ``result = cmd()`` form is
        used in Matlab -- this also makes literal Matlab commands legal
        (eg. cmd=``get(gca, 'Children')``).

        If ``nout=0`` is specified, the Matlab command is executed as
        procedure, otherwise it is executed as function (default), nout
        specifying how many values should be returned (default 1).

        **Beware that if you use don't specify ``nout=0`` for a `cmd` that
        never returns a value will raise an error** (because assigning a
        variable to a call that doesn't return a value is illegal in matlab).


        ``cast`` specifies which typecast should be applied to the result
        (e.g. `int`), it defaults to none.

        XXX: should we add ``parens`` parameter?
        """
        handle_out = kwargs.get('handle_out', _flush_write_stdout)
        #self._session = self._session or mlabraw.open()
        # HACK
        if self._autosync_dirs:
            mlabraw.eval(self._session,  "cd('%s');" % os.getcwd().replace("'", "''"))
        nout =  kwargs.get('nout', 1)
        #XXX what to do with matlab screen output
        argnames = []
        tempargs = []
        try:
            for count, arg in enumerate(args):
                if isinstance(arg, MlabObjectProxy):
                    argnames.append(arg._name)
                else:
                    nextName = 'arg%d__' % count
                    argnames.append(nextName)
                    tempargs.append(nextName)
                    # have to convert these by hand
    ##                 try:
    ##                     arg = self._as_mlabable_type(arg)
    ##                 except TypeError:
    ##                     raise TypeError("Illegal argument type (%s.:) for %d. argument" %
    ##                                     (type(arg), type(count)))
                    mlabraw.put(self._session,  argnames[-1], arg)

            if args:
                cmd = "%s(%s)%s" % (cmd, ", ".join(argnames),
                                    ('',';')[kwargs.get('show',0)])
            # got three cases for nout:
            # 0 -> None, 1 -> val, >1 -> [val1, val2, ...]
            if nout == 0:
                handle_out(mlabraw.eval(self._session, cmd))
                return
            # deal with matlab-style multiple value return
            resSL = ((["RES%d__" % i for i in range(nout)]))
            handle_out(mlabraw.eval(self._session, '[%s]=%s;' % (", ".join(resSL), cmd)))
            res = self._get_values(resSL)

            if nout == 1: res = res[0]
            else:         res = tuple(res)
            if kwargs.has_key('cast'):
                if nout == 0: raise TypeError("Can't cast: 0 nout")
                return kwargs['cast'](res)
            else:
                return res
        finally:
            if len(tempargs) and self._clear_call_args:
                mlabraw.eval(self._session, "clear('%s');" %
                             "','".join(tempargs))
Exemple #22
0
    def _execute(self):

        batch_size = self.batch_size
        pooling_region_counts = self.pooling_region_counts
        dataset_family = self.dataset_family
        which_set = self.which_set
        size = self.size

        nan = 0


        dataset_descriptor = dataset_family[which_set][size]

        dataset = dataset_descriptor.dataset_maker()
        expected_num_examples = dataset_descriptor.num_examples

        full_X = dataset.get_design_matrix()
        num_examples = full_X.shape[0]
        assert num_examples == expected_num_examples

        if self.restrict is not None:
            assert self.restrict[1]  <= full_X.shape[0]

            print 'restricting to examples ',self.restrict[0],' through ',self.restrict[1],' exclusive'
            full_X = full_X[self.restrict[0]:self.restrict[1],:]

            assert self.restrict[1] > self.restrict[0]

        #update for after restriction
        num_examples = full_X.shape[0]

        assert num_examples > 0

        dataset.X = None
        dataset.design_loc = None
        dataset.compress = False

        patchifier = ExtractGridPatches( patch_shape = (size,size), patch_stride = (1,1) )

        pipeline = serial.load(dataset_descriptor.pipeline_path)

        assert isinstance(pipeline.items[0], ExtractPatches)
        pipeline.items[0] = patchifier


        print 'defining features'
        Z = T.matrix('Z')

        if self.one_sided:
            feat = abs(Z)
        else:
            pos = T.clip(Z,0.,1e30)
            neg = T.clip(-Z,0.,1e30)

            feat = T.concatenate((pos, neg), axis=1)

        print 'compiling theano function'
        f = function([Z],feat)

        nfeat = self.W.shape[1] * (2 - self.one_sided)
        if not (nfeat == 1600 or nfeat == 3200):
            print nfeat
            assert False

        if config.device.startswith('gpu') and nfeat >= 4000:
            f = halver(f, nfeat)

        topo_feat_var = T.TensorType(broadcastable = (False,False,False,False), dtype='float32')()
        region_features = function([topo_feat_var],
                topo_feat_var.mean(axis=(1,2)) )

        def average_pool( stride ):
            def point( p ):
                return p * ns / stride

            rval = np.zeros( (topo_feat.shape[0], stride, stride, topo_feat.shape[3] ) , dtype = 'float32')

            for i in xrange(stride):
                for j in xrange(stride):
                    rval[:,i,j,:] = region_features( topo_feat[:,point(i):point(i+1), point(j):point(j+1),:] )

            return rval

        outputs = [ np.zeros((num_examples,count,count,nfeat),dtype='float32') for count in pooling_region_counts ]

        assert len(outputs) > 0

        fd = DenseDesignMatrix(X = np.zeros((1,1),dtype='float32'), view_converter = DefaultViewConverter([1, 1, nfeat] ) )

        ns = 32 - size + 1
        depatchifier = ReassembleGridPatches( orig_shape  = (ns, ns), patch_shape=(1,1) )

        if len(range(0,num_examples-batch_size+1,batch_size)) <= 0:
            print num_examples
            print batch_size

        for i in xrange(0,num_examples-batch_size+1,batch_size):
            print i
            t1 = time.time()

            d = copy.copy(dataset)
            d.set_design_matrix(full_X[i:i+batch_size,:])

            t2 = time.time()

            #print '\tapplying preprocessor'
            d.apply_preprocessor(pipeline, can_fit = False)
            X2 = d.get_design_matrix()

            t3 = time.time()

            M.put(s,'batch',X2)

            M.eval(s, 'Z = sparse_codes(batch, dictionary, lambda)')
            Z = M.get(s, 'Z')

            feat = f(np.cast['float32'](Z))

            t4 = time.time()

            assert feat.dtype == 'float32'

            feat_dataset = copy.copy(fd)

            if np.any(np.isnan(feat)):
                nan += np.isnan(feat).sum()
                feat[np.isnan(feat)] = 0

            feat_dataset.set_design_matrix(feat)

            #print '\treassembling features'
            feat_dataset.apply_preprocessor(depatchifier)

            #print '\tmaking topological view'
            topo_feat = feat_dataset.get_topological_view()
            assert topo_feat.shape[0] == batch_size

            t5 = time.time()

            #average pooling
            for output, count in zip(outputs, pooling_region_counts):
                output[i:i+batch_size,...] = average_pool(count)

            t6 = time.time()

            print (t6-t1, t2-t1, t3-t2, t4-t3, t5-t4, t6-t5)

        for output, save_path in zip(outputs, self.save_paths):
            if self.chunk_size is not None:
                assert save_path.endswith('.npy')
                save_path_pieces = save_path.split('.npy')
                assert len(save_path_pieces) == 2
                assert save_path_pieces[1] == ''
                save_path = save_path_pieces[0] + '_' + chr(ord('A')+self.chunk_id)+'.npy'
            np.save(save_path,output)


        if nan > 0:
            warnings.warn(str(nan)+' features were nan')
Exemple #23
0
    def _do(self, cmd, *args, **kwargs):
        """Semi-raw execution of a matlab command.

        Smartly handle calls to matlab, figure out what to do with `args`,
        and when to use function call syntax and not.

        If no `args` are specified, the ``cmd`` not ``result = cmd()`` form is
        used in Matlab -- this also makes literal Matlab commands legal
        (eg. cmd=``get(gca, 'Children')``).

        If ``nout=0`` is specified, the Matlab command is executed as
        procedure, otherwise it is executed as function (default), nout
        specifying how many values should be returned (default 1).

        **Beware that if you use don't specify ``nout=0`` for a `cmd` that
        never returns a value will raise an error** (because assigning a
        variable to a call that doesn't return a value is illegal in matlab).


        ``cast`` specifies which typecast should be applied to the result
        (e.g. `int`), it defaults to none.

        XXX: should we add ``parens`` parameter?
        """
        handle_out = kwargs.get('handle_out', _flush_write_stdout)
        #self._session = self._session or mlabraw.open()
        # HACK
        if self._autosync_dirs:
            mlabraw.eval(self._session,
                         "cd('%s');" % os.getcwd().replace("'", "''"))
        nout = kwargs.get('nout', 1)
        #XXX what to do with matlab screen output
        argnames = []
        tempargs = []
        try:
            for count, arg in enumerate(args):
                if isinstance(arg, MlabObjectProxy):
                    argnames.append(arg._name)
                else:
                    nextName = 'arg%d__' % count
                    argnames.append(nextName)
                    tempargs.append(nextName)
                    # have to convert these by hand
                    ##                 try:
                    ##                     arg = self._as_mlabable_type(arg)
                    ##                 except TypeError:
                    ##                     raise TypeError("Illegal argument type (%s.:) for %d. argument" %
                    ##                                     (type(arg), type(count)))
                    mlabraw.put(self._session, argnames[-1], arg)

            if args:
                cmd = "%s(%s)%s" % (cmd, ", ".join(argnames),
                                    ('', ';')[kwargs.get('show', 0)])
            # got three cases for nout:
            # 0 -> None, 1 -> val, >1 -> [val1, val2, ...]
            if nout == 0:
                handle_out(mlabraw.eval(self._session, cmd))
                return
            # deal with matlab-style multiple value return
            resSL = ((["RES%d__" % i for i in range(nout)]))
            handle_out(
                mlabraw.eval(self._session,
                             '[%s]=%s;' % (", ".join(resSL), cmd)))
            res = self._get_values(resSL)

            if nout == 1: res = res[0]
            else: res = tuple(res)
            if kwargs.has_key('cast'):
                if nout == 0: raise TypeError("Can't cast: 0 nout")
                return kwargs['cast'](res)
            else:
                return res
        finally:
            if len(tempargs) and self._clear_call_args:
                mlabraw.eval(self._session,
                             "clear('%s');" % "','".join(tempargs))
def put3d(handle, name, array):
    mlabraw.put(handle, "flat_array", array.flatten("F"))
    mlabraw.eval(handle, "%s = reshape(flat_array, %i, %i, %i);"%(name, array.shape[0], array.shape[1], array.shape[2]))