|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +''' |
| 6 | + ################################################## |
| 7 | + ## Scale trc coordinates ## |
| 8 | + ################################################## |
| 9 | + |
| 10 | + Scale trc coordinates by a desired factor. |
| 11 | + |
| 12 | + Usage: |
| 13 | + from Pose2Sim.Utilities import trc_scale; trc_scale.trc_scaled_func(r'<input_trc_file>', 0.001, r'<output_trc_file>') |
| 14 | + trc_scale -i input_trc_file -s 0.001 |
| 15 | + trc_scale -i input_trc_file -s 0.001 -o output_trc_file |
| 16 | +''' |
| 17 | + |
| 18 | + |
| 19 | +## INIT |
| 20 | +import pandas as pd |
| 21 | +import numpy as np |
| 22 | +import argparse |
| 23 | + |
| 24 | + |
| 25 | +## AUTHORSHIP INFORMATION |
| 26 | +__author__ = "David Pagnon" |
| 27 | +__copyright__ = "Copyright 2021, Pose2Sim" |
| 28 | +__credits__ = ["David Pagnon"] |
| 29 | +__license__ = "BSD 3-Clause License" |
| 30 | +from importlib.metadata import version |
| 31 | +__version__ = version('pose2sim') |
| 32 | +__maintainer__ = "David Pagnon" |
| 33 | + |
| 34 | +__status__ = "Development" |
| 35 | + |
| 36 | + |
| 37 | +## FUNCTIONS |
| 38 | +def main(): |
| 39 | + parser = argparse.ArgumentParser() |
| 40 | + parser.add_argument('-i', '--input', required = True, help='trc Zup input file') |
| 41 | + parser.add_argument('-o', '--output', required=False, help='trc Yup output file') |
| 42 | + parser.add_argument('-s', '--scale_factor', required=True, type=float, help='scaling factor to apply to the trc coordinates. mm to m would be 0.001') |
| 43 | + args = vars(parser.parse_args()) |
| 44 | + |
| 45 | + trc_scale_func(args) |
| 46 | + |
| 47 | + |
| 48 | +def trc_scale_func(*args): |
| 49 | + ''' |
| 50 | + Scale trc coordinates by a desired factor. |
| 51 | + |
| 52 | + Usage: |
| 53 | + from Pose2Sim.Utilities import trc_scale; trc_scale.trc_scaled_func(r'<input_trc_file>', 0.001, r'<output_trc_file>') |
| 54 | + trc_scale -i input_trc_file -s 0.001 |
| 55 | + trc_scale -i input_trc_file -s 0.001 -o output_trc_file |
| 56 | + ''' |
| 57 | + |
| 58 | + try: |
| 59 | + trc_path = args[0]['input'] # invoked with argparse |
| 60 | + scale_factor = args[0]['scale_factor'] |
| 61 | + if args[0]['output'] == None: |
| 62 | + trc_scaled_path = trc_path.replace('.trc', '_scaled.trc') |
| 63 | + else: |
| 64 | + trc_scaled_path = args[0]['output'] |
| 65 | + except: |
| 66 | + trc_path = args[0] # invoked as a function |
| 67 | + scale_factor = args[1] |
| 68 | + try: |
| 69 | + trc_scaled_path = args[2] |
| 70 | + except: |
| 71 | + trc_scaled_path = trc_path.replace('.trc', '_scaled.trc') |
| 72 | + |
| 73 | + # header |
| 74 | + with open(trc_path, 'r') as trc_file: |
| 75 | + header = [next(trc_file) for line in range(5)] |
| 76 | + |
| 77 | + # data |
| 78 | + trc_df = pd.read_csv(trc_path, sep="\t", skiprows=4) |
| 79 | + frames_col, time_col = trc_df.iloc[:,0], trc_df.iloc[:,1] |
| 80 | + Q_coord = trc_df.drop(trc_df.columns[[0, 1]], axis=1) |
| 81 | + |
| 82 | + # scaling |
| 83 | + Q_scaled = Q_coord * scale_factor |
| 84 | + |
| 85 | + # write file |
| 86 | + with open(trc_scaled_path, 'w') as trc_o: |
| 87 | + [trc_o.write(line) for line in header] |
| 88 | + Q_scaled.insert(0, 'Frame#', frames_col) |
| 89 | + Q_scaled.insert(1, 'Time', time_col) |
| 90 | + Q_scaled.to_csv(trc_o, sep='\t', index=False, header=None, lineterminator='\n') |
| 91 | + |
| 92 | + print(f"trc file scaled with a {scale_factor} factor. Saved to {trc_scaled_path}") |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + main() |
0 commit comments