示例#1
0
 def key2BSTM(self, key):
     trained = (self & key).fetch1()
     voxel = key['vx'], key['vy'], key['vz']
     b = RankDegenerateBernoulliProcess(voxel, quadratic_channels=key['quadratic_components'],
                                        linear_channels=key['linear_components'],
                                        common_channels=key['common_components'])
     b.set_parameters(**trained)
     return b
示例#2
0
def test_convolution():
    """Testing separable convolution"""
    channels = 2
    flt_row, flt_col, flt_depth = (7, 5, 3)
    in_channels = 1

    b = RankDegenerateBernoulliProcess((flt_row, flt_col, flt_depth),
                                       quadratic_channels=channels, linear_channels=channels)
    X = np.random.randn(50, 40, 30, 1)
    X_ = th.shared(np.require(X, dtype=floatX), borrow=True, name='stack')

    c_, (xy_, z_) = b._build_separable_convolution(channels, X_, X.shape)
    xy = np.random.randn(channels, flt_row, flt_col, in_channels)
    z = np.random.randn(channels, 1, flt_depth, in_channels)
    f = th.function([xy_, z_], c_)

    for k in range(channels):
        inter1 = [signal.convolve2d(e, xy[k, ...].squeeze(), mode='valid') for e in X.squeeze().transpose([2, 0, 1])]
        inter1 = np.stack(inter1, axis=2)
        inter2 = 0 * inter1[..., flt_depth - 1:]

        for i, j in itertools.product(range(44), range(36)):
            inter2[i, j, :] = signal.convolve(inter1[i, j, :], z[k].squeeze(), mode='valid')
        assert_true(np.abs(inter2 - f(xy, z)[k, ...]).max() < 1e-10)
示例#3
0
    def _make_tuples(self, key):
        X = Stacks().load(key)
        voxel = (VoxelSize() & key).fetch1['vx', 'vy', 'vz']
        b = RankDegenerateBernoulliProcess(voxel,
                                           quadratic_channels=key['quadratic_components'],
                                           linear_channels=key['linear_components'],
                                           common_channels=key['common_components']
                                           )
        cells = (CellLocations() & key).fetch1['cells']

        b.fit(X, cells, maxiter=100)
        key.update(b.parameters)
        key['train_cross_entropy'] = b.cross_entropy(X, cells)
        key['train_auc_weighted'] = b.auc(X, cells, average='weighted')
        key['train_auc'] = b.auc(X, cells, average='macro')
        self.insert1(key)
示例#4
0
                self.cells = np.asarray(fid['cells']) if 'cells' in fid else None
                self.P = np.asarray(fid['P']) if 'P' in fid else np.nan * np.empty(X.shape[:3])

    def save(self, stackfile):
        mode = 'r+' if os.path.exists(stackfile) else 'w'
        with h5py.File(stackfile, mode) as fid:
            fid.create_dataset('stack', self.stack.shape, dtype=float, data=self.stack)
            fid.create_dataset('P', self.P.shape, dtype=float, data=self.P)
            if self.cells is not None:
                fid.create_dataset('cells', self.cells.shape, dtype=int, data=self.cells)


if __name__ == "__main__":
    # s = Stack('data/smaller.hdf5', preprocessor=lambda x: preprocess(x).mean(axis=-1).squeeze())
    # b = FullBernoulliProcess((9, 9, 7), quadratic_channels=3, linear_channels=3)

    # s = Stack('data/sanity.hdf5', preprocessor=lambda x: x.mean(axis=-1).squeeze())
    # s_test = Stack('data/sanity_test.hdf5', preprocessor=lambda x: x.mean(axis=-1).squeeze())
    # b = RankDegenerateBernoulliProcess( (3,3,3), quadratic_channels=3, linear_channels=3, common_channels=3)


    s = Stack('data/smaller.hdf5',
              preprocessor=lambda x: histeq(unsharp_masking(medianfilter(center(x.squeeze()))), 500).mean(
                  axis=-1))
    b = RankDegenerateBernoulliProcess((11, 11, 9), quadratic_channels=40, linear_channels=40, common_channels=20)
    # s = Stack('data/2015-08-25_12-49-41_2015-08-25_13-02-18.h5',
    #           preprocessor=lambda x: histeq(unsharp_masking(medianfilter(center(x.squeeze()))),500).mean(axis=-1))
    # b = RankDegenerateBernoulliProcess((19, 19, 17), quadratic_channels=40, linear_channels=40, common_channels=20)
    b.fit(s.X, s.cells, maxiter=50)
    b.visualize(s.X, s.cells)