Mercurial > repos > fubar > bigwig_outlier_bed
comparison asciihist.py @ 8:032e930ef6a1 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
author | fubar |
---|---|
date | Wed, 24 Jul 2024 09:19:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
7:c8e22efcaeda | 8:032e930ef6a1 |
---|---|
1 from __future__ import print_function | |
2 import numpy as np | |
3 | |
4 | |
5 def asciihist( | |
6 it, | |
7 bins=10, | |
8 minmax=None, | |
9 str_tag="", | |
10 scale_output=30, | |
11 generate_only=False, | |
12 print_function=print, | |
13 ): | |
14 """Create an ASCII histogram from an interable of numbers. | |
15 Author: Boris Gorelik boris@gorelik.net. based on http://econpy.googlecode.com/svn/trunk/pytrix/pytrix.py | |
16 License: MIT | |
17 """ | |
18 ret = [] | |
19 itarray = np.asanyarray(it) | |
20 if minmax == "auto": | |
21 minmax = np.percentile(it, [5, 95]) | |
22 if minmax[0] == minmax[1]: | |
23 # for very ugly distributions | |
24 minmax = None | |
25 if minmax is not None: | |
26 # discard values that are outside minmax range | |
27 mn = minmax[0] | |
28 mx = minmax[1] | |
29 itarray = itarray[itarray >= mn] | |
30 itarray = itarray[itarray <= mx] | |
31 if itarray.size: | |
32 total = len(itarray) | |
33 counts, cutoffs = np.histogram(itarray, bins=bins) | |
34 cutoffs = cutoffs[1:] | |
35 if str_tag: | |
36 str_tag = "%s " % str_tag | |
37 else: | |
38 str_tag = "" | |
39 if scale_output is not None: | |
40 scaled_counts = counts.astype(float) / counts.sum() * scale_output | |
41 else: | |
42 scaled_counts = counts | |
43 | |
44 if minmax is not None: | |
45 ret.append("Trimmed to range (%s - %s)" % (str(minmax[0]), str(minmax[1]))) | |
46 for cutoff, original_count, scaled_count in zip(cutoffs, counts, scaled_counts): | |
47 ret.append( | |
48 "{:s}{:>8.2f} |{:<7,d} | {:s}".format( | |
49 str_tag, cutoff, original_count, "*" * int(scaled_count) | |
50 ) | |
51 ) | |
52 ret.append("{:s}{:s} |{:s} | {:s}".format(str_tag, "-" * 8, "-" * 7, "-" * 7)) | |
53 ret.append("{:s}{:>8s} |{:<7,d}".format(str_tag, "N=", total)) | |
54 else: | |
55 ret = [] | |
56 if not generate_only: | |
57 for line in ret: | |
58 print_function(line) | |
59 ret = "\n".join(ret) | |
60 return ret | |
61 | |
62 | |
63 if __name__ == "__main__": | |
64 np.random.seed(11) | |
65 asciihist(np.random.randn(10000), minmax="auto", str_tag="Normal") |