<commit_before>##############################################################################
# MDTraj: A Python Library for Loading, Saving, and Manipulating
#         Molecular Dynamics Trajectories.
# Copyright 2012-2014 Stanford University and the Authors
#
# Authors: <NAME>
# Contributors: <NAME>
#
# MDTraj is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with MDTraj. If not, see <http://www.gnu.org/licenses/>.
##############################################################################

import sys
import tempfile, os
import functools
from mdtraj.utils import enter_temp_directory
from mdtraj.testing import get_fn, eq, assert_raises, SkipTest
import numpy as np
import mdtraj as md
import mdtraj.utils
from mdtraj.utils import six
from mdtraj.utils.six.moves import xrange
from mdtraj.core import element

on_win = (sys.platform == 'win32')
on_py3 = (sys.version_info >= (3, 0))


fn = get_fn('traj.h5')
nat = get_fn('native.pdb')
tmpfns = {}
for suffix, (fd, temp) in {
      'xtc' : tempfile.mkstemp(suffix='.xtc'),
      'dcd' : tempfile.mkstemp(suffix='.dcd'),
      'binpos' : tempfile.mkstemp(suffix='.binpos'),
      'trr' : tempfile.mkstemp(suffix='.trr'),
      'h5' : tempfile.mkstemp(suffix='.h5'),
      'pdb' : tempfile.mkstemp(suffix='.pdb'),
      'pdb.gz' : tempfile.mkstemp(suffix='.pdb.gz'),
      'nc' : tempfile.mkstemp(suffix='.nc'),
      'lh5' : tempfile.mkstemp(suffix='.lh5'),
      'lammpstrj' : tempfile.mkstemp(suffix='.lammpstrj'),
      'xyz' : tempfile.mkstemp(suffix='.xyz')}.items():
    os.close(fd)
    tmpfns[suffix] = temp

def teardown_module(module):
    """remove the temporary file created by tests in this file
    this gets automatically called by nose"""

    for e in tmpfns.values():
        os.unlink(e)

def test_mismatch():
    # loading a 22 atoms xtc with a topology that has 2,000 atoms
    # some kind of error should happen!
    assert_raises(ValueError, lambda: md.load(get_fn('frame0.xtc'), top=get_fn('4K6Q.pdb')))


def test_box():
    t = md.load(get_fn('native.pdb'))
    yield lambda: eq(t.unitcell_vectors, None)
    yield lambda: eq(t.unitcell_lengths, None)
    yield lambda: eq(t.unitcell_angles, None)
    yield lambda: eq(t.unitcell_volumes, None)

    t.unitcell_vectors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).reshape(1, 3, 3)
    yield lambda: eq(np.array([1.0, 1.0, 1.0]), t.unitcell_lengths[0])
    yield lambda: eq(np.array([90.0, 90.0, 90.0]), t.unitcell_angles[0])
    yield lambda: eq(np.array([1.0]), t.unitcell_volumes)


def test_load_pdb_box():
    t = md.load(get_fn('native2.pdb'), no_boxchk=True)
    yield lambda: eq(t.unitcell_lengths[0], np.array([0.1, 0.2, 0.3]))
    yield lambda: eq(t.unitcell_angles[0], np.array([90.0, 90.0, 90.0]))
    yield lambda: eq(t.unitcell_vectors[0], np.array([[0.1, 0, 0], [0, 0.2, 0], [0, 0, 0.3]]))


def test_load_pdb_gz():
    t = md.load(get_fn('1ncw.pdb.gz'))
    yield lambda: eq(t.n_atoms, 3990)


def test_box_load_save():
    t = md.load(get_fn('native2.pdb'), no_boxchk=True)

    # these four tempfile have extensions (dcd, xtc, trr, h5) that
    # should store the box information. lets make sure than through a load/save
    # cycle, the box information is preserved:
    top = md.load_topology(get_fn('native.pdb'), no_boxchk=True)
    for temp_fn in [tmpfns['xtc'], tmpfns['dcd'], tmpfns['trr'], tmpfns['h5']]:
        t.save(temp_fn)
        if temp_fn.endswith('.h5'):
            t2 = md.load(temp_fn)
        else:
            t2 = md.load(temp_fn, top=top)

        assert t.unitcell_vectors is not None
        yield lambda: eq(t.xyz, t2.xyz, decimal=3)
        yield lambda: eq(t.unitcell_vectors, t2.unitcell_vectors)
        yield lambda: eq(t.unitcell_angles, t2.unitcell_angles)
        yield lambda: eq(t.unitcell_lengths, t2.unitcell_lengths)


