Skip to content

Commit

Permalink
Use enum & argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
Lai-YT committed Dec 19, 2023
1 parent 9f1159e commit 43a1b49
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions concentration/fuzzy/grader.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,30 @@ def _normalize_grade(raw_grade: float) -> float:
return 0.1382 * raw_grade - 0.1986


if __name__ == "__main__":
import sys

import matplotlib.pyplot as plt
import numpy as np

if len(sys.argv) != 2 or sys.argv[1] not in (
"membership",
"distribution",
"distribution-slice",
"input",
):
raise RuntimeError(
f"\n\t usage: python {__file__} membership | distribution | distribution-slice | input"
)
# The following code is not necessary for the grading system.
# It is included for interactive visualization purposes.

import argparse
from enum import Enum, auto, unique

import matplotlib.pyplot as plt


@unique
class _InteractiveMode(Enum):
MEMBERSHIP = auto()
DISTRIBUTION = auto()
DISTRIBUTION_SLICE = auto()
INPUT = auto()


def interact(mode: _InteractiveMode) -> None:
fuzzy_grader = FuzzyGrader()

if sys.argv[1] == "membership":
if mode is _InteractiveMode.MEMBERSHIP:
fuzzy_grader.view_membership_func()
input("(press any key to exit)")
elif sys.argv[1] == "distribution":
elif mode is _InteractiveMode.DISTRIBUTION:
# show distribution over all possible values, which makes the plot 4D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
Expand All @@ -191,7 +193,7 @@ def _normalize_grade(raw_grade: float) -> float:
img = ax.scatter(x, y, z, c=c, cmap=plt.hot())
fig.colorbar(img)
plt.show()
elif sys.argv[1] == "distribution-slice":
elif mode is _InteractiveMode.DISTRIBUTION_SLICE:
# show distribution under specific blink rate, which makes the plot 3D
fixed_blink_rate = int(input("fixed blink rate: "))

Expand All @@ -213,7 +215,7 @@ def _normalize_grade(raw_grade: float) -> float:

img = ax.scatter(x, y, z)
plt.show()
elif sys.argv[1] == "input":
elif mode is _InteractiveMode.INPUT:
# compute for a single case of grade
blink_rate = float(input("blink rate: "))
body_concent = float(input("body concent: "))
Expand All @@ -229,3 +231,25 @@ def _normalize_grade(raw_grade: float) -> float:

fuzzy_grader.view_grading_result()
input("(press any key to exit)")
else:
raise NotImplementedError(f"unknown mode {mode}")


def main() -> None:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"mode",
metavar="MODE",
choices=[mode for mode in _InteractiveMode],
type=lambda x: _InteractiveMode[x.upper()],
help="interactive modes: membership, distribution, distribution_slice, input",
)
args: argparse.Namespace = parser.parse_args()

interact(args.mode)


if __name__ == "__main__":
main()

0 comments on commit 43a1b49

Please sign in to comment.