def test_load_missing(self):
        """
        """
        path = paths.project('does_not_exist.fake')
        mm_end = mock.MagicMock(name='end')
        system.end = mm_end

        configs.load('trial', path)
        mm_end.assert_called_once_with(1)
    def test_load_invalid(self):
        """
        """
        path = paths.project('tests', 'resources', 'invalid.json')
        mm_end = mock.MagicMock(name='end')
        system.end = mm_end

        configs.load('trial', path)
        mm_end.assert_called_once_with(1)
def run(
        settings: typing.Union[str, dict],
        **kwargs
) -> dict:
    """
    Executes a grouped collection of simulation trials and returns the compiled
    results for the individual trials, as well as results calculated for the
    group of trials

    :param settings:
        Settings for running the group of trials. Each trial configuration
        will inherit values from these settings.
    :param kwargs:
        Optional setting overrides to be included in the group configuration
    """

    settings = configs.load('group', settings, **kwargs)
    trials = []

    system.log('[{}]: STARTING'.format(settings['id']))

    for source in fetch_trial_list(settings):
        if isinstance(source, str):
            original = source
            source = os.path.abspath(os.path.join(settings['path'], source))
            if not os.path.exists(source):
                source = '{}.json'.format(source)
            if not os.path.exists(source):
                system.log(
                    """
                    [ERROR]: Unable to locate simulation trial file "{}"
                    """.format(original)
                )
                raise FileNotFoundError('No such file {}'.format(source))

        trial_settings = configs.load('trial', source, inherits=settings)
        simulate_trial.run(trial_settings)
        trials.append(dict(
            settings=trial_settings,
            index=len(trials) + 1,
            id=trial_settings['id'],
        ))

    system.log('[{}]: ANALYZING'.format(settings['id']))

    url = analyze.create(settings, trials)

    system.log('[{}]: COMPLETE'.format(settings['id']))

    return url
def execute_command():
    """

    :return:
    """

    parser = ArgumentParser()

    parser.description = cli.reformat(DESCRIPTION)

    parser.add_argument(
        'generate_command',
        type=str,
        help='The generate command itself'
    )

    parser.add_argument(
        'trial_or_group',
        type=str,
        help='Path to a trial or group file where the data source is specified'
    )

    parser.add_argument(
        'output_filename',
        type=str,
        help='Name of the csv file to be created'
    )

    args = parser.parse_args()
    cli_configs = system.load_configs()

    path = get_path(args.trial_or_group, cli_configs)
    if path is None:
        system.log('ERROR: Invalid or missing trial/group path')
        sys.exit(1)

    settings = configs.load(None, path)
    out_path = os.path.join(settings['directory'], args.output_filename)

    simulate.load_trackway_positions(
        settings,
        save_as=out_path
    )
def run(
        settings: typing.Union[str, dict],
        trackway_positions: trackway.TrackPosition = None,
        **kwargs
) -> dict:
    """
    Runs and analyzes a simulation of the trackway under the conditions
    specified by the arguments and returns a dictionary of results for the
    trial

    :param settings:
        Either a dictionary containing the configuration values for the trial
        or an absolute path to a json format file that contains the
        configuration values for the trial
    :param trackway_positions:
        A TrackwayDefinition instance populated with phase and position values
    """

    settings = configs.load('trial', settings, **kwargs)

    if 'steps_per_cycle' not in settings:
        settings['steps_per_cycle'] = 20
    if 'moving_ambiguity' not in settings:
        # The coefficient of uncertainty while the foot is moving
        settings['moving_ambiguity'] = 0.1
    if 'duty_cycle' not in settings:
        settings['duty_cycle'] = 0.6

    system.log('[{}]: STARTING'.format(settings['id']))

    activity_phases = load_activity_phases(settings)
    trackway_positions = load_trackway_positions(settings, trackway_positions)

    trackway_definition = trackway.TrackwayDefinition(
        trackway_positions,
        activity_phases
    )
    trackway_definition.reorient_positions()

    foot_positions = limb.Property()

    time_steps = list(generate.time_steps_from_data(
        settings['steps_per_cycle'],
        trackway_definition
    ))

    for key in limb.KEYS:
        out = compute.positions_over_time(
            time_steps=time_steps,
            limb_positions=trackway_definition.limb_positions.get(key),
            activity_phase=trackway_definition.activity_phases.get(key),
            settings=settings
        )
        foot_positions.set(key, out)

    prune.invalid_positions(
        settings,
        time_steps,
        foot_positions
    )

    if len(time_steps) < 1:
        system.log(
            """
            [{}]: INVALID RESULTS
                There are no simulated results to analyze. Either the
                simulation is not valid, or you have set a start and end time
                that is not within the range of valid values. Please check your
                settings file.
            """.format(settings['id']))
        raise ValueError('Invalid Results')

    system.log('[{}]: ANALYZING'.format(settings['id']))

    reorientation_needed = prune.unused_foot_prints(
        trackway_definition.limb_positions,
        foot_positions
    )

    if reorientation_needed:
        # Reorient positions again now that the trackway has been pruned
        trackway_definition.reorient_positions(
            *foot_positions.left_pes,
            *foot_positions.right_pes,
            *foot_positions.left_manus,
            *foot_positions.right_manus
        )

    url = analyze.create(
        track_definition=trackway_definition,
        settings=settings,
        time_steps=time_steps,
        foot_positions=foot_positions
    )

    system.log('[{}]: COMPLETED'.format(settings['id']))

    return url