def test_load_save_fitresult(self, tmp_path): #todo missing read batch result test fpath = Path(tmp_path) / 'fit_result_single.csv' self.fit_result.to_file(fpath) df = csv_to_dataframe(fpath) assert df.attrs['metadata'] == self.fit_result.metadata fit_result_dir = Path(tmp_path) / 'fit_result' save_fitresult(fit_result_dir, self.fit_result, log_lines=['test123']) log_lines = Path(fit_result_dir / 'log.txt').read_text().split('\n') assert log_lines[-1] == 'test123' fit_result_loaded = load_fitresult(fit_result_dir) assert isinstance(fit_result_loaded.losses, pd.DataFrame) assert isinstance(fit_result_loaded.hdxm_set, HDXMeasurementSet) timepoints = np.linspace(0, 30*60, num=100) d_calc = fit_result_loaded(timepoints) assert d_calc.shape == (1, self.hdxm.Np, len(timepoints)) losses = csv_to_dataframe(fit_result_dir / 'losses.csv') fr_load_with_hdxm_and_losses = load_fitresult(fit_result_dir) assert len(fr_load_with_hdxm_and_losses.losses) == 100 assert fr_load_with_hdxm_and_losses.metadata['total_loss'] == losses.iloc[-1].sum()
df = checkpoint.to_dataframe(hdx_set.names) dataframe_to_file(output_dir / 'model_history.csv', df) dataframe_to_file(output_dir / 'model_history.txt', df, fmt='pprint') # Checkpoint history scatter plot # Note that these are raw dG values including interpolated values in regions of no coverage history = checkpoint.model_history num = len(history) cmap = mpl.cm.get_cmap('winter') norm = mpl.colors.Normalize(vmin=1, vmax=num*checkpoint.epoch_step) colors = iter(cmap(np.linspace(0, 1, num=num))) fig, ax = plt.subplots() for key, val in history.items(): n = len(val['dG'].numpy().squeeze()) ax.scatter(hdx_set.coverage.index, val['dG'].numpy().squeeze()[0], color=next(colors)) fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap, norm=norm), label='Epochs') plt.show() #Human readable output result.to_file(output_dir / 'Batch_fit_result.txt', fmt='pprint') #Machine readable output result.to_file(output_dir / 'Batch_fit_result.csv', fmt='csv') #Save full fitresult save_fitresult(output_dir / 'SecB_tetramer_dimer_batch', result)
pH=pH) data = pmt.get_state('SecB WT apo') reduced_data = data[data['end'] < 40] hdxm_reduced = HDXMeasurement(reduced_data, temperature=temperature, pH=pH) result = fit_rates_weighted_average(hdxm_reduced) reduced_guess = result.output dataframe_to_file(output_dir / 'ecSecB_reduced_guess.csv', reduced_guess) dataframe_to_file(output_dir / 'ecSecB_reduced_guess.txt', reduced_guess, fmt='pprint') gibbs_guess = hdxm_reduced.guess_deltaG(reduced_guess['rate']) fr_torch = fit_gibbs_global(hdxm_reduced, gibbs_guess, epochs=epochs, r1=2) save_fitresult(output_dir / 'ecsecb_reduced', fr_torch) if guess: wt_avg_result = fit_rates_weighted_average(hdxm, bounds=(1e-2 / 60., 800 / 60.)) guess_output = wt_avg_result.output dataframe_to_file(output_dir / 'ecSecB_guess.csv', guess_output) dataframe_to_file(output_dir / 'ecSecB_guess.txt', guess_output, fmt='pprint') else: guess_output = csv_to_dataframe(output_dir / 'ecSecB_guess.csv') # Export protein sequence and intrinsic rate of exchange hdxm.coverage.protein.to_file(output_dir / 'ecSecB_info.csv') hdxm.coverage.protein.to_file(output_dir / 'ecSecB_info.txt', fmt='pprint')
# Load the data of two Dynamx files, and combine the result to one table data = read_dynamx(input_dir / 'ecSecB_apo.csv', input_dir / 'ecSecB_dimer.csv') pmt = PeptideMasterTable(data, drop_first=1, ignore_prolines=True, remove_nan=False) pmt.set_control(('Full deuteration control', 0.167*60)) temperature, pH = 273.15 + 30, 8. hdxm = HDXMeasurement(pmt.get_state('SecB WT apo'), temperature=temperature, pH=pH) #%% if guess: client = default_client() wt_avg_result = fit_rates_weighted_average(hdxm, client=client) init_guess = wt_avg_result.output else: init_guess = csv_to_dataframe(test_data_dir / 'output' / 'ecSecB_guess.csv') gibbs_guess = hdxm.guess_deltaG(init_guess['rate']) #%% fr_torch = fit_gibbs_global(hdxm, gibbs_guess, **fit_kwargs) #Human readable output fr_torch.to_file(output_dir / 'SecB_fit_result.txt', fmt='pprint') #Machine readable output fr_torch.to_file(output_dir / 'SecB_fit_result.csv', fmt='csv') save_fitresult(output_dir / 'SecB_fit', fr_torch)
"""Obtain ΔG for ecSecB tetramer and dimer""" from pathlib import Path from pyhdx.batch_processing import yaml_to_hdxmset from pyhdx.fileIO import csv_to_dataframe, save_fitresult from pyhdx.fitting import fit_gibbs_global_batch import yaml cwd = Path(__file__).parent data_dir = cwd / 'test_data' / 'input' output_dir = cwd / 'test_data' / 'output' yaml_dict = yaml.safe_load(Path(data_dir / 'data_states.yaml').read_text()) hdx_set = yaml_to_hdxmset(yaml_dict, data_dir=data_dir) initial_guess_rates = csv_to_dataframe(output_dir / 'ecSecB_guess.csv') guesses = hdx_set[0].guess_deltaG(initial_guess_rates['rate']) fit_kwargs = yaml.safe_load(Path(data_dir / 'fit_settings.yaml').read_text()) fr = fit_gibbs_global_batch(hdx_set, guesses, **fit_kwargs) save_fitresult(output_dir / 'ecsecb_tetramer_dimer', fr)