Skip to content

Commit

Permalink
style: Linting with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
nvictus committed Jun 3, 2023
1 parent 94c23f5 commit a67598c
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 110 deletions.
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ ignore = [
"W503", # line break before binary operator
]

[tool.ruff]
exclude = [
"__init__.py",
"__main__.py",
]
extend-select = [
"E", # style errors
# "D", # pydocstyle
"F", # pyflakes
"I", # isort
"RUF", # ruff-specific rules
"UP", # pyupgrade
"W", # style warnings
]

[tool.isort]
profile = "black"
skip_gitignore = true
Expand Down
11 changes: 8 additions & 3 deletions src/cooler/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ def configure(
"""
if handlers is None:
if stream is not None and filename is not None:
raise ValueError("'stream' and 'filename' should not be specified together")
raise ValueError(
"'stream' and 'filename' should not be specified together"
)
else:
if stream is not None or filename is not None:
raise ValueError(
"'stream' or 'filename' should not be specified together with 'handlers'"
"'stream' or 'filename' should not be specified together with "
"'handlers'"
)

# Set up the new handlers
Expand Down Expand Up @@ -125,7 +128,9 @@ def set_logging_context(ctx):

if _logging_context != ctx:
if ctx == "lib":
configure(logger, stream=sys.stdout, level=logging.WARNING, format="{message}")
configure(
logger, stream=sys.stdout, level=logging.WARNING, format="{message}"
)
logging.captureWarnings(False)
elif ctx == "cli":
configure(logger, stream=sys.stderr, level=logging.INFO)
Expand Down
12 changes: 6 additions & 6 deletions src/cooler/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _balance_genomewide(

for _ in range(max_iters):
marg = (
split(clr, spans=spans, map=map, use_lock=use_lock) # noqa
split(clr, spans=spans, map=map, use_lock=use_lock)
.prepare(_init)
.pipe(filters)
.pipe(_timesouterproduct, bias)
Expand Down Expand Up @@ -146,7 +146,7 @@ def _balance_cisonly(
var = np.nan
for _ in range(max_iters):
marg = (
split(clr, spans=spans, map=map, use_lock=use_lock) # noqa
split(clr, spans=spans, map=map, use_lock=use_lock)
.prepare(_init)
.pipe(filters)
.pipe(_timesouterproduct, bias)
Expand Down Expand Up @@ -215,7 +215,7 @@ def _balance_transonly(

for _ in range(max_iters):
marg = (
split(clr, spans=spans, map=map, use_lock=use_lock) # noqa
split(clr, spans=spans, map=map, use_lock=use_lock)
.prepare(_init)
.pipe(filters)
.pipe(_zero_cis)
Expand Down Expand Up @@ -365,9 +365,9 @@ def balance_cooler(

# Drop bins with too few nonzeros from bias
if min_nnz > 0:
filters = [_binarize] + base_filters
filters = [_binarize, *base_filters]
marg_nnz = (
split(clr, spans=spans, map=map, use_lock=use_lock) # noqa
split(clr, spans=spans, map=map, use_lock=use_lock)
.prepare(_init)
.pipe(filters)
.pipe(_marginalize)
Expand All @@ -377,7 +377,7 @@ def balance_cooler(

filters = base_filters
marg = (
split(clr, spans=spans, map=map, use_lock=use_lock) # noqa
split(clr, spans=spans, map=map, use_lock=use_lock)
.prepare(_init)
.pipe(filters)
.pipe(_marginalize)
Expand Down
4 changes: 2 additions & 2 deletions src/cooler/cli/csort.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def make_index_command(index, fields, zero_based, outfile):
index_cmd = shlex.split(INDEX_PX2.format(**fields))
if zero_based:
index_cmd += ["-0"]
return index_cmd + [outfile]
return [*index_cmd, outfile]


@cli.command()
Expand Down Expand Up @@ -323,7 +323,7 @@ def csort(
signal(SIGPIPE, SIG_DFL)

# Check for required Unix tools
for tool in ["sort", "bgzip"] + [index]:
for tool in ["sort", "bgzip", index]:
if not cmd_exists(tool):
print(f"Command {tool} not found", file=sys.stderr)
sys.exit(1)
Expand Down
43 changes: 21 additions & 22 deletions src/cooler/cli/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def load_matrix(c, row_region, col_region, field, balanced, scale):
return mat


def interactive(ax, c, row_chrom, col_chrom, field, balanced, scale): # pragma: no cover
def interactive(
ax, c, row_chrom, col_chrom, field, balanced, scale
): # pragma: no cover
import matplotlib.pyplot as plt

# The code is heavily insired by
Expand Down Expand Up @@ -72,7 +74,7 @@ def update_heatmap(event):
im.set_data(np.ones(1)[:, None] * np.nan)

if not plotstate["placeholders"]:
box, = plt.plot(
(box,) = plt.plot(
[0, col_chrom_len, col_chrom_len, 0, 0, col_chrom_len],
[0, row_chrom_len, 0, 0, row_chrom_len, row_chrom_len],
c="k",
Expand Down Expand Up @@ -109,49 +111,44 @@ def update_heatmap(event):


@cli.command()
@click.argument(
"cool_uri",
metavar="COOL_PATH"
)
@click.argument(
"range",
type=str
)
@click.argument("cool_uri", metavar="COOL_PATH")
@click.argument("range", type=str)
@click.option(
"--range2", "-r2",
"--range2",
"-r2",
type=str,
help="The coordinates of a genomic region shown along the column dimension. "
"If omitted, the column range is the same as the row range. "
"Use to display asymmetric matrices or trans interactions.",
)
@click.option(
"--balanced", "-b",
"--balanced",
"-b",
is_flag=True,
default=False,
help="Show the balanced contact matrix. "
"If not provided, display the unbalanced counts.",
)
@click.option(
"--out", "-o",
"--out",
"-o",
help="Save the image of the contact matrix to a file. "
"If not specified, the matrix is displayed in an interactive window. "
"The figure format is deduced from the extension of the file, "
"the supported formats are png, jpg, svg, pdf, ps and eps.",
)
@click.option("--dpi", type=int, help="The DPI of the figure, if saving to a file")
@click.option(
"--dpi",
type=int,
help="The DPI of the figure, if saving to a file"
)
@click.option(
"--scale", "-s",
"--scale",
"-s",
type=click.Choice(["linear", "log2", "log10"]),
help="Scale transformation of the colormap: linear, log2 or log10. "
"Default is log10.",
default="log10",
)
@click.option(
"--force", "-f",
"--force",
"-f",
is_flag=True,
default=False,
help="Force display very large matrices (>=10^8 pixels). "
Expand All @@ -160,13 +157,15 @@ def update_heatmap(event):
@click.option(
"--zmin",
type=float,
help="The minimal value of the color scale. Units must match those of the colormap scale. "
help="The minimal value of the color scale. "
"Units must match those of the colormap scale. "
"To provide a negative value use a equal sign and quotes, e.g. -zmin='-0.5'",
)
@click.option(
"--zmax",
type=float,
help="The maximal value of the color scale. Units must match those of the colormap scale. "
help="The maximal value of the color scale. "
"Units must match those of the colormap scale. "
"To provide a negative value use a equal sign and quotes, e.g. -zmax='-0.5'",
)
@click.option(
Expand Down
15 changes: 8 additions & 7 deletions src/cooler/cli/zoomify.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def invoke_balance(args, resolutions, outfile):

try:
balance_cmd.main(
args=[uri] + args, prog_name='cooler'
args=[uri, *args], prog_name='cooler'
)
except SystemExit as e:
# exc_info = sys.exc_info()
Expand Down Expand Up @@ -84,9 +84,10 @@ def invoke_balance(args, resolutions, outfile):
)
@click.option(
"--balance-args",
help="Additional arguments to pass to cooler balance. To deal with space ambiguity, "
"use quotes to pass multiple arguments, e.g. --balance-args '--nproc 8 --ignore-diags 3' "
"Note that nproc for balancing must be specified independently of zoomify arguments.",
help="Additional arguments to pass to cooler balance. "
"To deal with space ambiguity, use quotes to pass multiple arguments, "
"e.g. --balance-args '--nproc 8 --ignore-diags 3'. Note that nproc for "
"balancing must be specified independently of zoomify arguments.",
type=str
)
@click.option(
Expand Down Expand Up @@ -164,7 +165,7 @@ def zoomify(
logger.info(f"Balancing zoom level {level}, bin size {res}")
try:
balance_cmd.main(
args=[uri] + balance_args, prog_name='cooler'
args=[uri, *balance_args], prog_name='cooler'
)
except SystemExit as e:
# exc_info = sys.exc_info()
Expand Down Expand Up @@ -208,7 +209,7 @@ def zoomify(
elif res == 'b':
r = preferred_sequence(curres, maxres, 'binary')
elif res == '4dn':
r = [1000, 2000] + preferred_sequence(5000, maxres, 'nice')
r = [1000, 2000, *preferred_sequence(5000, maxres, 'nice')]
elif res.endswith('n'):
res = int(res.split('n')[0])
r = preferred_sequence(res, maxres, 'nice')
Expand All @@ -235,7 +236,7 @@ def zoomify(
# logger.info("Applying resolutions {}".format(resolutions))

zoomify_cooler(
[cool_uri] + list(base_uri),
[cool_uri, *list(base_uri)],
outfile,
resolutions,
chunksize,
Expand Down

0 comments on commit a67598c

Please sign in to comment.