Skip to content
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 medoid method for calculation of cluster center #6

Merged
merged 11 commits into from
Jul 14, 2023
72 changes: 36 additions & 36 deletions .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@ jobs:
#python-version: ['3.8', '3.9', '3.10']
python-version: ['3.10']

defaults:
run:
shell: bash -el {0}
steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

# these libraries enable testing on Qt on linux
- uses: tlambert03/setup-qt-libs@v1

# strategy borrowed from vispy for installing opengl libs on windows
- name: Install Windows OpenGL
if: runner.os == 'Windows'
run: |
git clone --depth 1 https://github.com/pyvista/gl-ci-helpers.git
powershell gl-ci-helpers/appveyor/install_opengl.ps1

# note: if you need dependencies from conda, considering using
# setup-miniconda: https://github.com/conda-incubator/setup-miniconda
# and
# tox-conda: https://github.com/tox-dev/tox-conda
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install setuptools tox tox-gh-actions

# this runs the platform-specific tests declared in tox.ini
- name: Test with tox
uses: GabrielBB/xvfb-action@v1
with:
run: python -m tox
env:
PLATFORM: ${{ matrix.platform }}

- name: Coverage
uses: codecov/codecov-action@v2
- uses: actions/checkout@v3
- name: Set up conda ${{ matrix.python-version }}
uses: conda-incubator/setup-miniconda@v2
with:
mamba-version: "*"
activate-environment: napari-tomotwin
channel-priority: true
python-version: ${{ matrix.python-version }}
channels: conda-forge, defaults
environment-file: conda_env.yml
- run: conda --version
- run: conda init bash
- run: |
which python
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools setuptools_scm pylint tox tox-gh-actions pytest pytest-coverage twine build
pip install .
- name: Debug Info
run: |
which python
pip freeze
- name: Analysing the code with pylint
run: |
pylint -E $(git ls-files '*.py')
- name: Tests
run: |
pytest -v --cov=./ --cov-report=xml --cov-config=.coveragerc
env:
PLATFORM: ${{ matrix.platform }}
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3

deploy:
# this will run when you have tagged a commit, starting with "v*"
Expand Down
14 changes: 14 additions & 0 deletions conda_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: napari-tomotwin
channels:
- conda-forge
- defaults
dependencies:
- python=3.10
- napari=0.4.18
- pandas
- numpy
- pyqt
- matplotlib
- pip:
- tqdm
- napari-clusters-plotter>=0.7.2
43 changes: 43 additions & 0 deletions notebooks/test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Kannst du das lesen?"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

2 changes: 0 additions & 2 deletions src/napari_tomotwin/_tests/test_dummy.py

This file was deleted.

129 changes: 129 additions & 0 deletions src/napari_tomotwin/_tests/test_make_targets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import os
import tempfile
import unittest

import numpy as np
import pandas as pd
from napari_tomotwin.make_targets import _run

from glob import glob

class MyTestCase(unittest.TestCase):
def test_make_targets_single_cluster_medoid(self):
fake_embedding = {
"X": [0, 1, 2],
"Y": [0, 1, 2],
"Z": [0, 1, 2],
"1": [5, 6, 7],
"2": [5, 6, 7],
"filepath": ["a.mrc","b.mrc","c.mrc"]
}
cluster = np.array([1,1,1])
with tempfile.TemporaryDirectory() as tmpdirname:
_run(clusters=cluster,
embeddings=pd.DataFrame(fake_embedding),
average_method_name="Medoid",
output_folder=tmpdirname)

box_data: pd.DataFrame = pd.read_csv(
os.path.join(tmpdirname,"cluster_1_medoid.coords"),
delim_whitespace=True,
index_col=False,
header=None,
dtype=float,
names=["X","Y","Z"]
).astype(np.int32) # type: ignore
self.assertEqual(box_data.iloc[0, 0], 1)
self.assertEqual(box_data.iloc[0, 1], 1)
self.assertEqual(box_data.iloc[0, 2], 1)

def test_make_targets_two_clusters_medoid(self):
range(6)
fake_embedding = {
"X": [0, 1, 2, 8, 9, 10],
"Y": [0, 1, 2, 8, 9, 10],
"Z": [0, 1, 2, 8, 9, 10],
"1": [5, 6, 7, 10, 11, 12],
"2": [5, 6, 7, 10, 11, 12],
}
fake_embedding['filepath'] = [f"{i}.mrc" for i in range(len(fake_embedding["X"]))]
cluster = np.array([1,1,1,2,2,2])
with tempfile.TemporaryDirectory() as tmpdirname:
_run(clusters=cluster,
embeddings=pd.DataFrame(fake_embedding),
average_method_name="Medoid",
output_folder=tmpdirname)

box_data: pd.DataFrame = pd.read_csv(
os.path.join(tmpdirname,"cluster_1_medoid.coords"),
delim_whitespace=True,
index_col=False,
header=None,
dtype=float,
names=["X","Y","Z"]
).astype(np.int32) # type: ignore
self.assertEqual(box_data.iloc[0, 0], 1)
self.assertEqual(box_data.iloc[0, 1], 1)
self.assertEqual(box_data.iloc[0, 2], 1)

box_data: pd.DataFrame = pd.read_csv(
os.path.join(tmpdirname, "cluster_2_medoid.coords"),
delim_whitespace=True,
index_col=False,
header=None,
dtype=float,
names=["X", "Y", "Z"]
).astype(np.int32) # type: ignore
self.assertEqual(box_data.iloc[0, 0], 9)
self.assertEqual(box_data.iloc[0, 1], 9)
self.assertEqual(box_data.iloc[0, 2], 9)

def test_make_targets_single_cluster_average(self):
fake_embedding = {
"X": [0, 1, 2],
"Y": [0, 1, 2],
"Z": [0, 1, 2],
"1": [5, 6, 7],
"2": [5, 6, 7],
"filepath": ["a.mrc","b.mrc","c.mrc"]
}
cluster = np.array([1,1,1])
with tempfile.TemporaryDirectory() as tmpdirname:
_run(clusters=cluster,
embeddings=pd.DataFrame(fake_embedding),
average_method_name="Average",
output_folder=tmpdirname)

targets_emb: pd.DataFrame = pd.read_pickle(
os.path.join(tmpdirname,"cluster_targets.temb"),
)
self.assertEqual(targets_emb["1"].iloc[0],6)
self.assertEqual(targets_emb["2"].iloc[0],6)

def test_make_targets_single_cluster_no_coords_written(self):
fake_embedding = {
"X": [0, 1, 2],
"Y": [0, 1, 2],
"Z": [0, 1, 2],
"1": [5, 6, 7],
"2": [5, 6, 7],
"filepath": ["a.mrc","b.mrc","c.mrc"]
}
cluster = np.array([1,1,1])
with tempfile.TemporaryDirectory() as tmpdirname:
_run(clusters=cluster,
embeddings=pd.DataFrame(fake_embedding),
average_method_name="Average",
output_folder=tmpdirname)

r = glob(os.path.join(tmpdirname,"*.coords"))
print(r)
self.assertEqual(len(r),0)






if __name__ == '__main__':
unittest.main()
Loading