• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alxdrcirilo / apple-health-parser / 23042652217

13 Mar 2026 08:30AM UTC coverage: 92.447% (+25.5%) from 66.904%
23042652217

push

github

semantic-release
2.0.0

Automatically generated by python-semantic-release

563 of 609 relevant lines covered (92.45%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.74
/apple_health_parser/interfaces/preprocessor_interface.py
1
from abc import ABC, abstractmethod
1✔
2

3
import pandas as pd
1✔
4

5
from apple_health_parser.config.definitions import Metadata
1✔
6
from apple_health_parser.consts import FLAG_METADATA, OPERATIONS
1✔
7
from apple_health_parser.exceptions import (
1✔
8
    InvalidHeatmapOperation,
9
    InvalidOperation,
10
    InvalidSource,
11
    MissingFlag,
12
    MissingYear,
13
)
14
from apple_health_parser.models.parsed import ParsedData
1✔
15

16

17
class PreprocessorInterface(ABC):
1✔
18
    def __init__(
1✔
19
        self,
20
        data: ParsedData,
21
        year: int,
22
        source: str | None = None,
23
        operation: str | None = None,
24
        heatmap: bool = False,
25
    ) -> None:
26
        """
27
        Initialize the Preprocessor object.
28

29
        Prepare the DataFrame for the plot.
30

31
        Args:
32
            data (ParsedData): ParsedData object
33
            year (int, optional): Year, defaults to date.today().year
34
            source (str | None, optional): Source, defaults to None
35
            operation (str | None, optional): Operation, defaults to None
36
            heatmap (bool, optional): Flag to plot a heatmap, defaults to False
37
        """
38
        self.src = source
1✔
39
        self.data = data
1✔
40
        self.year = year
1✔
41
        self.oper = operation
1✔
42
        self.hmap = heatmap
1✔
43
        self.flag = data.flag
1✔
44
        self._validate()
1✔
45

46
    @property
1✔
47
    def meta(self) -> Metadata:
1✔
48
        """
49
        Metadata for the flag.
50

51
        Raises:
52
            MissingFlag: Missing flag metadata
53

54
        Returns:
55
            Metadata: Metadata for the flag
56
        """
57
        metadata = FLAG_METADATA.get(self.flag)
1✔
58
        if metadata is None:
1✔
59
            raise MissingFlag(self.flag)
×
60
        return metadata
1✔
61

62
    def _validate(self) -> None:
1✔
63
        """
64
        Validate the flag, operation, and year.
65
        Skip year validation for sleep data.
66

67
        Raises:
68
            MissingYear: Missing year in the records
69
            InvalidOperation: Invalid operation
70
            InvalidHeatmapOperation: Invalid operation for heatmap
71
            InvalidSource: Invalid source name
72
        """
73
        years = self.data.records.start_date.dt.year.unique().tolist()
1✔
74

75
        if (
1✔
76
            self.flag != "HKCategoryTypeIdentifierSleepAnalysis"
77
            and self.year not in years
78
        ):
79
            raise MissingYear(self.year, years)
1✔
80

81
        if self.oper is not None:
1✔
82
            if self.oper not in OPERATIONS:
1✔
83
                raise InvalidOperation(self.oper)
1✔
84
        else:
85
            if self.hmap:
1✔
86
                raise InvalidHeatmapOperation
×
87

88
        sources = self.data.records.source_name.unique()
1✔
89
        if self.src and self.src not in sources:
1✔
90
            raise InvalidSource(self.src, sources)
1✔
91

92
    @abstractmethod
1✔
93
    def get_heatmap(self, data: pd.DataFrame) -> pd.DataFrame:
1✔
94
        pass
×
95

96
    @abstractmethod
1✔
97
    def get_dataframe(self) -> pd.DataFrame:
1✔
98
        pass
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc