- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 python 中使用 PYCharm 和 unittest 来测试我的类。
逐一运行每个测试方法,所有测试都顺利通过。
一起运行所有测试失败并出现 assertionErrors。设置和拆卸方法已设置。
这是我的测试类的代码:
# -*- coding: utf-8 -*-
import unittest
import numpy as np
import os
import sys
from interpolation.interpolation import Interpolation
from StringIO import StringIO
class InterpolationTests(unittest.TestCase):
_interpolation = None
@classmethod
def setUpClass(cls):
cls._interpolation = Interpolation()
@classmethod
def tearDownClass(cls):
cls._interpolation = None
def test_load_valid_file(self):
self._interpolation.from_file('./testfiles/valid.json')
self.assertEqual('kriging', self._interpolation._method)
self.assertEqual(1.0, self._interpolation._xMin)
self.assertEqual(1.2, self._interpolation._xMax)
self.assertEqual(2.1, self._interpolation._yMin)
self.assertEqual(0.2, self._interpolation._yMax)
self.assertEqual(12, self._interpolation._nX)
self.assertEqual(13, self._interpolation._nY)
self.assertEqual(2, len(self._interpolation._points))
points = self._interpolation._points
self.assertEqual(1.1, points[0]['x'])
self.assertEqual(2.2, points[0]['y'])
self.assertEqual(3.4, points[0]['value'])
self.assertEqual(4.4, points[1]['x'])
self.assertEqual(5.5, points[1]['y'])
self.assertEqual(6.6, points[1]['value'])
def test_load_invalid_JSON_format(self):
self._interpolation.from_file('./testfiles/invalid.json')
saved_stdout = sys.stdout
try:
out = StringIO()
sys.stdout = out
self._interpolation.render_output()
output = out.getvalue().strip()
self.assertEqual('{"error":"Something went wrong with the json decoding"}',
output.encode('ascii', 'ignore'))
finally:
sys.stdout = saved_stdout
def test_load_empty_file(self):
self._interpolation.from_file('./testfiles/empty.json')
saved_stdout = sys.stdout
try:
out = StringIO()
sys.stdout = out
self._interpolation.render_output()
output = out.getvalue().strip()
self.assertEqual('{"error":"Something went wrong with the json decoding"}',
output.encode('ascii', 'ignore'))
finally:
sys.stdout = saved_stdout
def test_idw_with_file(self):
self._interpolation.from_file('./testfiles/valid_idw.json')
self._interpolation.calculate()
self.assertEqual(50, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_kriging(self):
self._interpolation.from_file('./testfiles/valid.json')
self._interpolation.calculate()
self.assertEqual(13, len(self._interpolation._output))
self.assertEqual(12, len(self._interpolation._output[0]))
def test_mean_with_file(self):
self._interpolation.from_file('./testfiles/valid_mean.json')
self._interpolation.calculate()
self.assertEqual(60, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_mean(self):
mean = Interpolation.mean(10, 20, np.array([1.2, 1.3, 1.4, 1.6, 1.9]))
self.assertEqual(20, len(mean))
self.assertEqual(10, len(mean[1]))
self.assertEqual(1.48, mean[1][2])
def test_gaussian(self):
self._interpolation.from_file('./testfiles/valid_gaussian.json')
self._interpolation.calculate()
self.assertEqual(50, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_with_two_points(self):
self._interpolation.from_string(json_input='{"bounding_box":{"x_min":0,"x_max":10,"y_min":0,"y_max":10},"grid_size":{"n_x":10,"n_y":11},"point_values":[{"x":1,"y":5,"value":800},{"x":2,"y":8,"value":3}],"type":"gaussian"}')
self._interpolation.calculate()
self.assertEqual('Exception raised in calculation of method gaussian', self._interpolation._error_message)
if __name__ == '__main__':
unittest.main()
以下是失败测试的部分结果:
/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "/Applications/PyCharm CE.app/Contents/helpers/pycharm/noserunner.py" /Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py
Testing started at 09:52 ...
..
..
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 77, in test_kriging
self.assertEqual(13, len(self._interpolation._output))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 13 != 50
-------------------- >> begin captured stdout << ---------------------
<type 'exceptions.Exception'> Matrix is not positive definite
--------------------- >> end captured stdout << ----------------------
F
..
..
..
..
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 83, in test_mean_with_file
self.assertEqual(60, len(self._interpolation._output))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 60 != 50
F
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 101, in test_with_two_points
self.assertEqual('Exception raised in calculation of method gaussian', self._interpolation._error_message)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 'Exception raised in calculation of method gaussian' != 'Something went wrong with the json decoding'
F
======================================================================
FAIL: test_kriging (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 77, in test_kriging
self.assertEqual(13, len(self._interpolation._output))
AssertionError: 13 != 50
-------------------- >> begin captured stdout << ---------------------
<type 'exceptions.Exception'> Matrix is not positive definite
--------------------- >> end captured stdout << ----------------------
======================================================================
FAIL: test_mean_with_file (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 83, in test_mean_with_file
self.assertEqual(60, len(self._interpolation._output))
AssertionError: 60 != 50
======================================================================
FAIL: test_render_to_file (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 122, in test_render_to_file
self.assertTrue(os.path.isfile(output_filename))
AssertionError: False is not true
-------------------- >> begin captured stdout << ---------------------
{"error":"Something went wrong with the json decoding"}
--------------------- >> end captured stdout << ----------------------
Ran 11 tests in 0.826s
FAILED (failures=5)
Process finished with exit code 0
最后是插值类:
#! /usr/env python
from pyKriging.krige import kriging
from sklearn.gaussian_process import GaussianProcess
import demjson
import numpy as np
class Interpolation:
"""The interpolation class"""
_method = ""
_xMin = 0.0
_xMax = 0.0
_yMin = 0.0
_yMax = 0.0
_nX = 0
_nY = 0
_dX = 0.0
_dY = 0.0
_X = []
_Y = []
_points = []
_output = ""
_json_output = ""
_error = False
_error_message = ""
def __init__(self):
pass
def from_file(self, input_file):
try:
_file = open(input_file, 'r')
json_input = _file.read()
except IOError as exc:
self._error = True
self._error_message = str(exc)
return
except Exception as exc:
self._error = True
self._error_message = str(exc)
return
self.decode_json(json_input)
def from_string(self, json_input):
self.decode_json(json_input)
def decode_json(self, json_input):
try:
json_dict = demjson.decode(json_input)
except Exception as exc:
self._error = True
self._error_message = "Something went wrong with the json decoding"
return
if 'type' in json_dict:
self._method = json_dict['type']
if 'bounding_box' in json_dict:
bounding_box = json_dict['bounding_box']
if 'x_min' in bounding_box:
self._xMin = float(bounding_box['x_min'])
if 'x_max' in bounding_box:
self._xMax = float(bounding_box['x_max'])
if 'y_min' in bounding_box:
self._yMin = float(bounding_box['y_min'])
if 'y_max' in bounding_box:
self._yMax = float(bounding_box['y_max'])
if 'grid_size' in json_dict:
grid_size = json_dict['grid_size']
if 'n_x' in grid_size:
self._nX = grid_size['n_x']
if 'n_y' in grid_size:
self._nY = grid_size['n_y']
self._dX = (self._xMax - self._xMin) / self._nX
self._dY = (self._yMax - self._yMin) / self._nY
if 'point_values' in json_dict:
self._points = json_dict['point_values']
for point in self._points:
if 'x' in point and 'y' in point:
self._X.append([point['x'], point['y']])
if 'value' in point:
self._Y.append(point['value'])
def calculate(self):
if not self._error:
try:
if self._method == 'kriging':
self._output = self.kriging(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
elif self._method == 'mean':
self._output = self.mean(self._nX, self._nY, self._Y)
elif self._method == 'gaussian':
self._output = self.gaussian_process(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
elif self._method == 'idw':
self._output = self.inverse_distance_weighting(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
else:
self._error = True
self._error_message = 'Method %s is not supported' % self._method
except:
self._error = True
self._error_message = 'Exception raised in calculation of method %s' % self._method
def render_output(self, output_file=''):
if self._error:
self.render_error()
return
output = self.render(self._method, self._output)
if output_file == '':
print output
else:
try:
output_file = open(output_file, 'w')
output_file.truncate()
output_file.write(output)
output_file.close()
except IOError as exc:
self._error = True
self._error_message = str(exc)
self.render_error()
return
self.render_success()
def render_success(self):
result = {"success": self._method}
print demjson.encode(result)
def render_error(self):
result = {"error": self._error_message}
print demjson.encode(result)
@staticmethod
def render(method, output):
if (method == 'kriging') or (method == 'mean') or (method == 'gaussian') or (method == 'idw'):
result = {"raster": output, "method": method}
return demjson.encode(result)
@staticmethod
def kriging(nx, ny, x, y, x_min, y_min, dx, dy):
grid = np.zeros((ny, nx))
k = kriging(np.array(x), np.array(y))
k.train()
for i in range(ny):
for j in range(nx):
cell = np.array([y_min + dy * j + .5 * dy, x_min + dx * i + .5 * dx])
grid[i][j] = k.predict(cell)
return grid
@staticmethod
def mean(nx, ny, values):
mean_value = np.mean(values)
grid = mean_value * np.ones((ny, nx))
return grid
@staticmethod
def gaussian_process(nx, ny, X, y, x_min, y_min, dx, dy):
"""
Gausian process method. To replace kriging.
Description:
http://scikit-learn.org/stable/modules/generated/sklearn.gaussian_process.GaussianProcess.html#sklearn.gaussian_process.GaussianProcess.predict
The scikit learn python library should be installed
Should be tested
"""
# Prediction is very sensetive to the parameters below, method to be used carefully!
gp = GaussianProcess(regr='quadratic', corr='cubic', theta0=0.1, thetaL=.001, thetaU=1., nugget=0.01)
gp.fit(X, y)
X_grid_x = np.linspace(x_min, x_min+dx*nx, nx)
X_grid_y = np.linspace(y_min, y_min+dy*ny, ny)
xv, yv = np.meshgrid(X_grid_x, X_grid_y)
X_grid = np.dstack(( xv.flatten(), yv.flatten()))[0]
grid = np.reshape(gp.predict(X_grid, eval_MSE=False, batch_size=None), (ny, nx))
return grid
@staticmethod
def inverse_distance_weighting(nx, ny, X, y, x_min, y_min, dx, dy):
"""
Inverse-distance weighting interpolation method
"""
def pointValue(x, y, power, smoothing, xv, yv, values):
""" This function is used inside the inverse_distance_weighting method. """
from math import pow
from math import sqrt
nominator=0
denominator=0
for i in range(0,len(values)):
dist = sqrt((x-xv[i])*(x-xv[i])+(y-yv[i])*(y-yv[i])+smoothing*smoothing)
#If the point is really close to one of the data points, return the data point value to avoid singularities
if(dist<0.0000000001):
return values[i]
nominator=nominator+(values[i]/pow(dist,power))
denominator=denominator+(1/pow(dist,power))
#Return NODATA if the denominator is zero
if denominator > 0:
value = nominator/denominator
else:
value = -9999
return value
power, smoothing = 5, 0
xv = [i[0] for i in X]
yv = [i[1] for i in X]
grid = np.zeros((ny, nx))
for i in range(nx):
for j in range(ny):
grid[j][i] = pointValue((x_min + dx/2)+dx*i, (y_min + dy/2)+dy*j, power, smoothing, xv, yv, y)
return grid
最佳答案
您正在使用 setUpClass()
和 tearDownClass()
,整个类只调用一次。这导致您的不同测试方法通过更改 _interpolation
属性相互干扰。
您可以改用 setUp()
和 tearDown()
,它们会在每个测试方法之前/之后调用一次。
更好的是,由于您是在每个测试中从一个文件中初始化这个对象,所以只需让每个测试方法在开始时创建自己的 Interpolation
对象即可。
关于python unittest 无法运行所有测试方法,而每个测试方法运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853023/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!