Ejemplo n.º 1
0
def JSD(sequence_P, P_P, sequence_Q, P_Q):
    """ alphabetic Jensen-Shannon Distance, calculated from discretized timeseries
    data in a combined probability space.

    Receives discretized sequences and their word counters using the discretize_sequence method.

    P:=prior, Q:=posterior
    See: https://stackoverflow.com/questions/15880133/jensen-shannon-divergence
    Together with: http://doi.org/10.1063/1.4999613
    """

    if len(P_P) > 0 and len(P_Q) > 0:

        # create alphabet dictionary
        # combine words for equal DPD lengths
        P_combined = P_P + P_Q
        sorted_keys = sorted(list(P_combined.keys()))

        # Unknown key returns 0! :)
        P_dpd = np.array([P_P[w] for w in sorted_keys])
        Q_dpd = np.array([P_Q[w] for w in sorted_keys])
        # norm probability distributions
        norm_P = np_dot(P_dpd, P_dpd)
        _P = P_dpd / norm_P
        norm_Q = np_dot(Q_dpd, Q_dpd)
        _Q = Q_dpd / norm_Q
        # calculate Jensen-Shannon Distance
        _M = 0.5 * (_P + _Q)
        js_distance = np_sqrt(0.5 * (kl_div(_P, _M) + kl_div(_Q, _M)))

        return js_distance
    print("JSD: Alphabets must contain at least one word!")
    return 1.0
def mel_scaled_spectrogram(spectrogram: ndarray,
                           sr: int,
                           n_mels: Optional[int] = 128,
                           fmin: Optional[float] = 0.0,
                           fmax: Optional[Union[float, None]] = None,
                           htk: Optional[bool] = False):
    """Calculates the mel scaled version of the spectrogram.

    :param spectrogram: Spectrogram to be used.
    :type spectrogram: numpy.ndarray
    :param sr: Sampling frequency of the original signal.
    :type sr: int
    :param n_mels: Amount of mel filters to use, defaults to 128.
    :type n_mels: int, optional
    :param fmin: Minimum frequency for mel filters, defaults to 0.0.
    :type fmin: float, optional
    :param fmax: Maximum frequency for mel filters. If `None`, \
                 sr/2.0 is used. Defaults to None
    :type fmax: float|None, optional
    :param htk: Use HTK formula, instead of Slaney, defaults to False.
    :type htk: bool, optional
    :return: Mel scaled version of the input spectrogram, with shape \
             (channels, nb_mels, values) for channels >= 2, else \
             (nb_mels, values).
    :rtype: numpy.ndarray
    """
    ndim = spectrogram.ndim

    if ndim not in [2, 3]:
        raise AttributeError('Input spectrogram must be of shape '
                             '(channels, nb_frames, frames). '
                             f'Current input has {ndim} dimensions. '
                             f'Allowed are either 2 or 3.')

    n_fft = 2 * (spectrogram[ndim - 2] - 1)

    mel_filters = mel(
        sr=sr,
        n_fft=n_fft,
        n_mels=n_mels,
        fmin=fmin,
        fmax=fmax,
        htk=htk)

    if ndim == 2:
        mel_spectrogram = np_dot(mel_filters, spectrogram)
    else:
        mel_spectrogram = np_cat([expand_dims(np_dot(mel_filters, i), 0)
                                  for i in spectrogram], axis=0)

    return mel_spectrogram
Ejemplo n.º 3
0
def num_lap(x, A, n, dim, D, c):
    ans = 0.0
    x = x.reshape(n, dim)
    for i in range(n):
        for j in range(n):
            ans = ans + (np_linalg_norm(x[i, :] - x[j, :])**2) * A[i, j]  #\
            # - (np.linalg.norm(x[i,:]-x[j,:])**2)*(1-A[i,j])
        lst_eq = []
        constraint = np_sum(
            np_square(np_diagonal(np_dot(x, np_trans(x)) - np_eye(n))))
        return ans + c * constraint
Ejemplo n.º 4
0
def array_pow(a, k):
    """
    Calculate the matrix power as a^k.
    
    Args:
        a(numpy.array): a square matrix to be mutiplied.
        k(int): the power index.
    """
    retval = np_copy(a)
    for i in xrange(1, k):
        retval = np_dot(retval, a)
    return retval
Ejemplo n.º 5
0
 def diff_dists(self, other):
     """ Calculate the difference between discrete probability distributions.
     Assume the distributions are equally sorted.
     """
     # check for size difference
     if self.dpd.shape[0] == other.shape[0]:
         diff = self.dpd[:, 0] - other[:, 0]
         return np_sqrt(np_dot(diff, diff))
     else:
         # defenitely different!
         # print("diff_dists: Could not calculate distribution difference as distributions are not equally long!")
         return 1.0
