Skip to content

Commit

Permalink
DEP: use scipy instead of opencv for dilation
Browse files Browse the repository at this point in the history
however, it doesn't look like either dilation method supports
scipy sparse matrices. maybe this is different in newer scipy
vsns / with scipy sparse arrays? check
  • Loading branch information
fedarko committed Aug 29, 2023
1 parent 3821dbe commit c2083da
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,7 @@ jobs:
run: which pip
shell: bash -el {0}

# use a specific opencv-python version to prevent wasting ~30 minutes
# building opencv; from https://stackoverflow.com/a/63669919
- name: Install wotplot and pip dependencies (Python = 3.6)
if: ${{ matrix.python-version == '3.6' }}
run: |
pip install opencv-python==4.3.0.38
pip install -e .[dev]
shell: bash -el {0}

- name: Install wotplot and pip dependencies (Python > 3.6)
- name: Install wotplot and pip dependencies
if: ${{ matrix.python-version != '3.6' }}
run: pip install -e .[dev]
shell: bash -el {0}
Expand Down
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ I'll try to put this on PyPI / conda eventually.

### Installation notes

#### Speeding up the installation of `opencv-python` in Python 3.6 environments

If you're using Python 3.6, installing `opencv-python` through pip can take a
long time (~30 minutes).
See [this Stack Overflow thread](https://stackoverflow.com/q/63669752)
for details.

I've found that
[running `pip install opencv-python==4.3.0.38`](https://stackoverflow.com/a/63669919)
before you install wotplot fixes this problem. See
[our GitHub Actions workflow](https://github.com/fedarko/wotplot/blob/ce702b63bf790c41d02b0493e3a7eebda6fcec70/.github/workflows/main.yml#L62-L63)
for an example of this.

## Performance

This code is not very optimized, so I don't recommend using it for sequences
Expand Down
40 changes: 24 additions & 16 deletions docs/Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
{
"cell_type": "markdown",
"id": "5afadb83",
"id": "e22e84ef",
"metadata": {},
"source": [
"## 1.1. Define a small dataset"
Expand All @@ -41,7 +41,7 @@
},
{
"cell_type": "markdown",
"id": "5fe7b9c1",
"id": "43e7f3eb",
"metadata": {},
"source": [
"## 1.2. Create a dot plot matrix"
Expand All @@ -50,7 +50,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "e1b20e21",
"id": "26a75572",
"metadata": {
"scrolled": true
},
Expand All @@ -61,7 +61,7 @@
},
{
"cell_type": "markdown",
"id": "5ede7540",
"id": "568ef0c1",
"metadata": {},
"source": [
"## 1.3. Inspect the dot plot matrix"
Expand All @@ -70,7 +70,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "1a527b76",
"id": "4856c174",
"metadata": {},
"outputs": [
{
Expand All @@ -91,7 +91,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "8f6c5155",
"id": "fe685aad",
"metadata": {},
"outputs": [
{
Expand All @@ -113,7 +113,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "0fc68844",
"id": "65955531",
"metadata": {},
"outputs": [
{
Expand All @@ -130,7 +130,7 @@
},
{
"cell_type": "markdown",
"id": "6b6915f2",
"id": "f145fcc4",
"metadata": {},
"source": [
"The `DotPlotMatrix` object we just created, `m`, contains some extra information about the way it was created (e.g. the value of `k` we used). But the most interesting part of it is the actual matrix describing the dot plot! This matrix is stored in the `mat` attribute."
Expand All @@ -139,7 +139,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "08e06a06",
"id": "83a8f0f0",
"metadata": {},
"outputs": [
{
Expand All @@ -160,7 +160,7 @@
},
{
"cell_type": "markdown",
"id": "dc3e7eb9",
"id": "b6240658",
"metadata": {},
"source": [
"### 1.3.1. Why is this matrix 16x16? Shouldn't it be 18x18?\n",
Expand All @@ -174,7 +174,7 @@
},
{
"cell_type": "markdown",
"id": "993fe20e",
"id": "656b8e02",
"metadata": {},
"source": [
"### 1.3.2. What's the deal with sparse matrices?\n",
Expand All @@ -187,7 +187,7 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "be896fb7",
"id": "119a4a76",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -222,7 +222,7 @@
},
{
"cell_type": "markdown",
"id": "9d243cb6",
"id": "bbe62927",
"metadata": {},
"source": [
"### 1.3.3. Sidenote about the matrix type\n",
Expand All @@ -231,7 +231,7 @@
},
{
"cell_type": "markdown",
"id": "0aa62098",
"id": "d228686f",
"metadata": {},
"source": [
"## 1.4. Visualize the dot plot matrix"
Expand Down Expand Up @@ -274,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "40a1ea34",
"metadata": {},
"outputs": [
Expand All @@ -284,7 +284,7 @@
"Text(0.5, 1.0, 'Top-to-bottom')"
]
},
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
Expand All @@ -311,6 +311,14 @@
"axLeft.set_title('Bottom-to-top', fontsize=18)\n",
"axRight.set_title('Top-to-bottom', fontsize=18)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77cdb212",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"numpy",
"scipy",
"matplotlib",
"opencv-python",
],
# you know tbh i'm not sure if you NEED to have already installed scipy
# before installing wotplot. i remember that trying to pip install numpy
Expand Down
11 changes: 7 additions & 4 deletions wotplot/_viz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cv2
import scipy
from scipy.ndimage import binary_dilation
import numpy as np
from math import ceil
from matplotlib import pyplot
Expand Down Expand Up @@ -148,9 +149,11 @@ def viz_binary(m, num_dilation_iterations="auto", title=None, ax=None,
num_dilation_iterations = 4 + ceil((maxlen - 2500) / 2500)

if num_dilation_iterations > 0:
matrix_to_show = cv2.dilate(
m.mat, DILATION_KERNEL, iterations=num_dilation_iterations
)
# scipy's dilation function doesn't seem to support sparse matrices...
# TODO TODO
matrix_to_show = scipy.sparse.coo_matrix(binary_dilation(
m.mat.toarray(), iterations=num_dilation_iterations
).astype(m.mat.dtype))
else:
matrix_to_show = m.mat

Expand Down

0 comments on commit c2083da

Please sign in to comment.