Skip to content

Commit

Permalink
Merge dddcfdf into aa6fce9
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaselsteiner committed Sep 28, 2020
2 parents aa6fce9 + dddcfdf commit 74ca396
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 12 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pytest-cov==2.10.1
python-dateutil==2.8.1
pytz==2020.1
requests==2.24.0
shapely==1.7.1
scikit-learn==0.23.2
scipy==1.5.2
six==1.15.0
Expand Down
97 changes: 95 additions & 2 deletions tests/test_contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
import os

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import pandas as pd

from .context import viroconcom
from shapely.geometry import Polygon, LineString

from viroconcom.params import ConstantParam, FunctionParam

from viroconcom.distributions import (
WeibullDistribution, ExponentiatedWeibullDistribution, LognormalDistribution,
NormalDistribution, MultivariateDistribution)
from viroconcom.contours import IFormContour, ISormContour, \
HighestDensityContour, DirectSamplingContour
HighestDensityContour, DirectSamplingContour, sort_points_to_form_continous_line
from viroconcom.read_write import read_contour
from viroconcom.plot import plot_contour


_here = os.path.dirname(__file__)
Expand Down Expand Up @@ -720,6 +724,95 @@ def test_setup_HDC_limits_Tuple_length(self):
# #plt.show()


def test_sort_coordinates_low_n(self):
"""
Sorts a limited number of points (low n).
"""
# Describe a contour with correctly ordered coordinates.
x_correct = np.array([0, 1, 2, 3, 3, 3, 3, 2, 1])
y_correct = np.array([0, 0, 0, 0, 1, 2, 3, 2, 1])

# Switch two entries such that the coordinates are not ordered anymore.
x_us = x_correct.copy()
y_us = y_correct.copy()
x_us[3] = x_correct[6]
x_us[6] = x_correct[3]
y_us[3] = y_correct[6]
y_us[6] = y_correct[3]

c_sorted = sort_points_to_form_continous_line(x_us, y_us)
x_s = c_sorted[0]
y_s = c_sorted[1]

# For a manual check during development: Plot the contours.
fig, ax = plt.subplots()
plot_contour(x_correct, y_correct, ax=ax)
plot_contour(x_us, y_us, ax=ax, style='-r')
plot_contour(x_s, y_s, ax=ax, style='-g')
#plt.show()

np.testing.assert_array_equal(x_correct, x_s)
np.testing.assert_array_equal(y_correct, y_s)

def test_sort_coordinates_high_n(self):
"""
Sorts an existing IFORM contour (high number of points).
"""
# Load a IFORM contour that was has been computed for th EC benchmark.
folder_name = 'contour-coordinates/'
file_name_median = 'doe_john_years_25_median.txt'
(x_correct, y_correct) = read_contour(folder_name + file_name_median)

# Switch two entries such that the coordinates are not ordered anymore.
x_us = x_correct.copy()
y_us = y_correct.copy()
i1 = 10
i2 = 100
x_us[i1] = x_correct[i2]
x_us[i2] = x_correct[i1]
y_us[i1] = y_correct[i2]
y_us[i2] = y_correct[i1]
i1 = 50
i2 = 150
x_us[i1] = x_correct[i2]
x_us[i2] = x_correct[i1]
y_us[i1] = y_correct[i2]
y_us[i2] = y_correct[i1]

c_sorted = sort_points_to_form_continous_line(x_us, y_us)
x_s = c_sorted[0]
y_s = c_sorted[1]

# Broadly define a polygon that spans the inside of the contour.
coords = [(1, 1), (10, 1),
(22, 7), (10, 5)]
poly = Polygon(coords)
x, y = poly.exterior.xy

# For a manual check during development: Plot the contours.
fig, ax = plt.subplots()
ax.plot(x, y)
plot_contour(x_correct, y_correct, ax=ax)
plot_contour(x_us, y_us, ax=ax, style='-r')
plot_contour(x_s, y_s, ax=ax, style='-g')
ax.plot(x_s[1], y_s[1], 'og')
#plt.show()

shapely_line_us = LineString(np.c_[x_us, y_us])
shapely_line_s = LineString(np.c_[x_s, y_s])

# Assert that the unsorted contour would cross the design region and
# that the sorted contour does not cross it.
self.assertTrue(poly.crosses(shapely_line_us))
self.assertFalse(poly.crosses(shapely_line_s))

# The sorting algorithm changes the direciton (clock-wise vs
# counter-clockwise. Thus np.testing.assert_array_equal(x_correct, x_s)
# would fail.
#np.testing.assert_array_equal(x_correct, x_s)
#np.testing.assert_array_equal(y_correct, y_s)


class DirectSamplingTest(unittest.TestCase):
def _setup(self,
dep1=(None, None, None),
Expand Down
5 changes: 5 additions & 0 deletions tests/test_distributions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-
"""
Tests the distribution classes.
"""

import unittest


Expand Down
6 changes: 5 additions & 1 deletion tests/test_distributions2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# -*- coding: utf-8 -*-
"""
Tests the distribution classes.
"""

import pytest

import numpy as np
import scipy.stats as sts

from .context import viroconcom
from viroconcom.distributions import (WeibullDistribution, NormalDistribution,
LognormalDistribution)
from viroconcom.params import ConstantParam
Expand Down
8 changes: 1 addition & 7 deletions tests/test_params.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 19 12:22:42 2017
@author: nb
Tests the parameter classes
"""
import unittest

import numpy as np


from .context import viroconcom

from viroconcom.params import ConstantParam, FunctionParam, Wrapper


Expand Down
2 changes: 0 additions & 2 deletions tests/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

from numpy.testing import assert_allclose

from .context import viroconcom

from viroconcom.utility import PCA
from viroconcom.fitting import Fit
from viroconcom.contours import IFormContour
Expand Down

0 comments on commit 74ca396

Please sign in to comment.