Skip to content

Commit

Permalink
Merge pull request #48 from eastgenomics/1.2-fix-rounding
Browse files Browse the repository at this point in the history
1.2 - fix rounding for sub-threshold table
  • Loading branch information
Aisha-D authored Jul 6, 2021
2 parents 524bfd8 + 5cb31a3 commit 5baf3cf
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions bin/coverage_report_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,10 @@ def summary_gene_plot(self, cov_summary):
class styleTables():
"""Functions for styling tables for displaying in report"""

def __init__(self, cov_stats, cov_summary, threshold, threshold_cols, vals):
def __init__(
self, cov_stats: pd.DataFrame, cov_summary: pd.DataFrame,
threshold: str, threshold_cols: list, vals: list
) -> None:
self.cov_stats = cov_stats
self.cov_summary = cov_summary
self.threshold = threshold
Expand All @@ -461,10 +464,6 @@ def style_sub_threshold(self):
"""
Styling of sub threshold stats df for displaying in report
Args:
- cov_stats (df): df of per exon coverage stats
- threshold (str): low coverage threshold value
- threshold_cols (list): threshold values for coverage
Returns:
- sub_threshold_stats (list): list of sub threshold coverage stats
- low_exon_columns (list): list of column headers for report
Expand All @@ -479,13 +478,13 @@ def style_sub_threshold(self):

dtypes = {
'gene': str, 'tx': str, 'chrom': str, 'exon': int, 'exon_len': int,
'exon_start': int, 'exon_end': int, 'min': int, 'mean': int,
'exon_start': int, 'exon_end': int, 'min': int, 'mean': float,
'max': int
}

for col in self.threshold_cols:
# add dtype for threshold columns
dtypes[col] = int
dtypes[col] = float

sub_threshold = pd.DataFrame(columns=column)
sub_threshold.astype(dtype=dtypes)
Expand Down Expand Up @@ -528,14 +527,15 @@ def style_sub_threshold(self):
# add index as column to have numbered rows in report
sub_threshold_stats.insert(0, ' ', sub_threshold_stats.index)

# make dict for rounding coverage columns to 2dp
rnd = {}
for col in list(sub_threshold_stats.columns[10:]):
rnd[col] = '{0:.2f}%'
# threshold_cols -> list of strings, add mean to loop over for rounding
round_cols = ['Mean'] + self.threshold_cols

sub_threshold_stats["Mean"] = sub_threshold_stats["Mean"].apply(
lambda x: int(x)
)
# limit to 2dp using math.floor, use of round() with
# 2dp may lead to inaccuracy such as 99.99 => 100.00
for col in round_cols:
sub_threshold_stats[col] = sub_threshold_stats[col].map(
lambda col: math.floor(col * 100) / 100
)

# generate list of dicts with column headers for styling
low_exon_columns = []
Expand Down

0 comments on commit 5baf3cf

Please sign in to comment.