Skip to content

Commit

Permalink
start on curve fit plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoic committed Jun 28, 2024
1 parent d2f2336 commit 58f4a16
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
35 changes: 34 additions & 1 deletion countess/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
import math
import os.path
import re
from typing import Any, Iterable, Mapping, Optional, Type, Union
from typing import Any, Dict, Iterable, List, Mapping, Optional, Type, Union

import pandas as pd

from countess.utils.pandas import get_all_columns

PARAM_DIGEST_HASH = "sha256"


def make_prefix_groups(strings: List[str]) -> Dict[str, List[str]]:
groups : Dict[str, List[str]] = {}
for s in strings:
if m := re.match(r'(.*?)_+([^_]+)$', s):
groups.setdefault(m.group(1), []).append(s)
return { k: v for k, v in groups.items() if len(v) > 1 }


class BaseParam:
"""Represents the parameters which can be set on a plugin."""

Expand Down Expand Up @@ -387,6 +397,29 @@ def get_column(self, df):
return _dataframe_get_column(df, self.value)


class ColumnGroupChoiceParam(ChoiceParam):

def set_column_choices(self, choices):
self.set_choices(make_prefix_groups(choices))

def get_column_names(self, df):
column_names = get_all_columns(df).keys()
return [ n for n in column_names if n.startswith(self.value) ]


class ColumnGroupOrNoneChoiceParam(ColumnGroupChoiceParam):
DEFAULT_VALUE = "— NONE —"

def set_choices(self, choices: Iterable[str]):
super().set_choices([self.DEFAULT_VALUE] + list(choices))

def is_none(self):
return self.value == self.DEFAULT_VALUE

def is_not_none(self):
return self.value != self.DEFAULT_VALUE


class ColumnOrIndexChoiceParam(ColumnChoiceParam):
DEFAULT_VALUE = "— INDEX —"

Expand Down
23 changes: 23 additions & 0 deletions countess/plugins/curve_fit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from scipy.optimize import curve_fit

from countess import VERSION
from countess.core.plugins import PandasTransformDictToDictPlugin
from countess.core.parameters import ChoiceParam, ColumnGroupChoiceParam, ColumnGroupOrNoneChoiceParam
from countess.core.logger import Logger

FUNCTIONS = [ 'foo', 'bar', 'baz' ]

class CurveFitPlugin(PandasTransformDictToDictPlugin):
name = "Curve Fit Plugin"
description = "Fit rows of data to curves"

version = VERSION

parameters = {
"xaxis": ColumnGroupOrNoneChoiceParam("X Axis", None, []),
"yaxis": ColumnGroupChoiceParam("Y Axis", None, []),
"function": ChoiceParam("Function", FUNCTIONS[0], FUNCTIONS),
}

def process_dict(self, data: dict, logger: Logger):
return data
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
'pandas~=2.1.0',
'psutil~=5.9.5',
'rapidfuzz~=2.15.1',
'scipy~=1.14.0',
'tkinterweb~=3.23.5',
'ttkthemes~=3.2.2',
'typing_extensions~=4.8.0',
Expand Down Expand Up @@ -64,6 +65,7 @@ data_table = "countess.plugins.data_table:DataTablePlugin"
correlation = "countess.plugins.correlation:CorrelationPlugin"
hgvs_parser = "countess.plugins.hgvs_parser:HgvsParserPlugin"
column = "countess.plugins.column:ColumnToolPlugin"
curve_fit = "countess.plugins.curve_fit:CurveFitPlugin"

[project.entry-points.gui_scripts]
countess_gui = "countess.gui.main:main"
Expand Down

0 comments on commit 58f4a16

Please sign in to comment.