'Returns a ``TIDS`` instance corresponding to the permutation ``g``
``g``  permutation corresponding to the tensor in the representation
used in canonicalization
``canon_bp``   if True, then ``g`` is the permutation
corresponding to the canonical form of the tensor'

def perm2tensor(self, g, canon_bp=False):

    vpos = []
    components = self.components
    pos = 0
    for t in components:
        vpos.append(pos)
        pos += t._rank
    sorted_free = [i[0] for i in self._get_sorted_free_indices_for_canon()]
    nfree = len(sorted_free)
    rank = self._ext_rank
    dum = [([None] * 4) for i in range(((rank - nfree) // 2))]
    free = []
    icomp = (-1)
    for i in range(rank):
        if (i in vpos):
            icomp += vpos.count(i)
            pos0 = i
        ipos = (i - pos0)
        gi = g[i]
        if (gi < nfree):
            ind = sorted_free[gi]
            free.append((ind, ipos, icomp))
        else:
            j = (gi - nfree)
            (idum, cov) = divmod(j, 2)
            if cov:
                dum[idum][1] = ipos
                dum[idum][3] = icomp
            else:
                dum[idum][0] = ipos
                dum[idum][2] = icomp
    dum = [tuple(x) for x in dum]
    return TIDS(components, free, dum)
