<commit_before>"""
Module for reading Gait2392's virtual markerset
"""

import os
import opensim
import numpy as np

from gias2.musculoskeletal.osim import Marker

SELF_DIR = os.path.split(os.path.realpath(__file__))[0]
MARKERSET_PATH = str(os.path.join(SELF_DIR, 'data', 'gait2392_Scale_MarkerSet.xml'))
MARKER_OFFSET_PATH = str(os.path.join(SELF_DIR, 'data/', 'marker_offsets.dat'))

# dictionary mapping fieldwork landmark names to gait2392's virtual marker
# names
marker_name_map = {
    'pelvis-RASIS': 'R.ASIS',
    'pelvis-LASIS': 'L.ASIS',
    'pelvis-Sacral': 'V.Sacral',
    'femur-LEC-r': 'R.Knee.Lat',
    'femur-MEC-r': 'R.Knee.Med',
    'tibiafibula-LM-r': 'R.Ankle.Lat',
    'tibiafibula-MM-r': 'R.Ankle.Med',
    'femur-LEC-l': 'L.Knee.Lat',
    'femur-MEC-l': 'L.Knee.Med',
    'tibiafibula-LM-l': 'L.Ankle.Lat',
    'tibiafibula-MM-l': 'L.Ankle.Med',
    'pelvis-LHJC': 'L.HJC',
    'pelvis-RHJC': 'R.HJC',
    'femur-HC-l': 'L.FHC',
    'femur-HC-r': 'R.FHC',
    'tibiafibula-KJC-l': 'L.Tib.KJC',
    'tibiafibula-KJC-r': 'R.Tib.KJC',
}

def _load_virtual_markers():
    markers = {}
    marker_coords = {}
    _osim_markerset = opensim.MarkerSet(MARKERSET_PATH)
    for mi in range(_osim_markerset.getSize()):
        osim_marker = _osim_markerset.get(mi)

        # create a copy because the markerset and its marers only exists
        # in the function
        _v = opensim.Vec3()
        osim_marker.getOffset(_v)
        offset = np.array([_v.get(i) for i in range(3)])
        marker = Marker(
                    name=osim_marker.getName(),
                    bodyname=osim_marker.getBodyName(),
                    offset=offset,
                    )
        markers[marker.name] = marker
        marker_coords[marker.name] = marker.offset

    return markers, marker_coords

def _add_synthetic_markers(marker, marker_coords):
    # add hip joint centres to markers (joint coords taken from gait2392)
    markers['L.HJC'] = Marker(
        bodyname='pelvis',
        offset=(-0.0707, -0.0661, -0.0835)
        )
    marker_coords['L.HJC'] = markers['L.HJC'].offset
    markers['R.HJC'] = Marker(
        bodyname='pelvis',
        offset=(-0.0707, -0.0661, 0.0835)
        )
    marker_coords['R.HJC'] = markers['R.HJC'].offset
<commit_msg>alternative markerset loading for opensim4.0<commit_after>"""
Module for reading Gait2392's virtual markerset
"""

import os
import opensim
import numpy as np

from gias2.musculoskeletal.osim import Marker

SELF_DIR = os.path.split(os.path.realpath(__file__))[0]
MARKERSET_PATH = str(os.path.join(SELF_DIR, 'data', 'gait2392_Scale_MarkerSet.xml'))
MARKER_OFFSET_PATH = str(os.path.join(SELF_DIR, 'data/', 'marker_offsets.dat'))
try:
    opensim_version = getattr(opensim, '__version__')
except AttributeError:
    opensim_version = None

# dictionary mapping fieldwork landmark names to gait2392's virtual marker
# names
marker_name_map = {
    'pelvis-RASIS': 'R.ASIS',
    'pelvis-LASIS': 'L.ASIS',
    'pelvis-Sacral': 'V.Sacral',
    'femur-LEC-r': 'R.Knee.Lat',
    'femur-MEC-r': 'R.Knee.Med',
    'tibiafibula-LM-r': 'R.Ankle.Lat',
    'tibiafibula-MM-r': 'R.Ankle.Med',
    'femur-LEC-l': 'L.Knee.Lat',
    'femur-MEC-l': 'L.Knee.Med',
    'tibiafibula-LM-l': 'L.Ankle.Lat',
    'tibiafibula-MM-l': 'L.Ankle.Med',
    'pelvis-LHJC': 'L.HJC',
    'pelvis-RHJC': 'R.HJC',
    'femur-HC-l': 'L.FHC',
    'femur-HC-r': 'R.FHC',
    'tibiafibula-KJC-l': 'L.Tib.KJC',
    'tibiafibula-KJC-r': 'R.Tib.KJC',
}

def _load_virtual_markers():
    markers = {}
    marker_coords = {}

    if opensim_version=='4.0':
        _dummy_model = opensim.Model()
        _osim_markerset = opensim.MarkerSet(_dummy_model, MARKERSET_PATH)
    else:
        _osim_markerset = opensim.MarkerSet(MARKERSET_PATH)

    for mi in range(_osim_markerset.getSize()):
        osim_marker = _osim_markerset.get(mi)

        # create a copy because the markerset and its marers only exists
        # in the function
        _v = opensim.Vec3()
        osim_marker.getOffset(_v)
        offset = np.array([_v.get(i) for i in range(3)])
        marker = Marker(
                    name=osim_marker.getName(),
                    bodyname=osim_marker.getBodyName(),
                    offset=offset,
                    )
        markers[marker.name] = marker
        marker_coords[marker.name] = marker.offset

    return markers, marker_coords

def _add_synthetic_markers(marker, marker_coords):
    # add hip joint centres to markers (joint coords taken from gait2392)
    markers['L.HJC'] = Marker(
        bodyname='pelvis',
        offset=(-0.0707, -0.0661, -0.0835)
        )
    marker_coords['L.HJC'] = markers['L.HJC'].offset
    markers['R.HJC'] = Marker(
        bodyname='pelvis',
        offset=(-0.0707, -0.0661, 0.0835)
        )
    marker_coords['R.HJC'] = markers['R.HJC'].offset

{'fraction_non_alphanumeric': 0.15400495458298927, 'fraction_numerical': 0.021469859620148638, 'mean_word_length': 2.8421887390959557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273740', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1892, 'n_words': 322}