def test_slice():
    t = md.load(fn)
    yield lambda: eq((t[0:5] + t[5:10]).xyz, t[0:10].xyz)
    yield lambda: eq((t[0:5] + t[5:10]).time, t[0:10].time)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_vectors, t[0:10].unitcell_vectors)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_lengths, t[0:10].unitcell_lengths)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_angles, t[0:10].unitcell_angles)


def test_slice2():
    t = md.load(get_fn('traj.h5'))
    yield lambda: t[0] == t[[0,1]][0]


def test_xtc():
    t = md.load(get_fn('frame0.xtc'), top=nat)
    for e in [tmpfns['xtc'], tmpfns['dcd'], tmpfns['binpos'], tmpfns['trr'], tmpfns['h5'], tmpfns['pdb'], tmpfns['pdb.gz'], tmpfns['nc']]:<commit_msg>Test added for trajectory.slice() with copy=True (in place)<commit_after>##############################################################################
# MDTraj: A Python Library for Loading, Saving, and Manipulating
#         Molecular Dynamics Trajectories.
# Copyright 2012-2014 Stanford University and the Authors
#
# Authors: <NAME>
# Contributors: <NAME>
#
# MDTraj is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with MDTraj. If not, see <http://www.gnu.org/licenses/>.
##############################################################################

import sys
import tempfile, os
import functools
from mdtraj.utils import enter_temp_directory
from mdtraj.testing import get_fn, eq, assert_raises, SkipTest
import numpy as np
import mdtraj as md
import mdtraj.utils
from mdtraj.utils import six
from mdtraj.utils.six.moves import xrange
from mdtraj.core import element

on_win = (sys.platform == 'win32')
on_py3 = (sys.version_info >= (3, 0))


fn = get_fn('traj.h5')
nat = get_fn('native.pdb')
tmpfns = {}
for suffix, (fd, temp) in {
      'xtc' : tempfile.mkstemp(suffix='.xtc'),
      'dcd' : tempfile.mkstemp(suffix='.dcd'),
      'binpos' : tempfile.mkstemp(suffix='.binpos'),
      'trr' : tempfile.mkstemp(suffix='.trr'),
      'h5' : tempfile.mkstemp(suffix='.h5'),
      'pdb' : tempfile.mkstemp(suffix='.pdb'),
      'pdb.gz' : tempfile.mkstemp(suffix='.pdb.gz'),
      'nc' : tempfile.mkstemp(suffix='.nc'),
      'lh5' : tempfile.mkstemp(suffix='.lh5'),
      'lammpstrj' : tempfile.mkstemp(suffix='.lammpstrj'),
      'xyz' : tempfile.mkstemp(suffix='.xyz')}.items():
    os.close(fd)
    tmpfns[suffix] = temp

def teardown_module(module):
    """remove the temporary file created by tests in this file
    this gets automatically called by nose"""

    for e in tmpfns.values():
        os.unlink(e)

def test_mismatch():
    # loading a 22 atoms xtc with a topology that has 2,000 atoms
    # some kind of error should happen!
    assert_raises(ValueError, lambda: md.load(get_fn('frame0.xtc'), top=get_fn('4K6Q.pdb')))


def test_box():
    t = md.load(get_fn('native.pdb'))
    yield lambda: eq(t.unitcell_vectors, None)
    yield lambda: eq(t.unitcell_lengths, None)
    yield lambda: eq(t.unitcell_angles, None)
    yield lambda: eq(t.unitcell_volumes, None)

    t.unitcell_vectors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).reshape(1, 3, 3)
    yield lambda: eq(np.array([1.0, 1.0, 1.0]), t.unitcell_lengths[0])
    yield lambda: eq(np.array([90.0, 90.0, 90.0]), t.unitcell_angles[0])
    yield lambda: eq(np.array([1.0]), t.unitcell_volumes)


