Skip to content

Commit

Permalink
chore: ruff everything (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
flekschas authored Dec 8, 2024
1 parent 254e527 commit a1b3ba4
Show file tree
Hide file tree
Showing 26 changed files with 1,308 additions and 722 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
version: "0.4.x"
- run: |
uv run ruff check
uv run ruff format --check
Test:
runs-on: ubuntu-latest
Expand Down
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.2
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
4 changes: 2 additions & 2 deletions jscatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from importlib_metadata import PackageNotFoundError, version # type: ignore

try:
__version__ = version("jupyter-scatter")
__version__ = version('jupyter-scatter')
except PackageNotFoundError:
__version__ = "uninstalled"
__version__ = 'uninstalled'

from .jscatter import Scatter, plot
from .annotations import Line, HLine, VLine, Rect
Expand Down
58 changes: 31 additions & 27 deletions jscatter/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,47 @@
from jscatter import __version__

DEV = False
IS_WINDOWS = sys.platform.startswith("win")
IS_WINDOWS = sys.platform.startswith('win')


def download_demo_notebook() -> Path:
notebook = pooch.retrieve(
url=f"https://github.com/flekschas/jupyter-scatter/raw/refs/tags/v{__version__}/notebooks/demo.ipynb",
path=pooch.os_cache("jupyter-scatter"),
fname="demo.ipynb",
url=f'https://github.com/flekschas/jupyter-scatter/raw/refs/tags/v{__version__}/notebooks/demo.ipynb',
path=pooch.os_cache('jupyter-scatter'),
fname='demo.ipynb',
known_hash=None,
)
return Path(notebook)


def check_uv_available():
if shutil.which("uv") is None:
if shutil.which('uv') is None:
print("Error: 'uv' command not found.", file=sys.stderr)
print("Please install 'uv' to run `jscatter demo` entrypoint.", file=sys.stderr)
print(
"For more information, visit: https://github.com/astral-sh/uv",
'For more information, visit: https://github.com/astral-sh/uv',
file=sys.stderr,
)
sys.exit(1)


def run_notebook(notebook_path: Path):
check_uv_available()

command = [
"uv",
"tool",
"run",
"--python",
"3.12",
"--from",
"jupyter-core",
"--with",
"jupyterlab",
"--with",
"." if DEV else f"jupyter-scatter=={__version__}",
"jupyter",
"lab",
'uv',
'tool',
'run',
'--python',
'3.12',
'--from',
'jupyter-core',
'--with',
'jupyterlab',
'--with',
'.' if DEV else f'jupyter-scatter=={__version__}',
'jupyter',
'lab',
str(notebook_path),
]

Expand All @@ -56,24 +59,25 @@ def run_notebook(notebook_path: Path):
completed_process = subprocess.run(command)
sys.exit(completed_process.returncode)
except subprocess.CalledProcessError as e:
print(f"Error executing {command[0]}: {e}", file=sys.stderr)
print(f'Error executing {command[0]}: {e}', file=sys.stderr)
sys.exit(1)
else:
try:
os.execvp(command[0], command)
except OSError as e:
print(f"Error executing {command[0]}: {e}", file=sys.stderr)
print(f'Error executing {command[0]}: {e}', file=sys.stderr)
sys.exit(1)


def main():
parser = argparse.ArgumentParser(prog="jupyter-scatter")
subparsers = parser.add_subparsers(dest="command", help="Available commands")
subparsers.add_parser("demo", help=f"Run the demo notebook in JupyterLab")
parser = argparse.ArgumentParser(prog='jupyter-scatter')
subparsers = parser.add_subparsers(dest='command', help='Available commands')
subparsers.add_parser('demo', help=f'Run the demo notebook in JupyterLab')
args = parser.parse_args()

if args.command == "demo":
if args.command == 'demo':
if DEV:
notebook_path = Path(__file__).parent.parent / "notebooks" / "demo.ipynb"
notebook_path = Path(__file__).parent.parent / 'notebooks' / 'demo.ipynb'
else:
notebook_path = download_demo_notebook()

Expand All @@ -82,5 +86,5 @@ def main():
parser.print_help()


if __name__ == "__main__":
if __name__ == '__main__':
main()
16 changes: 12 additions & 4 deletions jscatter/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
DEFAULT_LINE_COLOR = '#000000'
DEFAULT_LINE_WIDTH = 1


@dataclass
class HLine():
class HLine:
"""
A horizontal line annotation.
Expand Down Expand Up @@ -41,6 +42,7 @@ class HLine():
>>> HLine(0)
HLine(y=0, x_start=None, x_end=None, line_color=(0.0, 0.0, 0.0, 1.0), line_width=1)
"""

y: float
x_start: Optional[float] = DEFAULT_1D_LINE_START
x_end: Optional[float] = DEFAULT_1D_LINE_END
Expand All @@ -50,8 +52,9 @@ class HLine():
def __post_init__(self):
self.line_color = to_rgba(self.line_color)


@dataclass
class VLine():
class VLine:
"""
A vertical line annotation.
Expand Down Expand Up @@ -80,6 +83,7 @@ class VLine():
>>> VLine(0)
VLine(x=0, y_start=None, y_end=None, line_color=(0.0, 0.0, 0.0, 1.0), line_width=1)
"""

x: float
y_start: Optional[float] = DEFAULT_1D_LINE_START
y_end: Optional[float] = DEFAULT_1D_LINE_END
Expand All @@ -89,8 +93,9 @@ class VLine():
def __post_init__(self):
self.line_color = to_rgba(self.line_color)


@dataclass
class Rect():
class Rect:
"""
A rectangle annotation.
Expand Down Expand Up @@ -122,6 +127,7 @@ class Rect():
>>> Rect(0)
Rect(x_start=-1, x_end=1, y_start=-1, y_end=1, line_color=(0.0, 0.0, 0.0, 1.0), line_width=1)
"""

x_start: float
x_end: float
y_start: float
Expand All @@ -132,8 +138,9 @@ class Rect():
def __post_init__(self):
self.line_color = to_rgba(self.line_color)


@dataclass
class Line():
class Line:
"""
A line annotation.
Expand All @@ -156,6 +163,7 @@ class Line():
>>> Line([(-1, -1), (0, 0), (1, 1)])
Line(vertices=[(-1, -1), (0, 0), (1, 1)], line_color=(0.0, 0.0, 0.0, 1.0), line_width=1)
"""

vertices: List[Tuple[float]]
line_color: Color = DEFAULT_LINE_COLOR
line_width: int = DEFAULT_LINE_WIDTH
Expand Down
9 changes: 9 additions & 0 deletions jscatter/annotations_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Rect as ARect,
)


