clean up tests and folders
This commit is contained in:
+15
-14
@@ -65,7 +65,7 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
self.common_descriptions=None
|
||||
self.time_period=[]
|
||||
self.job_id = None # this does not make sense for an object accumulated from multiple job_ids
|
||||
self.job_ids = []
|
||||
self.job_ids = {}
|
||||
|
||||
self.use_error = error
|
||||
if self.uses_statistics():
|
||||
@@ -498,8 +498,8 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
expectedrows=len(self.job_ids))
|
||||
new_row=jobid_table.row
|
||||
for i,job_id in enumerate(self.job_ids):
|
||||
new_row["job_date"] = self.job_ids[job_id].isoformat(timespec='milliseconds')
|
||||
new_row["job_id"]=job_id
|
||||
new_row["job_date"]="Not Implemented"
|
||||
new_row.append()
|
||||
jobid_table.flush()
|
||||
# end save job_ids
|
||||
@@ -616,7 +616,7 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
r = Accumulation(x = numpy.array(self.x, dtype="float32"), y = tmp_y, y_2 = tmp_ysquare, n = self.n, index = self.index, sampl_freq = self.sampling_rate, error = self.use_error)
|
||||
else:
|
||||
r = Accumulation(x = numpy.array(self.x, dtype="float32"), y = tmp_y, n = self.n, index = self.index, sampl_freq = self.sampling_rate, error = self.use_error)
|
||||
r.job_id = self.job_id # added by Oleg Petrov
|
||||
r.job_id = self.job_id
|
||||
self.lock.release()
|
||||
return r
|
||||
|
||||
@@ -626,7 +626,7 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
# todo: this is seems to be bugy!!!! (Achim)
|
||||
if not other.contains_data():
|
||||
return
|
||||
# Self empty (copy)
|
||||
# Self empty (copy ADC_Result)
|
||||
if not self.contains_data():
|
||||
tmp_y = []
|
||||
tmp_ysquare = []
|
||||
@@ -645,11 +645,11 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
r.time_period=[other.job_date,other.job_date]
|
||||
r.job_id = other.job_id
|
||||
r.common_descriptions=other.description.copy()
|
||||
r.job_ids.append(other.job_id)
|
||||
r.job_ids[other.job_id] = other.get_job_date()
|
||||
self.lock.release()
|
||||
return r
|
||||
|
||||
# Other and self not empty (self + other)
|
||||
# Other and self not empty (self + other ADC_Result)
|
||||
else:
|
||||
self.lock.acquire()
|
||||
|
||||
@@ -679,7 +679,7 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
max(self.time_period[1],other.job_date)]
|
||||
r.job_id = other.job_id
|
||||
r.job_ids = self.job_ids
|
||||
r.job_ids.append(other.job_id)
|
||||
r.job_ids[other.job_id] = other.get_job_date()
|
||||
|
||||
if self.common_descriptions is not None:
|
||||
r.common_descriptions={}
|
||||
@@ -712,11 +712,11 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
tmp_ysquare.append(other.y_square[i])
|
||||
r.time_period=other.time_period[:]
|
||||
r.job_id = other.job_id
|
||||
r.job_ids = other.job_ids
|
||||
if other.common_descriptions is not None:
|
||||
r.common_descriptions=other.common_descriptions.copy()
|
||||
else:
|
||||
r.common_descriptions=None
|
||||
|
||||
self.lock.release()
|
||||
return r
|
||||
|
||||
@@ -750,7 +750,9 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
|
||||
r.time_period=[min(self.time_period[0],other.time_period[0]),
|
||||
max(self.time_period[1],other.time_period[1])]
|
||||
r.job_id = other.job_id # added by Oleg Petrov
|
||||
r.job_id = other.job_id
|
||||
for i in other.job_ids:
|
||||
r.job_ids[i] = other.job_ids[i]
|
||||
r.common_descriptions={}
|
||||
if self.common_descriptions is not None and other.common_descriptions is not None:
|
||||
for key in list(self.common_descriptions.keys()):
|
||||
@@ -817,13 +819,13 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
|
||||
self.time_period=[other.job_date,other.job_date]
|
||||
self.job_id = other.job_id # added by Oleg Petrov
|
||||
self.job_ids.append(other.job_id)
|
||||
self.job_ids[other.job_id] = other.get_job_date()
|
||||
self.common_descriptions=other.description.copy()
|
||||
|
||||
return self
|
||||
|
||||
|
||||
# Other and self not empty (self + other)
|
||||
# Other and self not empty (self + other/ADC_Result)
|
||||
else:
|
||||
self.lock.acquire()
|
||||
|
||||
@@ -847,15 +849,14 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
|
||||
max(self.time_period[1],other.job_date)]
|
||||
|
||||
self.job_id = other.job_id
|
||||
self.job_ids.append(other.job_id)
|
||||
|
||||
self.job_ids[other.job_id] = other.get_job_date()
|
||||
if self.common_descriptions is not None:
|
||||
for key in list(self.common_descriptions.keys()):
|
||||
if not (key in other.description and self.common_descriptions[key]==other.description[key]):
|
||||
del self.common_descriptions[key]
|
||||
|
||||
self.set_title(self.__title_pattern % self.n)
|
||||
self.lock.release()
|
||||
|
||||
return self
|
||||
|
||||
# Accumulation
|
||||
|
||||
@@ -905,9 +905,12 @@ class DamarisGUI:
|
||||
last_end = logtextbuffer.get_mark( "lastdumped" )
|
||||
if last_end is None:
|
||||
last_end = logtextbuffer.create_mark( "lastdumped", logtextbuffer.get_start_iter( ), left_gravity=True )
|
||||
logtext_start = logtextbuffer.get_iter_at_mark( last_end )
|
||||
#logtext_start = logtextbuffer.get_iter_at_mark( last_end )
|
||||
logtext_end = logtextbuffer.get_end_iter( )
|
||||
logtextbuffer.move_mark( last_end, logtext_end )
|
||||
# Create new iterators after the mark move
|
||||
logtext_start = logtextbuffer.get_iter_at_mark( last_end )
|
||||
logtext_end = logtextbuffer.get_end_iter( )
|
||||
# recode from unicode
|
||||
logtext = logtextbuffer.get_text( logtext_start, logtext_end, True )
|
||||
# avoid circular references (seems to be necessary with gtk-2.12)
|
||||
|
||||
+16
-14
@@ -56,11 +56,14 @@ class ResultReader:
|
||||
yield self.get_result_object(expected_filename)
|
||||
# purge result file
|
||||
if self.clear_results:
|
||||
if os.path.isfile(expected_filename): os.remove(expected_filename)
|
||||
if os.path.isfile(expected_filename):
|
||||
os.remove(expected_filename)
|
||||
if self.clear_jobs:
|
||||
if os.path.isfile(expected_filename[:-7]): os.remove(expected_filename[:-7])
|
||||
# job_name.result -> job_name
|
||||
if os.path.isfile(expected_filename[:-7]):
|
||||
os.remove(expected_filename[:-7])
|
||||
self.no+=1
|
||||
expected_filename=os.path.join(self.spool_dir,self.result_pattern%(self.no))
|
||||
expected_filename=os.path.join(self.spool_dir, self.result_pattern % self.no)
|
||||
return
|
||||
|
||||
def get_result_object(self, in_filename):
|
||||
@@ -80,8 +83,10 @@ class ResultReader:
|
||||
time.sleep(0.05)
|
||||
retries+=1
|
||||
|
||||
# get date of last modification
|
||||
self.result_job_date = datetime.fromtimestamp(os.stat(in_filename)[8])
|
||||
# get date of last modification
|
||||
stat_result = os.stat(in_filename)
|
||||
mtime = stat_result.st_mtime
|
||||
self.result_job_date = datetime.fromtimestamp(mtime)
|
||||
if ELEMENT_TREE:
|
||||
self.__parseFile = self.__parseFile_cETree
|
||||
else:
|
||||
@@ -155,7 +160,7 @@ class ResultReader:
|
||||
self.result.set_nChannels(int(elem.get("channels")))
|
||||
|
||||
self.result.set_description_dictionary(self.result_description.copy())
|
||||
title = "ADC-Result: job-id=%d"%int(self.result_job_number)
|
||||
title = "ADC_Result: job-id=%d"%int(self.result_job_number)
|
||||
if len(self.result_description) > 0:
|
||||
for k,v in self.result_description.items():
|
||||
title += ", %s=%s"%(k,v)
|
||||
@@ -166,22 +171,19 @@ class ResultReader:
|
||||
else:
|
||||
if float(elem.get("rate")) != self.result.get_sampling_rate():
|
||||
print("sample rate different in ADC_Result, found %f, former value %f"%\
|
||||
(float(in_attribute["rate"]),self.result.get_sampling_rate()))
|
||||
(float(elem.get("rate")),self.result.get_sampling_rate()))
|
||||
new_samples = int(elem.get("samples"))
|
||||
self.adc_result_sample_counter += new_samples
|
||||
|
||||
|
||||
# extract adcdata (here base64 encoded)
|
||||
self.adc_result_trailing_chars = "".join(elem.text.splitlines())
|
||||
tmp_string = base64.standard_b64decode(self.adc_result_trailing_chars)
|
||||
self.adc_result_trailing_chars = None
|
||||
tmp = numpy.fromstring(tmp_string,dtype='int16')
|
||||
tmp_string = None
|
||||
|
||||
tmp = numpy.fromstring(tmp_string, dtype='int16')
|
||||
self.adc_result_parts.append(tmp)
|
||||
tmp = None
|
||||
# we do not need this adcdata anymore, delete it
|
||||
elem.clear()
|
||||
|
||||
|
||||
|
||||
elif elem.tag == 'error':
|
||||
self.__filetype = ResultReader.ERROR_TYPE
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
# 1. disable any possible re-launch
|
||||
GIO_USE_VFS=local # stop gvfs daemonising
|
||||
GSETTINGS_BACKEND=memory # don’t spawn dconf-service
|
||||
|
||||
# 2. ask glib to abort on the first warning so nothing can “eat” it
|
||||
G_DEBUG=fatal-warnings # or =fatal-criticals
|
||||
|
||||
# 3. turn on *all* debug domains
|
||||
G_MESSAGES_DEBUG=all
|
||||
|
||||
# 4. run inside gdb so we can see the exact line that fails
|
||||
gdb -ex run --args python3 src/gui/DamarisGUI.py
|
||||
@@ -1,8 +0,0 @@
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
print("GTK imported successfully")
|
||||
win = Gtk.Window()
|
||||
win.connect("destroy", Gtk.main_quit)
|
||||
win.show_all()
|
||||
Gtk.main()
|
||||
@@ -1,46 +0,0 @@
|
||||
HDF5 "testaccu.hdf5" {
|
||||
DATASET "/name/accu_data" {
|
||||
DATATYPE H5T_IEEE_F32LE
|
||||
DATASPACE SIMPLE { ( 2, 4 ) / ( 2, 4 ) }
|
||||
DATA {
|
||||
(0,0): 10, 0, 15, 0,
|
||||
(1,0): 20, 0, 25, 0
|
||||
}
|
||||
ATTRIBUTE "CLASS" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 6;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "CARRAY"
|
||||
}
|
||||
}
|
||||
ATTRIBUTE "TITLE" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 9;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "accu data"
|
||||
}
|
||||
}
|
||||
ATTRIBUTE "VERSION" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 3;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "1.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
HDF5 "test.hdf5" {
|
||||
DATASET "/name/adc_data" {
|
||||
DATATYPE H5T_STD_I16LE
|
||||
DATASPACE SIMPLE { ( 2, 2 ) / ( 2, 2 ) }
|
||||
DATA {
|
||||
(0,0): 10, 15,
|
||||
(1,0): 20, 25
|
||||
}
|
||||
ATTRIBUTE "CLASS" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 6;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "CARRAY"
|
||||
}
|
||||
}
|
||||
ATTRIBUTE "TITLE" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 8;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "adc data"
|
||||
}
|
||||
}
|
||||
ATTRIBUTE "VERSION" {
|
||||
DATATYPE H5T_STRING {
|
||||
STRSIZE 3;
|
||||
STRPAD H5T_STR_NULLTERM;
|
||||
CSET H5T_CSET_ASCII;
|
||||
CTYPE H5T_C_S1;
|
||||
}
|
||||
DATASPACE SCALAR
|
||||
DATA {
|
||||
(0): "1.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 1) force GTK3 backend *before* pyplot is imported
|
||||
os.environ['MPLBACKEND'] = 'GTK3Agg' # or GTK3Cairo
|
||||
|
||||
# 2) turn on every debug message GLib/GTK will give us
|
||||
os.environ['G_MESSAGES_DEBUG'] = 'all'
|
||||
os.environ['GTK_DEBUG'] = 'interactive' # also enables Ctrl-Shift-I inspector
|
||||
|
||||
import matplotlib
|
||||
matplotlib.use('GTK3Agg') # belt-and-braces: make sure
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
print('GTK3Agg backend loaded, creating figure…')
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot([0, 1], [0, 1])
|
||||
ax.set_title('GTK3-Matplotlib window – close it to finish')
|
||||
print('Showing figure…')
|
||||
plt.show()
|
||||
print('plt.show() returned – script ends')
|
||||
|
||||
@@ -1,265 +0,0 @@
|
||||
import os
|
||||
import subprocess
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
import numpy as np
|
||||
from src.data.ADC_Result import ADC_Result
|
||||
|
||||
|
||||
class TestADCResult(unittest.TestCase):
|
||||
@staticmethod
|
||||
def create_adc_result():
|
||||
x = np.array([0.0, 1.0, 2.0])
|
||||
y = [np.array([10, 20, 30]), np.array([15, 25, 35])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id = 11
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
adc.set_nChannels(2)
|
||||
return adc
|
||||
|
||||
def test_constructor_with_no_arguments(self):
|
||||
"""Test initializer with no arguments."""
|
||||
adc = ADC_Result()
|
||||
self.assertFalse(adc.contains_data())
|
||||
self.assertEqual(adc.sampling_rate, 0)
|
||||
self.assertEqual(len(adc.index), 0)
|
||||
self.assertEqual(len(adc.x), 0)
|
||||
self.assertEqual(len(adc.y), 0)
|
||||
|
||||
def test_constructor_with_arguments(self):
|
||||
"""Test initializer with all arguments."""
|
||||
x = np.array([0.0, 1.0, 2.0])
|
||||
y = [np.array([10, 20, 30]), np.array([15, 25, 35])]
|
||||
index = [(0, 2)]
|
||||
sampl_freq = 1000.0
|
||||
desc = {"key1": "value1"}
|
||||
job_id = 1
|
||||
job_date = datetime(2023, 1, 1)
|
||||
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
|
||||
self.assertTrue(adc.contains_data())
|
||||
self.assertEqual(adc.sampling_rate, sampl_freq)
|
||||
self.assertEqual(adc.x.all(), x.all())
|
||||
self.assertEqual(adc.y[0].all(), y[0].all())
|
||||
self.assertEqual(adc.description, desc)
|
||||
self.assertEqual(adc.job_id, job_id)
|
||||
self.assertEqual(adc.job_date, job_date)
|
||||
self.assertTrue(np.array_equal(adc.x, np.array(x, dtype="float32")))
|
||||
self.assertTrue(np.array_equal(adc.y[0], np.array(y[0], dtype="int16")))
|
||||
self.assertTrue(np.array_equal(adc.y[1], np.array(y[1], dtype="int16")))
|
||||
|
||||
def test_create_data_space(self):
|
||||
"""Test creating a new data space."""
|
||||
adc = ADC_Result()
|
||||
adc.create_data_space(channels=2, samples=3)
|
||||
|
||||
self.assertTrue(adc.contains_data())
|
||||
self.assertEqual(len(adc.y), 2)
|
||||
self.assertEqual(len(adc.x), 3)
|
||||
self.assertTrue(np.array_equal(adc.x, np.zeros(3, dtype="float32")))
|
||||
self.assertTrue(np.array_equal(adc.y[0], np.zeros(3, dtype="int16")))
|
||||
self.assertEqual(adc.index, [(0, 2)])
|
||||
|
||||
def test_add_sample_space(self):
|
||||
"""Test adding sample space."""
|
||||
adc = ADC_Result()
|
||||
adc.create_data_space(channels=1, samples=3)
|
||||
adc.add_sample_space(2)
|
||||
|
||||
self.assertEqual(len(adc.x), 5)
|
||||
self.assertEqual(len(adc.y[0]), 5)
|
||||
self.assertEqual(adc.index[-1], (3, 4))
|
||||
|
||||
def test_get_result_by_index(self):
|
||||
"""Test retrieving data by index."""
|
||||
adc = self.create_adc_result()
|
||||
adc.index = [(0, 1), (1, 2)]
|
||||
adc.job_id = 42
|
||||
sub_result = adc.get_result_by_index(1)
|
||||
|
||||
self.assertEqual(sub_result.sampling_rate, 1000.0)
|
||||
self.assertTrue(np.array_equal(sub_result.x, np.array([1.0, 2.0])))
|
||||
self.assertTrue(np.array_equal(sub_result.y[0], np.array([20, 30])))
|
||||
self.assertTrue(np.array_equal(sub_result.y[1], np.array([25, 35])))
|
||||
self.assertEqual(sub_result.index, [(0, 1)])
|
||||
self.assertEqual(sub_result.description, {"key": "value"})
|
||||
self.assertEqual(sub_result.job_id, 42)
|
||||
|
||||
def test_set_sampling_rate(self):
|
||||
"""Test updating the sampling rate."""
|
||||
adc = ADC_Result()
|
||||
adc.set_sampling_rate(5000.0)
|
||||
self.assertEqual(adc.get_sampling_rate(), 5000.0)
|
||||
|
||||
def test_set_nChannels(self):
|
||||
"""Test setting the number of channels."""
|
||||
adc = ADC_Result()
|
||||
adc.set_nChannels(5)
|
||||
self.assertEqual(adc.get_nChannels(), 5)
|
||||
|
||||
def test_write_to_csv(self):
|
||||
"""Test the functionality of writing to CSV."""
|
||||
from io import StringIO
|
||||
x = np.array([0.0, 1.0])
|
||||
y = [np.array([10, 20]), np.array([15, 25])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id=9
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
output = StringIO()
|
||||
|
||||
adc.write_to_csv(output, delimiter=",")
|
||||
content = output.getvalue()
|
||||
expected = (
|
||||
"# adc_result\n"
|
||||
"# t y0 y1 ...\n"
|
||||
"0.000000e+00,1.000000e+01,1.500000e+01\n"
|
||||
"1.000000e+00,2.000000e+01,2.500000e+01\n"
|
||||
)
|
||||
self.assertEqual(content, expected)
|
||||
|
||||
|
||||
def test_write_to_hdf(self):
|
||||
"""Test the functionality of writing to HDF."""
|
||||
import tables
|
||||
# do not change the values here, or you need to recreate the h5dump with new values
|
||||
x = np.array([0.0, 1.0])
|
||||
y = [np.array([10, 20]), np.array([15, 25])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id = 10
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
# write out data
|
||||
hdffile = tables.open_file("test.hdf5", mode="w")
|
||||
adc.write_to_hdf(hdffile, where="/", name="name", title="title", complib="zlib", complevel=3)
|
||||
hdffile.close()
|
||||
# read back data with h5dump utility (apt-get -y install hdf5-tools)
|
||||
h5dump = subprocess.run(["h5dump", "-d", "/name/adc_data", "test.hdf5"], capture_output=True)
|
||||
content = h5dump.stdout.decode("utf-8")
|
||||
test_dir = os.path.dirname(__file__)
|
||||
with open(os.path.join(test_dir, "h5dump1_adc_data.ascii"), "r") as f:
|
||||
expected = f.read()
|
||||
self.assertEqual(content, expected)
|
||||
os.unlink("test.hdf5")
|
||||
|
||||
def test_operator_len(self):
|
||||
"""Test the functionality of __len__"""
|
||||
adc = self.create_adc_result()
|
||||
self.assertEqual(len(adc), 3)
|
||||
|
||||
def test_operator_add_scalar(self):
|
||||
"""
|
||||
Test the functionality of __add__ and __radd__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
# test integer addition
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i]+10, y[i] + 10, strict=True)
|
||||
adc += 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] + 10, strict=True)
|
||||
|
||||
# test float addition
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i] + 10., y[i] + 10., strict=True)
|
||||
adc += 10.
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] + 10 + 10., strict=True)
|
||||
|
||||
def test_operator_sub_scalar(self):
|
||||
"""
|
||||
Test the functionality of __sub__ and __rsub__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
# test integer subtraction
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i] - 10, y[i] - 10, strict=True)
|
||||
adc -= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] - 10, strict=True)
|
||||
|
||||
# test float subtraction
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i] - 10., y[i] - 10., strict=True)
|
||||
adc -= 10.
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] - 10 - 10., strict=True)
|
||||
|
||||
def test_operator_mul_scalar(self):
|
||||
"""
|
||||
Test the functionality of __mul and __rmul__
|
||||
"""
|
||||
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
# test integer multiplication
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i] * 10, y[i] * 10, strict=True)
|
||||
adc *= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] * 10, strict=True)
|
||||
|
||||
# test float multiplication
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i] * 10.0, y[i] * 10.0, strict=True)
|
||||
adc *= 10.0
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] * 10 * 10.0, strict=True)
|
||||
|
||||
def test_operator_truediv_scalar(self):
|
||||
"""
|
||||
Test the functionality of __div__ and __rdiv__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i]/10, y[i] / 10, strict=True)
|
||||
adc /= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] / 10, strict=True)
|
||||
|
||||
def test_operator_floordiv_scalar(self):
|
||||
"""
|
||||
Test the functionality of __div__ and __rdiv__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i]//10, y[i] // 10, strict=True)
|
||||
adc //= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] // 10, strict=True)
|
||||
self.assertRaises(ValueError, adc.__floordiv__, 1.0)
|
||||
self.assertRaises(ValueError, adc.__rfloordiv__, 1.0)
|
||||
|
||||
def test_operator_overload_adc_result_exception(self):
|
||||
"""
|
||||
Test the ValueError raised when ADC_Result is used as an operand in an arithmetic operation
|
||||
"""
|
||||
adc1 = self.create_adc_result()
|
||||
adc2 = self.create_adc_result()
|
||||
self.assertRaises(ValueError, adc1.__add__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__radd__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__sub__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__rsub__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__mul__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__rmul__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__truediv__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__rtruediv__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__floordiv__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__rfloordiv__, adc2)
|
||||
self.assertRaises(ValueError, adc1.__pow__, adc2)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,286 +0,0 @@
|
||||
import os
|
||||
import subprocess
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
import numpy as np
|
||||
|
||||
from src.data.ADC_Result import ADC_Result
|
||||
from src.data.Accumulation import Accumulation
|
||||
|
||||
|
||||
class TestAccumulation(unittest.TestCase):
|
||||
@staticmethod
|
||||
def create_adc_result():
|
||||
x = np.array([0.0, 1.0, 2.0])
|
||||
y = [np.array([10, 20, 30]), np.array([15, 25, 35])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id = 11
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
adc.set_nChannels(2)
|
||||
return adc
|
||||
|
||||
def test_constructor_with_no_arguments(self):
|
||||
"""Test initializer with no arguments."""
|
||||
accu = Accumulation()
|
||||
self.assertFalse(accu.contains_data())
|
||||
self.assertEqual(accu.sampling_rate, 0)
|
||||
self.assertEqual(len(accu.index), 0)
|
||||
self.assertEqual(len(accu.x), 0)
|
||||
self.assertEqual(len(accu.y), 0)
|
||||
|
||||
def test_constructor_with_arguments(self):
|
||||
"""Test initializer with all arguments."""
|
||||
x = np.array([0.0, 1.0, 2.0])
|
||||
y = [np.array([10, 20, 30]), np.array([15, 25, 35])]
|
||||
n = 1
|
||||
index = [(0, 2)]
|
||||
sampl_freq = 1000.0
|
||||
|
||||
accu = Accumulation(x=x,
|
||||
y=y,
|
||||
y_2=None,
|
||||
index=index,
|
||||
sampl_freq=sampl_freq,
|
||||
n=n)
|
||||
|
||||
self.assertTrue(accu.contains_data())
|
||||
self.assertEqual(accu.sampling_rate, sampl_freq)
|
||||
self.assertEqual(accu.x.all(), x.all())
|
||||
self.assertEqual(accu.y[0].all(), y[0].all())
|
||||
# TODO make testing strict with array types!
|
||||
np.testing.assert_array_equal(accu.x, np.array(x, dtype="float32"), strict=False)
|
||||
np.testing.assert_array_equal(accu.y[0], np.array(y[0], dtype="int16"), strict=False)
|
||||
np.testing.assert_array_equal(accu.y[1], np.array(y[1], dtype="int16"), strict=False)
|
||||
|
||||
def test_add_adc_result(self):
|
||||
"""Test adding sample space."""
|
||||
adc = self.create_adc_result()
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
self.assertEqual(len(adc.x), len(accu.x))
|
||||
self.assertEqual(len(adc.y[0]), len(accu.y[0]))
|
||||
self.assertEqual(adc.index[-1], accu.index[-1])
|
||||
np.testing.assert_array_equal(accu.x, adc.x)
|
||||
np.testing.assert_array_equal(accu.y[0], adc.y[0])
|
||||
|
||||
def test_add_accumulation(self):
|
||||
"""Test adding sample space."""
|
||||
adc1 = self.create_adc_result()
|
||||
adc1.set_description("common", "somerandomtext")
|
||||
adc1.set_description("same_variable", "somerandomtext_variable1")
|
||||
adc1.set_description("variable1", "somerandomtext_variable1_1")
|
||||
adc2 = self.create_adc_result()
|
||||
adc2.set_description("common", "somerandomtext")
|
||||
adc2.set_description("same_variable", "somerandomtext_variable2")
|
||||
adc2.set_description("variable2", "somerandomtext_variable2_2")
|
||||
accu1 = Accumulation()
|
||||
accu1 += adc1
|
||||
print(accu1.common_descriptions)
|
||||
accu2 = Accumulation()
|
||||
accu2 += adc2
|
||||
accu2 += accu1
|
||||
|
||||
self.assertEqual(len(accu1.x), len(accu2.x))
|
||||
self.assertEqual(len(accu1.y[0]), len(accu2.y[0]))
|
||||
self.assertEqual(accu1.index[-1], accu2.index[-1])
|
||||
self.assertEqual(accu1.common_descriptions["same_variable"], "somerandomtext_variable1")
|
||||
self.assertEqual(accu2.common_descriptions["common"], "somerandomtext")
|
||||
np.testing.assert_array_equal(accu2.x, accu1.x)
|
||||
np.testing.assert_array_equal(accu2.y[0], accu1.y[0]*2)
|
||||
|
||||
def test_get_accu_by_index(self):
|
||||
"""Test retrieving data by index."""
|
||||
accu = Accumulation()
|
||||
adc = self.create_adc_result()
|
||||
adc.index = [(0, 1), (1, 2)]
|
||||
adc.job_id = 42
|
||||
accu += adc
|
||||
sub_result = accu.get_accu_by_index(1)
|
||||
self.assertEqual(sub_result.sampling_rate, 1000.0)
|
||||
self.assertTrue(np.array_equal(sub_result.x, np.array([1.0, 2.0])))
|
||||
self.assertTrue(np.array_equal(sub_result.y[0], np.array([20, 30])))
|
||||
self.assertTrue(np.array_equal(sub_result.y[1], np.array([25, 35])))
|
||||
self.assertEqual(sub_result.index, [(0, 1)])
|
||||
# TODO Fix #13: self.assertEqual(sub_result.common_descriptions, {"key": "value"})
|
||||
|
||||
def test_write_to_csv_without_error(self):
|
||||
"""Test the functionality of writing to CSV."""
|
||||
from io import StringIO
|
||||
x = np.array([0.0, 1.0])
|
||||
y = [np.array([10, 20]), np.array([15, 25])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id=9
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
output = StringIO()
|
||||
|
||||
accu.write_to_csv(output, delimiter=",")
|
||||
content = output.getvalue()
|
||||
expected = (
|
||||
"# accumulation 1\n"
|
||||
"# key : value\n"
|
||||
"# t ch0_mean ch1_mean\n"
|
||||
"0.000000e+00,1.000000e+01,1.500000e+01\n"
|
||||
"1.000000e+00,2.000000e+01,2.500000e+01\n"
|
||||
)
|
||||
self.assertEqual(content, expected)
|
||||
|
||||
def test_write_to_csv_with_error(self):
|
||||
"""Test the functionality of writing to CSV."""
|
||||
from io import StringIO
|
||||
x = np.array([0.0, 1.0])
|
||||
y = [np.array([10, 20]), np.array([15, 25])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id=9
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
accu = Accumulation(error=True)
|
||||
accu += adc
|
||||
output = StringIO()
|
||||
|
||||
accu.write_to_csv(output, delimiter=",")
|
||||
content = output.getvalue()
|
||||
expected = (
|
||||
"# accumulation 1\n"
|
||||
"# key : value\n"
|
||||
"# t ch0_mean ch0_err ch1_mean ch1_err\n"
|
||||
"0.000000e+00,1.000000e+01,0.000000e+00,1.500000e+01,0.000000e+00\n"
|
||||
"1.000000e+00,2.000000e+01,0.000000e+00,2.500000e+01,0.000000e+00\n"
|
||||
)
|
||||
self.assertEqual(content, expected)
|
||||
|
||||
def test_write_to_hdf(self):
|
||||
"""Test the functionality of writing to HDF."""
|
||||
import tables
|
||||
# do not change the values here, or you need to recreate the h5dump with new values
|
||||
x = np.array([0.0, 1.0])
|
||||
y = [np.array([10, 20]), np.array([15, 25])]
|
||||
index = [(0, 1)]
|
||||
job_date = datetime(2023, 1, 1)
|
||||
desc = {"key": "value"}
|
||||
sampl_freq = 1000.0
|
||||
job_id = 10
|
||||
adc = ADC_Result(x, y, index, sampl_freq, desc, job_id, job_date)
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
# write out data
|
||||
hdffile = tables.open_file("testaccu.hdf5", mode="w")
|
||||
accu.write_to_hdf(hdffile, where="/", name="name", title="title", complib="zlib", complevel=3)
|
||||
hdffile.close()
|
||||
# read back data with h5dump utility (apt-get -y install hdf5-tools)
|
||||
h5dump = subprocess.run(["h5dump", "-d", "/name/accu_data", "testaccu.hdf5"], capture_output=True)
|
||||
content = h5dump.stdout.decode("utf-8")
|
||||
# Use path relative to project root
|
||||
test_dir = os.path.dirname(__file__)
|
||||
with open(os.path.join(test_dir, "h5dump1_accu.ascii"), "r") as f:
|
||||
expected = f.read()
|
||||
self.assertEqual(content, expected)
|
||||
os.unlink("testaccu.hdf5")
|
||||
|
||||
def test_operator_len(self):
|
||||
"""Test the functionality of __len__"""
|
||||
adc = self.create_adc_result()
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
self.assertEqual(len(accu), 3)
|
||||
|
||||
def test_operator_add_scalar(self):
|
||||
"""
|
||||
Test the functionality of __add__ and __radd__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
y = adc.y
|
||||
# test integer addition
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i]+10, y[i] + 10)
|
||||
accu += 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] + 10)
|
||||
|
||||
# test float addition
|
||||
accu += 10.
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] + 10 + 10.)
|
||||
|
||||
def test_operator_sub_scalar(self):
|
||||
"""
|
||||
Test the functionality of __sub__ and __rsub__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
|
||||
# test integer subtraction
|
||||
accu -= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] - 10)
|
||||
|
||||
# test float subtraction
|
||||
adc -= 10.
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] - 10 - 10.)
|
||||
|
||||
@unittest.skip("Not implmented")
|
||||
def test_operator_mul_scalar(self):
|
||||
"""
|
||||
Test the functionality of __mul and __rmul__
|
||||
"""
|
||||
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
accu = Accumulation()
|
||||
accu += adc
|
||||
|
||||
# test integer multiplication
|
||||
accu *= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] * 10)
|
||||
|
||||
# test float multiplication
|
||||
accu *= 10.0
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(accu.y[i], y[i] * 10 * 10.0)
|
||||
|
||||
@unittest.skip("Not implmented")
|
||||
def test_operator_truediv_scalar(self):
|
||||
"""
|
||||
Test the functionality of __div__ and __rdiv__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i]/10, y[i] / 10, strict=True)
|
||||
adc /= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] / 10, strict=True)
|
||||
|
||||
@unittest.skip("Not implmented")
|
||||
def test_operator_floordiv_scalar(self):
|
||||
"""
|
||||
Test the functionality of __div__ and __rdiv__
|
||||
"""
|
||||
adc = self.create_adc_result()
|
||||
y = adc.y
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i]//10, y[i] // 10, strict=True)
|
||||
adc //= 10
|
||||
for i in range(adc.get_nChannels()):
|
||||
np.testing.assert_array_equal(adc.y[i], y[i] // 10, strict=True)
|
||||
self.assertRaises(ValueError, adc.__floordiv__, 1.0)
|
||||
self.assertRaises(ValueError, adc.__rfloordiv__, 1.0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user