Skip to content

Commit 31d03a7

Browse files
committed
flexible dtype results class
1 parent 6dc04b2 commit 31d03a7

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

fooof/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Data sub-module for FOOOF."""
22

3-
from .data import FOOOFSettings, FOOOFMetaData, FOOOFResults, SimParams
3+
from .data import FOOOFSettings, FOOOFMetaData, FOOOFResults, SimParams, FitParams

fooof/data/data.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
- this means no additional attributes can be defined (which is more memory efficient)
99
"""
1010

11-
from collections import namedtuple
11+
from collections import namedtuple, OrderedDict
12+
13+
import numpy as np
14+
15+
from fooof.core.modutils import safe_import
16+
17+
pd = safe_import('pandas')
1218

1319
###################################################################################################
1420
###################################################################################################
@@ -98,3 +104,33 @@ class SimParams(namedtuple('SimParams', ['aperiodic_params', 'periodic_params',
98104
This object is a data object, based on a NamedTuple, with immutable data attributes.
99105
"""
100106
__slots__ = ()
107+
108+
109+
class FitParams(np.ndarray):
110+
111+
def __new__(cls, results, labels):
112+
113+
return np.asarray(results).view(cls)
114+
115+
def __init__(self, results, labels):
116+
117+
self.results = results
118+
self.labels = labels
119+
120+
def to_dict(self):
121+
122+
results = OrderedDict((k, v) for k, v in \
123+
zip(self.labels, self.results.transpose()))
124+
125+
return results
126+
127+
def to_pandas(self):
128+
129+
if pd is None:
130+
raise ValueError("Pandas is not installed.")
131+
132+
results = self.to_dict()
133+
134+
results = pd.DataFrame(results)
135+
136+
return results

0 commit comments

Comments
 (0)