예제 #1
0
    def test_jsondump(self):
        dict_list = {'a': [1.0, 2.0, 3.0]}
        file_list = os.path.join(TMPDIR, 'list.json')
        jsondump(file_list, dict_list)
        with open(file_list) as fd:
            self.assertEqual(json.load(fd), dict_list)

        dict_vec = {'a': np.repeat(3, 4)}
        file_vec = os.path.join(TMPDIR, 'vec.json')
        jsondump(file_vec, dict_vec)
        with open(file_vec) as fd:
            self.assertEqual(json.load(fd), dict_vec)

        dict_zero_vec = {'a': []}
        file_zero_vec = os.path.join(TMPDIR, 'empty_vec.json')
        jsondump(file_zero_vec, dict_zero_vec)
        with open(file_zero_vec) as fd:
            self.assertEqual(json.load(fd), dict_zero_vec)

        dict_zero_matrix = {'a': [[], [], []]}
        file_zero_matrix = os.path.join(TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            self.assertEqual(json.load(fd), dict_zero_matrix)

        arr = np.zeros(shape=(5, 0))
        dict_zero_matrix = {'a': arr}
        file_zero_matrix = os.path.join(TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            self.assertEqual(json.load(fd), dict_zero_matrix)
예제 #2
0
def main():
    here = os.path.dirname(os.path.realpath(__file__))
    input_path_tecrdb = os.path.join(here, INPUT_PATH_TECRDB)
    input_path_compounds = os.path.join(here, INPUT_PATH_COMPOUNDS)
    input_path_microspecies = os.path.join(here, INPUT_PATH_MICROSPECIES)
    tecrdb = pd.read_csv(input_path_tecrdb, index_col=0).loc[filter_tecrdb]
    stoichiometry = get_stoichiometry(tecrdb)
    cids = stoichiometry['compound_id'].unique()
    rids = stoichiometry['reaction_id'].unique()
    compounds = (
        pd.read_csv(input_path_compounds, index_col=0)
        .loc[lambda df: df['compound_id'].isin(cids)]
    )
    microspecies = (
        pd.read_csv(input_path_microspecies, index_col=0)
        .loc[lambda df: df['compound_id'].isin(cids)]
    )
    pkas, pkmgs = (
        microspecies
        .groupby(['compound_id', n])[[pk, "name"]].first()
        .dropna()
        .reset_index()
        for n, pk in [('nh', 'pka'), ('nmg', 'pkmg')]
    )
    stan_codes = get_stan_codes(stoichiometry)
    stan_input = get_stan_input(
        tecrdb, stoichiometry, microspecies, compounds, pkas, pkmgs, stan_codes
    )
    pkas.to_csv(os.path.join(here, OUTPUT_PATH_PKAS))
    compounds.to_csv(os.path.join(here, OUTPUT_PATH_COMPOUNDS))
    tecrdb.to_csv(os.path.join(here, OUTPUT_PATH_TECRDB))
    jsondump(os.path.join(here, OUTPUT_PATH_STAN_CODES), stan_codes)
    jsondump(os.path.join(here, OUTPUT_PATH_JSON), stan_input)
    rdump(os.path.join(here, OUTPUT_PATH_R), stan_input)
예제 #3
0
def generate_samples(
    study_name: str,
    measurements: pd.DataFrame,
    model_configurations: List[ModelConfiguration],
) -> None:
    """Run cmdstanpy.CmdStanModel.sample, do diagnostics and save results.

    :param study_name: a string
    """
    infds = {}
    for model_config in model_configurations:
        fit_name = f"{study_name}-{model_config.name}"
        print(f"Fitting model {fit_name}...")
        loo_file = os.path.join(LOO_DIR, f"loo_{fit_name}.pkl")
        infd_file = os.path.join(INFD_DIR, f"infd_{fit_name}.ncdf")
        json_file = os.path.join(JSON_DIR, f"input_data_{fit_name}.json")
        stan_input = model_config.stan_input_function(measurements)
        print(f"Writing input data to {json_file}")
        jsondump(json_file, stan_input)
        model = CmdStanModel(
            model_name=fit_name, stan_file=model_config.stan_file
        )
        print(f"Writing csv files to {SAMPLES_DIR}...")
        mcmc = model.sample(
            data=stan_input,
            output_dir=SAMPLES_DIR,
            **model_config.sample_kwargs,
        )
        print(mcmc.diagnose().replace("\n\n", "\n"))
        infd = az.from_cmdstanpy(
            mcmc, **model_config.infd_kwargs_function(measurements)
        )
        print(az.summary(infd))
        infds[fit_name] = infd
        print(f"Writing inference data to {infd_file}")
        infd.to_netcdf(infd_file)
        print(f"Writing psis-loo results to {loo_file}\n")
        az.loo(infd, pointwise=True).to_pickle(loo_file)
    if len(infds) > 1:
        comparison = az.compare(infds)
        print(f"Loo comparison:\n{comparison}")
        comparison.to_csv(os.path.join(LOO_DIR, "loo_comparison.csv"))
예제 #4
0
    def test_jsondump(self):
        def cmp(d1, d2):
            self.assertEqual(d1.keys(), d2.keys())
            for k in d1:
                data_1 = d1[k]
                data_2 = d2[k]
                if isinstance(data_2, np.ndarray):
                    data_2 = data_2.tolist()
                self.assertEqual(data_1, data_2)

        dict_list = {'a': [1.0, 2.0, 3.0]}
        file_list = os.path.join(_TMPDIR, 'list.json')
        jsondump(file_list, dict_list)
        with open(file_list) as fd:
            cmp(json.load(fd), dict_list)

        dict_vec = {'a': np.repeat(3, 4)}
        file_vec = os.path.join(_TMPDIR, 'vec.json')
        jsondump(file_vec, dict_vec)
        with open(file_vec) as fd:
            cmp(json.load(fd), dict_vec)

        dict_zero_vec = {'a': []}
        file_zero_vec = os.path.join(_TMPDIR, 'empty_vec.json')
        jsondump(file_zero_vec, dict_zero_vec)
        with open(file_zero_vec) as fd:
            cmp(json.load(fd), dict_zero_vec)

        dict_zero_matrix = {'a': [[], [], []]}
        file_zero_matrix = os.path.join(_TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            cmp(json.load(fd), dict_zero_matrix)

        arr = np.zeros(shape=(5, 0))
        dict_zero_matrix = {'a': arr}
        file_zero_matrix = os.path.join(_TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            cmp(json.load(fd), dict_zero_matrix)
예제 #5
0
 def write_json(self, dict: Dict) -> None:
     jsondump(self._data_file, dict)
예제 #6
0
    def test_jsondump(self):
        def cmp(d1, d2):
            self.assertEqual(d1.keys(), d2.keys())
            for k in d1:
                data_1 = d1[k]
                data_2 = d2[k]
                if isinstance(data_2, np.ndarray):
                    data_2 = data_2.tolist()
                self.assertEqual(data_1, data_2)

        dict_list = {'a': [1.0, 2.0, 3.0]}
        file_list = os.path.join(_TMPDIR, 'list.json')
        jsondump(file_list, dict_list)
        with open(file_list) as fd:
            cmp(json.load(fd), dict_list)

        dict_vec = {'a': np.repeat(3, 4)}
        file_vec = os.path.join(_TMPDIR, 'vec.json')
        jsondump(file_vec, dict_vec)
        with open(file_vec) as fd:
            cmp(json.load(fd), dict_vec)

        dict_zero_vec = {'a': []}
        file_zero_vec = os.path.join(_TMPDIR, 'empty_vec.json')
        jsondump(file_zero_vec, dict_zero_vec)
        with open(file_zero_vec) as fd:
            cmp(json.load(fd), dict_zero_vec)

        dict_zero_matrix = {'a': [[], [], []]}
        file_zero_matrix = os.path.join(_TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            cmp(json.load(fd), dict_zero_matrix)

        arr = np.zeros(shape=(5, 0))
        dict_zero_matrix = {'a': arr}
        file_zero_matrix = os.path.join(_TMPDIR, 'empty_matrix.json')
        jsondump(file_zero_matrix, dict_zero_matrix)
        with open(file_zero_matrix) as fd:
            cmp(json.load(fd), dict_zero_matrix)

        arr = np.zeros(shape=(2, 3, 4))
        self.assertTrue(isinstance(arr, np.ndarray))
        self.assertEqual(arr.shape, (2, 3, 4))

        dict_3d_matrix = {'a': arr}
        file_3d_matrix = os.path.join(_TMPDIR, '3d_matrix.json')
        jsondump(file_3d_matrix, dict_3d_matrix)
        with open(file_3d_matrix) as fd:
            cmp(json.load(fd), dict_3d_matrix)

        scalr = np.int32(1)
        self.assertTrue(type(scalr).__module__ == 'numpy')
        dict_scalr = {'a': scalr}
        file_scalr = os.path.join(_TMPDIR, 'scalr.json')
        jsondump(file_scalr, dict_scalr)
        with open(file_scalr) as fd:
            cmp(json.load(fd), dict_scalr)