-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a CLI, add some light unit testing, and change config from Python to Yaml/Json #2
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e640a12
refactor: rename module from chartreview to chart_review
mikix 9429b8b
feat: read from a yaml/json config file and add CLI
mikix eb33d5a
ci: add initial test case
mikix 7ae1ce8
refactor: move accuracy logic to separate file
mikix 82ea4d6
build: bump minimum python to 3.10
mikix 2c816ff
ci: and bump the CI python too, whoops
mikix 06c56d4
Add a config file test suite
mikix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: CI | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
|
||
# The goal here is to cancel older workflows when a PR is updated (because it's pointless work) | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
unittest: | ||
name: unit tests | ||
runs-on: ubuntu-22.04 | ||
strategy: | ||
matrix: | ||
# don't go crazy with the Python versions as they eat up CI minutes | ||
python-version: ["3.10"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install .[tests] | ||
|
||
- name: Test with pytest | ||
run: | | ||
python -m pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.idea/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Run chart-review from the command-line""" | ||
|
||
import argparse | ||
import sys | ||
|
||
from chart_review import cohort | ||
from chart_review.commands.accuracy import accuracy | ||
|
||
|
||
############################################################################### | ||
# | ||
# CLI Helpers | ||
# | ||
############################################################################### | ||
|
||
def add_project_args(parser: argparse.ArgumentParser) -> None: | ||
parser.add_argument( | ||
"--project-dir", | ||
default=".", | ||
help="Directory holding project files, like config.yaml and labelstudio-export.json (default: current dir)", | ||
) | ||
|
||
|
||
def define_parser() -> argparse.ArgumentParser: | ||
"""Fills out an argument parser with all the CLI options.""" | ||
parser = argparse.ArgumentParser() | ||
subparsers = parser.add_subparsers(required=True) | ||
|
||
add_accuracy_subparser(subparsers) | ||
|
||
return parser | ||
|
||
|
||
############################################################################### | ||
# | ||
# Accuracy | ||
# | ||
############################################################################### | ||
|
||
def add_accuracy_subparser(subparsers) -> None: | ||
parser = subparsers.add_parser("accuracy") | ||
add_project_args(parser) | ||
parser.add_argument("one") | ||
parser.add_argument("two") | ||
parser.add_argument("base") | ||
parser.set_defaults(func=run_accuracy) | ||
|
||
|
||
def run_accuracy(args: argparse.Namespace) -> None: | ||
reader = cohort.CohortReader(args.project_dir) | ||
accuracy(reader, args.one, args.two, args.base) | ||
|
||
|
||
############################################################################### | ||
# | ||
# Main CLI entrypoints | ||
# | ||
############################################################################### | ||
|
||
def main_cli(argv: list[str] = None) -> None: | ||
"""Main entrypoint that wraps all the core program logic""" | ||
try: | ||
parser = define_parser() | ||
args = parser.parse_args(argv) | ||
args.func(args) | ||
except Exception as exc: | ||
sys.exit(str(exc)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main_cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Methods for high-level accuracy calculations.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is basically a generic version of the calculation in |
||
|
||
import os | ||
|
||
from chart_review import agree, cohort, common | ||
|
||
|
||
def accuracy(reader: cohort.CohortReader, first_ann: str, second_ann: str, base_ann: str) -> None: | ||
""" | ||
High-level accuracy calculation between three reviewers. | ||
|
||
The results will be written to the project directory. | ||
|
||
:param reader: the cohort configuration | ||
:param first_ann: the first annotator to compare | ||
:param second_ann: the second annotator to compare | ||
:param base_ann: the base annotator to compare the others against | ||
""" | ||
# Grab ranges | ||
first_range = reader.config.note_ranges[first_ann] | ||
second_range = reader.config.note_ranges[second_ann] | ||
|
||
# All labels first | ||
first_matrix = reader.confusion_matrix(first_ann, base_ann, first_range) | ||
second_matrix = reader.confusion_matrix(second_ann, base_ann, second_range) | ||
whole_matrix = agree.append_matrix(first_matrix, second_matrix) | ||
table = agree.score_matrix(whole_matrix) | ||
|
||
# Now do each labels separately | ||
for label in reader.class_labels: | ||
first_matrix = reader.confusion_matrix(first_ann, base_ann, first_range, label) | ||
second_matrix = reader.confusion_matrix(second_ann, base_ann, second_range, label) | ||
whole_matrix = agree.append_matrix(first_matrix, second_matrix) | ||
table[label] = agree.score_matrix(whole_matrix) | ||
|
||
# And write out the results | ||
output_stem = os.path.join(reader.project_dir, f"accuracy-{first_ann}-{second_ann}-{base_ann}") | ||
common.write_json(f"{output_stem}.json", table) | ||
print(f"Wrote {output_stem}.json") | ||
common.write_text(f"{output_stem}.csv", agree.csv_table(table, reader.class_labels)) | ||
print(f"Wrote {output_stem}.csv") |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your idea and I strongly prefer
config.json
over yamlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah fair - but note:
config.yaml
andconfig.json
-- it will read either oneSo the way I made this PR, either yaml or json works - whichever the researcher in question is more comfy with.
How do you feel about that? (Or do you feel like standardizing on a specific syntax is worth disallowing yaml?)