Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ktk.write_c3d generates an error if file doesn't end by .c3d #233

Open
felixchenier opened this issue Mar 8, 2024 · 5 comments
Open

ktk.write_c3d generates an error if file doesn't end by .c3d #233

felixchenier opened this issue Mar 8, 2024 · 5 comments
Assignees
Labels
bug Something isn't working standby Waiting for some external action
Milestone

Comments

@felixchenier
Copy link
Collaborator

Initially discussed in #229

Describe the bug

import kineticstoolkit as ktk
ts = ktk.read_c3d("data/kinematics_racing_full.c3d")
ktk.write_c3d("test_file", points=ts["Points"])

generates a correct file, but with this error message.

File "~/miniconda3/envs/ktk_env/lib/python3.12/site-packages/ezc3d/ezc3d.py", line 1874, in __init__
    _ezc3d.c3d_swiginit(self, _ezc3d.new_c3d(*args))
                              ^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Could not open the c3d file: unspecified iostream_category error

Python version
3.12

Waiting for @Alek050 to know this information:

Python installation
Select: python.org, Anaconda, conda-forge, etc.

Operating system

  • OS: [e.g. macOS, Windows, Linux]
  • Version [e.g. Big Sur, 10]
@felixchenier felixchenier added the bug Something isn't working label Mar 8, 2024
@felixchenier felixchenier added this to the 0.15 milestone Mar 8, 2024
@Alek050
Copy link
Contributor

Alek050 commented Mar 8, 2024

Hi @felixchenier ,

Thanks for looking into this. As a matter of fact, I am on MacOs (Sonoma 14.2, with M1 chip). I looked into the ezc3d issue you referred to, but since I am on mac the UTF-8 encoding should not be a problem. Furhtermore, you can see I am saving a file called test_file, which does not have any accents or spaces in it.

Some environment info:

  • Python version: 3.12.2
  • Python installation: conda-forge
  • Operating system: MacOs 14.2
  • ezc3d version: 1.5.9 (conda-forge)
  • kineticstoolkit version: the VERSION file states 0.13.master, but I am up to date with the current master branch which should be 0.14 I think.

For sake of completeness here:
Interstingly, I do not get an error when using the example provided by ezc3d:

import numpy as np

import ezc3d

# Load an empty c3d structure
c3d = ezc3d.c3d()

# Adjust some mandatory values of the parameters and fill the data with random values
c3d["parameters"]["POINT"]["RATE"]["value"] = [100]
c3d["parameters"]["POINT"]["LABELS"]["value"] = ("point1", "point2", "point3", "point4", "point5")
c3d["data"]["points"] = np.random.rand(3, 5, 100)

c3d["parameters"]["ANALOG"]["RATE"]["value"] = [1000]
c3d["parameters"]["ANALOG"]["LABELS"]["value"] = ("analog1", "analog2", "analog3", "analog4", "analog5", "analog6")
c3d["data"]["analogs"] = np.random.rand(1, 6, 1000)

# Create a custom parameter to the POINT group
c3d.add_parameter("POINT", "NewParam", [1, 2, 3])

# Create a custom parameter a new group
c3d.add_parameter("NewGroup", "NewParam", ["MyParam1", "MyParam2"])

# Write a new modified C3D and read back the data
c3d.write("temporary.c3d")

Similarly, the example of ktk also seems to work (even when reading in only the analogs data):

import kineticstoolkit.lab as ktk
import numpy as np

points = ktk.TimeSeries()
points.time = np.linspace(0, 10, 10*240, endpoint=False)
points.data["Marker1"] = np.ones((2400, 4))
points.data["Marker2"] = np.ones((2400, 4))

analogs = ktk.TimeSeries()
analogs.time = np.linspace(0, 10, 10*2400, endpoint=False)
analogs.data["Signal1"] = np.sin(analogs.time)
analogs.data["Signal2"] = np.cos(analogs.time)

rotations = ktk.TimeSeries()
rotations.time = np.linspace(0, 10, 10*480, endpoint=False)
rotations.data["Rotation1"] = np.ones((4800, 4, 4))
rotations.data["Rotation2"] = np.ones((4800, 4, 4))

ktk.write_c3d("testfile.c3d", rotations=rotations)
ktk.write_c3d("testfile.c3d", points=points)
ktk.write_c3d("testfile.c3d", analogs=analogs)
ktk.write_c3d("testfile.c3d", points=points, analogs=analogs, rotations=rotations)

I am not really sure why it does not work since type(ts["Points"]) and type(points)) both give are <class 'kineticstoolkit.timeseries.TimeSeries'>. (ts as defined in the first code block of the comment of @felixchenier and points as defined in my comment above in the last code block)

@felixchenier felixchenier changed the title Possible that ktk.write_c3d generates an error on Windows in a folder with accented characters ktk.write_c3d sometimes generates an error - ezc3d issue? Mar 8, 2024
@felixchenier
Copy link
Collaborator Author

felixchenier commented Mar 8, 2024

As an additional information, I am quite sure this is generated not when writing the C3D, but when we read it again to add the events, something that has been done as a workaround for pyomeca/ezc3d#263

This has not been reproduced by @pariterre and I forgot about it. I'll revisit it when I'll investigate this issue more deeply.

@felixchenier felixchenier self-assigned this Mar 8, 2024
@felixchenier
Copy link
Collaborator Author

Hi @Alek050

OK I think I got it :-)

In your original bug description:

import kineticstoolkit as ktk
ts = ktk.read_c3d("data/kinematics_racing_full.c3d")
ktk.write_c3d("test_file", points=ts["Points"])

You didn't put ".c3d" in the file name. If you do, it's ok.

Let's simply add

    if not filename.lower().endswith(".c3d"):
        raise ValueError("filename must end with .c3d")

just after the first check_param calls.

@felixchenier felixchenier changed the title ktk.write_c3d sometimes generates an error - ezc3d issue? ktk.write_c3d generates an error if file doesn't end by .c3d Mar 8, 2024
@Alek050
Copy link
Contributor

Alek050 commented Mar 8, 2024

Works as a charm, sorry for the inconvenience, will add it in the pull request as well.

@felixchenier
Copy link
Collaborator Author

Done in branch PR232

@felixchenier felixchenier added the standby Waiting for some external action label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working standby Waiting for some external action
Projects
None yet
Development

No branches or pull requests

2 participants