<commit_before>"""The tests here ensure that the Python and Java/Jython scripting are
consistent with each other. The initial tests here come from
pre-existing Jython scripting code.

For consistency with the rest of the API, we use camel-case for variable names.
This breaks Python PEP 8 convention, but allows us to be consistent within our
own project.

"""

import os

import opensim as osim
#import org.opensim.modeling as osim

def test_makeUlnaHeavy():

    # Get handle to the Arm26 model
    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")
    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy, if values need to be set in the model's state
    # pass along the variable myState  returned by initSystem
    myState = myModel.initSystem()

    # Name the copy for later display in the GUI
    oldName = oldModel.getName()
    myModel.setName(oldName + "_heavier")

    # A scale factor for mass of forarm
    massScale = 3.0

    # Change mass of forarm in the model
    forearm = myModel.getBodySet().get("r_ulna_radius_hand")
    forearm.setMass(forearm.getMass() * massScale)

    # Get full path name of original.old model
    fullPathName = oldModel.getInputFileName()

    # Change the name of the modified model
    newName = 'Arm26_makeUlnaHeavy.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())
    # The *_desired.osim file was created via running this same method through
    # Jython, with minor modifications (e.g., printToXML was changed to print).


def test_alterTendonSlackLength():

    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")

    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy, if values needed to be set in state
    # pass along the variable myState returned by initSystem
    myState = myModel.initSystem()

    # Name the copy for later display in the GUI
    oldName = oldModel.getName()
    myModel.setName(oldName + "_longerTSL")

    # A scale factor for tendonSlackLength of muscles
    tendonSlackLengthScale = 1.5

    # Change TendonSlackLength for all muscles in the model
    for i in range(myModel.getMuscles().getSize()):
    	currentMuscle = myModel.getMuscles().get(i)
    	oldSL = currentMuscle.getTendonSlackLength()
    	currentMuscle.setTendonSlackLength(oldSL * tendonSlackLengthScale)

    #get full path name of original model
    fullPathName = oldModel.getInputFileName()

    #Change pathname to output file name
    newName = 'Arm26_alterTendonSlackLength.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())


def test_strengthenModel():

    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")

    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy
    myModel.initSystem()

    # Name the copy for later showing in GUI
    oldName = oldModel.getName()
    myModel.setName(oldName+"_Stronger")

    # Define a scale factor for MaxIsometricForce of muscles
    scaleFactor = 1.2

    # Apply scale factor to MaxIsometricForce
    for i in range(myModel.getMuscles().getSize()):
    	currentMuscle = myModel.getMuscles().get(i)
        currentMuscle.setMaxIsometricForce(currentMuscle.getMaxIsometricForce()
                * scaleFactor)

    # Save resulting model
    newName = 'Arm26_strengthenModel.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())

def test_StorageToPieceWiseLinearFunction():

    sto = osim.Storage('storage.sto')
    column_name = 'column1'
    scale_factor = 2.5

    time = osim.ArrayDouble()
    sto.getTimeColumn(time)

    state_index = sto.getStateIndex(column_name)

    if type(scale_factor) == float:
        sto.multiplyColumn(state_index, scale_factor)
    elif scale_factor == None:
        pass
    else:
        raise Exception('scale_factor, if specified, must be a float.')
<commit_msg>Update python tests to address error reading file in same directory as tests.<commit_after>"""The tests here ensure that the Python and Java/Jython scripting are
consistent with each other. The initial tests here come from
pre-existing Jython scripting code.

For consistency with the rest of the API, we use camel-case for variable names.
This breaks Python PEP 8 convention, but allows us to be consistent within our
own project.

"""

import inspect
import os

this_file_dir = os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe())))

import opensim as osim
#import org.opensim.modeling as osim

def test_makeUlnaHeavy():

    # Get handle to the Arm26 model
    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")
    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy, if values need to be set in the model's state
    # pass along the variable myState  returned by initSystem
    myState = myModel.initSystem()

    # Name the copy for later display in the GUI
    oldName = oldModel.getName()
    myModel.setName(oldName + "_heavier")

    # A scale factor for mass of forarm
    massScale = 3.0

    # Change mass of forarm in the model
    forearm = myModel.getBodySet().get("r_ulna_radius_hand")
    forearm.setMass(forearm.getMass() * massScale)

    # Get full path name of original.old model
    fullPathName = oldModel.getInputFileName()

    # Change the name of the modified model
    newName = 'Arm26_makeUlnaHeavy.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())
    # The *_desired.osim file was created via running this same method through
    # Jython, with minor modifications (e.g., printToXML was changed to print).


def test_alterTendonSlackLength():

    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")

    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy, if values needed to be set in state
    # pass along the variable myState returned by initSystem
    myState = myModel.initSystem()

    # Name the copy for later display in the GUI
    oldName = oldModel.getName()
    myModel.setName(oldName + "_longerTSL")

    # A scale factor for tendonSlackLength of muscles
    tendonSlackLengthScale = 1.5

    # Change TendonSlackLength for all muscles in the model
    for i in range(myModel.getMuscles().getSize()):
    	currentMuscle = myModel.getMuscles().get(i)
    	oldSL = currentMuscle.getTendonSlackLength()
    	currentMuscle.setTendonSlackLength(oldSL * tendonSlackLengthScale)

    #get full path name of original model
    fullPathName = oldModel.getInputFileName()

    #Change pathname to output file name
    newName = 'Arm26_alterTendonSlackLength.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())


def test_strengthenModel():

    oldModel = osim.Model(os.environ['OPENSIM_HOME'] +
            "/Models/Arm26/arm26.osim")

    # Create a fresh copy
    myModel = osim.Model(oldModel)

    # Initialize the copy
    myModel.initSystem()

    # Name the copy for later showing in GUI
    oldName = oldModel.getName()
    myModel.setName(oldName+"_Stronger")

    # Define a scale factor for MaxIsometricForce of muscles
    scaleFactor = 1.2

    # Apply scale factor to MaxIsometricForce
    for i in range(myModel.getMuscles().getSize()):
    	currentMuscle = myModel.getMuscles().get(i)
        currentMuscle.setMaxIsometricForce(currentMuscle.getMaxIsometricForce()
                * scaleFactor)

    # Save resulting model
    newName = 'Arm26_strengthenModel.osim'
    myModel.printToXML(newName)

    # Ensure the files are the same.
    assert (open(newName).readlines() ==
            open(newName.replace('.osim', '_desired.osim')).readlines())

def test_StorageToPieceWiseLinearFunction():

    sto = osim.Storage(os.path.join(this_file_dir, 'storage.sto'))
    column_name = 'column1'
    scale_factor = 2.5

    time = osim.ArrayDouble()
    sto.getTimeColumn(time)

    state_index = sto.getStateIndex(column_name)

    if type(scale_factor) == float:
        sto.multiplyColumn(state_index, scale_factor)
    elif scale_factor == None:
        pass
    else:
        raise Exception('scale_factor, if specified, must be a float.')

{'fraction_non_alphanumeric': 0.09026528776978417, 'fraction_numerical': 0.006744604316546762, 'mean_word_length': 3.310562015503876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1562343', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2701, 'n_words': 786}