Example #1
0
File: base.py Project: hainm/pytraj
def worker_by_state(rank, n_cores=1, traj=None, lines=None, dtype='dict'):
    '''worker for CpptrajState
    '''
    # need to make a copy if lines since python's list is dangerous
    # it's easy to mess up with mutable list
    # do not use lines.copy() since this is not available in py2.7
    if lines is None:
        lines = []
    my_lines = [line for line in lines]
    from pytraj.utils import split_range
    from pytraj.core.c_core import _load_batch

    mylist = split_range(n_cores, 0, traj.n_frames)[rank]
    start, stop = mylist
    crdframes_string = 'crdframes ' + ','.join((str(start + 1), str(stop)))

    for idx, line in enumerate(my_lines):
        if not line.lstrip().startswith('reference'):
            my_lines[idx] = ' '.join(
                ('crdaction traj', line, crdframes_string))

    # do not use 'extend' in this case
    # will get weird output
    my_lines = [
        'loadtraj name traj',
    ] + my_lines

    state = _load_batch(my_lines, traj)

    state.run()
    if dtype == 'dict':
        # exclude DatasetTopology and TrajectoryCpptraj
        return (rank, state.data[2:].to_dict())
    else:
        raise ValueError('must use dtype="dict"')
Example #2
0
def load_cpptraj_state(txt, traj=None):
    """load text to CpptrajState

    Examples
    --------
    >>> # load from string
    >>> import pytraj as pt
    >>> state  = pt.datafiles.load_cpptraj_state('''
    ...                        parm data/tz2.ortho.parm7
    ...                        trajin data/tz2.ortho.nc
    ...                        autoimage
    ...                        center origin
    ...                        rms''')
    >>> state.run()
    CpptrajState, include:
    <datasetlist: 2 datasets>

    >>> # load from string with given TrajectoryIterator
    >>> traj = pt.datafiles.load_tz2_ortho()
    >>> state  = pt.datafiles.load_cpptraj_state('''
    ...                        autoimage
    ...                        center origin
    ...                        rms''', traj=traj)
    >>> state.run()
    CpptrajState, include:
    <datasetlist: 2 datasets>
    """
    from pytraj.core.c_core import _load_batch
    return _load_batch(txt, traj=traj)
Example #3
0
def load_cpptraj_state(txt, traj=None):
    """load text to CpptrajState

    Examples
    --------
    >>> # load from string
    >>> import pytraj as pt
    >>> state  = pt.datafiles.load_cpptraj_state('''
    ...                        parm data/tz2.ortho.parm7
    ...                        trajin data/tz2.ortho.nc
    ...                        autoimage
    ...                        center origin
    ...                        rms''')
    >>> state.run()
    CpptrajState, include:
    <datasetlist: 2 datasets>

    >>> # load from string with given TrajectoryIterator
    >>> traj = pt.datafiles.load_tz2_ortho()
    >>> state  = pt.datafiles.load_cpptraj_state('''
    ...                        autoimage
    ...                        center origin
    ...                        rms''', traj=traj)
    >>> state.run()
    CpptrajState, include:
    <datasetlist: 2 datasets>
    """
    if os.path.exists(txt):
        # txt is a file
        return load_cpptraj_file(txt)
    else:
        from pytraj.core.c_core import _load_batch
        return _load_batch(txt, traj=traj)
Example #4
0
def worker_state(rank, n_cores=1, traj=None, lines=None, dtype='dict'):
    '''worker for CpptrajState
    '''
    # need to make a copy if lines since python's list is dangerous
    # it's easy to mess up with mutable list
    # do not use lines.copy() since this is not available in py2.7
    if lines is None:
        lines = []
    my_lines = [line for line in lines]
    from pytraj.utils import split_range
    from pytraj.core.c_core import _load_batch

    mylist = split_range(n_cores, 0, traj.n_frames)[rank]
    start, stop = mylist
    crdframes_string = 'crdframes ' + ','.join((str(start + 1), str(stop)))

    for idx, line in enumerate(my_lines):
        if not line.lstrip().startswith('reference'):
            my_lines[idx] = ' '.join(('crdaction traj', line, crdframes_string
                                      ))

    # do not use 'extend' in this case
    # will get weird output
    my_lines = ['loadtraj name traj', ] + my_lines

    state = _load_batch(my_lines, traj)

    state.run()
    if dtype == 'dict':
        # exclude DatasetTopology and TrajectoryCpptraj
        return (rank, state.data[2:].to_dict())
    else:
        raise ValueError('must use dtype="dict"')
Example #5
0
def load_cpptraj_output(txt, dtype=None):
    """load output from cpptraj

    Parameters
    ----------
    txt : str
        cpptraj's trajin
    dtype : str, return data type

    Returns
    -------
    if dtype is 'state', return CpptrajState
    if dtype is 'ndarray', return ndarray and so on

    """
    from pytraj.core.c_core import _load_batch
    from pytraj.datasetlist import DatasetList
    from pytraj import ArgList

    commands = list(filter(lambda x: x, txt.split("\n")))

    for idx, line in enumerate(commands):
        if 'parm' in line:
            arglist = ArgList(line)
            # use absolute path
            fname = os.path.abspath(arglist.get_string_key('parm'))
            commands[idx] = " ".join(('parm', fname))

        if 'trajin' in line:
            arglist = ArgList(line)
            # use absolute path
            relative_fname = arglist.get_string_key('trajin')
            the_rest_of_line = ' '.join(line.split(relative_fname)[1:])
            fname = os.path.abspath(relative_fname)
            commands[idx] = " ".join(('trajin', fname, the_rest_of_line))

    txt = "\n".join([line for line in commands])
    state = _load_batch(txt, traj=None)

    state.run()

    if dtype == 'state':
        out = state
    else:
        out = DatasetList(state.datasetlist)
    return out