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

adaptive-machine-learning / CapyMOA / 21811412795

09 Feb 2026 03:33AM UTC coverage: 63.304%. First build
21811412795

Pull #330

github

web-flow
Merge 2054b9323 into c3f021633
Pull Request #330: WIP code coverage

1 of 2 new or added lines in 1 file covered. (50.0%)

5196 of 8208 relevant lines covered (63.3%)

1.27 hits per line

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

56.67
/src/capymoa/misc.py
1
from jpype.pickle import JPickler, JUnpickler
2✔
2
from jpype import JException
2✔
3
from typing import BinaryIO, TextIO
2✔
4
from pathlib import Path
2✔
5
from io import RawIOBase, BufferedIOBase
2✔
6
from capymoa.stream._stream import Stream
2✔
7
import tqdm
2✔
8

9

10
def save_model(model: object, file: BinaryIO) -> None:
2✔
11
    """Save a model to a jpype pickle file.
12

13
    >>> from capymoa.classifier import AdaptiveRandomForestClassifier
14
    >>> from capymoa.datasets import ElectricityTiny
15
    >>> from tempfile import TemporaryFile
16
    >>> stream = ElectricityTiny()
17
    >>> learner = AdaptiveRandomForestClassifier(schema=stream.get_schema())
18
    >>> with TemporaryFile() as fd:
19
    ...     save_model(learner, fd)
20

21
    See https://jpype.readthedocs.io/en/latest/api.html#jpype-pickle-module for
22
    more information.
23

24
    :param model: A python object optionally containing Java objects.
25
    :param file: The file-like object to save the model to.
26
    """
27
    if not file.writable():
2✔
28
        raise ValueError("File must be writable.")
×
29
    JPickler(file).dump(model)
2✔
30

31

32
def load_model(file: BinaryIO) -> object:
2✔
33
    """Load a model from a jpype pickle file.
34

35
    See also: :func:`save_model`.
36

37
    :param file: The file-like object to load the model from.
38
    :return: The loaded model.
39
    """
40
    if not isinstance(file, (RawIOBase, BufferedIOBase)):
2✔
41
        raise ValueError("File must be opened in binary mode.")
×
42
    if not file.readable():
2✔
43
        raise ValueError("File must be readable.")
×
44
    try:
2✔
45
        return JUnpickler(file).load()
2✔
46
    except JException as e:
×
47
        raise RuntimeError(
×
48
            "Exception loading model.\n"
49
            "If you are trying to load a model saved with a version of CapyMOA < 0.8.2, "
50
            "use `legacy_load_model` and `save_model` to reformat the model."
51
        ) from e
52

53

54
def save_stream_arff(file: TextIO | Path | str, stream: Stream) -> None:
2✔
55
    """Save a CapyMOA stream to an ARFF file.
56

57
    Usage for classification datastream:
58

59
    >>> from capymoa.stream import NumpyStream
60
    >>> import numpy as np
61
    >>> from io import StringIO
62
    >>>
63
    >>> stream = NumpyStream(
64
    ...     X=np.array([[0, 1], [1, 0], [0, 0]]),
65
    ...     y=np.array([0, 1, 0]),
66
    ...     dataset_name="SimpleDataset",
67
    ...     target_type="categorical"
68
    ... )
69
    >>> fd = StringIO() # You can 'open' a real file instead
70
    >>> save_stream_arff(fd, stream)
71
    >>> print(fd.getvalue())
72
    @relation SimpleDataset
73
    <BLANKLINE>
74
    @attribute 0 numeric
75
    @attribute 1 numeric
76
    @attribute target {0,1}
77
    <BLANKLINE>
78
    @data
79
    0.0,1.0,0,
80
    1.0,0.0,1,
81
    0.0,0.0,0,
82
    <BLANKLINE>
83

84
    Usage for regression datastream:
85

86
    >>> stream = NumpyStream(
87
    ...     X=np.array([[0, 1], [1, 0], [0, 0]]),
88
    ...     y=np.array([0, 1, 0]),
89
    ...     dataset_name="SimpleDataset",
90
    ...     target_type="numeric"
91
    ... )
92
    >>> fd = StringIO() # You can 'open' a real file instead
93
    >>> save_stream_arff(fd, stream)
94
    >>> print(fd.getvalue())
95
    @relation SimpleDataset
96
    <BLANKLINE>
97
    @attribute 0 numeric
98
    @attribute 1 numeric
99
    @attribute target numeric
100
    <BLANKLINE>
101
    @data
102
    0.0,1.0,0.0,
103
    1.0,0.0,1.0,
104
    0.0,0.0,0.0,
105
    <BLANKLINE>
106

107

108
    :param file: A file-like object or path to write the ARFF to.
109
    :param stream: The stream to save.
110
    """
111
    if isinstance(file, (Path, str)):
×
112
        with open(file, "w") as fd:
×
113
            return save_stream_arff(fd, stream)
×
114
    else:
115
        moa_header = stream.get_schema().get_moa_header()
×
116
        file.write(str(moa_header))
×
117
        for instance in tqdm.tqdm(stream, desc="Saving to ARFF"):
×
118
            file.write(str(instance.java_instance.toString()) + "\n")
×
119

120

121
def _TODO_REMOVE_ME_I_AM_A_TEST():
2✔
NEW
122
    print("This is a test function that should be removed.")
×
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