Ejemplo n.º 6
0
def f(x, A, n, dim, D, c):
    ans = 0.0
    x = x.reshape(n, dim)
    for i in range(n):
        for j in range(n):
            ans = ans + (np_linalg_norm(x[i, :] - x[j, :])**2) * A[i, j]  #\
            # - (np.linalg.norm(x[i,:]-x[j,:])**2)*(1-A[i,j])
        lst_eq = []
        # for i in range(dim):
        # 	for j in range(dim):
        # 		Mij = 0
        # 		for k in range(n):
        # 			Mij = Mij + D[k]*x[k,i]*x[k,j]
        # 		if i == j: lst_eq.append(Mij - 1)
        # 		else: lst_eq.append(Mij)
        # constraint = np.array(lst_eq)
        constraint = np_sum(
            np_square(np_diagonal(np_dot(x, np_trans(x)) - np_eye(n))))
        return ans + c * constraint
Ejemplo n.º 7
0
def passed_test(dtype, as_matrix, x_is_row, y_is_row, stride):
    """
    Run one dot (inner) product test.

    Arguments:
        dtype:        either 'float64' or 'float32', the NumPy dtype to test
        as_matrix:    True to test a NumPy matrix, False to test a NumPy ndarray
        x_is_row:     True to test a row vector as parameter x, False to test a column vector
        y_is_row:     True to test a row vector as parameter y, False to test a column vector
        stride:       stride of x and y to test; if None, a random stride is assigned

    Returns:
        True if the expected result is within the margin of error of the actual result,
        False otherwise.
    """

    # generate random sizes for vector dimensions and vector stride (if necessary)
    length = randint(N_MIN, N_MAX)
    stride = randint(N_MIN, STRIDE_MAX) if stride is None else stride

    # create random vectors to test
    x = random_vector(length, x_is_row, dtype, as_matrix)
    y = random_vector(length, y_is_row, dtype, as_matrix)

    # create views of x and y that can be used to calculate the expected result
    x_2 = x if x_is_row else x.T
    y_2 = y.T if y_is_row else y

    # compute the expected result
    if stride == 1:
        expected = np_dot(x_2, y_2)[0][0]
    else:
        expected = 0
        for i in range(0, length, stride):
            expected += x_2[0, i] * y_2[i, 0]

    # get the actual result
    actual = dot(x, y, stride, stride)

    # compare the actual result to the expected result and return result of the test
    return abs(actual - expected) / expected < EPSILON
Ejemplo n.º 8
0
def enum( n, lattice ):
  from math import pow
  from numpy import dot as np_dot
  from pylada import enumeration, math, crystal

  supercells = enumeration.find_all_cells(lattice, n)
  smiths = enumeration.create_smith_groups(lattice, supercells)
  nflavors = enumeration.count_flavors(lattice)
  nsites = len([0 for i in lattice.sites if len(i.type) > 1])
  transforms = enumeration.create_transforms(lattice)
  for smith in smiths:
    card = int(smith.smith[0]*smith.smith[1]*smith.smith[2]*nsites)
    label_exchange=enumeration.LabelExchange( card, nflavors )
    flavorbase = enumeration.create_flavorbase(card, nflavors)
    translations = enumeration.Translation(smith.smith, nsites)
    database = enumeration.Database(card, nflavors)
    maxterm = 0
    for x in xrange(1, int(pow(nflavors, card))-1):
      if not database[x]: continue
      if not is_valid(flavorbase, x):
        database[x] = False
        continue
      maxterm = x
      for labelperm in label_exchange:
        t = labelperm(x, flavorbase)
        if t > x: database[t] = False
      for translation in translations:
        t = translation(x, flavorbase)
        if t > x: database[t] = False
        elif t == x: 
          database[t] = False
          continue
        for labelperm in label_exchange:
          u = labelperm(t, flavorbase)
          if u > x: database[u] = False

    # checks supercell dependent transforms.
    for nsupercell, supercell in enumerate(smith.supercells):
      mine = []
      # creates list of transformation which leave the supercell invariant.
      cell = np_dot(lattice.cell, supercell.hermite)
      specialized = []
      for transform in transforms:
        if not transform.invariant(cell): continue
        transform.init(supercell.transform, smith.smith)
        if not transform.is_trivial:
          specialized.append( transform )

      specialized_database = enumeration.Database(database)
      for x in xrange(1, maxterm+1):
        if not database[x]: continue
        maxterm = x
        
        for transform in specialized:
          t = transform(x, flavorbase)
          if t == x: continue
          specialized_database[t] = False

          for labelperm in label_exchange:
            u = labelperm(t, flavorbase)
            if u == x: continue
            specialized_database[u] = False
            
          for translation in translations:
            u = translation(t, flavorbase)
            if u == x: continue
            specialized_database[u] = False
            for labelperm in label_exchange:
              v = labelperm(u, flavorbase)
              if v == x: continue
              specialized_database[v] = False
        if specialized_database[x]: yield x, smith, supercell, flavorbase
