Skip to content

Commit

Permalink
updated analyzer with coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
smythi93 committed May 29, 2024
1 parent 902f45f commit 211221a
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 1,401 deletions.
1,177 changes: 166 additions & 1,011 deletions BugsInPy.ipynb

Large diffs are not rendered by default.

440 changes: 58 additions & 382 deletions Demo.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/sflkit/analysis/analysis_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def set_finder(function_finder, loop_finder, branch_finder):
def __init__(self, event):
self.suspiciousness: float = 0
self.last_evaluation: EvaluationResult = EvaluationResult.UNOBSERVED
self.hits = dict()

def get_last_evaluation(self, id_: int) -> EvaluationResult:
return self.last_evaluation
Expand Down
31 changes: 27 additions & 4 deletions src/sflkit/analysis/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List, Callable
import os
from typing import List, Callable, Set, Dict

from sflkit.analysis.analysis_type import AnalysisType
from sflkit.analysis.analysis_type import AnalysisType, AnalysisObject
from sflkit.analysis.factory import AnalysisFactory
from sflkit.analysis.suggestion import Suggestion
from sflkit.model.event_file import EventFile
Expand All @@ -17,6 +18,7 @@ def __init__(
self.relevant_event_files = relevant_event_files
self.irrelevant_event_files = irrelevant_event_files
self.model = Model(factory)
self.paths: Dict[int, os.PathLike] = dict()

def _analyze(self, event_file):
self.model.prepare(event_file)
Expand All @@ -29,16 +31,17 @@ def _finalize(self):

def analyze(self):
for event_file in self.relevant_event_files + self.irrelevant_event_files:
self.paths[event_file.run_id] = event_file.path
self._analyze(event_file)
self._finalize()

def dump(self, path):
pass

def get_analysis(self):
def get_analysis(self) -> Set[AnalysisObject]:
return list(self.model.get_analysis())

def get_analysis_by_type(self, type_: AnalysisType):
def get_analysis_by_type(self, type_: AnalysisType) -> Set[AnalysisObject]:
return list(
filter(lambda p: p.analysis_type() == type_, self.model.get_analysis())
)
Expand Down Expand Up @@ -66,3 +69,23 @@ def get_sorted_suggestions(
],
reverse=True,
)[:]

def get_coverage_per_run(
self, type_: AnalysisType = None
) -> Dict[EventFile, Set[AnalysisObject]]:
if type_:
objects = self.get_analysis_by_type(type_)
else:
objects = self.get_analysis()
coverage = dict()
for obj in objects:
for run_id in obj.hits:
if run_id not in coverage:
coverage[run_id] = {obj}
else:
coverage[run_id].add(obj)
return coverage

def get_coverage(self, type_: AnalysisType = None) -> Set[AnalysisObject]:
coverage = self.get_coverage_per_run(type_)
return set.union(*coverage.values())
1 change: 0 additions & 1 deletion src/sflkit/analysis/spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def __init__(
self.failed = failed_observed + failed_not_observed
self.failed_observed = failed_observed
self.failed_not_observed = failed_not_observed
self.hits = dict()
self.last_evaluation: EvaluationResult = EvaluationResult.FALSE

def __str__(self):
Expand Down
7 changes: 6 additions & 1 deletion src/sflkit/model/event_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pickle import PickleError

from sflkitlib.events import event
Expand All @@ -7,7 +8,11 @@

class EventFile(object):
def __init__(
self, path: str, run_id: int, mapping: EventMapping, failing: bool = False
self,
path: os.PathLike,
run_id: int,
mapping: EventMapping,
failing: bool = False,
):
self.path = path
self.run_id = run_id
Expand Down
5 changes: 3 additions & 2 deletions src/sflkit/model/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Set

from sflkit.model.scope import Scope

Expand Down Expand Up @@ -68,7 +68,8 @@ def enter_scope(self):
def exit_scope(self):
self.variables = self.variables.exit()

def get_analysis(self) -> List:
# noinspection PyUnresolvedReferences
def get_analysis(self) -> Set["AnalysisObject"]:
return self.factory.get_all()

def finalize(self, passed, failed):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List

from sflkit.analysis.analysis_type import AnalysisType
from sflkit.analysis.spectra import Line
from utils import BaseTest


class TestCoverage(BaseTest):
def test_coverage(self):
analyzer = self.run_analysis(
self.TEST_SUGGESTIONS,
"line",
"line",
relevant=[["2", "1", "3"]],
irrelevant=[["3", "2", "1"], ["3", "1", "2"]],
)
coverage: List[Line] = analyzer.get_coverage(AnalysisType.LINE)
coverage = {line.line for line in coverage}
self.assertEqual(coverage, {1, 5, 6, 7, 9, 10, 12, 13, 16, 19, 20})

0 comments on commit 211221a

Please sign in to comment.