Skip to content

Commit cbef049

Browse files
authored
Merge pull request #33 from upb-lea/CorrectionFunctionParameterDescription
Correct evaluation of input variable in functions from_geckocircuits and correct function parameter description
2 parents ffd9d70 + 09c97e3 commit cbef049

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

pysignalscope/scope.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,9 @@ def from_geckocircuits(txt_datafile: str, f0: Optional[float] = None) -> List['C
604604
"""
605605
if not isinstance(txt_datafile, str):
606606
raise TypeError("txt_datafile must be type str to show the full filepath.")
607-
if not isinstance(f0, (float, int)) != f0 is not None:
607+
elif os.path.exists(txt_datafile) is False:
608+
raise ValueError("invalid file name or file does not exist.")
609+
if not isinstance(f0, (float, int)) and f0 is not None:
608610
raise TypeError("f0 must be type float/int/None.")
609611

610612
channel_source = 'GeckoCIRCUITS simulation'
@@ -1226,7 +1228,7 @@ def plot_shiftchannels(channels: List['Channel'], shiftstep_x: Optional[float] =
12261228
def unify_sampling_rate(*channel_datasets: 'Channel', sample_calc_mode: str, sampling_rate: Optional[float] = None,
12271229
shift: Optional[float] = None, mastermode: bool = True) -> list['Channel']:
12281230
"""
1229-
unifies the sampling rate of datasets.
1231+
Unifies the sampling rate of datasets.
12301232
12311233
:Examples:
12321234
@@ -1532,11 +1534,11 @@ def short_to_period(channel: Channel, f0: Union[float, int, None] = None, time_p
15321534
:param start_time: start time in seconds (optional parameter)
15331535
:type start_time: float
15341536
"""
1535-
if not isinstance(f0, (float, int)) != f0 is not None:
1537+
if not isinstance(f0, (float, int)) and f0 is not None:
15361538
raise TypeError("f0 must be type float/int/None.")
1537-
if not isinstance(time_period, (float, int)) != f0 is not None:
1539+
if not isinstance(time_period, (float, int)) and f0 is not None:
15381540
raise TypeError("time_period must be type float/int/None.")
1539-
if not isinstance(start_time, (float, int)) != f0 is not None:
1541+
if not isinstance(start_time, (float, int)) and f0 is not None:
15401542
raise TypeError("start_time must be type float/int/None.")
15411543

15421544
if start_time is None:

tests/test_scope.py

+93-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,96 @@ def test_from_numpy(valid_input_flag: bool, tst_mode, tst_f0, exp_factor_or_erro
222222
scope_object = pss.Scope.from_numpy(period_vector_t_i_invalid, mode=tst_mode, f0=tst_f0,
223223
label=tst_label, unit=tst_unit)
224224

225+
226+
#########################################################################################################
227+
# test of from_geckocircuits
228+
#########################################################################################################
229+
230+
# test frequency
231+
frequency1 = 20e-1
232+
frequency2 = 1
233+
234+
# parameterset for values
235+
@pytest.mark.parametrize("valid_file_name_flag, tst_datafile_name, tst_f0, exp_result_or_error, error_flag", [
236+
# --invalid inputs----
237+
# wrong type for one attribute
238+
(False, "wrong_file_name", None, ValueError, True),
239+
(False, 2, None, TypeError, True),
240+
(False, None, None, TypeError, True),
241+
(True, "test_data_gecko", "Freq", TypeError, True),
242+
# --valid inputs----
243+
(True, "test_data_gecko", None, 0, False),
244+
(True, "test_data_gecko", frequency1, 1, False),
245+
(True, "test_data_gecko", frequency2, 2, False)
246+
])
247+
# definition of the testfunction
248+
def test_from_geckocircuits(valid_file_name_flag: bool, tst_datafile_name, tst_f0, exp_result_or_error, error_flag: bool):
249+
"""Test for the method test_from_geckocircuits().
250+
251+
:param valid_file_name_flag: flag to indicate if the file name is valid
252+
:type valid_file_name_flag: bool
253+
:param tst_datafile_name: string, which contains the test file name
254+
:type tst_datafile_name: any
255+
:param tst_f0: test variable for the frequency
256+
:type tst_f0: any
257+
:param exp_result_or_error: result vector index or expected error
258+
:type exp_result_or_error: any
259+
:param error_flag: flag to indicate, if an error or a valid result is expected
260+
:type error_flag: bool
261+
"""
262+
# Define value input
263+
res_label_lst = ['u1', 'u2']
264+
res_source = 'GeckoCIRCUITS simulation'
265+
tst_header = "# t " + res_label_lst[0] + " " + res_label_lst[1] + " "
266+
tst_time = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2]
267+
tst_data0 = [-1, -2.1, -3.2, -4.4, -2.2, -0.2, 0, 2.1, 4.4, 2.1, 0.2]
268+
tst_data1 = [1, 2.1, 3.2, 4.4, 3.2, 0.2, 0, -2.1, -4.4, -2.1, -0.2]
269+
270+
# Check if file name is valid
271+
if valid_file_name_flag is True:
272+
# generate a valid file
273+
with open(tst_datafile_name, "w") as file:
274+
# write the header
275+
file.write(tst_header + "\n")
276+
# write data
277+
for act_time, act_data1, act_data2 in zip(tst_time, tst_data0, tst_data1):
278+
file.write(f"{act_time} {act_data1} {act_data2}\n")
279+
280+
# Check if expected test result is no error
281+
if error_flag is False:
282+
283+
# Resultvector definition
284+
if exp_result_or_error == 0:
285+
resvector = [tst_time, tst_data0, tst_data1]
286+
elif exp_result_or_error == 1:
287+
resvector = [np.linspace(0, 1 / tst_f0, 3), tst_data0[-3:], tst_data1[-3:]]
288+
elif exp_result_or_error == 2:
289+
resvector = [np.linspace(0, 1 / tst_f0, 6), tst_data0[-6:], tst_data1[-6:]]
290+
291+
# Perform command
292+
scope_object_lst = pss.Scope.from_geckocircuits(tst_datafile_name, f0=tst_f0)
293+
294+
# verification of attributes
295+
numpy.testing.assert_equal(scope_object_lst[0].label, res_label_lst[0])
296+
numpy.testing.assert_equal(scope_object_lst[1].label, res_label_lst[1])
297+
numpy.testing.assert_equal(scope_object_lst[0].source, res_source)
298+
numpy.testing.assert_equal(scope_object_lst[1].source, res_source)
299+
300+
# verification of data
301+
np.testing.assert_array_almost_equal(scope_object_lst[0].time, np.array(resvector[0]))
302+
np.testing.assert_array_almost_equal(scope_object_lst[1].time, np.array(resvector[0]))
303+
np.testing.assert_array_almost_equal(scope_object_lst[0].data, np.array(resvector[1]))
304+
np.testing.assert_array_almost_equal(scope_object_lst[1].data, np.array(resvector[2]))
305+
# delete the file
306+
else: # generate_channel raises an error
307+
with pytest.raises(exp_result_or_error):
308+
scope_object_lst = pss.Scope.from_geckocircuits(tst_datafile_name, f0=tst_f0)
309+
310+
# delete the file if exists
311+
if valid_file_name_flag is True:
312+
if os.path.exists(tst_datafile_name):
313+
os.remove(tst_datafile_name)
314+
225315
#########################################################################################################
226316
# test of low_pass_filter
227317
#########################################################################################################
@@ -292,8 +382,10 @@ def test_low_pass_filter(tst_vector, tst_order, tst_angular_freq, exp_result_or_
292382
# channel values
293383
test_vec1 = pss.Scope.generate_channel([0, 1, 2, 3, 4, 5, 6], [1, 4, 2, 3, 7, 3, 2])
294384

385+
# Remark: findiff v0.12.0 works with "test_res1 = [6, 1, 0, 2, 0, -2, 0]".
386+
# Bugfix in v0.12.1 leads to update of the result vector test_res1
295387
# result values
296-
test_res1 = [6, 1, 0, 2, 0, -2, 0]
388+
test_res1 = [5.5, 0.5, -0.5, 2.5, 0.0, -2.5, 0.5]
297389

298390
# parameterset for values
299391
@pytest.mark.parametrize("tst_vector, tst_order, exp_result_or_error, error_flag", [

0 commit comments

Comments
 (0)