Ejemplo n.º 1
0
def test_array(setup_cluster):
    import dask.array as da
    from numpy.core.numeric import array_equal

    x = da.random.random((10000, 10000), chunks=(1000, 1000))
    y = x + x.T
    z = y[::2, 5000:].mean(axis=1)

    dask_res = z.compute()
    assert array_equal(dask_res, z.compute(scheduler=mars_scheduler))
    assert array_equal(dask_res, convert_dask_collection(z).execute().fetch())
Ejemplo n.º 2
0
 def test_mul_ndarray_variable_forward(self):
     x0 = np.array([1, 2, 3])
     x1 = Variable(np.array([1, 2, 3]))
     y = x0 * x1
     res = y.data
     expected = np.array([1, 4, 9])
     self.assertTrue(array_equal(res, expected))
Ejemplo n.º 3
0
def test_score_pairs_warning(estimator, build_dataset):
  """Tests that score_pairs returns a FutureWarning regarding deprecation.
  Also that score_pairs and pair_distance have the same behaviour"""
  input_data, labels, _, X = build_dataset()
  model = clone(estimator)
  set_random_state(model)

  # We fit the metric learner on it and then we call score_pairs on some
  # points
  model.fit(*remove_y(model, input_data, labels))

  msg = ("score_pairs will be deprecated in release 0.7.0. "
         "Use pair_score to compute similarity scores, or "
         "pair_distances to compute distances.")
  with pytest.warns(FutureWarning) as raised_warning:
    score = model.score_pairs([[X[0], X[1]], ])
    dist = model.pair_distance([[X[0], X[1]], ])
    assert array_equal(score, dist)
  assert any([str(warning.message) == msg for warning in raised_warning])
Ejemplo n.º 4
0
def logisticRegression(trainData, trainLabels, testData, testLabels):
    #adjust the data, adding the 'free parameter' to the train data
    trainDataWithFreeParam = hstack((trainData.copy(), ones(trainData.shape[0])[:,newaxis]))
    testDataWithFreeParam = hstack((testData.copy(), ones(testData.shape[0])[:,newaxis]))
    
    alpha = 10
    oldW = zeros(trainDataWithFreeParam.shape[1])
    newW = ones(trainDataWithFreeParam.shape[1])
    iteration = 0
    
    trainDataWithFreeParamTranspose = transpose(trainDataWithFreeParam)
    alphaI = alpha * identity(oldW.shape[0])
    
    while not array_equal(oldW, newW):
        if iteration == 100:
            break
        oldW = newW.copy()
        
        yVect = yVector(oldW, trainDataWithFreeParam)
        r = R(yVect)

        firstTerm = inv(alphaI + dot(dot(trainDataWithFreeParamTranspose, r), trainDataWithFreeParam))
        secondTerm = dot(trainDataWithFreeParamTranspose, (yVect-trainLabels)) + alpha * oldW
        newW = oldW - dot(firstTerm, secondTerm)
        iteration += 1
                              
        
    #see how well we did
    numCorrect  = 0
    for x,t in izip(testDataWithFreeParam, testLabels):
        
        if yScalar(newW, x) >= 0.5:
            if t == 1:
                numCorrect += 1
        else:
            if t == 0:
                numCorrect += 1
    return float(numCorrect) / float(len(testLabels))
Ejemplo n.º 5
0
    def simulate_transients_old(self, init_grid, max_steps):
        """
        this method simulates the thing until an attractor is found
        """
        if type(init_grid) == int:
            self.grid = self.rng.choice([0, 1], size=[init_grid, init_grid])
        else:
            self.grid = init_grid

        # flag to set NaN if attractor is not found
        found_attractor = False

        self.history = np.zeros(
            (max_steps + 1, self.grid.shape[0], self.grid.shape[0]))

        for st in range(max_steps):
            self.history[st] = self.grid
            self.grid = self.step(self.grid)

            for hi, prev in enumerate(self.history[:st]):
                if array_equal(prev, self.grid):
                    last_idx_transient = hi - 1
                    found_attractor = True
                    break

            # exit loop if we're done with the attractor
            if found_attractor:
                end_step = st + 1
                break

        # if we dont find the attractor the transient index should be NaN
        if not found_attractor:
            end_step = max_steps
            last_idx_transient = np.nan

        return self.history[:end_step], last_idx_transient
