Skip to content

Commit

Permalink
STY: ruff applied to paper scripts and notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinHuttley committed Nov 11, 2024
1 parent 28d0590 commit 4bbb3f9
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 22 deletions.
17 changes: 11 additions & 6 deletions paper/nbks/benchmark_ctree.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import itertools
from pathlib import Path

import cogent3
import itertools
from diverse_seq import cli as dvs_cli
from diverse_seq import util as dvs_util
from click.testing import CliRunner
from benchmark import TempWorkingDir, TimeIt
from click.testing import CliRunner
from rich import progress as rich_progress

from diverse_seq import cli as dvs_cli
from diverse_seq import util as dvs_util

RUNNER = CliRunner()


Expand All @@ -29,15 +31,18 @@ def main():
repeats = progress.add_task("Doing reps", total=len(reps))
for _ in reps:
combo = progress.add_task(
"Doing k x num_seq", total=len(num_seqs) * len(kmer_sizes)
"Doing k x num_seq",
total=len(num_seqs) * len(kmer_sizes),
)
for k, num_seq in itertools.product(kmer_sizes, num_seqs):
with TempWorkingDir() as temp_dir:
out_tree_file = temp_dir / f"{in_file.name}.tre"
args = f"-s {in_file} -o {out_tree_file} -k {k} --distance mash --sketch-size 3000 -hp --limit {num_seq}".split()
with TimeIt() as timer:
r = RUNNER.invoke(
dvs_cli.ctree, args, catch_exceptions=False
dvs_cli.ctree,
args,
catch_exceptions=False,
)
assert r.exit_code == 0, r.output
results["time(s)"].append(timer.get_elapsed_time())
Expand Down
7 changes: 4 additions & 3 deletions paper/nbks/ctree.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"metadata": {},
"outputs": [],
"source": [
"import project_path\n",
"import cogent3\n",
"import plotly.express as px\n",
"import project_path\n",
"\n",
"color_discrete_sequence = px.colors.sequential.Rainbow\n",
"\n",
Expand All @@ -22,15 +22,16 @@
"\n",
" inpath = project_path.PAPER_DIR / \"nbks\" / \"ctree\" / \"out\" / fname\n",
" lines = [l.split(\"\\t\") for l in inpath.read_text().splitlines()]\n",
" data = dict(zip(header, zip(*lines)))\n",
" data = dict(zip(header, zip(*lines, strict=False), strict=False))\n",
" for name, vals in data.items():\n",
" data[name] = cast_str_to_array(vals)\n",
" return cogent3.make_table(header=header, data=data)\n",
"\n",
"\n",
"def get_plot_for(*, x_axis, limit_to, colour, legend_title, log_x):\n",
" table = load_table(\n",
" \"results_with_ls.tsv\", [\"k\", \"ss\", \"cpus\", \"time\", \"lnL\", \"tree\"]\n",
" \"results_with_ls.tsv\",\n",
" [\"k\", \"ss\", \"cpus\", \"time\", \"lnL\", \"tree\"],\n",
" )\n",
" table = table.get_columns([c for c in table.header if c != \"tree\"])\n",
" table = table.filtered(lambda x: int(x) in limit_to, columns=colour)\n",
Expand Down
4 changes: 3 additions & 1 deletion paper/nbks/ctree/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

from cogent3 import load_aligned_seqs, make_aligned_seqs

from diverse_seq.cluster import dvs_par_ctree

MAMMALS_PATH = Path("data/mammals-aligned")
Expand All @@ -30,7 +31,8 @@

def load_alignment(directory: Path):
fasta_files = filter(
lambda file_name: file_name.endswith(".fa"), os.listdir(directory)
lambda file_name: file_name.endswith(".fa"),
os.listdir(directory),
)

seqs = {}
Expand Down
7 changes: 4 additions & 3 deletions paper/nbks/ctree/iq_experiment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from pathlib import Path
import time
from piqtree2 import build_tree
from pathlib import Path

from cogent3 import load_aligned_seqs, make_aligned_seqs
from piqtree2 import build_tree

MAMMALS_PATH = Path("data/mammals-aligned")

Expand All @@ -13,7 +13,8 @@

def load_alignment(directory: Path):
fasta_files = filter(
lambda file_name: file_name.endswith(".fa"), os.listdir(directory)
lambda file_name: file_name.endswith(".fa"),
os.listdir(directory),
)

seqs = {}
Expand Down
7 changes: 4 additions & 3 deletions paper/nbks/ctree/likelihoods.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from pathlib import Path
from piqtree2 import fit_tree

from cogent3 import load_aligned_seqs, make_aligned_seqs, make_tree
from piqtree2 import fit_tree

MAMMALS_PATH = Path("data/mammals-aligned")

Expand All @@ -15,7 +15,8 @@

def load_alignment(directory: Path):
fasta_files = filter(
lambda file_name: file_name.endswith(".fa"), os.listdir(directory)
lambda file_name: file_name.endswith(".fa"),
os.listdir(directory),
)

seqs = {}
Expand Down Expand Up @@ -53,7 +54,7 @@ def do_likelihoods():
likelihood = tree.params["lnL"]
with OUT_FILE.open("a") as f:
f.write(
"\t".join([k, ss, cpus, time, str(likelihood), str(tree)]) + "\n"
"\t".join([k, ss, cpus, time, str(likelihood), str(tree)]) + "\n",
)
with IQ_FILE.open() as f:
for line in f:
Expand Down
7 changes: 4 additions & 3 deletions paper/nbks/get_data_sets.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import pathlib
import re
import sys
import pathlib
import zipfile

import requests
from rich import progress as rich_progress
import zipfile

DATA_DIR = pathlib.Path(__file__).parent.parent / "data"

if not DATA_DIR.exists():
if DATA_DIR.parent.name != "paper":
print(
f"cannot find the data directory {DATA_DIR}, this file belongs in the `paper/nbks` directory"
f"cannot find the data directory {DATA_DIR}, this file belongs in the `paper/nbks` directory",
)
sys.exit(1)
else:
Expand Down
5 changes: 4 additions & 1 deletion paper/nbks/jsd_v_dist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@
"\n",
"fig.update_yaxes(title_text=\"<b>Significant %</b>\", row=1, col=1, title_font=titlefont)\n",
"fig.update_yaxes(\n",
" title_text=\"<b>Mean Number Selected</b>\", row=2, col=1, title_font=titlefont\n",
" title_text=\"<b>Mean Number Selected</b>\",\n",
" row=2,\n",
" col=1,\n",
" title_font=titlefont,\n",
")\n",
"fig.update_xaxes(title_text=\"<b><i>k</i></b>\", row=2, col=1, title_font=titlefont)\n",
"fig.update_layout(legend=legend, width=1600, height=1200)\n",
Expand Down
1 change: 1 addition & 0 deletions paper/nbks/jsd_v_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from numpy.random import choice
from rich.progress import track
from scipy import special

from diverse_seq import util as dvs_utils
from diverse_seq.records import dvs_max, dvs_nmost

Expand Down
4 changes: 3 additions & 1 deletion paper/nbks/plugin_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@
"from cogent3 import get_app, open_data_store\n",
"\n",
"in_data = open_data_store(\n",
" project_path.DATA_DIR / \"mammals-aligned.zip\", suffix=\"fa\", mode=\"r\"\n",
" project_path.DATA_DIR / \"mammals-aligned.zip\",\n",
" suffix=\"fa\",\n",
" mode=\"r\",\n",
")\n",
"loader = get_app(\"load_aligned\", moltype=\"dna\")\n",
"aln = loader(in_data[64])"
Expand Down
1 change: 1 addition & 0 deletions paper/nbks/project_path.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pathlib
import time

from plotly.io import write_image

PAPER_DIR = pathlib.Path(__file__).parent.parent
Expand Down
3 changes: 2 additions & 1 deletion paper/nbks/synthetic_known.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from itertools import product

import project_path
from cogent3 import make_table, make_unaligned_seqs
from cogent3.app import typing as c3_types
from cogent3.app.composable import NotCompleted, define_app
from numpy import array
from numpy.random import choice, shuffle
from rich.progress import track
import project_path

from diverse_seq import util as dvs_utils
from diverse_seq.record import KmerSeq, SeqArray, seqarray_to_kmerseq
from diverse_seq.records import max_divergent
Expand Down

0 comments on commit 4bbb3f9

Please sign in to comment.