class Line(TraitType):
info_text = 'line annotation'

Expand All @@ -21,6 +22,7 @@ def validate(self, obj: Any, value: Any):
return value
self.error(obj, value)


class HLine(TraitType):
info_text = 'horizontal line annotation'

Expand All @@ -29,6 +31,7 @@ def validate(self, obj: Any, value: Any):
return value
self.error(obj, value)


class VLine(TraitType):
info_text = 'vertical line annotation'

Expand All @@ -37,6 +40,7 @@ def validate(self, obj: Any, value: Any):
return value
self.error(obj, value)


class Rect(TraitType):
info_text = 'rectangle annotation'

Expand All @@ -45,15 +49,18 @@ def validate(self, obj: Any, value: Any):
return value
self.error(obj, value)


def to_json(value, *args, **kwargs):
d = None if value is None else asdict(value)
return json.dumps(d, allow_nan=False)


def annotations_to_json(value, *args, **kwargs):
if value is None:
return None
return [to_json(v) for v in value]


def from_json(value, *args, **kwargs):
d = json.loads(value)

Expand All @@ -71,6 +78,7 @@ def from_json(value, *args, **kwargs):

return None


def annotations_from_json(value):
value = json.loads(value)

Expand All @@ -79,6 +87,7 @@ def annotations_from_json(value):

return [from_json(v) for v in value]


serialization = dict(
to_json=annotations_to_json,
from_json=annotations_from_json,
Expand Down
20 changes: 10 additions & 10 deletions jscatter/color_maps.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from matplotlib.colors import to_rgba

okabe_ito = [
to_rgba('#56B4E9'), # sky blue
to_rgba('#E69F00'), # orange
to_rgba('#009E73'), # blueish green
to_rgba('#F0E442'), # yellow
to_rgba('#0072B2'), # blue
to_rgba('#D55E00'), # vermilion
to_rgba('#CC79A7'), # reddish purple
to_rgba('#000000'), # black
to_rgba('#56B4E9'), # sky blue
to_rgba('#E69F00'), # orange
to_rgba('#009E73'), # blueish green
to_rgba('#F0E442'), # yellow
to_rgba('#0072B2'), # blue
to_rgba('#D55E00'), # vermilion
to_rgba('#CC79A7'), # reddish purple
to_rgba('#000000'), # black
]

glasbey_light = [
Expand Down Expand Up @@ -267,7 +267,7 @@
to_rgba('#247c2a'),
to_rgba('#826723'),
to_rgba('#bfbc4d'),
to_rgba('#ddd3a5')
to_rgba('#ddd3a5'),
]

glasbey_dark = [
Expand Down Expand Up @@ -526,5 +526,5 @@
to_rgba('#a7423b'),
to_rgba('#006e70'),
to_rgba('#97833d'),
to_rgba('#dbafc8')
to_rgba('#dbafc8'),
]
Loading

0 comments on commit a1b3ba4

Please sign in to comment.