Example #1
0
def _runMatlabCode(matrix, matlab_session):
    #matrix = scipy.sparse.coo_matrix(matrix)
    dir = os.path.dirname(__file__)
    visuAddress = os.path.join(dir, "GenLouvain-master")

    #print("saving matrix for matlab ")
    scipy.io.savemat(visuAddress + '/file.mat', {"B": matrix})
    result_file = visuAddress + '/result.mat'
    ###matFormat = matlab.double(matrix.tolist())

    #print("starting matlab engine")
    eng = matlab_session
    if eng == None:
        eng = engine.start_matlab()
    eng.addpath(visuAddress, nargout=0)
    #print("matlab engine started successfully")
    start_time = time.time()

    out = io.StringIO()
    err = io.StringIO()
    #(S, Q) = eng.genlouvain('file.mat', nargout=2)
    eng.genlouvain('file.mat', result_file, stdout=out, stderr=err)

    duration = time.time() - start_time

    #print("matlab code ran successfully")

    #print(err.getvalue())

    res = scipy.io.loadmat(result_file)
    S = res["S"]

    return (S, duration)
def read_xyz(file_xyz):
    with open(file_xyz) as f:
        fxyz_contents = f.read()
    # Replace custom elements (e.g. for spin-pol calcs)
    fxyz_contents = re.sub("([a-zA-Z]+)[0-9]+", r"\1", fxyz_contents)
    atoms = ase.io.read(io.StringIO(fxyz_contents), format="xyz")
    return atoms
Example #3
0
def read_day_pairs(day_pairs):
    if os.path.isfile(day_pairs):
        target = day_pairs
        args = {'engine': 'python', 'sep': None}
    else:
        import io
        target = io.StringIO(day_pairs)
        args = {'sep': ',', 'lineterminator': ';'}
    return pd.read_table(target, **args)
Example #4
0
def generate_PMS_output(filename):
    # params:
    # * M (mask): np.array — 256 x 256 (True for keep, False for mask out)
    # * I (Intensities): np.array — 256 x 256 greyscale intensities
    # * L (Lighting Direction): np.array — Dummy K x 3 greyscale intensities (all zeros)
    # returns:
    # * output: np.array — N x 256 x 256 x 1 for greyscale x 2 for (0 = intensity / 1 = depth)

    eng = matlab.engine.start_matlab()

    # for image_num in range(img.shape[0]):
    #     mlb_img = generate_Mimage(M, I, L)

    out = io.StringIO()
    err = io.StringIO()

    # eng.demo_tv2(M, I, L, stdout=out, stderr=err)
    U = eng.demo_tv2(filename, stdout=out, stderr=err, nargout=1)
    print('out',out.getvalue())
    print('err',err.getvalue())
    return U, eng
    def read_xyz(self, file_xyz):
        """ Read atomic positions from .xyz file (in Bohr radiuses) """
        with open(file_xyz) as f:
            fxyz_contents = f.readlines()

        self.atom_kinds = []
        for i_line, line in enumerate(fxyz_contents):
            if i_line >= 2:
                kind = line.split()[0]
                self.atom_kinds.append(kind)
                # Replace custom kinds with their corresponding element (e.g. for spin-pol calcs)
                fxyz_contents[i_line] = self.kind_elem_basis[kind][0] + " " + " ".join(line.split()[1:]) + "\n"

        self.ase_atoms = ase.io.read(io.StringIO("".join(fxyz_contents)), format="xyz")

        if self.cell is not None:
            self.ase_atoms.cell = self.cell / ang_2_bohr
Example #6
0
    def _load_experiment_design_factors(self, dataset_id: str) -> dict:
        """
        Downloads the experiment design file and returns all characteristics
        in a dict with the cell id as key and a second dict as value that
        contains all characteristics and values.
        :param dataset_id: The dataset's identifier
        :return: dict {cell_id => {param_name => param_value, ...}}
        """
        # Download the file
        logger.debug(
            "Loading experimental design for {}...".format(dataset_id))

        exp_design_data = self._download_file(
            "https://www.ebi.ac.uk/gxa/sc/experiment/{}/download?fileType=experiment-design&accessKey="
            .format(dataset_id)).decode()

        # Clean the data
        clean_exp_design_data = expression_atlas_fetcher.ExpressionAtlasFetcher._filter_metadata(
            exp_design_data)

        # Convert to a numpy array
        exp_design = numpy.genfromtxt(io.StringIO(clean_exp_design_data),
                                      delimiter="\t",
                                      names=True,
                                      dtype=None)

        # process the data for every cell
        cell_factors = dict()

        for row in exp_design:
            # first column is the id
            cell_id = row[0].decode().replace("\"", "").strip()
            cell_factors[cell_id] = dict()

            # process every column
            for col_index in range(1, len(row)):
                col_name = exp_design.dtype.names[col_index]

                # save the value
                value = row[col_index].decode()
                value = value.replace("\"", "").strip()

                cell_factors[cell_id][col_name] = value

        return cell_factors