-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrated evidence fuser to calculate likelihoods
- Loading branch information
Showing
5 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from abc import abstractmethod, ABC | ||
from typing import Optional, Dict | ||
|
||
import numpy as np | ||
|
||
|
||
class EvidenceFuser(ABC): | ||
|
||
@abstractmethod | ||
def fuse(self, prior_likelihood: Optional[np.ndarray], evidence: Dict) -> np.ndarray: | ||
... | ||
|
||
|
||
class MultipyFuser(EvidenceFuser): | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def fuse(self, prior_likelihood, evidence) -> np.ndarray: | ||
|
||
len_dist = len(list(evidence.values())[0]) | ||
prior_likelihood = prior_likelihood if prior_likelihood is not None else self.__make_prior(len_dist) | ||
ret_likelihood = prior_likelihood.copy() | ||
|
||
for value in evidence.values(): | ||
ret_likelihood *= value[:] | ||
ret_likelihood = self.__clean_likelihood(ret_likelihood) | ||
|
||
return ret_likelihood | ||
|
||
def __make_prior(self, len_dist): | ||
return np.ones(len_dist) / len_dist | ||
|
||
def __clean_likelihood(self, likelihood): | ||
|
||
cleaned_likelihood = likelihood.copy() | ||
if np.isinf(np.sum(likelihood)): | ||
tmp = np.zeros(len(likelihood)) | ||
tmp[np.where(likelihood == np.inf)[0][0]] = 1 | ||
cleaned_likelihood = tmp | ||
|
||
if not np.isnan(np.sum(likelihood)): | ||
cleaned_likelihood = likelihood / np.sum(likelihood) | ||
|
||
return cleaned_likelihood |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, List | ||
|
||
import numpy as np | ||
|
||
|
||
@dataclass | ||
class InquiryResult: | ||
target: Optional[str] | ||
time_spent: int # TODO what does time_spent mean? | ||
stimuli: List | ||
evidence_likelihoods: List | ||
evidence_likelihoods: List # TODO make this into a dictionary to support multimodal. e.g {SignalModel: evidence_list, LanguageModel:evidence_list} | ||
fused_likelihood: np.ndarray | ||
decision: Optional[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters