Skip to content

Commit

Permalink
Fix denominator
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jan 25, 2025
1 parent 7dc5277 commit e39af30
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/jcvi/projects/sugarcane.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
savefig,
)
from ..graphics.chromosome import Chromosome as ChromosomePlot
from ..utils.cbook import short_float

SoColor = "#7436a4" # Purple
SsColor = "#5a8340" # Green
Expand Down Expand Up @@ -356,23 +357,25 @@ def summary(self) -> List[Tuple[str, int, int, int]]:
group_chrom_count = group_count / len(chromosomes[0])
group_so_size = group_count if subgenome == "SO" else 0
group_ss_size = group_count if subgenome == "SS" else 0
group_size = group_so_size + group_ss_size
ans.append(
(
subgenome,
group_chrom_count,
group_so_size / SO_GENE_COUNT,
group_ss_size / SS_GENE_COUNT,
group_so_size / group_size,
group_ss_size / group_size,
)
)
total_chrom_count += group_chrom_count
total_so_size += group_so_size
total_ss_size += group_ss_size
total_size = total_so_size + total_ss_size
ans.append(
(
"Total",
total_chrom_count,
total_so_size / SO_GENE_COUNT,
total_ss_size / SS_GENE_COUNT,
total_so_size / total_size,
total_ss_size / total_size,
)
)
return ans
Expand Down Expand Up @@ -409,11 +412,11 @@ def _percent_summary(self, a, tag, precision=1):
round(np.min(a), precision),
round(np.max(a), precision),
)
s = f"*{tag}*%: {mean:.1f}%"
s = f"*{tag}*%: {short_float(mean, precision)}%"
print(s)
if mn == mean and mx == mean:
return s
return s + f" ({mn:.1f}-{mx:.1f}%)"
return s + f" ({short_float(mn, precision)}-{short_float(mx, precision)}%)"

@property
def percent_SO_summary(self):
Expand Down
16 changes: 16 additions & 0 deletions src/jcvi/utils/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ def percentage(a, b, precision=1, mode: Optional[int] = 0):
return pct


def short_float(f, precision=1, trim_zeros=True):
"""
Format a float to a string with a fixed precision, and optionally trim
trailing zeros.
>>> short_float(3.1415926)
'3.1'
>>> short_float(3.002)
'3'
"""
f = f"{f:.{precision}f}"
if trim_zeros:
f = f.rstrip("0").rstrip(".")
return f


def thousands(x):
"""
>>> thousands(12345)
Expand Down
15 changes: 14 additions & 1 deletion tests/utils/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from jcvi.apps.base import cleanup
from jcvi.utils.cbook import autoscale, depends, gene_name, seqid_parse
from jcvi.utils.cbook import autoscale, depends, gene_name, seqid_parse, short_float


@pytest.mark.parametrize(
Expand Down Expand Up @@ -52,3 +52,16 @@ def func1(infile="a", outfile="b"):
func1(infile="a", outfile="b")
assert op.exists("b")
cleanup("a", "b")


@pytest.mark.parametrize(
"f,precision,trim_zeros,output",
[
(0.123456, 3, True, "0.123"),
(0.0000, 2, True, "0"),
(3.1000, 2, False, "3.10"),
(3.0, 2, True, "3"),
],
)
def test_short_float(f, precision, trim_zeros, output):
assert short_float(f, precision, trim_zeros) == output

0 comments on commit e39af30

Please sign in to comment.