API Reference¶
This section is auto-generated from the docstrings of the sfa package
using mkdocstrings.
sfa.base¶
sfa.base
¶
Algorithm
¶
Bases: ContainerItem
The base class of Algorithm classes.
Attributes:
| Name | Type | Description |
|---|---|---|
abbr |
str
|
|
name |
str
|
|
data |
Data
|
|
params |
ParameterSet
|
|
result |
Result
|
|
Examples:
>>> class AnAlgorithm(sfa.base.Algorithm):
# Definition of algorithm ...
...
>>> alg = AnAlgorithm()
>>> alg.params = params_obj # Parameters of the algorithm
>>> alg.data = data_obj # Data to be analyzed by the algorithm
>>> alg.initialize()
>>> res = alg.compute()
Source code in sfa/base.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
data
property
writable
¶
The object of sfa.base.Data.
Data to be processed based on the algorithm
can accessed through this member.
params
property
writable
¶
The object of sfa.base.ParameterSet.
Parameters of the algorithm can accessed
through this member.
result
property
¶
The object of sfa.base.Result.
The result of computing the batch.
compute(b)
abstractmethod
¶
Process the assigned data
with the given basal activity, :math:b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
ndarray
|
1D array of basal activity. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
1D-array object of activity at steady-state. |
Source code in sfa/base.py
compute_batch()
abstractmethod
¶
Process the assigned data that contains a batch data.
The result is stored in result member.
copy(is_deep=False)
¶
initialize(network=True, ba=True)
¶
initialize_basal_activity()
¶
ContainerItem
¶
The base class that defines the item object of
sfa.containers.Container.
Source code in sfa/base.py
ParameterSet
¶
Bases: FrozenClass
The base class of ParameterSet objects.
Source code in sfa/base.py
sfa.containers¶
sfa.containers
¶
AlgorithmSet
¶
Bases: Container
Source code in sfa/containers.py
Container
¶
Bases: MutableMapping
A simple container class, which handles multiple objects with its hashable functionality (using dictionary).
Source code in sfa/containers.py
__init__(*args, **kwargs)
¶
_dict: internal data structure, which is hashable. _dpath: Path of the directory containing algorithms, data, etc.
Source code in sfa/containers.py
create(keys=None)
¶
Create a single or multiple objects according to keys. keys: a single string or multiple strings in an iterable object. All related objects are created if 'keys' is None.
Source code in sfa/containers.py
DataSet
¶
Bases: Container
The name of this class is similar to that of 'DataSet' in C#. The instance of this class handles multiple sfa.base.Data objects.
Source code in sfa/containers.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
sfa.fileio¶
sfa.fileio
¶
create_from_sif(fpath, abbr=None, inputs=None, outputs=None)
¶
Create sfv.base.Data object from SIF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fpath
|
str
|
Absolute path of SIF file |
required |
abbr
|
str
|
Abbreviation to denote this data object for the network. |
None
|
inputs
|
dict
|
Input information with default values |
None
|
outputs
|
sequence
|
Output information. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
obj |
Data
|
Data object with the information of network topology. |
Source code in sfa/fileio.py
sfa.utils¶
sfa.utils
¶
get_akey(d)
¶
Get a key from a given dictionary. It returns the first key in d.keys().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Dictionary of objects. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obj |
object
|
First item of iter(d.keys()). |
Source code in sfa/utils.py
get_avalue(d)
¶
Get a value from a given dictionary. It returns the value designated by sfa.get_akey().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Dictionary of objects. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obj |
object
|
First item of d[iter(d.keys())]. |
Source code in sfa/utils.py
rand_flip(A, nsamp=10, pivots=None, inplace=False)
¶
Randomly flip the signs of connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
Adjacency matrix (connection matrix). |
required |
nsamp
|
int
|
Number of sampled connections to be flipped. |
10
|
pivots
|
list
|
Indices of pivot nodes |
None
|
inplace
|
bool
|
Modify the given adjacency matrix for rewiring. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
B |
ndarray
|
The randomized matrix. The reference of the given W is returned, when inplace is True. |
Source code in sfa/utils.py
rand_swap(A, nsamp=10, noself=True, pivots=None, inplace=False)
¶
Randomly rewire the network connections by swapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
Adjacency matrix (connection matrix). |
required |
nsamp
|
int
|
Number of sampled connections to rewire |
10
|
noself
|
bool
|
Whether to allow self-loop link. |
True
|
pivots
|
list
|
Indices of pivot nodes |
None
|
inplace
|
bool
|
Modify the given adjacency matrix for rewiring. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
B |
ndarray
|
The randomized matrix. The reference of the given W is returned, when inplace is True. |
Source code in sfa/utils.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
rand_weights(W, lb=-3, ub=3, inplace=False)
¶
Randomly sample the weights of connections in W from 10^(lb, ub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Adjacency (connection) or weight matrix. |
required |
lb
|
float
|
The 10's exponent for lower bound |
-3
|
inplace
|
bool
|
Modify the given adjacency matrix for rewiring. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
B |
ndarray
|
The randomly sampled weight matrix. The reference of the given W is returned, when inplace is True. |
Source code in sfa/utils.py
sfa.stats¶
sfa.stats
¶
calc_accuracy(df1, df2, get_cons=False)
¶
Count the same sign of each element between df1 and df2
df1: pandas.DataFrame or numpy.ndarray to be compared df2: pandas.DataFrame or numpy.ndarray to be compared getcons: decide whether to return consensus array in DataFrame or not
Source code in sfa/stats.py
sfa.topology¶
sfa.topology
¶
max_spl(nxdg)
¶
Find the maximum length of the shortest path
Source code in sfa/topology.py
splo(nxdg, sources, outputs, rtype='df')
¶
Calculate the shortest path length from each source node to the outputs. SPLO represents 'shortest path length to output'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nxdg
|
A directed network in NetworkX. |
required | |
sources
|
Names of source nodes in nxdg. |
required | |
outputs
|
Names of output nodes in nxdg. |
required | |
rtype
|
Return object type: 'df' or 'dict'. |
'df'
|
Returns:
| Name | Type | Description |
|---|---|---|
splo |
dict
|
All the shortest path lengths to the outputs. |
Source code in sfa/topology.py
sfa.algorithms.np¶
sfa.algorithms.np
¶
NetworkPropagation
¶
Bases: Algorithm
A base class that defines the basic functionality of network propagation algorithms.
Source code in sfa/algorithms/np.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
__init__(abbr)
¶
Constructor of NetworkPropagation.
Source code in sfa/algorithms/np.py
apply_inputs(inds, vals)
¶
Source code in sfa/algorithms/np.py
prepare_exact_solution()
¶
prepare_iterative_solution()
¶
propagate_exact(b)
¶
Obtain the activity at steady-state based on the exact solution of network propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
ndarray
|
1D array of basal activity. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
The exact solution of the activity at steady-state in 1D array. |
Source code in sfa/algorithms/np.py
propagate_iterative(W, xi, b, a=0.5, lim_iter=1000, tol=1e-05, get_trj=False)
¶
Compute network propagation based on the iterative method. This method should be used if we want to obtain the trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
2D array for weight matrix. |
required | |
xi
|
1D array for initial state. |
required | |
b
|
1D array for basal activity. |
required | |
a
|
Hyperparameter, :math: |
0.5
|
|
lim_iter
|
Number of maximum iterations in the iterative method.
Computation terminates when the iteration reaches |
1000
|
|
tol
|
Tolerance for terminating iteration.
Iteration continues, if Frobenius norm of
:math: |
1e-05
|
|
get_trj
|
Determine whether the trajectory of the state is returned. If get_trj is true, the trajectory is returned. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
1D array of the activity after the computation. |
trj |
ndarray
|
2D array where the row represents a state of the activity. |
See also
Source code in sfa/algorithms/np.py
NetworkPropagationParameterSet
¶
Bases: ParameterSet
An object that deals with the parameters
of sfa.algorithms.np.NetworkPropagation base algorithm.
Attributes:
| Name | Type | Description |
|---|---|---|
alpha |
float
|
|
lim_iter |
int
|
|
apply_weight_norm |
bool
|
|
use_rel_change |
bool
|
|
exsol_forbidden |
bool
|
|
no_inputs |
bool
|
|
Source code in sfa/algorithms/np.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
alpha
property
writable
¶
Hyperparameter, :math:\alpha ~ (0, 1).
It controls the portion of signal flow
in determining the activity.
The default value is 0.5.
apply_weight_norm
property
writable
¶
Apply the link weight normalization.
exsol_forbidden
property
writable
¶
Forbid the propagation computation based on the exact solution. In other words, use the iterative method.
lim_iter
property
writable
¶
Number of maximum iterations in the iterative method.
no_inputs
property
writable
¶
Do not apply the effects of inputs in a given network.
use_rel_change
property
writable
¶
Use relative change for prediction.
initialize()
¶
Initialize the parameters with default values.
Source code in sfa/algorithms/np.py
sfa.algorithms.sp¶
sfa.algorithms.sp
¶
SignalPropagation
¶
Bases: NetworkPropagation
Source code in sfa/algorithms/sp.py
prepare_exact_solution()
¶
Prepare to get the matrix for the exact solution:
.. :math x(t+1) = a_W.dot(x(t)) + (1-a)b, where \(a\) is alpha.
When \(t -> inf\), both \(x(t+1)\) and \(x(t)\) converges to the stationary state.
Then, s = aWs + (1-a)b (I-aW)s = (1-a)b s = (I-aW)^-1 * (1-a)b s = M*b, where M is (1-a)(I-aW)^-1.
This method is to get the matrix, M for preparing the exact solution
Source code in sfa/algorithms/sp.py
sfa.analysis.perturb¶
sfa.analysis.perturb
¶
analyze_perturb(alg, data, targets, b=None, get_trj=False)
¶
Perform signal flow analysis under perturbations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alg
|
Algorithm
|
Algorithm object. |
required |
data
|
Data
|
Data object which has perturbation data. |
required |
targets
|
list
|
List of node names, which are the keys of data.n2i. |
required |
b
|
ndarray
|
Basic vector for signaling sources or basal activities. |
None
|
get_trj
|
bool(optional)
|
Decide to get the trajectory of activity change. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
act |
ndarray
|
Change in the activities. It is usually calculated as x2 - x1, where x is the a vector of activities at steady-state. |
F |
ndarray
|
A matrix of signal flows. |
trj |
ndarray(optional)
|
Trajectory of activity change, which is returned if get_trj is True. |
Source code in sfa/analysis/perturb.py
sfa.analysis.random.base¶
sfa.analysis.random.base
¶
BaseRandomSimulator
¶
Bases: object
Source code in sfa/analysis/random/base.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
sfa.analysis.random.structure¶
sfa.analysis.random.structure
¶
sfa.analysis.random.weight¶
sfa.analysis.random.weight
¶
sfa.control.influence¶
sfa.control.influence
¶
compute_influence(W, alpha=0.9, beta=0.1, S=None, rtype='df', outputs=None, n2i=None, max_iter=1000, tol=1e-07, get_iter=False, device='cpu', sparse=False)
¶
Compute the influence. It estimates the effects of a node to the other nodes, by calculating partial derivative with respect to source nodes, based on a simple iterative method.
Based on the below difference equation,
x(t+1) = alphaW.dot(x(t)) + (1-alpha)b
The influence matrix, S, is computed using chain rule of partial derivative as follows.
\begin{align} S_{ij} &= \frac{\partial{x_i}}{\partial{x_j}} \ &= (I + \alpha W + \alpha^2 W^2 + ... + \alpha^{\infty}W^{\infty}){ij} \ &\approx (I + \alpha W + \alpha^2 W^2 + ... + \alpha^{l}W^{l}) \ \end{align}
This is the summation of the weight multiplications along all paths including cycles. \(S_{ij}\) denotes the influence of node (j) on node (i).
An iterative method for an approximated solution is as follows.
S(t+1) = \alpha WS(t) + I,
where \(S(0) = \beta I\) and \(S(1) = \beta(I + \alpha W)\) \((t>1)\).
The iteration continues until \(||S(t+1) - S(t)|| \leq tol\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
W
|
ndarray
|
Weight matrix. |
required |
alpha
|
float
|
Hyperparameter for adjusting the effect of signal flow. |
0.9
|
beta
|
float
|
Hyperparameter for adjusting the effect of basal activity. |
0.1
|
S
|
ndarray
|
Initial influence matrix. |
None
|
rtype
|
Return object type: 'df' or 'array'. |
'df'
|
|
outputs
|
Names of output nodes, which is necessary for 'df' rtype. |
None
|
|
n2i
|
Name to index dict, which is necessary for 'df' rtype. |
None
|
|
max_iter
|
int
|
The maximum iteration number for the estimation. |
1000
|
tol
|
float
|
Tolerance for terminating the iteration. |
1e-07
|
get_iter
|
bool
|
Determine whether the actual iteration number is returned. |
False
|
device
|
(str, optional, {CPU, 'GPU:0', 'GPU:1', ...})
|
Select which device to use. 'CPU' is default. |
'cpu'
|
sparse
|
bool
|
Use sparse matrices for the computation. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
S |
(ndarray, optional)
|
2D array of influence. |
df |
(DataFrame, optional)
|
Influences for each output in DataFrame. |
num_iter |
(int, optional)
|
The actual number of iteration. |
Source code in sfa/control/influence.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
prioritize(df_splo, df_inf, output, dac, thr_rank=3, min_group_size=0, min_splo=None, max_splo=None, thr_inf=1e-10)
¶
Prioritize target candiates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_splo
|
DataFrame
|
Dataframe for SPLO information. |
required |
df_inf
|
DataFrame
|
Dataframe for influence information. |
required |
output
|
str
|
Names of output node, which is necessary for 'df_inf'. |
required |
dac
|
int
|
Direction of activity change (DAC) of the output. |
required |
thr_rank
|
int or float
|
Rank to filter out the entities. The entities whose ranks are greater than thr_rank survive. |
3
|
min_group_size
|
int
|
Minimum group size to be satisfied. |
0
|