#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jan 28 19:35:04 2020 @author: luna """ import unittest import helpers import numpy as np import sys sys.path.append("C://Users//xbrjos//Desktop//Python") from PyQt5 import QtCore, QtGui import gepard from gepard.analysis.particleAndMeasurement import Particle from gepard import dataset class TestBinSorter(unittest.TestCase): def setUp(self): self.sorter = helpers.ParticleBinSorter() self.bins = self.sorter.bins def test_sort_particles_into_bins(self): particleList = [] upperBinLimit = None for upperBinLimit in self.bins: newParticle = Particle() newParticle.longSize = newParticle.shortSize = upperBinLimit - 1 particleList.append(newParticle) lastParticle = Particle() lastParticle.longSize = lastParticle.shortSize = upperBinLimit + 1 particleList.append(lastParticle) particlesInBins = self.sorter.sort_particles_into_bins(particleList) self.assertEqual(len(particlesInBins), len(self.bins)+1) for binContent in particlesInBins: self.assertEqual(len(binContent), 1) def test_get_empty_bins(self): emptyBins = self.sorter._get_empty_bins() self.assertEqual(len(emptyBins), len(self.bins)+1) def test_get_binIndex_of_particle(self): particle = Particle() particle.longSize = particle.shortSize = 0 binIndex = self.sorter._get_binIndex_of_particle(particle) self.assertEqual(binIndex, 0) particle.longSize = particle.shortSize = 5 binIndex = self.sorter._get_binIndex_of_particle(particle) self.assertEqual(binIndex, 0) particle.longSize = particle.shortSize = 5.01 binIndex = self.sorter._get_binIndex_of_particle(particle) self.assertEqual(binIndex, 1) particle.longSize = particle.shortSize = 100 binIndex = self.sorter._get_binIndex_of_particle(particle) self.assertEqual(binIndex, 4) particle.longSize = particle.shortSize = 1000 binIndex = self.sorter._get_binIndex_of_particle(particle) self.assertEqual(binIndex, 7) class TestOther(unittest.TestCase): def test_box_overlaps_contour(self): boxXY: tuple = 0, 0 boxWidthHeight: tuple = 10, 10 contourPoints = np.array([[[0, 0]], [[5, 5]], [[3, 3]]]) # fully enclosed self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints)) contourPoints = np.array([[[1, 1]], [[5, 5]], [[3, 3]]]) # fully enclosed self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints)) contourPoints = np.array([[[-5, -5]], [[0, 5]], [[-5, -10]]]) # only one point touches border self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints)) offset: tuple = (1, 0) # now it does not touch it anymore self.assertFalse(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints, offset)) contourPoints = np.array([[[-5, -5]], [[-1, 5]], [[-5, -10]]]) # outside the box self.assertFalse(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints)) offset = (-5, -5) # now it overlaps self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints, offset)) def test_get_overlapping_fraction(self): polygon1: QtGui.QPolygonF = QtGui.QPolygonF() polygon1.append(QtCore.QPointF(0, 0)) polygon1.append(QtCore.QPointF(10, 0)) polygon1.append(QtCore.QPointF(10, 10)) polygon1.append(QtCore.QPointF(0, 10)) polygon2: QtGui.QPolygonF = QtGui.QPolygonF() # shifted +5 in x, so half of it should overlap polygon2.append(QtCore.QPointF(5, 0)) polygon2.append(QtCore.QPointF(15, 0)) polygon2.append(QtCore.QPointF(15, 10)) polygon2.append(QtCore.QPointF(5, 10)) self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0.5) polygon2: QtGui.QPolygonF = QtGui.QPolygonF() # made the second poly much larger, this shouldn't have an impact polygon2.append(QtCore.QPointF(5, 0)) polygon2.append(QtCore.QPointF(100, 0)) polygon2.append(QtCore.QPointF(100, 100)) polygon2.append(QtCore.QPointF(5, 100)) self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0.5) polygon2: QtGui.QPolygonF = QtGui.QPolygonF() # shifted another +5 in x, it should not overlap at all polygon2.append(QtCore.QPointF(15, 0)) polygon2.append(QtCore.QPointF(15, 10)) polygon2.append(QtCore.QPointF(25, 10)) polygon2.append(QtCore.QPointF(25, 0)) self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0) polygon2: QtGui.QPolygonF = QtGui.QPolygonF() # not the second poly completely encloses the first one polygon2.append(QtCore.QPointF(-100, -100)) polygon2.append(QtCore.QPointF(100, -100)) polygon2.append(QtCore.QPointF(100, 100)) polygon2.append(QtCore.QPointF(-100, 100)) self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 1.0) def test_get_polygon_area(self): polygon1: QtGui.QPolygonF = QtGui.QPolygonF() polygon1.append(QtCore.QPointF(0, 0)) polygon1.append(QtCore.QPointF(10, 0)) polygon1.append(QtCore.QPointF(10, 10)) polygon1.append(QtCore.QPointF(0, 10)) self.assertEqual(helpers.get_polygon_area(polygon1), 100) polygon1: QtGui.QPolygonF = QtGui.QPolygonF() polygon1.append(QtCore.QPointF(0, 0)) polygon1.append(QtCore.QPointF(20, 0)) polygon1.append(QtCore.QPointF(20, 10)) polygon1.append(QtCore.QPointF(0, 10)) self.assertEqual(helpers.get_polygon_area(polygon1), 200) polygon1: QtGui.QPolygonF = QtGui.QPolygonF() polygon1.append(QtCore.QPointF(-10, 0)) polygon1.append(QtCore.QPointF(10, 0)) polygon1.append(QtCore.QPointF(10, 10)) polygon1.append(QtCore.QPointF(-10, 10)) self.assertEqual(helpers.get_polygon_area(polygon1), 200) class TestDatasetOperations(unittest.TestCase): def setUp(self) -> None: self.dataset: dataset.DataSet = dataset.DataSet('test') self.dataset.imagescanMode = 'df' def test_get_filtersize(self): def setMaxDim(): self.dataset.maxdim = minX + imgdim / 2, maxY - imgdim / 2, maxX - imgdim / 2, minY + imgdim / 2 imgdim = 10 self.dataset.imagedim_df = [imgdim, imgdim] minX, maxX, minY, maxY = 0, 10, 0, 10 setMaxDim() offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset) self.assertEqual(diameter, 10) self.assertEqual(offset, (0, 0)) self.assertEqual(widthHeight, [10, 10]) minX, maxX, minY, maxY = -10, 10, -10, 10 setMaxDim() offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset) self.assertEqual(diameter, 20) self.assertEqual(widthHeight, [20, 20]) self.assertEqual(offset, (0, 0)) minX, maxX, minY, maxY = 0, 20, 0, 10 setMaxDim() offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset) self.assertEqual(diameter, 10) self.assertEqual(widthHeight, [20, 10]) self.assertEqual(offset, (5, 0)) def testconvert_microns_to_pixels(self): self.dataset.lastpos = [0, 0] # in microns self.dataset.pixelscale_bf = 0.5 # micrometer / px self.dataset.imagedim_bf = [10, 10] # in microns self.dataset.imagescanMode = 'bf' diameter = 10 # in microns widthHeight = [20, 10] # in microns self.assertEqual(helpers.convert_length_to_pixels(self.dataset, diameter), 20) self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[0]), 40) self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[1]), 20)