Ejemplo n.º 6
0
 def test_sub_variable_ndarray_forward(self):
     x0 = Variable(np.array([4, 5, 6]))
     x1 = np.array([1, 2, 3])
     result = (x0 - x1).data
     expected = np.array([3, 3, 3])
     return self.assertTrue(array_equal(expected, result))
Ejemplo n.º 7
0
 def test_change_sign_of_numpy_when_forward(self):
     x = np.array([1, 2, 3])
     y = -x
     res = y.data
     expected = np.array([-1, -2, -3])
     self.assertTrue(array_equal(res, expected))
Ejemplo n.º 8
0
 def test_pow_variable_forward2(self):
     x = np.array([1, 2, 3])
     result = (x**3).data
     expected = np.array([1, 8, 27])
     return self.assertTrue(array_equal(result, expected))
Ejemplo n.º 9
0
 def test_pow_variable_forward(self):
     x = Variable(np.array([4, 5, 6]))
     result = (x**2).data
     expected = np.array([16, 25, 36])
     return self.assertTrue(array_equal(result, expected))
Ejemplo n.º 10
0
 def test_div_ndarray_variable_forward(self):
     x0 = np.array([1, 2, 3])
     x1 = Variable(np.array([4, 5, 6]))
     result = (x0 / x1).data
     expected = np.array([1 / 4, 2 / 5, 3 / 6])
     return self.assertTrue(array_equal(result, expected))
Ejemplo n.º 11
0
 def test_div_variable_ndarray_forward(self):
     x0 = Variable(np.array([4, 5, 6]))
     x1 = np.array([1, 2, 3])
     result = (x0 / x1).data
     expected = np.array([4 / 1, 5 / 2, 6 / 3])
     return self.assertTrue(array_equal(result, expected))
