jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_godin_15min(self):        
        """ test godin filtering on a 15min constant values 
            data series with a nan.
        """        
        data=[1.0]*800+[2.0]*400+[1.0]*400
        data=numpy.array(data)
        data[336]=numpy.nan
        st=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        delta=time_interval(minutes=15)
        test_ts=rts(data,st,delta,{})
        nt3=godin(test_ts)
        self.assert_(numpy.all(numpy.isnan(nt3.data[0:144])))
        assert_array_almost_equal(nt3.data[144:192],[1]*48,12)
        self.assert_(numpy.all(numpy.isnan(nt3.data[192:481])))
        assert_array_almost_equal(nt3.data[481:656],[1]*175,12)
        self.assert_(numpy.all(numpy.greater(nt3.data[656:944],1)))
        self.assertAlmostEqual(nt3.data[868],1.916618441)
        assert_array_almost_equal(nt3.data[944:1056],[2]*112,12)
        self.assert_(numpy.all(numpy.greater(nt3.data[1056:1344],1)))
        self.assertAlmostEqual(nt3.data[1284],1.041451845)
        assert_array_almost_equal(nt3.data[1344:1456],[1]*112,12)
        self.assert_(numpy.all(numpy.isnan(nt3.data[1456:1600])))
//  [python]
def test_data_frame_parser_target_index(data_frame, mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = DataFrameParser(preprocessor, labels='labelA',
                             smiles_col='smiles')
    result = parser.parse(data_frame, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.alltrue(is_successful)
    assert len(is_successful) == 2

    # We assume NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_features(dataset[0], expect, label_a[0])

    expect = preprocessor.get_input_features(mols[2])
    check_features(dataset[1], expect, label_a[2])

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_data_frame_parser_target_index(data_frame, mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = DataFrameParser(preprocessor, labels='labelA',
                             smiles_col='smiles')
    result = parser.parse(data_frame, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.all(is_successful)
    assert len(is_successful) == 2

    # We assume NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_features(dataset[0], expect, label_a[0])

    expect = preprocessor.get_input_features(mols[2])
    check_features(dataset[1], expect, label_a[2])

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_data_frame_parser_return_is_successful(mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = DataFrameParser(preprocessor, labels='labelA',
                             smiles_col='smiles')
    df = pandas.DataFrame({
        'smiles': ['var', 'CN=C=O', 'hoge', 'Cc1ccccc1', 'CC1=CC2CC(CC1)O2'],
        'labelA': [0., 2.1, 0., 5.3, -1.2],
    })
    result = parser.parse(df, return_smiles=True, return_is_successful=True)

    dataset = result['dataset']
    # smiles = result['smiles']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    # print('is_successful', is_successful)
    assert numpy.alltrue(is_successful[[1, 3, 4]])
    assert numpy.alltrue(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_features(dataset[i], expect, label_a[i])
//  [python]
def test_data_frame_parser_return_is_successful(mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = DataFrameParser(preprocessor, labels='labelA',
                             smiles_col='smiles')
    df = pandas.DataFrame({
        'smiles': ['var', 'CN=C=O', 'hoge', 'Cc1ccccc1', 'CC1=CC2CC(CC1)O2'],
        'labelA': [0., 2.1, 0., 5.3, -1.2],
    })
    result = parser.parse(df, return_smiles=True, return_is_successful=True)

    dataset = result['dataset']
    # smiles = result['smiles']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    # print('is_successful', is_successful)
    assert numpy.all(is_successful[[1, 3, 4]])
    assert numpy.all(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_features(dataset[i], expect, label_a[i])
//  [python]
def product(self, **kwargs:dict)->list:
        """Cartesian product of input variables.
        This method is inspired by `itertools.product <https://docs.python.org/3/library/itertools.html#itertools.product>`_.

        Must pass a list for each ``sampling_var`` that should be considered. Not all ``sampling_vars`` must be referenced. 
        Sampling vars that are excluded, will generate a value according to their assigned ``fun_var_pdf`` (see :py:meth:`set_sampling_var`).

        Args:
            kwargs: Keyword arguments of the form ``var_name=var_values``.

        Returns:
            Returns the newly created sampling plan.
        """
        # Check if all key word values are lists:
        check = np.alltrue([isinstance(v, list) for v in kwargs.values()])
        if not check:
            raise ValueError('keyword values must be lists')

        # Check if all key words are existing sampling variables:
        keys = kwargs.keys()
        check = np.alltrue([v in self.sampling_var_names for v in keys])
        if not check:
            raise ValueError('keyword names must be existing sampling variables')

        # Create cartesian product of all values passen in kwargs:
        values = list(itertools.product(*list(kwargs.values())))

        # Create new sampling cases:
        for value in values:
            # Zip together the value(s) of the current case with the respective keys:
            case = dict(zip(keys, value))
            # Add sampling case
            self.add_sampling_case(**case)

        return self.sampling_plan
//  [python]
def product(self, **kwargs:dict)->list:
        """Cartesian product of input variables.
        This method is inspired by `itertools.product <https://docs.python.org/3/library/itertools.html#itertools.product>`_.

        Must pass a list for each ``sampling_var`` that should be considered. Not all ``sampling_vars`` must be referenced. 
        Sampling vars that are excluded, will generate a value according to their assigned ``fun_var_pdf`` (see :py:meth:`set_sampling_var`).

        Args:
            kwargs: Keyword arguments of the form ``var_name=var_values``.

        Returns:
            Returns the newly created sampling plan.
        """
        # Check if all key word values are lists:
        check = np.all([isinstance(v, list) for v in kwargs.values()])
        if not check:
            raise ValueError('keyword values must be lists')

        # Check if all key words are existing sampling variables:
        keys = kwargs.keys()
        check = np.all([v in self.sampling_var_names for v in keys])
        if not check:
            raise ValueError('keyword names must be existing sampling variables')

        # Create cartesian product of all values passen in kwargs:
        values = list(itertools.product(*list(kwargs.values())))

        # Create new sampling cases:
        for value in values:
            # Zip together the value(s) of the current case with the respective keys:
            case = dict(zip(keys, value))
            # Add sampling case
            self.add_sampling_case(**case)

        return self.sampling_plan
//  [python]
def test_fromiter_bytes(self):
        """Ticket #1058"""
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_fromiter_bytes(self):
        """Ticket #1058"""
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.all(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.all(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_godin_2d(self):
        
        """ Test godin filter on 2-dimensional data set."""
        
        d1=[1.0]*800+[2.0]*400+[1.0]*400
        d2=[1.0]*800+[2.0]*400+[1.0]*400
        data=numpy.array([d1,d2])
        data=numpy.transpose(data)
        data[336,:]=numpy.nan
        st=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        delta=time_interval(minutes=15)
        test_ts=rts(data,st,delta,{})
        
        nt3=godin(test_ts)
        d1=nt3.data[:,0]
        d2=nt3.data[:,1]
        self.assert_(numpy.alltrue(numpy.isnan(d1[0:144])))
        assert_array_almost_equal(d1[144:192],[1]*48,12)
        self.assert_(numpy.alltrue(numpy.isnan(d1[192:481])))
        assert_array_almost_equal(d1[481:656],[1]*175,12)
        self.assert_(numpy.alltrue(numpy.greater(d1[656:944],1)))
        self.assertAlmostEqual(d1[868],1.916618441)
        assert_array_almost_equal(d1[944:1056],[2]*112,12)
        self.assert_(numpy.alltrue(numpy.greater(d1[1056:1344],1)))
        self.assertAlmostEqual(d1[1284],1.041451845)
        assert_array_almost_equal(d1[1344:1456],[1]*112,12)
        self.assert_(numpy.alltrue(numpy.isnan(d1[1456:1600]))) 
        
        self.assert_(numpy.alltrue(numpy.isnan(d2[0:144])))
        assert_array_almost_equal(d2[144:192],[1]*48,12)
        self.assert_(numpy.alltrue(numpy.isnan(d2[192:481])))
        assert_array_almost_equal(d2[481:656],[1]*175,12)
        self.assert_(numpy.alltrue(numpy.greater(d2[656:944],1)))
        self.assertAlmostEqual(d2[868],1.916618441)
        assert_array_almost_equal(d2[944:1056],[2]*112,12)
        self.assert_(numpy.alltrue(numpy.greater(d2[1056:1344],1)))
        self.assertAlmostEqual(d2[1284],1.041451845)
        assert_array_almost_equal(d2[1344:1456],[1]*112,12)
        self.assert_(numpy.alltrue(numpy.isnan(d2[1456:1600])))
//  [python]
def test_godin_2d(self):
        
        """ Test godin filter on 2-dimensional data set."""
        
        d1=[1.0]*800+[2.0]*400+[1.0]*400
        d2=[1.0]*800+[2.0]*400+[1.0]*400
        data=numpy.array([d1,d2])
        data=numpy.transpose(data)
        data[336,:]=numpy.nan
        st=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        delta=time_interval(minutes=15)
        test_ts=rts(data,st,delta,{})
        
        nt3=godin(test_ts)
        d1=nt3.data[:,0]
        d2=nt3.data[:,1]
        self.assert_(numpy.all(numpy.isnan(d1[0:144])))
        assert_array_almost_equal(d1[144:192],[1]*48,12)
        self.assert_(numpy.all(numpy.isnan(d1[192:481])))
        assert_array_almost_equal(d1[481:656],[1]*175,12)
        self.assert_(numpy.all(numpy.greater(d1[656:944],1)))
        self.assertAlmostEqual(d1[868],1.916618441)
        assert_array_almost_equal(d1[944:1056],[2]*112,12)
        self.assert_(numpy.all(numpy.greater(d1[1056:1344],1)))
        self.assertAlmostEqual(d1[1284],1.041451845)
        assert_array_almost_equal(d1[1344:1456],[1]*112,12)
        self.assert_(numpy.all(numpy.isnan(d1[1456:1600]))) 
        
        self.assert_(numpy.all(numpy.isnan(d2[0:144])))
        assert_array_almost_equal(d2[144:192],[1]*48,12)
        self.assert_(numpy.all(numpy.isnan(d2[192:481])))
        assert_array_almost_equal(d2[481:656],[1]*175,12)
        self.assert_(numpy.all(numpy.greater(d2[656:944],1)))
        self.assertAlmostEqual(d2[868],1.916618441)
        assert_array_almost_equal(d2[944:1056],[2]*112,12)
        self.assert_(numpy.all(numpy.greater(d2[1056:1344],1)))
        self.assertAlmostEqual(d2[1284],1.041451845)
        assert_array_almost_equal(d2[1344:1456],[1]*112,12)
        self.assert_(numpy.all(numpy.isnan(d2[1456:1600])))
//  [python]
def test_from_numpy():
  arr = np.random.randint(0, high=256, size=(128,128, 128))
  arr = np.asarray(arr, dtype=np.uint8)
  vol = CloudVolume.from_numpy(arr, max_mip=1)
  arr2 = vol[:,:,:]
  np.alltrue(arr == arr2)
  
  arr = np.random.randn(128,128, 128, 3)
  arr = np.asarray(arr, dtype=np.float32)
  vol = CloudVolume.from_numpy(arr, max_mip=1)
  arr2 = vol[:,:,:]
  np.alltrue(arr == arr2)
  shutil.rmtree('/tmp/image')
//  [python]
def test_from_numpy():
  arr = np.random.randint(0, high=256, size=(128,128, 128))
  arr = np.asarray(arr, dtype=np.uint8)
  vol = CloudVolume.from_numpy(arr, max_mip=1)
  arr2 = vol[:,:,:]
  np.all(arr == arr2)
  
  arr = np.random.randn(128,128, 128, 3)
  arr = np.asarray(arr, dtype=np.float32)
  vol = CloudVolume.from_numpy(arr, max_mip=1)
  arr2 = vol[:,:,:]
  np.all(arr == arr2)
  shutil.rmtree('/tmp/image')
//  [python]
def test_correct_word_counts(self):
        """Test that the correct word count matrix has been created."""
        expected_word_counts_in_documents = np.array([
            [1, 1, 1, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 1, 0, 0],
            [1, 0, 0, 0, 0, 0, 2, 1]
        ])
        self.assertTrue(np.alltrue(expected_word_counts_in_documents == self.corpus.word_counts_in_documents))
//  [python]
def test_correct_word_counts(self):
        """Test that the correct word count matrix has been created."""
        expected_word_counts_in_documents = np.array([
            [1, 1, 1, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 1, 0, 0],
            [1, 0, 0, 0, 0, 0, 2, 1]
        ])
        self.assertTrue(np.all(expected_word_counts_in_documents == self.corpus.word_counts_in_documents))
//  [python]
def test_boxcar(self):
        
        """ test boxcar performance in each steps of a godin filter"""
                
        data=[1.0]*200+[2.0]*100+[1.0]*100
        data=numpy.array(data)
        st=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        delta=time_interval(hours=1)
        test_ts=rts(data,st,delta,{})
        
        
        ## first round of boxcar in 24 (11,12).
        nt=boxcar(test_ts,11,12)
        self.assert_(numpy.alltrue(numpy.isnan(nt.data[0:11])))
        assert_array_almost_equal(nt.data[11:188],[1]*177,12)
        self.assert_(numpy.alltrue(numpy.greater(nt.data[188:211],1)))
        self.assertAlmostEqual(nt.data[196],1.375)
        assert_array_almost_equal(nt.data[211:288],[2]*77,12)
        self.assert_(numpy.alltrue(numpy.greater(nt.data[288:311],1)))
        self.assertAlmostEqual(nt.data[301],1.4166666667)
        assert_array_almost_equal(nt.data[311:388],[1]*77,12)
        self.assert_(numpy.alltrue(numpy.isnan(nt.data[388:400])))
    
        ## second round of boxcar in 24 (12,11)
        nt2=boxcar(nt,12,11)
        self.assert_(numpy.alltrue(numpy.isnan(nt2.data[0:23])))
        assert_array_almost_equal(nt2.data[23:177],[1]*154,12)
        self.assert_(numpy.alltrue(numpy.greater(nt2.data[177:223],1)))
        self.assertAlmostEqual(nt2.data[196],1.364583333)
        assert_array_almost_equal(nt2.data[223:277],[2]*54,12)
        self.assert_(numpy.alltrue(numpy.greater(nt2.data[277:323],1)))
        self.assertAlmostEqual(nt2.data[301],1.439236111)
        assert_array_almost_equal(nt2.data[323:377],[1]*54,12)
        self.assert_(numpy.alltrue(numpy.isnan(nt2.data[377:400])))
        
        ## third round of boxcar.   
        nt3=boxcar(nt2,12,12)
        self.assert_(numpy.alltrue(numpy.isnan(nt3.data[0:35])))
        assert_array_almost_equal(nt3.data[35:165],[1]*130,12)
        self.assert_(numpy.alltrue(numpy.greater(nt3.data[165:235],1)))
        self.assertAlmostEqual(nt3.data[196],1.393055556)
        assert_array_almost_equal(nt3.data[235:265],[2]*30,12)
        self.assert_(numpy.alltrue(numpy.greater(nt3.data[265:335],1)))
        self.assertAlmostEqual(nt3.data[301],1.453819444)
        assert_array_almost_equal(nt3.data[335:365],[1]*30,12)
        self.assert_(numpy.alltrue(numpy.isnan(nt3.data[365:400])))
//  [python]
def test_boxcar(self):
        
        """ test boxcar performance in each steps of a godin filter"""
                
        data=[1.0]*200+[2.0]*100+[1.0]*100
        data=numpy.array(data)
        st=datetime.datetime(year=1990,month=2,day=3,hour=11, minute=15)
        delta=time_interval(hours=1)
        test_ts=rts(data,st,delta,{})
        
        
        ## first round of boxcar in 24 (11,12).
        nt=boxcar(test_ts,11,12)
        self.assert_(numpy.all(numpy.isnan(nt.data[0:11])))
        assert_array_almost_equal(nt.data[11:188],[1]*177,12)
        self.assert_(numpy.all(numpy.greater(nt.data[188:211],1)))
        self.assertAlmostEqual(nt.data[196],1.375)
        assert_array_almost_equal(nt.data[211:288],[2]*77,12)
        self.assert_(numpy.all(numpy.greater(nt.data[288:311],1)))
        self.assertAlmostEqual(nt.data[301],1.4166666667)
        assert_array_almost_equal(nt.data[311:388],[1]*77,12)
        self.assert_(numpy.all(numpy.isnan(nt.data[388:400])))
    
        ## second round of boxcar in 24 (12,11)
        nt2=boxcar(nt,12,11)
        self.assert_(numpy.all(numpy.isnan(nt2.data[0:23])))
        assert_array_almost_equal(nt2.data[23:177],[1]*154,12)
        self.assert_(numpy.all(numpy.greater(nt2.data[177:223],1)))
        self.assertAlmostEqual(nt2.data[196],1.364583333)
        assert_array_almost_equal(nt2.data[223:277],[2]*54,12)
        self.assert_(numpy.all(numpy.greater(nt2.data[277:323],1)))
        self.assertAlmostEqual(nt2.data[301],1.439236111)
        assert_array_almost_equal(nt2.data[323:377],[1]*54,12)
        self.assert_(numpy.all(numpy.isnan(nt2.data[377:400])))
        
        ## third round of boxcar.   
        nt3=boxcar(nt2,12,12)
        self.assert_(numpy.all(numpy.isnan(nt3.data[0:35])))
        assert_array_almost_equal(nt3.data[35:165],[1]*130,12)
        self.assert_(numpy.all(numpy.greater(nt3.data[165:235],1)))
        self.assertAlmostEqual(nt3.data[196],1.393055556)
        assert_array_almost_equal(nt3.data[235:265],[2]*30,12)
        self.assert_(numpy.all(numpy.greater(nt3.data[265:335],1)))
        self.assertAlmostEqual(nt3.data[301],1.453819444)
        assert_array_almost_equal(nt3.data[335:365],[1]*30,12)
        self.assert_(numpy.all(numpy.isnan(nt3.data[365:400])))
//  [python]
def test_toy_crdir_assay_feats_table(toy_crdir_reader):
    assert np.alltrue(
        toy_crdir_reader.assayFeats.columns
        == np.array(["RNA", "ADT", "RNA", "HTO", "RNA"])
    )
    assert np.alltrue(
        toy_crdir_reader.assayFeats.values[1:]
        == [[0, 1, 3, 5, 6], [1, 3, 5, 6, 7], [1, 2, 2, 1, 1]]
    )
//  [python]
def test_toy_crdir_assay_feats_table(toy_crdir_reader):
    assert np.all(
        toy_crdir_reader.assayFeats.columns
        == np.array(["RNA", "ADT", "RNA", "HTO", "RNA"])
    )
    assert np.all(
        toy_crdir_reader.assayFeats.values[1:]
        == [[0, 1, 3, 5, 6], [1, 3, 5, 6, 7], [1, 2, 2, 1, 1]]
    )
//  [python]
def isSubsetVector(vec1, vec2, tol):
    """Checks if ``vec1`` is a subset of ``vec2``.

    Parameters
    ----------
    vec1 : (cdms2.TransientAxis, cdms2.FileAxis, numpy.ndarray)
        Subset data.
    vec2 : (cdms2.TransientAxis, cdms2.FileAxis, numpy.ndarray)
        Superset data.
    tol : float
        Tolerance used when checking for subset.

    Returns
    -------
    (bool/int), int
        First value of ``True`` denotes ``vec1`` is a subset of ``vec2``.
        A value of 0 or ``False`` denotes the opposite.

        Second value is the starting index of ``vec1`` in ``vec2`` if it
        is a subset.
    """
    # TODO this needs fixing return should be None if not subset otherwise the index value.
    index = lookupArray(vec2, vec1[0])
    if index > (len(vec2) - len(vec1)):
        # vec1 is too large, cannot be a subset
        return (0, -1)
    issubset = numpy.alltrue(numpy.less(numpy.absolute(
        vec1 - vec2[index:index + len(vec1)]), tol))
    if issubset:
        return (issubset, index)
    else:
        return (0, -1)
//  [python]
def isSubsetVector(vec1, vec2, tol):
    """Checks if ``vec1`` is a subset of ``vec2``.

    Parameters
    ----------
    vec1 : (cdms2.TransientAxis, cdms2.FileAxis, numpy.ndarray)
        Subset data.
    vec2 : (cdms2.TransientAxis, cdms2.FileAxis, numpy.ndarray)
        Superset data.
    tol : float
        Tolerance used when checking for subset.

    Returns
    -------
    (bool/int), int
        First value of ``True`` denotes ``vec1`` is a subset of ``vec2``.
        A value of 0 or ``False`` denotes the opposite.

        Second value is the starting index of ``vec1`` in ``vec2`` if it
        is a subset.
    """
    # TODO this needs fixing return should be None if not subset otherwise the index value.
    index = lookupArray(vec2, vec1[0])
    if index > (len(vec2) - len(vec1)):
        # vec1 is too large, cannot be a subset
        return (0, -1)
    issubset = numpy.all(numpy.less(numpy.absolute(
        vec1 - vec2[index:index + len(vec1)]), tol))
    if issubset:
        return (issubset, index)
    else:
        return (0, -1)
//  [python]
def set_weights(self, weight_dict):
        """Update weights with a dictionary keyed by test_mi, whose values are
        either:
         (1) dicts of feature -> scalar weight.
         (2) a scalar which will apply to all features of that model interface
        Features and model interfaces must correspond to those declared for the
        context.
        """
        for test_mi, fs in weight_dict.items():
            try:
                flist = list(self.metric_features[test_mi]['features'].keys())
            except KeyError:
                raise AssertionError("Invalid test model interface")
            if isinstance(fs, common._num_types):
                feat_dict = {}.fromkeys(flist, fs)
            elif isinstance(fs, dict):
                assert npy.alltrue([isinstance(w, common._num_types) for \
                            w in fs.values()]), "Invalid scalar weight"
                assert npy.alltrue([f in flist for f in fs.keys()]), \
                       "Invalid features given for this test model interface"
                feat_dict = fs
            for f, w in feat_dict.items():
                self.feat_weights[(test_mi, f)] = w
                # update weight value
                start_ix, end_ix = self.weight_index_mapping[test_mi][f]
                self.weights[start_ix:end_ix] = w
//  [python]
def set_weights(self, weight_dict):
        """Update weights with a dictionary keyed by test_mi, whose values are
        either:
         (1) dicts of feature -> scalar weight.
         (2) a scalar which will apply to all features of that model interface
        Features and model interfaces must correspond to those declared for the
        context.
        """
        for test_mi, fs in weight_dict.items():
            try:
                flist = list(self.metric_features[test_mi]['features'].keys())
            except KeyError:
                raise AssertionError("Invalid test model interface")
            if isinstance(fs, common._num_types):
                feat_dict = {}.fromkeys(flist, fs)
            elif isinstance(fs, dict):
                assert npy.all([isinstance(w, common._num_types) for \
                            w in fs.values()]), "Invalid scalar weight"
                assert npy.all([f in flist for f in fs.keys()]), \
                       "Invalid features given for this test model interface"
                feat_dict = fs
            for f, w in feat_dict.items():
                self.feat_weights[(test_mi, f)] = w
                # update weight value
                start_ix, end_ix = self.weight_index_mapping[test_mi][f]
                self.weights[start_ix:end_ix] = w
//  [python]
def _get_initiator_cache(self, ics=None, t0=0):
        """
        Return initating object for given initial conditions and start time.
        For internal use only.
        "initiator" is the ModelInterface or GeneratorInterface object that
        will be chosen to begin an integration at the specified ICs and t0.
        """
        if self._initiator_cache is None:
            if ics is None:
                raise ValueError("Must pass initial conditions")
            else:
                initiator = self.model._findTrajInitiator(None, 0,
                                                t0, dict(ics))[0]
                self._initiator_cache = (ics, t0, initiator)
        else:
            if npy.alltrue(self._initiator_cache[0] == ics) and \
               self._initiator_cache[1] == t0:
                ics, t0, initiator = self._initiator_cache
            elif ics is None:
                raise ValueError("Must pass initial conditions")
            else:
                # initial conditions or t0 don't match -- don't use cache
                initiator = self.model._findTrajInitiator(None, 0,
                                                t0, dict(ics))[0]
                self._initiator_cache = (ics, t0, initiator)
        return (ics, t0, initiator)
//  [python]
def _get_initiator_cache(self, ics=None, t0=0):
        """
        Return initating object for given initial conditions and start time.
        For internal use only.
        "initiator" is the ModelInterface or GeneratorInterface object that
        will be chosen to begin an integration at the specified ICs and t0.
        """
        if self._initiator_cache is None:
            if ics is None:
                raise ValueError("Must pass initial conditions")
            else:
                initiator = self.model._findTrajInitiator(None, 0,
                                                t0, dict(ics))[0]
                self._initiator_cache = (ics, t0, initiator)
        else:
            if npy.all(self._initiator_cache[0] == ics) and \
               self._initiator_cache[1] == t0:
                ics, t0, initiator = self._initiator_cache
            elif ics is None:
                raise ValueError("Must pass initial conditions")
            else:
                # initial conditions or t0 don't match -- don't use cache
                initiator = self.model._findTrajInitiator(None, 0,
                                                t0, dict(ics))[0]
                self._initiator_cache = (ics, t0, initiator)
        return (ics, t0, initiator)
//  [python]
def test_smiles_parser_target_index(mol_smiles, mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = SmilesParser(preprocessor)
    result = parser.parse(mol_smiles, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.alltrue(is_successful)
    assert len(is_successful) == 2

    # As we want test CSVFileParser, we assume
    # NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_input_features(dataset[0], expect)

    expect = preprocessor.get_input_features(mols[2])
    check_input_features(dataset[1], expect)

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_smiles_parser_target_index(mol_smiles, mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = SmilesParser(preprocessor)
    result = parser.parse(mol_smiles, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.all(is_successful)
    assert len(is_successful) == 2

    # As we want test CSVFileParser, we assume
    # NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_input_features(dataset[0], expect)

    expect = preprocessor.get_input_features(mols[2])
    check_input_features(dataset[1], expect)

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_smiles_parser_return_is_successful(mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = SmilesParser(preprocessor)
    mol_smiles_with_invalid = [
        'var', 'CN=C=O', 'hoge', 'Cc1ccccc1', 'CC1=CC2CC(CC1)O2']
    result = parser.parse(mol_smiles_with_invalid, return_smiles=True,
                          return_is_successful=True)

    dataset = result['dataset']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    assert numpy.alltrue(is_successful[[1, 3, 4]])
    assert numpy.alltrue(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_input_features(dataset[i], expect)
//  [python]
def test_smiles_parser_return_is_successful(mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = SmilesParser(preprocessor)
    mol_smiles_with_invalid = [
        'var', 'CN=C=O', 'hoge', 'Cc1ccccc1', 'CC1=CC2CC(CC1)O2']
    result = parser.parse(mol_smiles_with_invalid, return_smiles=True,
                          return_is_successful=True)

    dataset = result['dataset']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    assert numpy.all(is_successful[[1, 3, 4]])
    assert numpy.all(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_input_features(dataset[i], expect)
//  [python]
def test_add_vector_episode_statistics():
    NUM_ENVS = 5

    info = {}
    for i in range(NUM_ENVS):
        episode_info = {
            "episode": {
                "r": i,
                "l": i,
                "t": i,
            }
        }
        info = add_vector_episode_statistics(info, episode_info["episode"], NUM_ENVS, i)
        assert np.alltrue(info["_episode"][: i + 1])

        for j in range(NUM_ENVS):
            if j <= i:
                assert info["episode"]["r"][j] == j
                assert info["episode"]["l"][j] == j
                assert info["episode"]["t"][j] == j
            else:
                assert info["episode"]["r"][j] == 0
                assert info["episode"]["l"][j] == 0
                assert info["episode"]["t"][j] == 0
//  [python]
def test_add_vector_episode_statistics():
    NUM_ENVS = 5

    info = {}
    for i in range(NUM_ENVS):
        episode_info = {
            "episode": {
                "r": i,
                "l": i,
                "t": i,
            }
        }
        info = add_vector_episode_statistics(info, episode_info["episode"], NUM_ENVS, i)
        assert np.all(info["_episode"][: i + 1])

        for j in range(NUM_ENVS):
            if j <= i:
                assert info["episode"]["r"][j] == j
                assert info["episode"]["l"][j] == j
                assert info["episode"]["t"][j] == j
            else:
                assert info["episode"]["r"][j] == 0
                assert info["episode"]["l"][j] == 0
                assert info["episode"]["t"][j] == 0
//  [python]
def test_extend_adj(adj):
    adj_extended = extend_adj(adj, out_size=6)
    assert adj_extended.shape == (batchsize, 6, 6)
    assert adj_extended.dtype == adj.dtype
    assert numpy.array_equal(adj_extended[:, :num_node, :num_node], adj)
    assert numpy.alltrue(adj_extended[:, num_node:, :] == 0)
    assert numpy.alltrue(adj_extended[:, :, num_node:] == 0)
//  [python]
def test_extend_adj(adj):
    adj_extended = extend_adj(adj, out_size=6)
    assert adj_extended.shape == (batchsize, 6, 6)
    assert adj_extended.dtype == adj.dtype
    assert numpy.array_equal(adj_extended[:, :num_node, :num_node], adj)
    assert numpy.all(adj_extended[:, num_node:, :] == 0)
    assert numpy.all(adj_extended[:, :, num_node:] == 0)
//  [python]
def test_is_picklable_numpy(self):
        arr = np.array([1, 2, 3])
        self.assertTrue(is_picklable(arr))
        
        arr_pickled = pickle.dumps(arr)
        arr_recovered = pickle.loads(arr_pickled)
        self.assertTrue(isinstance(arr_recovered, np.ndarray))
        self.assertTrue(np.alltrue(arr == arr_recovered))
//  [python]
def test_is_picklable_numpy(self):
        arr = np.array([1, 2, 3])
        self.assertTrue(is_picklable(arr))
        
        arr_pickled = pickle.dumps(arr)
        arr_recovered = pickle.loads(arr_pickled)
        self.assertTrue(isinstance(arr_recovered, np.ndarray))
        self.assertTrue(np.all(arr == arr_recovered))
//  [python]
def test_reduce_out(self):
        from pycuda.curandom import rand as curand

        a_gpu = curand((10, 200), dtype=np.float32)
        a = a_gpu.get()

        from pycuda.reduction import ReductionKernel

        red = ReductionKernel(
            np.float32, neutral=0, reduce_expr="max(a,b)", arguments="float *in"
        )
        max_gpu = gpuarray.empty(10, dtype=np.float32)
        for i in range(10):
            red(a_gpu[i], out=max_gpu[i])

        assert np.alltrue(a.max(axis=1) == max_gpu.get())
//  [python]
def test_reduce_out(self):
        from pycuda.curandom import rand as curand

        a_gpu = curand((10, 200), dtype=np.float32)
        a = a_gpu.get()

        from pycuda.reduction import ReductionKernel

        red = ReductionKernel(
            np.float32, neutral=0, reduce_expr="max(a,b)", arguments="float *in"
        )
        max_gpu = gpuarray.empty(10, dtype=np.float32)
        for i in range(10):
            red(a_gpu[i], out=max_gpu[i])

        assert np.all(a.max(axis=1) == max_gpu.get())
//  [python]
def test_dots(self):
        #NB we can't use screenshots here - just check that no errors are raised
        win = self.win
        #using init
        dots =visual.DotStim(win, color=(1.0,1.0,1.0), dir=270,
            nDots=500, fieldShape='circle', fieldPos=(0.0,0.0),fieldSize=1*self.scaleFactor,
            dotLife=5, #number of frames for each dot to be drawn
            signalDots='same', #are the signal and noise dots 'different' or 'same' popns (see Scase et al)
            noiseDots='direction', #do the noise dots follow random- 'walk', 'direction', or 'position'
            speed=0.01*self.scaleFactor, coherence=0.9)
        dots.draw()
        win.flip()
        "{}".format(dots) #check that str(xxx) is working

        #using .set() and check the underlying variable changed
        prevDirs = copy.copy(dots._dotsDir)
        prevSignals = copy.copy(dots._signalDots)
        prevVerticesPix = copy.copy(dots.verticesPix)
        dots.dir = 20
        dots.coherence = 0.5
        dots.fieldPos = [-0.5, 0.5]
        dots.speed = 0.1 * self.scaleFactor
        dots.opacity = 0.8
        dots.contrast = 0.8
        dots.draw()
        #check that things changed
        assert (prevDirs-dots._dotsDir).sum()!=0, \
            "dots._dotsDir failed to change after dots.setDir()"
        assert prevSignals.sum()!=dots._signalDots.sum(), \
            "dots._signalDots failed to change after dots.setCoherence()"
        assert not numpy.alltrue(prevVerticesPix==dots.verticesPix), \
            "dots.verticesPix failed to change after dots.setPos()"
//  [python]
def test_dots(self):
        #NB we can't use screenshots here - just check that no errors are raised
        win = self.win
        #using init
        dots =visual.DotStim(win, color=(1.0,1.0,1.0), dir=270,
            nDots=500, fieldShape='circle', fieldPos=(0.0,0.0),fieldSize=1*self.scaleFactor,
            dotLife=5, #number of frames for each dot to be drawn
            signalDots='same', #are the signal and noise dots 'different' or 'same' popns (see Scase et al)
            noiseDots='direction', #do the noise dots follow random- 'walk', 'direction', or 'position'
            speed=0.01*self.scaleFactor, coherence=0.9)
        dots.draw()
        win.flip()
        "{}".format(dots) #check that str(xxx) is working

        #using .set() and check the underlying variable changed
        prevDirs = copy.copy(dots._dotsDir)
        prevSignals = copy.copy(dots._signalDots)
        prevVerticesPix = copy.copy(dots.verticesPix)
        dots.dir = 20
        dots.coherence = 0.5
        dots.fieldPos = [-0.5, 0.5]
        dots.speed = 0.1 * self.scaleFactor
        dots.opacity = 0.8
        dots.contrast = 0.8
        dots.draw()
        #check that things changed
        assert (prevDirs-dots._dotsDir).sum()!=0, \
            "dots._dotsDir failed to change after dots.setDir()"
        assert prevSignals.sum()!=dots._signalDots.sum(), \
            "dots._signalDots failed to change after dots.setCoherence()"
        assert not numpy.all(prevVerticesPix==dots.verticesPix), \
            "dots.verticesPix failed to change after dots.setPos()"
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable= True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if numpy.alltrue(numpy.ravel(footprint),axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable= True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if numpy.all(numpy.ravel(footprint),axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def testDelegate(self):
       assert (numpy.alltrue(self.a.flat == self.a.cpt.flat)), \
              " Delegation does not work check __getattr__"
//  [python]
def testDelegate(self):
       assert (numpy.all(self.a.flat == self.a.cpt.flat)), \
              " Delegation does not work check __getattr__"
//  [python]
def test_fda(self):
        """ Train FDA and test on training data """
        fda_node = FDAClassifierNode(
        )  # (generalized) Fisher Discriminant Analysis
        for x in self.x_b:
            fda_node.train(FeatureVector(x), 'b')
        for x in self.x_a:
            fda_node.train(FeatureVector(x), 'a')
        fda_node.stop_training()

        # for calling execute we need FeatureVectors since meta data is handled
        # there
        self.x_a = [FeatureVector(numpy.atleast_2d(elem)) for elem in self.x_a]
        self.x_b = [FeatureVector(numpy.atleast_2d(elem)) for elem in self.x_b]
        classification_a = [fda_node.execute(fv).label for fv in self.x_a]
        classification_b = [fda_node.execute(fv).label for fv in self.x_b]

        self.assert_(numpy.alltrue(map(lambda x: x == 'a', classification_a)))
        self.assert_(numpy.alltrue(map(lambda x: x == 'b', classification_b)))
//  [python]
def test_fda(self):
        """ Train FDA and test on training data """
        fda_node = FDAClassifierNode(
        )  # (generalized) Fisher Discriminant Analysis
        for x in self.x_b:
            fda_node.train(FeatureVector(x), 'b')
        for x in self.x_a:
            fda_node.train(FeatureVector(x), 'a')
        fda_node.stop_training()

        # for calling execute we need FeatureVectors since meta data is handled
        # there
        self.x_a = [FeatureVector(numpy.atleast_2d(elem)) for elem in self.x_a]
        self.x_b = [FeatureVector(numpy.atleast_2d(elem)) for elem in self.x_b]
        classification_a = [fda_node.execute(fv).label for fv in self.x_a]
        classification_b = [fda_node.execute(fv).label for fv in self.x_b]

        self.assert_(numpy.all(numpy.array(list(map(lambda x: x == 'a', classification_a)))))
        self.assert_(numpy.all(numpy.array(list(map(lambda x: x == 'b', classification_b)))))
//  [python]
def load_lammps(lammps_file, tol=_TOL):
  """Load a lammpstraj file.
  Args:
    lammps_file: file path
  Returns:
    coords: shape (F, N, 3)
    lattices: shape (F, 3)
    types: shape (N,)
    raw_types: shape (N,)
    unwrapped_coords: shape (F, N, 3)
  """
  # Loads lammpstraj file.
  with ExtendedLAMMPSTrajectoryFile(lammps_file) as f:
    coords, lattices, angles, types, ixyz, masses = f.read()
  assert ixyz is not None, 'needs to have ixyz'
  assert np.allclose(angles, 90.), 'trajs are not in orthogonal boxes'
  assert np.isclose(types, types[0]).all(), 'atom type changes'
  assert np.isclose(masses, masses[0]).all(), 'atom mass changes'
  if not np.isclose(lattices, lattices[0]).all():
    warnings.warn('box size changes.')
  ixyz = ixyz.astype('int32')
  types = types[0]
  masses = masses[0]
  assert types.shape[0] == coords.shape[1] == ixyz.shape[1] == masses.shape[0]
  assert coords.shape[0] == lattices.shape[0] == ixyz.shape[0]

  unwrapped_coords = coords + lattices[:, np.newaxis] * ixyz

  # Wrap coords
  imag_loc = np.floor_divide(coords, lattices[:, np.newaxis])
  wrapped_coords = coords - imag_loc * lattices[:, np.newaxis]
  assert np.alltrue((np.max(wrapped_coords, axis=1) -
             np.min(wrapped_coords, axis=1)) <= lattices)

  atom_types = []
  for mass in masses:
    diffs = np.abs(mass - _ATOM_MASSES)
    atomic_number = np.argmin(diffs)
    assert diffs[atomic_number] <= tol, 'diff is {} for {}'.format(
      diffs[atomic_number], mass)
    atom_types.append(atomic_number)
  atom_types = np.array(atom_types, dtype=np.int32)
  return wrapped_coords, unwrapped_coords, lattices, types, atom_types