def test_load_pdb_box():
    t = md.load(get_fn('native2.pdb'), no_boxchk=True)
    yield lambda: eq(t.unitcell_lengths[0], np.array([0.1, 0.2, 0.3]))
    yield lambda: eq(t.unitcell_angles[0], np.array([90.0, 90.0, 90.0]))
    yield lambda: eq(t.unitcell_vectors[0], np.array([[0.1, 0, 0], [0, 0.2, 0], [0, 0, 0.3]]))


def test_load_pdb_gz():
    t = md.load(get_fn('1ncw.pdb.gz'))
    yield lambda: eq(t.n_atoms, 3990)


def test_box_load_save():
    t = md.load(get_fn('native2.pdb'), no_boxchk=True)

    # these four tempfile have extensions (dcd, xtc, trr, h5) that
    # should store the box information. lets make sure than through a load/save
    # cycle, the box information is preserved:
    top = md.load_topology(get_fn('native.pdb'), no_boxchk=True)
    for temp_fn in [tmpfns['xtc'], tmpfns['dcd'], tmpfns['trr'], tmpfns['h5']]:
        t.save(temp_fn)
        if temp_fn.endswith('.h5'):
            t2 = md.load(temp_fn)
        else:
            t2 = md.load(temp_fn, top=top)

        assert t.unitcell_vectors is not None
        yield lambda: eq(t.xyz, t2.xyz, decimal=3)
        yield lambda: eq(t.unitcell_vectors, t2.unitcell_vectors)
        yield lambda: eq(t.unitcell_angles, t2.unitcell_angles)
        yield lambda: eq(t.unitcell_lengths, t2.unitcell_lengths)


def test_slice():
    t = md.load(fn)

    # with copying
    yield lambda: eq((t[0:5] + t[5:10]).xyz, t[0:10].xyz)
    yield lambda: eq((t[0:5] + t[5:10]).time, t[0:10].time)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_vectors, t[0:10].unitcell_vectors)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_lengths, t[0:10].unitcell_lengths)
    yield lambda: eq((t[0:5] + t[5:10]).unitcell_angles, t[0:10].unitcell_angles)

    # without copying (in place)
    yield lambda: eq((t.slice(key=range(5), copy=False) + t.slice(key=range(5,10), copy=False)).xyz             , t.slice(key=range(10), copy=False).xyz             )
    yield lambda: eq((t.slice(key=range(5), copy=False) + t.slice(key=range(5,10), copy=False)).time            , t.slice(key=range(10), copy=False).time            )
    yield lambda: eq((t.slice(key=range(5), copy=False) + t.slice(key=range(5,10), copy=False)).unitcell_vectors, t.slice(key=range(10), copy=False).unitcell_vectors)
    yield lambda: eq((t.slice(key=range(5), copy=False) + t.slice(key=range(5,10), copy=False)).unitcell_lengths, t.slice(key=range(10), copy=False).unitcell_lengths)
    yield lambda: eq((t.slice(key=range(5), copy=False) + t.slice(key=range(5,10), copy=False)).unitcell_angles , t.slice(key=range(10), copy=False).unitcell_angles )
 


def test_slice2():
    t = md.load(get_fn('traj.h5'))
    # with copying
    yield lambda: t[0] == t[[0,1]][0]
    # without copying (in place)
    yield lambda: t.slice(key=[0], copy=False) == t.slice(key=[0,1], copy=True)[0]


def test_xtc():
    t = md.load(get_fn('frame0.xtc'), top=nat)
    for e in [tmpfns['xtc'], tmpfns['dcd'], tmpfns['binpos'], tmpfns['trr'], tmpfns['h5'], tmpfns['pdb'], tmpfns['pdb.gz'], tmpfns['nc']]:
{'fraction_non_alphanumeric': 0.18293991416309013, 'fraction_numerical': 0.030758226037195996, 'mean_word_length': 4.015695067264574, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4800140', 'n_tokens_mistral': 4306, 'n_tokens_neox': 4072, 'n_words': 994}