sentropy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sentropy/__init__.py +10 -0
- sentropy/__main__.py +73 -0
- sentropy/abundance.py +184 -0
- sentropy/backend.py +468 -0
- sentropy/components.py +52 -0
- sentropy/exceptions.py +29 -0
- sentropy/log.py +26 -0
- sentropy/parameters.py +150 -0
- sentropy/powermean.py +81 -0
- sentropy/ray.py +231 -0
- sentropy/sentropy.py +221 -0
- sentropy/set.py +292 -0
- sentropy/similarity.py +317 -0
- sentropy/tests/S_2b_df_after_removing_zero_abundance_species.csv +9 -0
- sentropy/tests/base_tests/abundance_test.py +255 -0
- sentropy/tests/base_tests/backend_test.py +199 -0
- sentropy/tests/base_tests/components_test.py +43 -0
- sentropy/tests/base_tests/main_test.py +203 -0
- sentropy/tests/base_tests/parameters_test.py +32 -0
- sentropy/tests/base_tests/powermean_test.py +321 -0
- sentropy/tests/base_tests/sentropy_test.py +136 -0
- sentropy/tests/base_tests/set_test.py +661 -0
- sentropy/tests/base_tests/similarity_test.py +915 -0
- sentropy/tests/base_tests/test_material/S_2b.csv +10 -0
- sentropy/tests/base_tests/test_material/counts_2b_1.csv +10 -0
- sentropy/tests/base_tests/test_material/counts_2b_2.csv +10 -0
- sentropy/tests/mockray.py +57 -0
- sentropy/tests/mockray_test.py +31 -0
- sentropy/tests/ray_test.py +337 -0
- sentropy-0.1.0.dist-info/METADATA +675 -0
- sentropy-0.1.0.dist-info/RECORD +33 -0
- sentropy-0.1.0.dist-info/WHEEL +4 -0
- sentropy-0.1.0.dist-info/licenses/LICENSE +21 -0
sentropy/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Package for calculating Similarity-sensitive diversities.
|
|
2
|
+
|
|
3
|
+
The diversity measures that can be calculated are described rigorously
|
|
4
|
+
in https://arxiv.org/abs/1404.6520.
|
|
5
|
+
|
|
6
|
+
See sentropy.set.Set
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from sentropy.set import Set
|
|
10
|
+
from sentropy.sentropy import sentropy
|
sentropy/__main__.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Main module for executing diversity on command-line.
|
|
2
|
+
|
|
3
|
+
Functions
|
|
4
|
+
---------
|
|
5
|
+
main
|
|
6
|
+
Calculates diversities according to command-line specifications.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from sys import argv
|
|
10
|
+
from platform import python_version
|
|
11
|
+
from logging import captureWarnings, getLogger
|
|
12
|
+
from numpy import int64
|
|
13
|
+
|
|
14
|
+
from pandas import read_csv
|
|
15
|
+
|
|
16
|
+
from sentropy.log import LOG_HANDLER, LOGGER
|
|
17
|
+
from sentropy import sentropy
|
|
18
|
+
from sentropy.parameters import configure_arguments
|
|
19
|
+
import json, pickle
|
|
20
|
+
|
|
21
|
+
# Ensure warnings are handled properly.
|
|
22
|
+
captureWarnings(True)
|
|
23
|
+
getLogger("py.warnings").addHandler(LOG_HANDLER)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def main(args):
|
|
27
|
+
"""Calculates diversity from species counts and similarities.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
args: argparse.Namespace
|
|
32
|
+
Return object of argparse.ArgumentParser object created by
|
|
33
|
+
diversity.parameters.configure_arguments and applied to command
|
|
34
|
+
line arguments.
|
|
35
|
+
"""
|
|
36
|
+
LOGGER.setLevel(args.log_level)
|
|
37
|
+
LOGGER.info(" ".join([f"python{python_version()}", *argv]))
|
|
38
|
+
LOGGER.debug(f"args: {args}")
|
|
39
|
+
|
|
40
|
+
if len(args.input_filepath) == 1:
|
|
41
|
+
counts = read_csv(args.input_filepath[0], sep=None, engine="python", dtype=int64)
|
|
42
|
+
df = sentropy(counts, similarity=args.similarity,\
|
|
43
|
+
viewpoint=args.viewpoint, measures=args.measure, chunk_size=args.chunk_size, \
|
|
44
|
+
return_dataframe=True, which=args.which, eff_no=args.eff_no, \
|
|
45
|
+
backend=args.backend, device=args.device)
|
|
46
|
+
|
|
47
|
+
print(df)
|
|
48
|
+
|
|
49
|
+
if args.output_filepath is not None:
|
|
50
|
+
df.to_csv(args.output_filepath, sep="\t", float_format="%.4f", index=False)
|
|
51
|
+
|
|
52
|
+
else:
|
|
53
|
+
counts_a = read_csv(args.input_filepath[0], sep=None, engine="python", dtype=int64)
|
|
54
|
+
counts_b = read_csv(args.input_filepath[1], sep=None, engine="python", dtype=int64)
|
|
55
|
+
result = sentropy(counts_a, counts_b, similarity=args.similarity,\
|
|
56
|
+
viewpoint=args.viewpoint[0], measures=args.measure, chunk_size=args.chunk_size, \
|
|
57
|
+
return_dataframe=True, which=args.which, eff_no=args.eff_no, \
|
|
58
|
+
backend=args.backend, device=args.device)
|
|
59
|
+
|
|
60
|
+
print("set-level relative entropy:", result[0])
|
|
61
|
+
print("subset-level relative entropies:", result[1])
|
|
62
|
+
|
|
63
|
+
if args.output_filepath is not None:
|
|
64
|
+
with open(args.output_filepath, 'wb') as f:
|
|
65
|
+
pickle.dump(result, f)
|
|
66
|
+
|
|
67
|
+
LOGGER.info("Done!")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__": # pragma: no cover
|
|
71
|
+
parser = configure_arguments()
|
|
72
|
+
args = parser.parse_args()
|
|
73
|
+
main(args)
|
sentropy/abundance.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# abundance.py
|
|
2
|
+
|
|
3
|
+
"""Module for calculating relative sub- and metacomunity abundances.
|
|
4
|
+
|
|
5
|
+
Classes
|
|
6
|
+
-------
|
|
7
|
+
Abundance
|
|
8
|
+
Relative (normalized) species abundances in (meta-/sub-) communities
|
|
9
|
+
AbundanceForDiversity
|
|
10
|
+
Species abundances-- normalized over set, normalized over each subset,
|
|
11
|
+
and totalled across set-- as is required for diversity calculations
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from functools import cached_property
|
|
16
|
+
from typing import Iterable, Union
|
|
17
|
+
|
|
18
|
+
from numpy import arange
|
|
19
|
+
from pandas import DataFrame
|
|
20
|
+
from scipy.sparse import issparse # type: ignore[import]
|
|
21
|
+
|
|
22
|
+
from sentropy.backend import get_backend, NumpyBackend
|
|
23
|
+
from sentropy.exceptions import InvalidArgumentError
|
|
24
|
+
|
|
25
|
+
# We'll avoid importing numpy or torch directly here. Use backend ops when available.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Abundance:
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
counts,
|
|
32
|
+
subset_names: Iterable[Union[str, int]],
|
|
33
|
+
backend=None,
|
|
34
|
+
) -> None:
|
|
35
|
+
"""
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
counts:
|
|
39
|
+
2-d array with one column per subset, one row per
|
|
40
|
+
species, containing the count of each species in the
|
|
41
|
+
corresponding subsets.
|
|
42
|
+
backend:
|
|
43
|
+
An instance of a backend (from sentropy.backend.get_backend or backend class).
|
|
44
|
+
If None, numpy backend is used.
|
|
45
|
+
"""
|
|
46
|
+
self.backend = backend if backend is not None else get_backend("numpy")
|
|
47
|
+
# convert counts to backend array
|
|
48
|
+
self.counts = self.backend.asarray(counts) if hasattr(self.backend, "asarray") else self.backend.array(counts)
|
|
49
|
+
self.subsets_names = subset_names
|
|
50
|
+
self.num_subsets = self.counts.shape[1]
|
|
51
|
+
# min_count : small nonzero for numerical stability
|
|
52
|
+
total = self.backend.sum(self.counts)
|
|
53
|
+
# guard: total could be scalar tensor -> convert to python float if needed
|
|
54
|
+
total_scalar = float(total)
|
|
55
|
+
# compute min_count using backend's array semantics
|
|
56
|
+
# fallback to numpy small value
|
|
57
|
+
self.min_count = min(1.0 / (total_scalar if total_scalar != 0 else 1.0), 1e-9)
|
|
58
|
+
|
|
59
|
+
self.subset_abundance = self.make_subset_abundance(counts=self.counts)
|
|
60
|
+
self.normalized_subset_abundance = (
|
|
61
|
+
self.make_normalized_subset_abundance()
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def make_subset_abundance(self, counts):
|
|
65
|
+
"""Calculates the relative abundances in subsets."""
|
|
66
|
+
# counts / counts.sum()
|
|
67
|
+
total = self.backend.sum(counts)
|
|
68
|
+
# broadcasting semantics should match numpy/torch
|
|
69
|
+
return counts / total
|
|
70
|
+
|
|
71
|
+
def make_subset_normalizing_constants(self):
|
|
72
|
+
"""Calculates subset normalizing constants."""
|
|
73
|
+
return self.backend.sum(self.subset_abundance, axis=0)
|
|
74
|
+
|
|
75
|
+
def make_normalized_subset_abundance(self):
|
|
76
|
+
"""Calculates normalized relative abundances in subsets."""
|
|
77
|
+
self.subset_normalizing_constants = (
|
|
78
|
+
self.make_subset_normalizing_constants()
|
|
79
|
+
)
|
|
80
|
+
# divide by constants: ensure broadcasting
|
|
81
|
+
return self.subset_abundance / self.subset_normalizing_constants
|
|
82
|
+
|
|
83
|
+
def premultiply_by(self, similarity):
|
|
84
|
+
return similarity.weighted_abundances(self.normalized_subset_abundance)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class AbundanceForDiversity(Abundance):
|
|
88
|
+
"""Calculates metacommuntiy and subset relative abundance
|
|
89
|
+
components from a numpy.ndarray containing species counts
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
def __init__(
|
|
93
|
+
self, counts, subset_names: Iterable[Union[str, int]], backend=None
|
|
94
|
+
) -> None:
|
|
95
|
+
super().__init__(counts, subset_names, backend=backend)
|
|
96
|
+
self.set_abundance = self.make_set_abundance()
|
|
97
|
+
self.unified_abundance_array = None
|
|
98
|
+
|
|
99
|
+
def unify_abundance_array(self) -> None:
|
|
100
|
+
"""Creates one matrix containing all the abundance matrices:
|
|
101
|
+
set, subset, and normalized subset.
|
|
102
|
+
"""
|
|
103
|
+
self.unified_abundance_array = self.backend.concatenate(
|
|
104
|
+
(
|
|
105
|
+
self.set_abundance,
|
|
106
|
+
self.subset_abundance,
|
|
107
|
+
self.normalized_subset_abundance,
|
|
108
|
+
),
|
|
109
|
+
axis=1,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def get_unified_abundance_array(self):
|
|
113
|
+
if self.unified_abundance_array is None:
|
|
114
|
+
self.unify_abundance_array()
|
|
115
|
+
# update views (these are backend arrays)
|
|
116
|
+
self.set_abundance = self.unified_abundance_array[:, [0]]
|
|
117
|
+
self.subset_abundance = self.unified_abundance_array[
|
|
118
|
+
:, 1 : (1 + self.num_subsets)
|
|
119
|
+
]
|
|
120
|
+
self.normalized_subset_abundance = self.unified_abundance_array[
|
|
121
|
+
:, (1 + self.num_subsets) :
|
|
122
|
+
]
|
|
123
|
+
return self.unified_abundance_array
|
|
124
|
+
|
|
125
|
+
def premultiply_by(self, similarity):
|
|
126
|
+
if similarity.is_expensive():
|
|
127
|
+
all_ordinariness = similarity.self_similar_weighted_abundances(
|
|
128
|
+
self.get_unified_abundance_array()
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
set_ordinariness = all_ordinariness[:, [0]]
|
|
132
|
+
subset_ordinariness = all_ordinariness[
|
|
133
|
+
:, 1 : (1 + self.num_subsets)
|
|
134
|
+
]
|
|
135
|
+
normalized_subset_ordinariness = all_ordinariness[
|
|
136
|
+
:, (1 + self.num_subsets) :
|
|
137
|
+
]
|
|
138
|
+
else:
|
|
139
|
+
set_ordinariness = similarity.self_similar_weighted_abundances(
|
|
140
|
+
self.set_abundance
|
|
141
|
+
)
|
|
142
|
+
subset_ordinariness = similarity.self_similar_weighted_abundances(
|
|
143
|
+
self.subset_abundance
|
|
144
|
+
)
|
|
145
|
+
normalized_subset_ordinariness = (
|
|
146
|
+
similarity.self_similar_weighted_abundances(
|
|
147
|
+
self.normalized_subset_abundance
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
return (
|
|
151
|
+
set_ordinariness,
|
|
152
|
+
subset_ordinariness,
|
|
153
|
+
normalized_subset_ordinariness,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
def make_set_abundance(self):
|
|
157
|
+
"""Calculates the relative abundances in set."""
|
|
158
|
+
return self.backend.sum(self.subset_abundance, axis=1, keepdims=True)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def make_abundance(counts, for_diversity=True, backend=None):
|
|
162
|
+
"""Initializes a concrete subclass of Abundance."""
|
|
163
|
+
if not for_diversity:
|
|
164
|
+
specific_class = Abundance
|
|
165
|
+
else:
|
|
166
|
+
specific_class = AbundanceForDiversity
|
|
167
|
+
if isinstance(counts, DataFrame):
|
|
168
|
+
arr = counts.to_numpy()
|
|
169
|
+
return specific_class(
|
|
170
|
+
counts=arr, subset_names=counts.columns.to_list(), backend=backend
|
|
171
|
+
)
|
|
172
|
+
elif hasattr(counts, "shape"):
|
|
173
|
+
if issparse(counts):
|
|
174
|
+
raise TypeError("sparse abundance matrix not yet implemented")
|
|
175
|
+
else:
|
|
176
|
+
return specific_class(
|
|
177
|
+
counts=counts, subset_names=arange(counts.shape[1]), backend=backend
|
|
178
|
+
)
|
|
179
|
+
else:
|
|
180
|
+
raise NotImplementedError(
|
|
181
|
+
f"Type {type(counts)} is not supported for argument "
|
|
182
|
+
"'counts'. Valid types include pandas.DataFrame or"
|
|
183
|
+
"numpy.ndarray"
|
|
184
|
+
)
|