@@ -1421,6 +1421,170 @@ def test___regular_waveform_and_irregular_waveform_list___append___raises_runtim
14211421 assert waveform .timing .sample_interval == dt .timedelta (milliseconds = 1 )
14221422
14231423
1424+ ###############################################################################
1425+ # load data
1426+ ###############################################################################
1427+ def test___empty_ndarray___load_data___clears_data () -> None :
1428+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1429+ array = np .array ([], np .int32 )
1430+
1431+ waveform .load_data (array )
1432+
1433+ assert list (waveform .raw_data ) == []
1434+
1435+
1436+ def test___int32_ndarray___load_data___overwrites_data () -> None :
1437+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1438+ array = np .array ([3 , 4 , 5 ], np .int32 )
1439+
1440+ waveform .load_data (array )
1441+
1442+ assert list (waveform .raw_data ) == [3 , 4 , 5 ]
1443+
1444+
1445+ def test___float64_ndarray___load_data___overwrites_data () -> None :
1446+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .float64 )
1447+ array = np .array ([3 , 4 , 5 ], np .float64 )
1448+
1449+ waveform .load_data (array )
1450+
1451+ assert list (waveform .raw_data ) == [3 , 4 , 5 ]
1452+
1453+
1454+ def test___ndarray_with_mismatched_dtype___load_data___raises_type_error () -> None :
1455+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .float64 )
1456+ array = np .array ([3 , 4 , 5 ], np .int32 )
1457+
1458+ with pytest .raises (TypeError ) as exc :
1459+ waveform .load_data (array ) # type: ignore[arg-type]
1460+
1461+ assert exc .value .args [0 ].startswith (
1462+ "The data type of the input array must match the waveform data type."
1463+ )
1464+
1465+
1466+ def test___ndarray_2d___load_data___raises_value_error () -> None :
1467+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .float64 )
1468+ array = np .array ([[3 , 4 , 5 ], [6 , 7 , 8 ]], np .float64 )
1469+
1470+ with pytest .raises (ValueError ) as exc :
1471+ waveform .load_data (array )
1472+
1473+ assert exc .value .args [0 ].startswith ("The input array must be a one-dimensional array." )
1474+
1475+
1476+ def test___smaller_ndarray___load_data___preserves_capacity () -> None :
1477+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1478+ array = np .array ([3 ], np .int32 )
1479+
1480+ waveform .load_data (array )
1481+
1482+ assert list (waveform .raw_data ) == [3 ]
1483+ assert waveform .capacity == 3
1484+
1485+
1486+ def test___larger_ndarray___load_data___grows_capacity () -> None :
1487+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1488+ array = np .array ([3 , 4 , 5 , 6 ], np .int32 )
1489+
1490+ waveform .load_data (array )
1491+
1492+ assert list (waveform .raw_data ) == [3 , 4 , 5 , 6 ]
1493+ assert waveform .capacity == 4
1494+
1495+
1496+ def test___waveform_with_start_index___load_data___clears_start_index () -> None :
1497+ waveform = AnalogWaveform .from_array_1d (
1498+ np .array ([0 , 1 , 2 ], np .int32 ), np .int32 , copy = False , start_index = 1 , sample_count = 1
1499+ )
1500+ assert waveform ._start_index == 1
1501+ array = np .array ([3 ], np .int32 )
1502+
1503+ waveform .load_data (array )
1504+
1505+ assert list (waveform .raw_data ) == [3 ]
1506+ assert waveform ._start_index == 0
1507+
1508+
1509+ def test___ndarray_subset___load_data___overwrites_data () -> None :
1510+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1511+ array = np .array ([3 , 4 , 5 ], np .int32 )
1512+
1513+ waveform .load_data (array , start_index = 1 , sample_count = 1 )
1514+
1515+ assert list (waveform .raw_data ) == [4 ]
1516+ assert waveform ._start_index == 0
1517+ assert waveform .capacity == 3
1518+
1519+
1520+ def test___smaller_ndarray_no_copy___load_data___takes_ownership_of_array () -> None :
1521+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1522+ array = np .array ([3 ], np .int32 )
1523+
1524+ waveform .load_data (array , copy = False )
1525+
1526+ assert list (waveform .raw_data ) == [3 ]
1527+ assert waveform ._data is array
1528+
1529+
1530+ def test___larger_ndarray_no_copy___load_data___takes_ownership_of_array () -> None :
1531+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1532+ array = np .array ([3 , 4 , 5 , 6 ], np .int32 )
1533+
1534+ waveform .load_data (array , copy = False )
1535+
1536+ assert list (waveform .raw_data ) == [3 , 4 , 5 , 6 ]
1537+ assert waveform ._data is array
1538+
1539+
1540+ def test___ndarray_subset_no_copy___load_data___takes_ownership_of_array_subset () -> None :
1541+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1542+ array = np .array ([3 , 4 , 5 , 6 ], np .int32 )
1543+
1544+ waveform .load_data (array , copy = False , start_index = 1 , sample_count = 2 )
1545+
1546+ assert list (waveform .raw_data ) == [4 , 5 ]
1547+ assert waveform ._data is array
1548+
1549+
1550+ def test___irregular_waveform_and_int32_ndarray_with_timestamps___load_data___overwrites_data_but_not_timestamps () -> (
1551+ None
1552+ ):
1553+ start_time = dt .datetime .now (dt .timezone .utc )
1554+ waveform_offsets = [dt .timedelta (0 ), dt .timedelta (1 ), dt .timedelta (2 )]
1555+ waveform_timestamps = [start_time + offset for offset in waveform_offsets ]
1556+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1557+ waveform .timing = Timing .create_with_irregular_interval (waveform_timestamps )
1558+ array = np .array ([3 , 4 , 5 ], np .int32 )
1559+
1560+ waveform .load_data (array )
1561+
1562+ assert list (waveform .raw_data ) == [3 , 4 , 5 ]
1563+ assert waveform .timing .sample_interval_mode == SampleIntervalMode .IRREGULAR
1564+ assert waveform .timing ._timestamps == waveform_timestamps
1565+
1566+
1567+ def test___irregular_waveform_and_int32_ndarray_with_wrong_sample_count___load_data___raises_value_error_and_does_not_overwrite_data () -> (
1568+ None
1569+ ):
1570+ start_time = dt .datetime .now (dt .timezone .utc )
1571+ waveform_offsets = [dt .timedelta (0 ), dt .timedelta (1 ), dt .timedelta (2 )]
1572+ waveform_timestamps = [start_time + offset for offset in waveform_offsets ]
1573+ waveform = AnalogWaveform .from_array_1d ([0 , 1 , 2 ], np .int32 )
1574+ waveform .timing = Timing .create_with_irregular_interval (waveform_timestamps )
1575+ array = np .array ([3 , 4 ], np .int32 )
1576+
1577+ with pytest .raises (ValueError ) as exc :
1578+ waveform .load_data (array )
1579+
1580+ assert exc .value .args [0 ].startswith (
1581+ "The input array length must be equal to the number of irregular timestamps."
1582+ )
1583+ assert list (waveform .raw_data ) == [0 , 1 , 2 ]
1584+ assert waveform .timing .sample_interval_mode == SampleIntervalMode .IRREGULAR
1585+ assert waveform .timing ._timestamps == waveform_timestamps
1586+
1587+
14241588###############################################################################
14251589# magic methods
14261590###############################################################################
0 commit comments