Skip to content

Commit

Permalink
Merge pull request #56 from TomSiegl/equality-selector-from-str-bool
Browse files Browse the repository at this point in the history
Support boolean attribute values in EqualitySelector.from_str()
  • Loading branch information
mgbckr authored Jul 19, 2024
2 parents 1d22844 + d1c8330 commit 7ff6b62
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
outputs:
wheel-distribution: ${{ steps.wheel-distribution.outputs.path }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with: {fetch-depth: 0} # deep clone for setuptools-scm
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
id: setup-python
with: {python-version: "3.11"}
- name: Run static analysis and format checkers
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Store the distribution files for use in other stages
# `tests` and `publish` will use the same pre-built distributions,
# so we make sure to release the exact same package that was tested
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: python-distribution-files
path: dist/
Expand All @@ -60,21 +60,21 @@ jobs:
strategy:
matrix:
python:
- "3.7" # oldest Python supported by PSF
- "3.8" # oldest Python supported by PSF
- "3.11" # newest Python that is stable
platform:
- ubuntu-latest
- macos-latest
- windows-latest
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
id: setup-python
with:
python-version: ${{ matrix.python }}
- name: Retrieve pre-built distribution files
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with: {name: python-distribution-files, path: dist/}
- name: Run tests
run: >-
Expand Down Expand Up @@ -108,11 +108,11 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: {python-version: "3.11"}
- name: Retrieve pre-built distribution files
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with: {name: python-distribution-files, path: dist/}
# - name: Publish Package (TEST)
# env:
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ package_dir =
=src

# Require a min/specific Python version (comma-separated conditions)
python_requires = >=3.6
python_requires = >=3.8

# Add here dependencies of your project (line-separated), e.g. requests>=2.2,<3.0.
# Version specifiers like >=2.2,<3.0 avoid problems due to API changes in
Expand All @@ -64,7 +64,7 @@ install_requires =
importlib-metadata; python_version<"3.8"
pandas>=0.24.0
scipy
numpy
numpy<2.0.0
matplotlib


Expand Down
5 changes: 4 additions & 1 deletion src/pysubgroup/subgroup_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ def from_str(s):
try:
attribute_value = float(attribute_value)
except ValueError:
pass
try:
attribute_value = ps.str_to_bool(attribute_value)
except ValueError:
pass
return ps.EqualitySelector(attribute_name, attribute_value)


Expand Down
10 changes: 10 additions & 0 deletions src/pysubgroup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
from .algorithms import SubgroupDiscoveryTask


def str_to_bool(s):
s = s.lower()
if s in ["y", "yes", "t", "true", "on", "1"]:
return True
elif s in ["n", "no", "f", "false", "off", "0"]:
return False

raise ValueError(f"'{s}' is not a valid string representation of a boolean value")


def minimum_required_quality(result, task):
if len(result) < task.result_set_size:
return task.min_quality
Expand Down
13 changes: 9 additions & 4 deletions tests/test_subgroup_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ def test_remove_target_attributes(self):
self.assertEqual(selectors[1:], bin_sel)

def test_EqualitySelector_from_str(self):
selector1 = ps.EqualitySelector.from_str("A=='hello'")
selector2 = ps.EqualitySelector("A", "hello")
self.assertEqual(selector1, selector2)
self.assertIs(selector1, selector2)
def test_str(s, attribute_name, attribute_value):
selector = ps.EqualitySelector.from_str(s)
_selector = ps.EqualitySelector(attribute_name, attribute_value)
self.assertEqual(selector, _selector)
self.assertIs(selector, _selector)

test_str("A=='hello'", "A", "hello")
test_str("A==True", "A", True)
test_str("A==False", "A", False)

def test_IntervalSelector_from_str(self):
def test_str(s, attribute_name, lb, ub):
Expand Down

0 comments on commit 7ff6b62

Please sign in to comment.