Skip to content

Commit

Permalink
added support notebook in ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Borg93 committed Apr 8, 2024
1 parent dcbfeb8 commit d316945
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
44 changes: 25 additions & 19 deletions docs/notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"outputs": [],
"source": [
"import random\n",
"\n",
"\n",
"random.seed(123)"
]
},
Expand All @@ -16,6 +18,7 @@
"metadata": {},
"source": [
"## Models / inferencers\n",
"\n",
"Models & inferencers accept lists of images, and return lists of results (either segmentation or recognition results)\n",
"\n",
"I have made a dummy `SegmentationModel` and `RecognitionModel` in `models.py`. These do the same thing as the current inferencers.\n",
Expand All @@ -34,7 +37,6 @@
" labels: np.ndarray\n",
"```\n",
"\n",
"\n",
"(It would be nice to wrap all models in a \"batching\" function, which divides an input list into chunks if it is too long) -> This is a card in DevOps\n"
]
},
Expand All @@ -45,7 +47,7 @@
"source": [
"## Using the Volume class\n",
"\n",
"To load images, create a `Volume`. The name of this class is not set in stone... It represents what Catrin called a \"batch\", a divison of an archive volume, but I don't want to use \"batch\" because of potential confusion with a model's batch (the number of inputs it operates on simultaneously). \n"
"To load images, create a `Volume`. The name of this class is not set in stone... It represents what Catrin called a \"batch\", a divison of an archive volume, but I don't want to use \"batch\" because of potential confusion with a model's batch (the number of inputs it operates on simultaneously).\n"
]
},
{
Expand All @@ -56,7 +58,8 @@
"source": [
"from htrflow_core.volume import Volume\n",
"\n",
"images = ['../assets/demo_image.jpg'] * 5 \n",
"\n",
"images = [\"../assets/demo_image.jpg\"] * 5\n",
"\n",
"volume = Volume(images)"
]
Expand All @@ -66,7 +69,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Volume` instance holds a tree. We see the root `node` and its five children, each representing one input image:"
"The `Volume` instance holds a tree. We see the root `node` and its five children, each representing one input image:\n"
]
},
{
Expand Down Expand Up @@ -96,7 +99,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The images are available through `volume.images()`. We pass them through a segmentation model:"
"The images are available through `volume.images()`. We pass them through a segmentation model:\n"
]
},
{
Expand Down Expand Up @@ -180,6 +183,7 @@
"source": [
"from htrflow_core.models.dummy_models import SegmentationModel\n",
"\n",
"\n",
"model = SegmentationModel()\n",
"results = model(volume.images())\n",
"print(results[0])"
Expand All @@ -190,7 +194,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The results are a list of `SegmentationResult`. To apply the results to the input images, we pass them back to the volume with its `update` method. It returns the new regions as a list of images."
"The results are a list of `SegmentationResult`. To apply the results to the input images, we pass them back to the volume with its `update` method. It returns the new regions as a list of images.\n"
]
},
{
Expand All @@ -207,7 +211,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The volume tree has now grown:"
"The volume tree has now grown:\n"
]
},
{
Expand Down Expand Up @@ -252,7 +256,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The new regions can be passed through a segmentation model (such as a line model) again. The `update` method always updates the leaves of the tree."
"The new regions can be passed through a segmentation model (such as a line model) again. The `update` method always updates the leaves of the tree.\n"
]
},
{
Expand Down Expand Up @@ -345,7 +349,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"When the segmentation is done, the segments can be passed to a text recognition model. The results are passed to the workbench in the same manner as before:"
"When the segmentation is done, the segments can be passed to a text recognition model. The results are passed to the workbench in the same manner as before:\n"
]
},
{
Expand Down Expand Up @@ -430,6 +434,7 @@
"source": [
"from htrflow_core.models.dummy_models import RecognitionModel\n",
"\n",
"\n",
"recognition_model = RecognitionModel()\n",
"results = recognition_model(volume.segments())\n",
"volume.update(results)\n",
Expand All @@ -442,7 +447,8 @@
"metadata": {},
"source": [
"## Accessing nodes\n",
"Specific nodes are accessed by tuple indexing. Here we extract the first line of the first region of the first image:"
"\n",
"Specific nodes are accessed by tuple indexing. Here we extract the first line of the first region of the first image:\n"
]
},
{
Expand Down Expand Up @@ -479,12 +485,12 @@
"```python\n",
"\n",
"class BaseImageNode:\n",
" \n",
"\n",
" @property\n",
" def image(self):\n",
" x1, x2, y1, y2 = self.box\n",
" return self.parent.image[y1:y2, x1:x2]\n",
" \n",
"\n",
" ...\n",
"```\n"
]
Expand Down Expand Up @@ -564,7 +570,7 @@
"source": [
"## Coordinates\n",
"\n",
"All nodes have a `coordinate` attribute. This is the location of the node's top-left corner *relative to the original image*. The base image node's coordinate is thus (0,0):"
"All nodes have a `coordinate` attribute. This is the location of the node's top-left corner _relative to the original image_. The base image node's coordinate is thus (0,0):\n"
]
},
{
Expand Down Expand Up @@ -593,7 +599,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"For first-level regions `coordinate` is the same as the corner of the segment bounding box."
"For first-level regions `coordinate` is the same as the corner of the segment bounding box.\n"
]
},
{
Expand All @@ -614,16 +620,16 @@
}
],
"source": [
"print('Coordinate:', volume[0, 0].coordinate)\n",
"print('Bounding box:', volume[0, 0].data['segment'].box, '(x1, x2, y1, y2)')"
"print(\"Coordinate:\", volume[0, 0].coordinate)\n",
"print(\"Bounding box:\", volume[0, 0].data[\"segment\"].box, \"(x1, x2, y1, y2)\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"But for nested regions the two differ, because `coordinate` is relative to the original image, while the segment bounding box is relative to the parent region."
"But for nested regions the two differ, because `coordinate` is relative to the original image, while the segment bounding box is relative to the parent region.\n"
]
},
{
Expand All @@ -644,8 +650,8 @@
}
],
"source": [
"print('Global coordinate:', volume[0, 0, 0].coordinate)\n",
"print('Local bounding box:', volume[0, 0, 0].data['segment'].box)"
"print(\"Global coordinate:\", volume[0, 0, 0].coordinate)\n",
"print(\"Local bounding box:\", volume[0, 0, 0].data[\"segment\"].box)"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ exclude_lines = [
[tool.ruff]
line-length = 119
target-version = "py310"
extend-include = ["*.ipynb"]

[tool.ruff.lint]
ignore = ["C901", "E741", "W605"]
Expand Down
2 changes: 1 addition & 1 deletion src/htrflow_core/serialization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from collections import defaultdict
import datetime
import json
import logging
import os
from collections import defaultdict
from typing import TYPE_CHECKING, Iterable, Sequence, Union

import xmlschema
Expand Down

0 comments on commit d316945

Please sign in to comment.