def test_connecting_and_iterate_inputs(self): m = osim.Model() b = osim.Body('b1', 2.0, osim.Vec3(1, 0, 0), osim.Inertia(1)) j = osim.PinJoint('pin', m.getGround(), b) # Source. source = osim.TableSource() source.setName("source") table = osim.TimeSeriesTable() table.setColumnLabels(('col1', 'col2', 'col3', 'col4')) row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.0, row) row = osim.RowVector([2, 3, 4, 5]) table.appendRow(1.0, row) source.setTable(table) # Reporter. rep = osim.ConsoleReporter() rep.setName("rep") m.addBody(b) m.addJoint(j) m.addComponent(source) m.addComponent(rep) # Connect. # There are multiple ways to perform the connection, especially # for reporters. coord = j.get_coordinates(0) rep.updInput('inputs').connect(coord.getOutput('value')) rep.connectInput_inputs(coord.getOutput('speed'), 'spd') rep.connectInput_inputs(source.getOutput('column').getChannel('col1')) rep.addToReport( source.getOutput('column').getChannel('col2'), 'second_col') s = m.initSystem() # Access and iterate through AbstractInputs, using names. expectedLabels = [ '/model/jointset/pin/pin_coord_0|value', 'spd', '/model/source|column:col1', 'second_col' ] i = 0 for name in rep.getInputNames(): # Actually, there is only one input, which we connected to 4 # channels. assert rep.getInput(name).getNumConnectees() == 4 for j in range(4): assert (rep.getInput(name).getLabel(j) == expectedLabels[j]) i += 1 # Access concrete Input. # Input value is column 2 at time 0. assert (osim.InputDouble.safeDownCast(rep.getInput('inputs')).getValue( s, 3) == 2.0)
def test_vectorview_typemaps(self): # Use a TimeSeriesTable to obtain VectorViews. table = osim.TimeSeriesTable() table.setColumnLabels(['a']) table.appendRow(0.0, osim.RowVector([1.5])) table.appendRow(1.0, osim.RowVector([2.5])) column = table.getDependentColumn('a').to_numpy() assert len(column) == 2 assert column[0] == 1.5 assert column[1] == 2.5 row = table.getRowAtIndex(0).to_numpy() assert len(row) == 1 assert row[0] == 1.5 row = table.getRowAtIndex(1).to_numpy() assert len(row) == 1 assert row[0] == 2.5
def test_vector_rowvector(self): print() print('Test transpose RowVector to Vector.') row = osim.RowVector([1, 2, 3, 4]) col = row.transpose() assert (col[0] == row[0] and col[1] == row[1] and col[2] == row[2] and col[3] == row[3]) print('Test transpose Vector to RowVector.') row_copy = col.transpose() assert (row_copy[0] == row[0] and row_copy[1] == row[1] and row_copy[2] == row[2] and row_copy[3] == row[3]) print('Test transpose RowVectorOfVec3 to VectorOfVec3.') row = osim.RowVectorOfVec3([osim.Vec3(1, 2, 3), osim.Vec3(4, 5, 6), osim.Vec3(7, 8, 9)]) col = row.transpose() assert (str(col[0]) == str(row[0]) and str(col[1]) == str(row[1]) and str(col[2]) == str(row[2])) print('Test transpose VectorOfVec3 to RowVectorOfVec3.') row_copy = col.transpose() assert (str(row_copy[0]) == str(row[0]) and str(row_copy[1]) == str(row[1]) and str(row_copy[2]) == str(row[2]))
def test_TimeSeriesTable(self): print table = osim.TimeSeriesTable() table.setColumnLabels(('col1', 'col2', 'col3', 'col4')) assert(table.getColumnLabels() == ('col1', 'col2', 'col3', 'col4')) # Append a row to the table. row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.1, row) assert table.getNumRows() == 1 assert table.getNumColumns() == 4 row0 = table.getRowAtIndex(0) assert (row0[0] == row[0] and row0[1] == row[1] and row0[2] == row[2] and row0[3] == row[3]) # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.2, row) assert table.getNumRows() == 2 assert table.getNumColumns() == 4 row1 = table.getRow(0.2) assert (row1[0] == row[0] and row1[1] == row[1] and row1[2] == row[2] and row1[3] == row[3]) # Append another row to the table with a timestamp # less than the previous one. Exception expected. try: table.appendRow(0.15, row) assert False except RuntimeError: pass
def test_MocoTrajectory(self): time = osim.Vector(3, 0) time.set(0, 0) time.set(1, 0.1) time.set(2, 0.2) st = osim.Matrix(3, 2) ct = osim.Matrix(3, 3) mt = osim.Matrix(3, 1) dt = osim.Matrix(3, 1) p = osim.RowVector(2, 0.0) it = osim.MocoTrajectory(time, ['s0', 's1'], ['c0', 'c1', 'c2'], ['m0'], ['d0'], ['p0', 'p1'], st, ct, mt, dt, p) it.setTime([15, 25, 35]) assert (it.getTime().get(0) == 15) assert (it.getTime().get(1) == 25) assert (it.getTime().get(2) == 35) it.setState('s0', [5, 3, 10]) s0traj = it.getState('s0') assert (s0traj[0] == 5) assert (s0traj[1] == 3) assert (s0traj[2] == 10) it.setState('s1', [2, 6, 1]) s1traj = it.getState('s1') assert (s1traj[0] == 2) assert (s1traj[1] == 6) assert (s1traj[2] == 1) it.setControl('c0', [10, 46, -5]) c0traj = it.getControl('c0') assert (c0traj[0] == 10) assert (c0traj[1] == 46) assert (c0traj[2] == -5) it.setControl('c2', [5, 12, -1]) c2traj = it.getControl('c2') assert (c2traj[0] == 5) assert (c2traj[1] == 12) assert (c2traj[2] == -1) it.setMultiplier('m0', [326, 1, 42]) m0traj = it.getMultiplier('m0') assert (m0traj[0] == 326) assert (m0traj[1] == 1) assert (m0traj[2] == 42) it.setDerivative('d0', [-10, 477, 125]) d0traj = it.getDerivative('d0') assert (d0traj[0] == -10) assert (d0traj[1] == 477) assert (d0traj[2] == 125) it.setParameter('p0', 25) it.setParameter('p1', 30) p = it.getParameters() assert (p[0] == 25) assert (p[1] == 30) p0 = it.getParameter('p0') assert (p0 == 25)
def test_TableSourceReporter(self): m = osim.Model() # Source. source = osim.TableSource() source.setName("source") table = osim.TimeSeriesTable() table.setColumnLabels(('col1', 'col2', 'col3', 'col4')) row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.0, row) row = osim.RowVector([2, 3, 4, 5]) table.appendRow(1.0, row) source.setTable(table) # Reporter. c_rep = osim.ConsoleReporter() c_rep.setName("c_rep") t_rep = osim.TableReporter() t_rep.setName("t_rep") # Add. m.addComponent(source) m.addComponent(c_rep) m.addComponent(t_rep) # Connect. c_rep.updInput("inputs").connect( source.getOutput("column").getChannel("col1")) t_rep.updInput("inputs").connect( source.getOutput("column").getChannel("col2")) # Realize. s = m.initSystem() s.setTime(0.5) m.realizeReport(s) # Test. # This value is the average of col2 (2.0 and 3.0). assert t_rep.getReport().getRowAtIndex(0)[0] == 2.5
def test_DataTable(self): table = osim.DataTable() # Set column labels. table.setColumnLabels(['0', '1', '2', '3']) assert table.getColumnLabels() == ('0', '1', '2', '3') assert table.hasColumn('0') assert table.hasColumn('2') table.setColumnLabel(0, 'zero') table.setColumnLabel(2, 'two') assert table.getColumnLabel(0) == 'zero' assert table.getColumnLabel(2) == 'two' assert table.getColumnIndex('zero') == 0 assert table.getColumnIndex('two') == 2 # Append a row to the table. row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.1, row) assert table.getNumRows() == 1 assert table.getNumColumns() == 4 row0 = table.getRowAtIndex(0) assert (row0[0] == row[0] and row0[1] == row[1] and row0[2] == row[2] and row0[3] == row[3]) # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.2, row) assert table.getNumRows() == 2 assert table.getNumColumns() == 4 row1 = table.getRow(0.2) assert (row1[0] == row[0] and row1[1] == row[1] and row1[2] == row[2] and row1[3] == row[3]) # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.3, row) assert table.getNumRows() == 3 assert table.getNumColumns() == 4 row2 = table.getRow(0.3) assert (row2[0] == row[0] and row2[1] == row[1] and row2[2] == row[2] and row2[3] == row[3]) # Retrieve independent column. assert table.getIndependentColumn() == (0.1, 0.2, 0.3) # Retrieve dependent columns. col1 = table.getDependentColumnAtIndex(1) assert (col1[0] == 2 and col1[1] == 4 and col1[2] == 8) col3 = table.getDependentColumn('3') assert (col3[0] == 4 and col3[1] == 8 and col3[2] == 16) assert table.hasColumn(0) assert table.hasColumn(2)
def to_sto(self, filename, metadata=None): """ Write a sto file from a Analogs3dOsim Parameters ---------- filename : string path of the file to write metadata : dict, optional dict with optional metadata to add in the output file """ filename = Path(filename) # Make sure the directory exists, otherwise create it if not filename.parents[0].is_dir(): filename.parents[0].mkdir() table = osim.TimeSeriesTable() # set metadata table.setColumnLabels(self.get_labels) if metadata: for key, value in metadata.items(): table.addTableMetaDataString(key, str(value)) if 'nColumns' not in metadata: table.addTableMetaDataString('nColumns', str(self.shape[1])) else: table.addTableMetaDataString('nColumns', str(self.shape[1])) table.addTableMetaDataString('nRows', str(self.shape[-1])) time_vector = np.arange(start=0, stop=1 / self.get_rate * self.shape[2], step=1 / self.get_rate) for iframe in range(self.shape[-1]): a = self.get_frame(iframe) row = osim.RowVector(a.ravel().tolist()) table.appendRow(time_vector[iframe], row) adapter = osim.STOFileAdapter() adapter.write(table, str(filename))
def test_DataTable(self): print table = osim.DataTable() # Set column labels. table.setColumnLabels(['0', '1', '2', '3']) assert table.getColumnLabels() == ('0', '1', '2', '3') assert table.hasColumn('0') assert table.hasColumn('2') assert not table.hasColumn('not-found') table.setColumnLabel(0, 'zero') table.setColumnLabel(2, 'two') assert table.getColumnLabel(0) == 'zero' assert table.getColumnLabel(2) == 'two' assert table.getColumnIndex('zero') == 0 assert table.getColumnIndex('two') == 2 # Append a row to the table. row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.1, row) assert table.getNumRows() == 1 assert table.getNumColumns() == 4 row0 = table.getRowAtIndex(0) assert (row0[0] == row[0] and row0[1] == row[1] and row0[2] == row[2] and row0[3] == row[3]) print table # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.2, row) assert table.getNumRows() == 2 assert table.getNumColumns() == 4 row1 = table.getRow(0.2) assert (row1[0] == row[0] and row1[1] == row[1] and row1[2] == row[2] and row1[3] == row[3]) print table # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.3, row) assert table.getNumRows() == 3 assert table.getNumColumns() == 4 row2 = table.getRow(0.3) assert (row2[0] == row[0] and row2[1] == row[1] and row2[2] == row[2] and row2[3] == row[3]) print table # Retrieve independent column. assert table.getIndependentColumn() == (0.1, 0.2, 0.3) # Retrieve dependent columns. col1 = table.getDependentColumnAtIndex(1) assert (col1[0] == 2 and col1[1] == 4 and col1[2] == 8) col3 = table.getDependentColumn('3') assert (col3[0] == 4 and col3[1] == 8 and col3[2] == 16) assert table.hasColumn(0) assert table.hasColumn(2) # Edit rows of the table. row0 = table.getRowAtIndex(0) row0[0] = 10 row0[1] = 10 row0[2] = 10 row0[3] = 10 row0 = table.getRowAtIndex(0) assert (row0[0] == 10 and row0[1] == 10 and row0[2] == 10 and row0[3] == 10) row2 = table.getRow(0.3) row2[0] = 20 row2[1] = 20 row2[2] = 20 row2[3] = 20 row2 = table.getRow(0.3) assert (row2[0] == 20 and row2[1] == 20 and row2[2] == 20 and row2[3] == 20) print table # Edit columns of the table. col1 = table.getDependentColumnAtIndex(1) col1[0] = 30 col1[1] = 30 col1[2] = 30 col1 = table.getDependentColumnAtIndex(1) assert (col1[0] == 30 and col1[1] == 30 and col1[2] == 30) col3 = table.getDependentColumn('3') col3[0] = 40 col3[1] = 40 col3[2] = 40 col3 = table.getDependentColumn('3') assert (col3[0] == 40 and col3[1] == 40 and col3[2] == 40) print table # Add table metadata. table.addTableMetaDataString('subject-name', 'Python') table.addTableMetaDataString('subject-yob', '1991') assert table.getTableMetaDataString('subject-name') == 'Python' assert table.getTableMetaDataString('subject-yob') == '1991' print table # Access eleemnt with index out of bounds. Exception expected. try: shouldThrow = row0[5] assert false except RuntimeError: pass try: shouldThrow = col1[5] assert false except RuntimeError: pass # Access row with index out of bounds. Exception expected. try: shouldThrow = table.getRowAtIndex(5) assert false except RuntimeError: pass # Access row with timestamp that does not exist. Exception expected. try: shouldThrow = table.getRow(5.5) assert false except RuntimeError: pass # Access column with index out of bounds. Exception expected. try: shouldThrow = table.getDependentColumnAtIndex(5) assert false except RuntimeError: pass # Access column with label that does not exist. Exception expected. try: shouldThrow = table.getDependentColumn('not-found') assert false except RuntimeError: pass # Test pack-ing of columns of DataTable. table = osim.DataTable() table.setColumnLabels( ('col0_x', 'col0_y', 'col0_z', 'col1_x', 'col1_y', 'col1_z', 'col2_x', 'col2_y', 'col2_z', 'col3_x', 'col3_y', 'col3_z')) row = osim.RowVector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) table.appendRow(1, row) row = osim.RowVector([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) table.appendRow(2, row) row = osim.RowVector([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]) table.appendRow(3, row) assert len(table.getColumnLabels()) == 12 assert table.getNumRows() == 3 assert table.getNumColumns() == 12 print table tableVec3 = table.packVec3(('_x', '_y', '_z')) tableVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableVec3.getNumRows() == 3 tableVec3.getNumColumns() == 4 print tableVec3 tableFlat = tableVec3.flatten() assert len(tableFlat.getColumnLabels()) == 12 assert tableFlat.getColumnLabel(0) == 'col0_1' assert tableFlat.getColumnLabel(11) == 'col3_3' assert tableFlat.getNumRows() == 3 assert tableFlat.getNumColumns() == 12 print tableFlat tableVec3 = table.packVec3() tableVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableVec3.getNumRows() == 3 tableVec3.getNumColumns() == 4 print tableVec3 tableFlat = tableVec3.flatten() assert len(tableFlat.getColumnLabels()) == 12 assert tableFlat.getColumnLabel(0) == 'col0_1' assert tableFlat.getColumnLabel(11) == 'col3_3' assert tableFlat.getNumRows() == 3 assert tableFlat.getNumColumns() == 12 print tableFlat tableUnitVec3 = table.packUnitVec3() tableUnitVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableUnitVec3.getNumRows() == 3 tableUnitVec3.getNumColumns() == 4 print tableUnitVec3 tableFlat = tableUnitVec3.flatten() assert len(tableFlat.getColumnLabels()) == 12 assert tableFlat.getColumnLabel(0) == 'col0_1' assert tableFlat.getColumnLabel(11) == 'col3_3' assert tableFlat.getNumRows() == 3 assert tableFlat.getNumColumns() == 12 print tableFlat table.setColumnLabels( ('col0.0', 'col0.1', 'col0.2', 'col0.3', 'col1.0', 'col1.1', 'col1.2', 'col1.3', 'col2.0', 'col2.1', 'col2.2', 'col2.3')) tableQuat = table.packQuaternion() tableQuat.getColumnLabels() == ('col0', 'col1', 'col2') tableQuat.getNumRows() == 3 tableQuat.getNumColumns() == 3 print tableQuat tableFlat = tableQuat.flatten() assert len(tableFlat.getColumnLabels()) == 12 assert tableFlat.getColumnLabel(0) == 'col0_1' assert tableFlat.getColumnLabel(11) == 'col2_4' assert tableFlat.getNumRows() == 3 assert tableFlat.getNumColumns() == 12 print tableFlat table.setColumnLabels( ('col0_0', 'col0_1', 'col0_2', 'col0_3', 'col0_4', 'col0_5', 'col1_0', 'col1_1', 'col1_2', 'col1_3', 'col1_4', 'col1_5')) tableSVec = table.packSpatialVec() tableSVec.getColumnLabels() == ('col0', 'col1') tableSVec.getNumRows() == 3 tableSVec.getNumColumns() == 2 print tableSVec tableFlat = tableSVec.flatten() assert len(tableFlat.getColumnLabels()) == 12 assert tableFlat.getColumnLabel(0) == 'col0_1' assert tableFlat.getColumnLabel(11) == 'col1_6' assert tableFlat.getNumRows() == 3 assert tableFlat.getNumColumns() == 12 print tableFlat
def test_TimeSeriesTable(self): print table = osim.TimeSeriesTable() table.setColumnLabels(('col1', 'col2', 'col3', 'col4')) assert (table.getColumnLabels() == ('col1', 'col2', 'col3', 'col4')) # Append a row to the table. row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.1, row) assert table.getNumRows() == 1 assert table.getNumColumns() == 4 row0 = table.getRowAtIndex(0) assert (row0[0] == row[0] and row0[1] == row[1] and row0[2] == row[2] and row0[3] == row[3]) # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.2, row) assert table.getNumRows() == 2 assert table.getNumColumns() == 4 row1 = table.getRow(0.2) assert (row1[0] == row[0] and row1[1] == row[1] and row1[2] == row[2] and row1[3] == row[3]) # Append another row to the table with a timestamp # less than the previous one. Exception expected. try: table.appendRow(0.15, row) assert False except RuntimeError: pass # Test pack-ing of columns of TimeSeriesTable. table = osim.TimeSeriesTable() table.setColumnLabels( ('col0_x', 'col0_y', 'col0_z', 'col1_x', 'col1_y', 'col1_z', 'col2_x', 'col2_y', 'col2_z', 'col3_x', 'col3_y', 'col3_z')) row = osim.RowVector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) table.appendRow(1, row) row = osim.RowVector([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) table.appendRow(2, row) row = osim.RowVector([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]) table.appendRow(3, row) assert len(table.getColumnLabels()) == 12 assert table.getNumRows() == 3 assert table.getNumColumns() == 12 print table avgRow = table.averageRow(1, 3) assert avgRow.ncol() == 12 assert abs(avgRow[0] - 2) < 1e-8 #epsilon assert abs(avgRow[11] - 2) < 1e-8 #epsilon nearRow = table.getNearestRow(1.1) assert nearRow.ncol() == 12 assert nearRow[0] == 1 assert nearRow[11] == 1 tableVec3 = table.packVec3(('_x', '_y', '_z')) tableVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableVec3.getNumRows() == 3 tableVec3.getNumColumns() == 4 print tableVec3 tableVec3 = table.packVec3() tableVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableVec3.getNumRows() == 3 tableVec3.getNumColumns() == 4 print tableVec3 avgRow = tableVec3.averageRow(1, 2) assert avgRow.ncol() == 4 assert abs(avgRow[0][0] - 1.5) < 1e-8 #epsilon assert abs(avgRow[3][2] - 1.5) < 1e-8 #epsilon nearRow = tableVec3.getNearestRow(1.1) assert nearRow.ncol() == 4 assert nearRow[0][0] == 1 assert nearRow[3][2] == 1 tableUnitVec3 = table.packUnitVec3() tableUnitVec3.getColumnLabels() == ('col0', 'col1', 'col2', 'col3') tableUnitVec3.getNumRows() == 3 tableUnitVec3.getNumColumns() == 4 print tableUnitVec3 table.setColumnLabels( ('col0.0', 'col0.1', 'col0.2', 'col0.3', 'col1.0', 'col1.1', 'col1.2', 'col1.3', 'col2.0', 'col2.1', 'col2.2', 'col2.3')) tableQuat = table.packQuaternion() tableQuat.getColumnLabels() == ('col0', 'col1', 'col2') tableQuat.getNumRows() == 3 tableQuat.getNumColumns() == 3 print tableQuat table.setColumnLabels( ('col0_0', 'col0_1', 'col0_2', 'col0_3', 'col0_4', 'col0_5', 'col1_0', 'col1_1', 'col1_2', 'col1_3', 'col1_4', 'col1_5')) tableSVec = table.packSpatialVec() tableSVec.getColumnLabels() == ('col0', 'col1') tableSVec.getNumRows() == 3 tableSVec.getNumColumns() == 2 print tableSVec
def test_DataTable(self): print table = osim.DataTable() # Set column labels. table.setColumnLabels(['0', '1', '2', '3']) assert table.getColumnLabels() == ('0', '1', '2', '3') assert table.hasColumn('0') assert table.hasColumn('2') assert not table.hasColumn('not-found') table.setColumnLabel(0, 'zero') table.setColumnLabel(2, 'two') assert table.getColumnLabel(0) == 'zero' assert table.getColumnLabel(2) == 'two' assert table.getColumnIndex('zero') == 0 assert table.getColumnIndex('two') == 2 # Append a row to the table. row = osim.RowVector([1, 2, 3, 4]) table.appendRow(0.1, row) assert table.getNumRows() == 1 assert table.getNumColumns() == 4 row0 = table.getRowAtIndex(0) assert (row0[0] == row[0] and row0[1] == row[1] and row0[2] == row[2] and row0[3] == row[3]) print table # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.2, row) assert table.getNumRows() == 2 assert table.getNumColumns() == 4 row1 = table.getRow(0.2) assert (row1[0] == row[0] and row1[1] == row[1] and row1[2] == row[2] and row1[3] == row[3]) print table # Append another row to the table. row[0] *= 2 row[1] *= 2 row[2] *= 2 row[3] *= 2 table.appendRow(0.3, row) assert table.getNumRows() == 3 assert table.getNumColumns() == 4 row2 = table.getRow(0.3) assert (row2[0] == row[0] and row2[1] == row[1] and row2[2] == row[2] and row2[3] == row[3]) print table # Retrieve independent column. assert table.getIndependentColumn() == (0.1, 0.2, 0.3) # Retrieve dependent columns. col1 = table.getDependentColumnAtIndex(1) assert (col1[0] == 2 and col1[1] == 4 and col1[2] == 8) col3 = table.getDependentColumn('3') assert (col3[0] == 4 and col3[1] == 8 and col3[2] == 16) assert table.hasColumn(0) assert table.hasColumn(2) # Edit rows of the table. row0 = table.getRowAtIndex(0) row0[0] = 10 row0[1] = 10 row0[2] = 10 row0[3] = 10 row0 = table.getRowAtIndex(0) assert (row0[0] == 10 and row0[1] == 10 and row0[2] == 10 and row0[3] == 10) row2 = table.getRow(0.3) row2[0] = 20 row2[1] = 20 row2[2] = 20 row2[3] = 20 row2 = table.getRow(0.3) assert (row2[0] == 20 and row2[1] == 20 and row2[2] == 20 and row2[3] == 20) print table # Edit columns of the table. col1 = table.getDependentColumnAtIndex(1) col1[0] = 30 col1[1] = 30 col1[2] = 30 col1 = table.getDependentColumnAtIndex(1) assert (col1[0] == 30 and col1[1] == 30 and col1[2] == 30) col3 = table.getDependentColumn('3') col3[0] = 40 col3[1] = 40 col3[2] = 40 col3 = table.getDependentColumn('3') assert (col3[0] == 40 and col3[1] == 40 and col3[2] == 40) print table # Access eleemnt with index out of bounds. Exception expected. try: shouldThrow = row0[5] assert false except RuntimeError: pass try: shouldThrow = col1[5] assert false except RuntimeError: pass # Access row with index out of bounds. Exception expected. try: shouldThrow = table.getRowAtIndex(5) assert false except RuntimeError: pass # Access row with timestamp that does not exist. Exception expected. try: shouldThrow = table.getRow(5.5) assert false except RuntimeError: pass # Access column with index out of bounds. Exception expected. try: shouldThrow = table.getDependentColumnAtIndex(5) assert false except RuntimeError: pass # Access column with label that does not exist. Exception expected. try: shouldThrow = table.getDependentColumn('not-found') assert false except RuntimeError: pass
# %% import os import opensim as osim from utils import read_from_storage #%% subject_dir = os.path.abspath('../') output_dir = os.path.join(subject_dir, 'experimental_data') trc_file = os.path.join(subject_dir, 'experimental_data/task.trc') #%% # load and resample markers at 0.01 df = read_from_storage(trc_file) # create timeseries table table = osim.TimeSeriesTable() table.setColumnLabels(df.columns[1:]) table.addTableMetaDataString('DataRate', '100') table.addTableMetaDataString('Units', 'mm') # append data to table for i in range(df.shape[0]): table.appendRow(df.time.values[i], osim.RowVector(df.iloc[i, 1:].values)) # write table to file osim.TRCFileAdapter.write(table.packVec3(), os.path.join(output_dir, 'task_resampled.trc')) #%%