Ejemplo n.º 9
0
def read_database(filename, withperms=True):
  from numpy import array as np_array, dot as np_dot, zeros as np_zeros
  from pylada import crystal, enumeration

  lattice = crystal.Lattice()
  with open(filename, "r") as file:
    
    # reads lattice
    for line in file:
      data = line.split()
      if len(data) == 0: continue
      if data[0] == "cell:":
        lattice.cell = np_array(data[1:], dtype="float64").reshape(3,3)
      elif data[0] == "scale:":
        lattice.scale = float(line.split()[1])
      elif data[0] == "site:":
        data = line.split()
        data.pop(0)
        site = crystal.Site()
        for i in range(3): site.pos[i] = float(data.pop(0))
        while len(data): site.type.append( data.pop(0) )
        lattice.sites.append(site)
      elif data[0] == "endlattice": break

    lattice.set_as_crystal_lattice()
    transforms = enumeration.create_transforms(lattice)
    specialized = []
    nsites = len([0 for i in lattice.sites if len(i.type) > 1])

    hermite = None
    flavorbase = None
    transform = None
    structure = crystal.Structure()
    structure.scale = lattice.scale
    for line in file:
      data = line.split()
      if len(data) == 0: continue
      if data[0] == "n:": continue
      if data[0] == "hermite:":
        hermite = np_array(data[1:], dtype="float64").reshape(3,3)
        specialized = []
      elif data[0] == "transform:": 
        transform = np_array(data[1:], dtype="float64").reshape(3,3)
      elif data[0] == "smith:":
        smith = np_array( data[1:], dtype = "int64" )
        translations = enumeration.Translation(smith, nsites)
        cell = np_dot(lattice.cell, hermite)
        structure = crystal.fill_structure(cell)
        for transformation in transforms:
          if not transformation.invariant(cell): continue
          transformation.init(transform, smith)
          if not transformation.is_trivial:
            specialized.append( transformation )
      elif data[0] == "flavorbase:":
        card, nflavors = int(data[1]),  int(data[2])
        flavorbase = enumeration.create_flavorbase(card, nflavors)
        label_exchange=enumeration.LabelExchange(card, nflavors)
      elif len(data) > 0:
        assert flavorbase is not None
        assert hermite is not None
        for x in data:
          # adds label permutations that are not equivalent by affine transforms.
          x = int(x)
          others = set([x])
          if withperms:
            for labelperm in label_exchange:
              u = labelperm(x, flavorbase)
              if u in others: continue
  
              dont = False
              for transform in specialized:
                t = transform(u, flavorbase)
                if t in others:
                  dont = True
                  break
  
                for translation in translations:
                  v = translation(t, flavorbase)
                  if v in others:
                    dont = True
                    break
              if not dont: others.add(u)

          for u in others:
            enumeration.as_structure(structure, u, flavorbase)
            yield structure
Ejemplo n.º 10
0
def gaussian(x, mu, sig):
    """ Not normally distributed!
    """
    diff = np.array([x - mu])
    return np_exp((-np_sqrt(np_dot(diff, diff))**2.) / (2. * sig**2.))
Ejemplo n.º 11
0
def time_precision(mu, x, pi):
    # fit time 'x' to predicted time 'mu' with precision 'pi'
    # TODO: the scaling of sigma needs to be adapted to SoA empirical evidence
    diff = np_sqrt(np_dot(x - mu, x - mu))
    sig = 2 * (1 - pi) if pi < 1. else 0.1
    return np_e**(-(diff**2) / (sig**2)) + 0.001
 def get_grayscale(self, image=None):
     if image is not None:
         return np_dot(image[..., :3], [0.2989, 0.5870, 0.1140])
     else:
         return np_dot(self.image[..., :3], [0.2989, 0.5870, 0.1140])
Ejemplo n.º 13
0
        # 		Mij = 0
        # 		for k in range(n):
        # 			Mij = Mij + D[k]*x[k,i]*x[k,j]
        # 		if i == j: lst_eq.append(Mij - 1)
        # 		else: lst_eq.append(Mij)
        # constraint = np.array(lst_eq)
        constraint = np_sum(
            np_square(np_diagonal(np_dot(x, np_trans(x)) - np_eye(n))))
        return ans + c * constraint


# def f_jac(x,A,n,dim,D,c):
# 	ans = 0.0
# 	jac = np_zeros((dim,1))
# 	jac[] = np_dot(A,x)

A = [[1, 1, 0, 0], [1, 1, 1, 1], [0, 1, 1, 0], [0, 1, 0, 1]]
A_dense = np_array(A)
D = np_sum(A_dense, axis=0)
A = scipy.sparse.csr_matrix(A_dense)
# A = A_dense

A_sq = np_dot(A, A)

dim = 1
n = 4

res = optimize.minimize(partial(f, A=A, n=n, dim=dim, D=D, c=1),
                        np_rand(n * dim))

print res.x