Commit 655a8449 authored by andy.disch's avatar andy.disch

intitial commit

parents
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Debugging code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prints debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interactive shells\n",
"\n",
"python in Bash or other system shell\n",
"ipython with synthax highlighting, media support, tab completion etc."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Breakpoint debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pdb\n",
"Load as module\n",
"Breakpoint possible\n",
"Commands: n, p, l, c, r, q (and ENTER)\n",
"! (and the following will be a python statement: assigning variables) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--Return--\n",
"> <ipython-input-11-9442abc5edeb>(11)<module>()->None\n",
"-> pdb.set_trace()\n"
]
}
],
"source": [
"import pdb\n",
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"pdb.set_trace()\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"run -> view breakpoints: list of all breakpoints, condition when to trigger breakpoint\n",
"debug remote processes: run -> attach local process *never used \n",
"Interpreter with loaded environment (actual sandbox): run debugger, change to console (python with green arrow symbol)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"aaabbbaaa\"\n"
]
}
],
"source": [
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (Interpret errors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%run -i 'C:\\Users\\dischand\\PycharmProjects\\examplepydebuggingandtesting\\examplepydebuggingandtesting\\exampledebugging.py'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example debugging module."""
import datetime
import pandas
import pandas.api.types
import numpy
class UrbanDrainageTS(pandas.DataFrame):
""" class for handling single measurement time series. """
def __init__(self, time, value):
"""description of the classes initiation."""
if isinstance(time, list):
time = pandas.Series(time)
if isinstance(value, list):
value = pandas.Series(value)
d_f = pandas.DataFrame({'time': time, 'value': value})
assert pandas.api.types.is_datetime64_any_dtype(time), "time is not a datetime: %r" % time
assert pandas.api.types.is_float_dtype(value), "value is not a float: %r" % value
super().__init__(d_f)
def sample_entropy(self, m_m=2):
"""compute sample entropy."""
u_u = self.value
r_r = 0.2 * numpy.std(self.value)
def _maxdist(x_i, x_j):
"""compute sample entropy."""
result = max([abs(ua_ua - va_va) for ua_ua, va_va in zip(x_i, x_j)])
return result
def _phi(m_m):
"""compute sample entropy."""
x_x = [[u_u[j_j] for j_j in range(i_i, i_i + m_m - 1 + 1)]
for i_i in range(n_n - m_m + 1)]
c_c = 1. * numpy.array([len([1 for j_j in range(len(x_x))
if i_i != j_j and _maxdist(x_x[i_i], x_x[j_j]) <= r_r])
for i_i in range(len(x_x))])
return sum(c_c)
n_n = len(u_u)
return -numpy.log(_phi(m_m + 1) / _phi(m_m))
if __name__ == '__main__':
TIME_TIME = [datetime.datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:05:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:10:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:15:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:20:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:25:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:30:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:35:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:40:00', '%Y-%m-%d %H:%M:%S')]
VALUE_VALUE = [6.789, 4.567, 9.09, 6.70, 8.70, 3.4, 2.3, 4.56, 2.78]
TEST_TS = UrbanDrainageTS(TIME_TIME, VALUE_VALUE)
TEST_TS.sample_entropy()
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example testing module."""
import pandas
import numpy
import pandas.api.types
class UrbanDrainageTS(pandas.DataFrame):
""" class for handling single measurement time series. """
def __init__(self, time, value):
"""description of the classes initiation."""
if isinstance(time, list):
time = pandas.Series(time)
if isinstance(value, list):
value = pandas.Series(value)
d_f = pandas.DataFrame({'time': time, 'value': value})
assert pandas.api.types.is_datetime64_any_dtype(time), "time is not a datetime: %r" % time
assert pandas.api.types.is_float_dtype(value), "value is not a float: %r" % value
super().__init__(d_f)
return
def sample_entropy(self, m_m=2):
"""compute sample entropy."""
u_u = self.value
r_r = 0.2 * numpy.std(self.value)
def _maxdist(x_i, x_j):
"""compute sample entropy."""
result = max([abs(ua_ua - va_va) for ua_ua, va_va in zip(x_i, x_j)])
return result
def _phi(m_m):
"""compute sample entropy."""
x_x = [[u_u[j_j] for j_j in range(i_i, i_i + m_m - 1 + 1)] for i_i in range(n_n - m_m + 1)]
c_c = 1. * numpy.array([len([1 for j_j in range(len(x_x)) if i_i != j_j and _maxdist(x_x[i_i], x_x[j_j]) <= r_r]) for i_i in range(len(x_x))])
return sum(c_c)
n_n = len(u_u)
return -numpy.log(_phi(m_m + 1) / _phi(m_m))
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## unittests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## hooks for unittests (or than use a continuous-integration server)\n",
".git/hooks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"use test runner to explore results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example test context file."""
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from examplepydebuggingandtesting import examplepytesting
\ No newline at end of file
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example testing functions."""
import unittest
from .context import examplepytesting
class NamesTestCase(unittest.TestCase):
""" class testing functions. """
def setUp(self):
"""create simple data sets"""
def tearDown(self):
"""tear down"""
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example test file."""
import unittest
import datetime
from .context import examplepytesting
class TestUrbanDrainageTS(unittest.TestCase):
def setUp(self):
time = [datetime.datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:05:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:10:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:15:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:20:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:25:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:30:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:35:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:40:00', '%Y-%m-%d %H:%M:%S')]
value = [6.789, 4.567, 9.09, 6.70, 8.70, 3.4, 2.3, 4.56, 2.78]
self.test_case = examplepytesting.UrbanDrainageTS(time, value)
class TestInit(TestUrbanDrainageTS):
def test_initial_length(self):
self.assertIsNotNone(self.test_case.time)
self.assertIsNotNone(self.test_case.value)
class TestSampleEntropy(TestUrbanDrainageTS):
def test_sample_entropy(self):
self.assertIsNotNone(self.test_case)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment