Skip to content

Commit

Permalink
Remove unused API-calling code (will be covered by #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
caiw committed Nov 1, 2024
1 parent 161a075 commit f91409f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 74 deletions.
2 changes: 1 addition & 1 deletion kymata/entities/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
BLOCK_SCALP = "scalp"

# Column names
COL_LOGP_VALUE = "log-p-value"
COL_LOGP_VALUE = "log p-value"


class ExpressionSet(ABC):
Expand Down
6 changes: 1 addition & 5 deletions kymata/gridsearch/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
from kymata.entities.transform import Transform
from kymata.math.combinatorics import generate_derangement
from kymata.math.vector import normalize, get_stds
from kymata.entities.expression import (
ExpressionSet,
SensorExpressionSet,
HexelExpressionSet,
)
from kymata.entities.expression import ExpressionSet, SensorExpressionSet, HexelExpressionSet
from kymata.math.p_values import log_base, p_to_logp
from kymata.plot.plot import plot_top_five_channels_of_gridsearch

Expand Down
46 changes: 0 additions & 46 deletions kymata/ippm/data_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from kymata.entities.constants import HEMI_LEFT, HEMI_RIGHT
from kymata.entities.expression import HexelExpressionSet, DIM_TRANSFORM, DIM_LATENCY, COL_LOGP_VALUE
from kymata.io.atlas import fetch_data_dict
from kymata.math.p_values import logp_to_p


Expand Down Expand Up @@ -60,23 +59,6 @@ def add_pairing(self, hemi: str, expr_timing: ExpressionPairing):
SpikeDict = dict[str, IPPMSpike]


def fetch_spike_dict(api: str) -> SpikeDict:
"""
Fetches data from Kymata API and converts it into a dictionary of transform names as keys
and spike objects as values. Advantage of dict is O(1) look-up and spike object is readable
access to attributes.
Params
------
api : URL of the API from which to fetch data
Returns
-------
Dictionary containing data in the format [transform name, spike]
"""
return build_spike_dict_from_api_response(fetch_data_dict(api))


def build_spike_dict_from_expression_set(expression_set: HexelExpressionSet) -> SpikeDict:
"""
Builds the dictionary from an ExpressionSet. This function builds a new dictionary
Expand All @@ -102,34 +84,6 @@ def build_spike_dict_from_expression_set(expression_set: HexelExpressionSet) ->
return spikes


def build_spike_dict_from_api_response(dict_: dict) -> SpikeDict:
"""
Builds the dictionary from response dictionary. Response dictionary has unneccesary
keys and does not have transform names as keys. This function builds a new dictionary
which has transform names (fast look-up) and only necessary data.
Params
------
dict_ : JSON dictionary of HTTP GET response object.
Returns
-------
Dict of the format [transform name, spike(trans_name, id, left_timings, right_timings)]
"""
spikes = {}
for hemi in [HEMI_LEFT, HEMI_RIGHT]:
for _, latency, pval, trans in dict_[hemi]:
# we have id, latency (ms), pvalue (log_10), transform name.
# discard id as it conveys no useful information
if trans not in spikes:
# first time seeing transform, so create key and spike object.
spikes[trans] = IPPMSpike(trans)

spikes[trans].add_pairing(hemi, ExpressionPairing(latency, pow(10, pval)))

return spikes


def remove_excess_transforms(to_retain: list[str], spikes: SpikeDict) -> SpikeDict:
"""
Utility function to distill the spikes down to a subset of transforms. Use this to visualise a subset of transforms
Expand Down
20 changes: 20 additions & 0 deletions kymata/math/p_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ def p_to_logp(arraylike: ArrayLike) -> ArrayLike:
def logp_to_p(arraylike: ArrayLike) -> ArrayLike:
"""The one-stop-shop for converting from log p-values to p-values."""
return float(10) ** arraylike


def p_to_surprisal(arraylike: ArrayLike) -> ArrayLike:
"""Converts p-values to surprisal values."""
return logp_to_surprisal(p_to_logp(arraylike))


def logp_to_surprisal(arraylike: ArrayLike) -> ArrayLike:
"""Converts logp-values to surprisal values."""
return -1 * arraylike


def surprisal_to_logp(arraylike: ArrayLike) -> ArrayLike:
"""Converts surprisal values to logp values."""
return -1 * arraylike


def surprisal_to_p(arraylike: ArrayLike) -> ArrayLike:
"""Converts surprisal values to p-values."""
return logp_to_p(surprisal_to_logp(arraylike))
23 changes: 1 addition & 22 deletions tests/test_ippm_data_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"""

from kymata.entities.constants import HEMI_RIGHT, HEMI_LEFT
from kymata.ippm.data_tools import (
IPPMSpike, build_spike_dict_from_api_response,
copy_hemisphere, remove_excess_transforms,
)
from kymata.ippm.data_tools import IPPMSpike, copy_hemisphere, remove_excess_transforms


def test_hexel():
Expand All @@ -24,24 +21,6 @@ def test_hexel():
assert hexel.right_best_pairings == [(20, 10e-3), (50, 0.000012), (611, 0.00053)]


def test_build_spike_dict():
test_dict = {
HEMI_LEFT: [[2, 1, 0.012, "left1"], [2, 14, 0.213, "left1"]],
HEMI_RIGHT: [[3, 51, 0.1244, "left1"], [4, 345, 0.557, "right1"]],
}

hexels = build_spike_dict_from_api_response(test_dict)

# check transforms are saved correctly
assert list(hexels.keys()) == ["left1", "right1"]
# check p value is stored and calculated correctly
assert hexels["left1"].left_best_pairings == [
(1, pow(10, 0.012)),
(14, pow(10, 0.213)),
]
assert hexels["left1"].right_best_pairings == [(51, pow(10, 0.1244))]


def test_Should_removeExcessFuncs_When_validInput():
hexels = {"f1": IPPMSpike("f1"), "f2": IPPMSpike("f2"), "f3": IPPMSpike("f3")}
to_retain = ["f2"]
Expand Down

0 comments on commit f91409f

Please sign in to comment.