Ejemplo n.º 12
0
def test_ABF_properties(abfPath):

    abf = pyabf.ABF(abfPath)

    # file info
    assert isinstance(abf.abfDateTime, datetime.datetime)
    assert isinstance(abf.abfDateTimeString, str)
    assert isinstance(abf.abfFileComment, str)
    assert isinstance(abf.abfFilePath, str)
    assert isinstance(abf.abfID, str)
    assert isinstance(abf.fileGUID, str)
    assert isinstance(abf.protocol, str)
    assert isinstance(abf.protocolPath, str)

    # file version
    assert isinstance(abf.abfVersion, dict)
    assert isinstance(abf.abfVersion["major"], int)
    assert isinstance(abf.abfVersion["minor"], int)
    assert isinstance(abf.abfVersion["bugfix"], int)
    assert isinstance(abf.abfVersion["build"], int)
    assert isinstance(abf.abfVersionString, str)

    # abf creator version info
    assert isinstance(abf.creatorVersion["major"], int)
    assert isinstance(abf.creatorVersion["minor"], int)
    assert isinstance(abf.creatorVersion["bugfix"], int)
    assert isinstance(abf.creatorVersion["build"], int)
    assert isinstance(abf.creatorVersionString, str)

    # ADC names and units
    assert isinstance(abf.adcNames, list)
    for adcName in abf.adcNames:
        assert isinstance(adcName, str)
    assert isinstance(abf.adcUnits, list)
    for adcUnit in abf.adcUnits:
        assert isinstance(adcUnit, str)
    assert isinstance(abf.channelCount, int)
    assert isinstance(abf.channelList, list)

    # DAC names and units
    assert isinstance(abf.dacNames, list)
    for dacName in abf.dacNames:
        assert isinstance(dacName, str)
    assert isinstance(abf.dacUnits, list)
    for dacUnit in abf.dacUnits:
        assert isinstance(dacUnit, str)

    # data info
    assert (isinstance(abf.dataByteStart, int))
    assert (isinstance(abf.dataPointByteSize, int))
    assert (isinstance(abf.dataPointCount, int))
    assert (isinstance(abf.dataPointsPerMs, int))
    assert (isinstance(abf.dataRate, int))
    assert (isinstance(abf.dataSecPerPoint, float))
    assert (isinstance(abf.sweepCount, int))
    assert (isinstance(abf.sweepLengthSec, float))
    assert (isinstance(abf.sweepList, list))
    assert (isinstance(abf.sweepPointCount,  int))
    assert (isinstance(abf.sampleRate, int))
    assert (array_equal(abf.dataRate, abf.sampleRate))

    # holding level
    assert (isinstance(abf.holdingCommand, list))
    for value in abf.holdingCommand:
        assert isinstance(value, float) or isinstance(value, int)

    # data array size and shape
    assert isinstance(abf.data, np.ndarray)
    assert len(abf.data) == abf.channelCount
    assert len(abf.data[0]) == abf.dataPointCount/abf.channelCount

    # comments
    assert isinstance(abf.tagComments, list)
    assert isinstance(abf.tagSweeps, list)
    assert isinstance(abf.tagTimesMin, list)
    assert isinstance(abf.tagTimesSec, list)
    for i in range(len(abf.tagComments)):
        assert isinstance(abf.tagComments[i], str)
        assert isinstance(abf.tagSweeps[i], float)
        assert isinstance(abf.tagTimesMin[i], float)
        assert isinstance(abf.tagTimesSec[i], float)

    # raw data
    for channel in abf.channelList:
        dataY = abf.getAllYs(channel)
        dataX = abf.getAllXs(channel)
        assert isinstance(dataY, np.ndarray)
        assert isinstance(dataX, np.ndarray)
        assert len(dataY) != 0
        assert len(dataX) == len(dataY)

    # sweeps
    for channel in abf.channelList:
        for sweep in abf.sweepList:
            abf.setSweep(sweep, channel)
            assert isinstance(abf.sweepChannel, int)
            assert isinstance(abf.sweepNumber, int)

            # time units are manually defined
            assert isinstance(abf.sweepUnitsX, str)
            assert abf.sweepUnitsX == "sec"

            # labels are pyabf inventions and will always be present
            assert isinstance(abf.sweepLabelX, str)
            assert isinstance(abf.sweepLabelY, str)
            assert isinstance(abf.sweepLabelC, str)

            # ADC labels are likely available for every channel
            if (abf.sweepLabelY is not None):
                assert isinstance(abf.sweepUnitsY, str)

            # DAC labels are likely available for every channel
            # but may not if there are more ADCs than DACs
            if (abf.sweepUnitsC is not None):
                assert isinstance(abf.sweepUnitsC, str)
                assert isinstance(abf.sweepD(0), np.ndarray)
Ejemplo n.º 13
0
            if msg.is_meta:
                if msg.type == "time_signature":
                    outfile.write("midi time signature num: {} den: {}\n".format(msg.numerator, msg.denominator))
                if msg.type == "key_signature":
                    outfile.write("midi key_signature:{}\n".format(msg.key))
                if msg.type == "set_tempo":
                    outfile.write("midi tempo: {}\n".format(msg.tempo))
    if i != 0:
        for msg in track:
            if msg.is_meta:
                pass
            else:
                print("{}".format(msg))
                note_map[msg.note-1] = 1
                print(msg.note)
                piano_keys = np.append(piano_keys,note_map,axis=1)
                note_map = np.zeros(note_map.shape,dtype= np.int8)
        for raw in piano_keys:
            print(raw)
            
            if (True = array_equal(raw,))
        print(piano_keys.shape)
        temp = np.zeros(piano_keys.shape[1])
        print(temp.shape)
        temp[0] = msg.note
#         np.savetxt(outfile,piano_keys,delimiter=',',fmt='%d')
#         outfile.write("{}\n".format(piano_keys))
#             print(piano_keys.shape)
            #outfile.write("{}\n".format(msg))
outfile.close()
Ejemplo n.º 14
0
 def test_square_variable_forward(self):
     x = Variable(np.array([1, 2, 3]))
     y = F.square(x)
     expected = np.array([1, 4, 9])
     self.assertTrue(array_equal(y.data, expected))
Ejemplo n.º 15
0
 def test_exp_variable_forward(self):
     x = Variable(np.array([1, 2, 3]))
     y = F.exp(x)
     expected = np.exp(x.data)
     self.assertTrue(array_equal(y.data, expected))