Skip to content

Commit

Permalink
chore: fmt black
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhsmit committed Aug 25, 2023
1 parent 40f34bf commit 5b70f13
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 50 deletions.
9 changes: 6 additions & 3 deletions pyhdx/batch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class DataFile(object):
filepath_or_buffer: Union[Path, StringIO]

def __post_init__(self):
warnings.warn("Will be removed in favour of the `hdxms-datasets` package ", DeprecationWarning)
warnings.warn(
"Will be removed in favour of the `hdxms-datasets` package ", DeprecationWarning
)

@cached_property
def data(self) -> pd.DataFrame:
Expand Down Expand Up @@ -62,8 +64,9 @@ def __init__(
# filter_kwargs: Optional[dict[str, Any]] = None,
# correction_kwargs: Optional[dict[str, Any]] = None,
) -> None:

warnings.warn("Will be removed in favour of the `hdxms-datasets` package ", DeprecationWarning)
warnings.warn(
"Will be removed in favour of the `hdxms-datasets` package ", DeprecationWarning
)
self.hdx_spec = hdx_spec
self.data_files: dict[str, DataFile] = {}

Expand Down
6 changes: 6 additions & 0 deletions pyhdx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pyhdx.datasets import DataVault

app = typer.Typer()


@app.command()
def serve(
scheduler_address: Optional[str] = typer.Option(None, help="Address for dask scheduler to use"),
Expand Down Expand Up @@ -61,6 +63,8 @@ def serve(


datasets_app = typer.Typer(help="Manage HDX datasets")


@datasets_app.command()
def fetch(num: int = typer.Option(10, min=1, help="Maximum number of datasets to download")):
"""Update the datasets from the PyHDX repository"""
Expand All @@ -72,6 +76,7 @@ def fetch(num: int = typer.Option(10, min=1, help="Maximum number of datasets to
for data_id in tqdm(todo):
vault.fetch_dataset(data_id)


@datasets_app.command()
def clear():
"""Clear the local dataset cache"""
Expand All @@ -81,6 +86,7 @@ def clear():

app.add_typer(datasets_app, name="datasets")


@app.callback()
def callback():
pass
Expand Down
1 change: 1 addition & 0 deletions pyhdx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def context(self, settings: dict) -> Generator[PyHDXConfig, None, None]:
finally:
cfg.conf = original_config


def valid_config() -> bool:
"""Checks if the current config file in the user home directory is a valid config
file for the current pyhdx version.
Expand Down
2 changes: 1 addition & 1 deletion pyhdx/datasets.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from hdxms_datasets import *
from hdxms_datasets import *
4 changes: 3 additions & 1 deletion pyhdx/fileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def read_header(file_obj: Union[TextIO, BinaryIO], comment: str = "#") -> List[s
return header


def parse_header(filepath_or_buffer: Union[Path[str], str, StringIO, BytesIO], comment: str = "#") -> dict:
def parse_header(
filepath_or_buffer: Union[Path[str], str, StringIO, BytesIO], comment: str = "#"
) -> dict:
"""
Reads the header from a file and returns JSON metadata from header lines marked as comment.
Expand Down
2 changes: 1 addition & 1 deletion pyhdx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ def linear_bars(
sort=False,
**figure_kwargs,
):

# input data should always be 3 levels
# grouping is done by the first level
# second level gives each bar
Expand Down Expand Up @@ -1141,6 +1140,7 @@ def get(self, item, default=None):
except KeyError:
return default


# should be a frozen dataclas
CMAP_NORM_DEFAULTS = ColorTransforms()

Expand Down
4 changes: 3 additions & 1 deletion pyhdx/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ def filter_peptides(
"""

warnings.warn("`filter_peptides` will be moved to the `hdxms-datasets` package", DeprecationWarning)
warnings.warn(
"`filter_peptides` will be moved to the `hdxms-datasets` package", DeprecationWarning
)
if state:
df = df[df["state"] == state]

Expand Down
1 change: 1 addition & 0 deletions pyhdx/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ def array_intersection(arrays: Iterable[np.ndarray], fields: Iterable[str]) -> l

return selected


# https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-subobjects-chained-properties
def rsetattr(obj, attr, val):
pre, _, post = attr.rpartition(".")
Expand Down
43 changes: 24 additions & 19 deletions pyhdx/web/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
from pyhdx.datasets import HDXDataSet, DataVault, DataFile
from pyhdx.fitting_torch import TorchFitResultSet
from pyhdx.models import (
PeptideUptakeModel, HDXMeasurement,
PeptideUptakeModel,
HDXMeasurement,
)
from pyhdx.plot import (
dG_scatter_figure,
Expand Down Expand Up @@ -293,7 +294,9 @@ class PeptideFileInputControl(PyHDXControlPanel):

batch_file = param.Parameter(doc="Batch file input:")

dataset_id = param.Selector(label="Dataset ID", doc="Dataset ID to load from hdxms-datasets database")
dataset_id = param.Selector(
label="Dataset ID", doc="Dataset ID to load from hdxms-datasets database"
)

nd_control = param.Boolean(
default=False, precedence=-1, doc="Whether to allow users to input a ND control"
Expand Down Expand Up @@ -410,7 +413,7 @@ def __init__(self, parent, **params):
todo = list(missing_datasets)[:num]
for data_id in tqdm(todo):
self.data_vault.fetch_dataset(data_id)
self.param['dataset_id'].objects = self.data_vault.datasets
self.param["dataset_id"].objects = self.data_vault.datasets
if self.data_vault.datasets:
self.dataset_id = self.data_vault.datasets[0]

Expand Down Expand Up @@ -512,19 +515,17 @@ def _update_mode(self):
"measurement_name",
"download_spec_button",
},
"Batch": {
"input_files_label",
"input_files",
"batch_file",
"batch_file_label"
},
"Batch": {"input_files_label", "input_files", "batch_file", "batch_file_label"},
"Database": {
"dataset_id",
}
},
}

#widget_dict.pop(self.input_mode)
excluded = set.union(*(v for k, v in widget_dict.items() if k != self.input_mode)) - widget_dict[self.input_mode]
# widget_dict.pop(self.input_mode)
excluded = (
set.union(*(v for k, v in widget_dict.items() if k != self.input_mode))
- widget_dict[self.input_mode]
)
#
#
# if self.input_mode == "Manual":
Expand Down Expand Up @@ -814,7 +815,9 @@ def _action_load_datasets(self) -> None:
"""Load all specified HDX measurements"""
if self.input_mode == "Manual":
data_src = self.data_file_history
dataset = HDXDataSet(data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec)
dataset = HDXDataSet(
data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec
)
elif self.input_mode == "Batch":
if self.hdxm_list:
self.parent.logger.info("Cannot add data in batch after manually inputting data")
Expand All @@ -831,7 +834,9 @@ def _action_load_datasets(self) -> None:
self.state_spec = hdx_spec["states"]
self.data_spec = hdx_spec["data_files"]

dataset = HDXDataSet(data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec)
dataset = HDXDataSet(
data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec
)
self.param["hdxm_list"].objects = dataset.states
elif self.input_mode == "Database":
if self.dataset_id is None:
Expand All @@ -841,18 +846,18 @@ def _action_load_datasets(self) -> None:
self.parent.logger.info(f"Loaded dataset {dataset.data_id} from hdxms database")

try:
authors = ", ".join([author['name'] for author in dataset.metadata['authors']])
authors = ", ".join([author["name"] for author in dataset.metadata["authors"]])
self.parent.logger.info(f"Author(s): {authors}")
except KeyError:
pass

publications = dataset.metadata.get('publications', [])
publications = dataset.metadata.get("publications", [])
if publications:
for pub in publications:
pub_str = pub['title']
if 'DOI' in pub:
pub_str = pub["title"]
if "DOI" in pub:
pub_str += f' ([{pub["DOI"]}](https://doi.org/{pub["DOI"]}))'
elif 'URL' in pub:
elif "URL" in pub:
pub_str += f' ([URL]({pub["URL"]}))'
self.parent.logger.info("Publication: " + pub_str)
else:
Expand Down
8 changes: 4 additions & 4 deletions templates/02_load_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
hdx_spec = yaml.safe_load(yaml_stream)
input_dir = current_dir.parent / "tests" / "test_data" / "input"

#%%
# %%

dataset = HDXDataSet.from_spec(
hdx_spec,
Expand All @@ -26,9 +26,9 @@

print(dataset.describe())

#%%
# %%
# Load an HDX measurement by state name
hdxm = HDXMeasurement.from_dataset(dataset, state='SecB_tetramer')
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_tetramer")
print(hdxm)
print(hdxm.timepoints)

Expand All @@ -39,6 +39,6 @@
print(hdxm.timepoints)


#%%
# %%
hdxm_set = HDXMeasurementSet.from_dataset(dataset)
print(hdxm_set)
4 changes: 3 additions & 1 deletion templates/03_initial_guesses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
hdxm = HDXMeasurement.from_dataset(dataset, state=name)

# Save sequence info + intrinsic rates
dataframe_to_file(output_dir / f"{name}_intrinsic_rates.csv", hdxm.coverage.protein, fmt='pprint')
dataframe_to_file(
output_dir / f"{name}_intrinsic_rates.csv", hdxm.coverage.protein, fmt="pprint"
)

fr = fit_rates_weighted_average(hdxm, client=client)
dataframe_to_file(output_dir / f"{name}_rates_guess.csv", fr.output)
5 changes: 2 additions & 3 deletions tests/test_fileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def hdxm() -> HDXMeasurement:
peptides_corrected = correct_d_uptake(peptides_control)

temperature, pH = 273.15 + 30, 8.0
hdxm = HDXMeasurement(
peptides_corrected, temperature=temperature, pH=pH, c_term=155
)
hdxm = HDXMeasurement(peptides_corrected, temperature=temperature, pH=pH, c_term=155)

return hdxm

Expand Down Expand Up @@ -122,6 +120,7 @@ def test_read_write_tables(tmp_path):
assert len(lines) == 38
assert lines[0].strip() == pyhdx.VERSION_STRING


def test_load_save_fitresult(tmp_path, fit_result: TorchFitResult, hdxm: HDXMeasurement):
# todo missing read batch result test

Expand Down
44 changes: 28 additions & 16 deletions tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def dataset() -> HDXDataSet:
hdx_spec = yaml.safe_load(yaml_pth.read_text())

# add truncated tetramer state
hdx_spec['states']['SecB_tetramer_red'] = copy.deepcopy(hdx_spec['states']['SecB_tetramer'])
hdx_spec['states']['SecB_tetramer_red']['peptides']['experiment']['query'] = ["stop < 40"]
hdx_spec["states"]["SecB_tetramer_red"] = copy.deepcopy(hdx_spec["states"]["SecB_tetramer"])
hdx_spec["states"]["SecB_tetramer_red"]["peptides"]["experiment"]["query"] = ["stop < 40"]

dataset = HDXDataSet.from_spec(hdx_spec, data_dir=input_dir)

Expand All @@ -53,24 +53,24 @@ def dataset() -> HDXDataSet:

@pytest.fixture()
def hdxm_apo(dataset: HDXDataSet) -> HDXMeasurement:
with cfg.context({'analysis.drop_first': 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_tetramer", d_percentage=100.)
with cfg.context({"analysis.drop_first": 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_tetramer", d_percentage=100.0)

return hdxm


@pytest.fixture()
def hdxm_dimer(dataset: HDXDataSet) -> HDXMeasurement:
with cfg.context({'analysis.drop_first': 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_dimer", d_percentage=100.)
with cfg.context({"analysis.drop_first": 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_dimer", d_percentage=100.0)

return hdxm


@pytest.fixture()
def hdxm_apo_red(dataset: HDXDataSet) -> HDXMeasurement:
with cfg.context({'analysis.drop_first': 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_tetramer_red", d_percentage=100.)
with cfg.context({"analysis.drop_first": 1}):
hdxm = HDXMeasurement.from_dataset(dataset, state="SecB_tetramer_red", d_percentage=100.0)

return hdxm

Expand Down Expand Up @@ -260,16 +260,28 @@ def test_batch_fit(hdxm_apo: HDXMeasurement, hdxm_dimer: HDXMeasurement, tmp_pat
assert errors.shape == (hdx_set.Ns, hdx_set.Np, hdx_set.Nt)

test = fr_global.get_peptide_mse().fillna(-1)
name_mapping = {'SecB his dimer apo': 'SecB_dimer', 'SecB WT apo': 'SecB_tetramer'}
ref = csv_to_dataframe(output_dir / "ecSecB_batch_peptide_mse.csv").fillna(-1).rename(columns=name_mapping)
name_mapping = {"SecB his dimer apo": "SecB_dimer", "SecB WT apo": "SecB_tetramer"}
ref = (
csv_to_dataframe(output_dir / "ecSecB_batch_peptide_mse.csv")
.fillna(-1)
.rename(columns=name_mapping)
)
assert_frame_equal(test, ref, atol=1e-1, rtol=5e-1)

test = fr_global.get_residue_mse().fillna(-1)
ref = csv_to_dataframe(output_dir / "ecSecB_batch_residue_mse.csv").fillna(-1).rename(columns=name_mapping)
ref = (
csv_to_dataframe(output_dir / "ecSecB_batch_residue_mse.csv")
.fillna(-1)
.rename(columns=name_mapping)
)
assert_frame_equal(test, ref, atol=1e-1, rtol=5e-1)

test = fr_global.losses.fillna(-1)
ref = csv_to_dataframe(output_dir / "ecSecB_batch_loss.csv").fillna(-1).rename(columns=name_mapping)
ref = (
csv_to_dataframe(output_dir / "ecSecB_batch_loss.csv")
.fillna(-1)
.rename(columns=name_mapping)
)
assert_frame_equal(test, ref, atol=1e-3, rtol=1e-2)

# test alignment fit
Expand All @@ -281,11 +293,11 @@ def test_batch_fit(hdxm_apo: HDXMeasurement, hdxm_dimer: HDXMeasurement, tmp_pat
hdx_set.add_alignment(list(mock_alignment.values()))

gibbs_guess = hdx_set[0].guess_deltaG(guess["rate"]) # Guesses from first measurement
aligned_result = fit_gibbs_global_batch_aligned(
hdx_set, gibbs_guess, r1=2, r2=5, epochs=1000
)
aligned_result = fit_gibbs_global_batch_aligned(hdx_set, gibbs_guess, r1=2, r2=5, epochs=1000)
output = aligned_result.output
check_df = csv_to_dataframe(output_dir / "ecSecB_batch_aligned.csv").rename(columns=name_mapping)
check_df = csv_to_dataframe(output_dir / "ecSecB_batch_aligned.csv").rename(
columns=name_mapping
)
states = ["SecB_tetramer", "SecB_dimer"]

for state in states:
Expand Down

0 comments on commit 5b70f13

Please sign in to comment.