Last active
October 10, 2024 03:48
-
-
Save bsidhom/d8c11817a1f9411b4b24589e05478cd3 to your computer and use it in GitHub Desktop.
Inspect and strip fields from FIT activity files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This tool operates on CSV files as created (and read) by the FitCSVTool.jar | |
# tool included in the FIT SDK. Input is always read from stdin, and output is | |
# written to stdout. | |
# | |
# Example usage: | |
# java -jar FitCSVTool.jar -b original_activity.fit original_activity.csv | |
# ./fixfit.py detect <original_activity.csv | |
# ./fixfit.py strip --field=accumulated_power --field=avg_power --field=functional_threshold_power --field=max_power --field=normalized_power --field=power --field=power_zone_high_boundary --field=threshold_power --field=time_in_power_zone <original_activity.csv >fixed_activity.csv | |
# java -jar FitCSVTool.jar -c fixed_activity.csv fixed_activity.fit | |
# | |
# This tool is intended to be fully information-preserving _except_ for the | |
# fields that are stripped. That means that output files should include _all_ | |
# original metadata, etc. It is currently a blunt instrument because it does not | |
# attempt to limit stripping to a particular subset of data message _types_, | |
# just field names as they appear in their original definitions. The result is | |
# that, if multiple message type definitions happen to have fields of the same | |
# name, then both such message types will be impacted by this tool. | |
import argparse | |
import csv | |
import itertools | |
import sys | |
def main(): | |
parser = argparse.ArgumentParser("fixfit") | |
subparsers = parser.add_subparsers(dest="command", required=True) | |
detect_fields_parser = subparsers.add_parser( | |
"detect", help="Detect data fields in a FIT file") | |
strip_fields_parser = subparsers.add_parser( | |
"strip", | |
help= | |
"Strip specified data fields from a FIT file. Does NOT remove or modify field definitions." | |
) | |
strip_fields_parser.add_argument( | |
"--field", | |
default=[], | |
action="append", | |
help="Remove this field from the output. May be set multiple times.") | |
args = parser.parse_args() | |
reader = csv.reader(sys.stdin) | |
if args.command == "detect": | |
detect_fields(reader) | |
else: | |
writer = csv.writer(sys.stdout) | |
strip_fields(reader, writer, args.field) | |
sys.stdout.flush() | |
def is_data_message(record): | |
return record[0] == "Data" | |
def detect_fields(reader): | |
fields = set() | |
for record in filter(is_data_message, itertools.islice(reader, 1, None)): | |
for field_idx in range(3, len(record), 3): | |
field = record[field_idx] | |
if len(field) > 0: | |
fields.add(field) | |
for field in sorted(fields): | |
print(field) | |
def strip_fields(reader, writer, fields): | |
def scrub_message(record): | |
record = record[:] | |
for idx in range(3, len(record), 3): | |
field = record[idx] | |
if len(field) > 0 and field in fields: | |
record[idx] = "" | |
record[idx + 1] = "" | |
record[idx + 2] = "" | |
return record | |
for record in reader: | |
if is_data_message(record): | |
writer.writerow(scrub_message(record)) | |
else: | |
writer.writerow(record) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2024 Benjamin Sidhom | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment