polyvertex / fitdecode

A FIT file parsing and decoding library written in Python3
MIT License
155 stars 33 forks source link

Help with fitdecode #3

Closed ikerrg closed 5 years ago

ikerrg commented 5 years ago

Hello,

I am trying to use your fitdecode Python module to read a list of FIT files from my Garmin vivosmart 4. I am completely lost on how to use it. I run the code proposed in your help:

import fitdecode
with fitdecode.FitReader("38539156602.fit") as fit:
for frame in fit:
pass

and it runs without errors, but the resulting “frame” object does not have any of the information I am searching (heart rate, etc..). Please, could you give me some hints or examples on how to have access to the different fields inside the file? When I decode the file with the java example inside the SDK, all the values are exported to a CSV file, but I would prefer to do that conversion inside Python and then process the fields to create my own plots. Any help or examples you can provide me as a starting point are very welcome.

I wanted to give fitdecode a try because it is newer that fitparse, but to be fair, I got fitparse to return some meaningful information very easily with a couple of commands:

from fitparse import FitFile
fitfile = FitFile("38539156602.fit")
print (fitfile.messages[23].fields[1].name,':', fitfile.messages[23].fields[1].value, fitfile.messages[23].fields[1].units)

which returns: heart_rate : 55 bpm

There is a list of messages under FitFile.messages with all the .fit file contents. Now it is just a matter of programming, sorting and plotting the data that I want. I could not find that list in fitdecode under any of the objects.

Kind regards.

polyvertex commented 5 years ago

fitdecode: feature-wise, fitdecode is mostly a superset of fitparse, so yes, you do get access to all the data contained in the FIT file by using fitdecode. That includes data that I was missing with fitparse like the ones from the hr message, as well as the FIT header, CRC, ...

FIT: bear in mind FIT is a rather over-engineered format. Anything you might find obscure or complex in either library may actually find its roots in how FIT has been designed. This is why I can only encourage you to learn more about it if you really want to read your FIT files yourself, since the data you are seeking may not be located at the same place depending on the FIT activity recording device, and the FIT version.

Having said that if you do feel more comfortable with fitparse and if it suits your needs, using - or trying to use - fitdecode just because it is "newer" may not be the right factor of choice for you?

Also, I do not have any example ready for you unfortunately since FIT is a quite broad format with many uses, but as described in fitdecode's documentation, fitdecode.FitReader yields objects that can be of different types. fitdecode.FitDataMessage is the one that is of interest to you. The documentation exposes the fields member that points you to the data you want, but you would better use the has_field, get_field, get_fields, get_value and get_values methods in order to access them.

If you need to print the content of your FIT to know where to find the desired data, use the fittxt utility script provided with fitdecode, which can be helpful.

import fitdecode

# fitparse-style (you will get non-FitDataMessage frames as well)
messages = tuple(fitdecode.FitReader(src_file))

# or, more efficiently, easier on memory, and Pythonic
with fitdecode.FitReader(src_file) as fit:
    for frame in fit:
        if isinstance(frame, fitdecode.FitDataMessage):
            print(frame.name)
            # frame.get_field()
            # print(frame.fields[0].name)
ikerrg commented 5 years ago

Thanks a lot, polyvertex. That gives me a baseline to start investigating. This is going to require some time and effort, I think!

Best regards.