Beispiel #1
0
    def savanalysis(self, adataname, adatarray):
        '''
        prerequisite: accesstructure, mkanalysis
        '''
        m, n = adatarray.shape[0], adatarray.shape[1]
        with open_file(self.analysispath / (self.analysisfolder + ".h5"),
                       'w') as f:
            filters = Filters(complevel=5, complib='blosc')
            acontainer = f.create_carray(f.root,
                                         adataname,
                                         Float64Atom(),
                                         shape=(m, n),
                                         filters=filters)
            acontainer[:, :] = adatarray

            # Create a table in the root directory and append data...
            class About(IsDescription):
                task = StringCol(len(self.task), pos=1)  # N-character String
                comment = StringCol(len(self.comment),
                                    pos=2)  # N-character String

            tableroot = f.create_table(f.root, 'info', About,
                                       "A table at root", Filters(1))
            tableroot.append(
                [(self.task, self.comment)]
            )  # , ("Mediterranean", 11, -1, 11*11, 11**2), ("Adriatic", 12, -2, 12*12, 12**2)])

        return
Beispiel #2
0
    def save(self, db):
        """Save the input data to disk.

        Notes
        -----
        Saves predictions, measurements, observables, and prior_pops to the
        HDF5 PyMC database.
        """

        if db != "hdf5":
            return

        from tables import Float64Atom, Filters
        compression = Filters(complevel=9, complib='blosc', shuffle=True)
        F = self.mcmc.db._h5file

        F.createCArray("/",
                       "predictions",
                       Float64Atom(),
                       self.predictions.shape,
                       filters=compression)
        F.root.predictions[:] = self.predictions

        F.createCArray("/",
                       "measurements",
                       Float64Atom(),
                       self.measurements.shape,
                       filters=compression)
        F.root.measurements[:] = self.measurements

        F.createCArray("/",
                       "uncertainties",
                       Float64Atom(),
                       self.uncertainties.shape,
                       filters=compression)
        F.root.uncertainties[:] = self.uncertainties

        F.createCArray("/",
                       "prior_pops",
                       Float64Atom(),
                       self.prior_pops.shape,
                       filters=compression)
        F.root.prior_pops[:] = self.prior_pops
def create_correlation_matrix(infiles, roi, out_type, package):
    import os
    import numpy as np
    import scipy.io as sio
    import nibabel as nb
    from nipype.utils.filemanip import split_filename, filename_to_list
    for idx, fname in enumerate(filename_to_list(infiles)):
        data = np.squeeze(nb.load(fname).get_data())
        if idx == 0:
            timeseries = data
        else:
            timeseries = np.vstack((timeseries, data))
    roi_data = np.genfromtxt(roi)
    if not len(roi_data.shape) == 2:
        roi_data = roi_data[:, None]
    corrmat = np.zeros((roi_data.shape[1], timeseries.shape[0]))
    print timeseries.shape
    for i in xrange(roi_data.shape[1]):
        for j in xrange(timeseries.shape[0]):
            r = np.corrcoef(timeseries[j, :], roi_data[:, i])[0][1]
            corrmat[i, j] = np.sqrt(timeseries.shape[1] - 3) * 0.5 * np.log(
                (1 + r) / (1 - r))

    #corrmat = np.corrcoef(timeseries,roi_data.T)
    print corrmat.shape

    _, name, _ = split_filename(filename_to_list(infiles)[0])
    if len(filename_to_list(infiles)) > 1:
        name = 'combined_' + name
    if 'mat' in out_type:
        matfile = os.path.abspath(name + '.mat')
        sio.savemat(matfile, {'corrmat': corrmat})
        output = matfile
    elif 'hdf5' in out_type:
        hdf5file = os.path.abspath(name + '.hf5')
        if package == 'h5py':
            import h5py
            f = h5py.File(hdf5file, 'w')
            f.create_dataset('corrmat', data=corrmat, compression=5)
            f.close()
        else:
            from tables import openFile, Float64Atom, Filters
            h5file = openFile(hdf5file, 'w')
            arr = h5file.createCArray(h5file.root,
                                      'corrmat',
                                      Float64Atom(),
                                      corrmat.shape,
                                      filters=Filters(complevel=5))
            arr[:] = corrmat
            h5file.close()
        output = hdf5file
    else:
        raise Exception('Unknown output type')
    return output
Beispiel #4
0
def create_correlation_matrix(infiles, out_type, package):
    import os
    import numpy as np
    import scipy.io as sio
    import nibabel as nb
    from nipype.utils.filemanip import split_filename, filename_to_list
    for idx, fname in enumerate(filename_to_list(infiles)):
        data = np.squeeze(nb.load(fname).get_data())
        if idx == 0:
            timeseries = data
        else:
            timeseries = np.vstack((timeseries, data))

    corrmat = np.corrcoef(timeseries)
    _, name, _ = split_filename(filename_to_list(infiles)[0])
    if len(filename_to_list(infiles)) > 1:
        name = 'combined_' + name
    if 'mat' in out_type:
        matfile = os.path.abspath(name + '.mat')
        sio.savemat(matfile, {'corrmat': corrmat})
        output = matfile
    elif 'hdf5' in out_type:
        hdf5file = os.path.abspath(name + '.hf5')
        if package == 'h5py':
            import h5py
            f = h5py.File(hdf5file, 'w')
            f.create_dataset('corrmat', data=corrmat, compression=5)
            f.close()
        else:
            from tables import openFile, Float64Atom, Filters
            h5file = openFile(hdf5file, 'w')
            arr = h5file.createCArray(h5file.root,
                                      'corrmat',
                                      Float64Atom(),
                                      corrmat.shape,
                                      filters=Filters(complevel=5))
            arr[:] = corrmat
            h5file.close()
        output = hdf5file
    else:
        raise Exception('Unknown output type')
    return output
 def test_from_dtype_04(self):
     atom1 = Atom.from_dtype(numpy.dtype('Float64'))
     atom2 = Float64Atom(shape=(), dflt=0.0)
     self.assertEqual(atom1, atom2)
     self.assertEqual(str(atom1), str(atom2))