Example #1
0
    def run_processing(self):
        ''' '''

        sol_fname = os.path.normpath(str(self.i1.text()))
        coef_fname = os.path.normpath(str(self.i2.text()))
        summ_fname = os.path.normpath(str(self.i3.text()))

        # confirm fname  format
        if not re.match('.+[.]json', sol_fname):
            print('Invalid filename format...')
            return

        if not coef_fname:
            print('Missing coef file')
            return

        try:
            sol = DWAVE_Sol(sol_fname)
        except IOError:
            print('Failed to read solution file')
            return

        try:
            # load coef file
            h, J = self.load_coef_file(coef_fname)
            efunc = lambda s: compute_E(h, J, s)
        except:
            print('Invalid coef file given...')
            return

        try:
            # load summary file
            embeds = self.process_summ_file(summ_fname)
            subsols = {}
            params = {}
            for k, embed in embeds.items():
                qca_file = os.path.basename(embed['qca_file'])
                if not qca_file == EMBED_FILTER:
                    continue
                qbits = list(
                    reduce(lambda x, y: x + y, embed['models'].values()))
                qbits = [
                    tuple_to_linear(qb, M=12, N=12, L=4, index0=False)
                    for qb in qbits
                ]
                subsols[k] = sol.get_reduced_solution(qbits, efunc)
                h_, J_ = reduce_coefs(h, J, qbits)
                params[k] = {'h': h_, 'J': J_, 'qca_file': qca_file}
        except:
            print('Failed to read embed summary...')
            return

        for k, subsol in subsols.items():
            self.export_subsol(subsol, params[k])

        return
        for k, subsol in subsols.items():
            print(k)
            keys, spins, cell_occ, occ = subsol.model_reduction(embeds[k]['models'], \
                ind_map=lambda qb: tuple_to_linear(qb, 12, 12, index0=False))

            # outcome statistics
            #        cell_occ = sol.cell_occ
            #            cell_occ = subsols[k].cell_occ
            M = max(cell_occ)  # number fo gauge transformation
            N = max(max(x) for k, x in cell_occ.items())  # number of states
            D = np.zeros([M, N], dtype=int)

            for g, co in cell_occ.items():
                for s, o in co.items():
                    D[g - 1, s - 1] = o

            # reject rare outcomes
            if False:
                inds = np.nonzero(np.max(D, axis=0) > 2)[0]
                D = D[:, inds]

            if False:
                if True:
                    stack_plot = stackPlot(label='GT')
                    stack_plot.set_data(D)
                    stack_plot.plot(block=True)
                else:
                    plt.figure('GT')
                    plt.clf()
                    plt.plot(D.transpose(), 'x')
                    plt.show(block=True)

            # statistical distance
            SD = np.zeros([M, M], dtype=float)

            print('Computing statistical distances...')
            k = 0
            for i in range(M - 1):
                for j in range(i + 1, M):
                    k += 1
                    sys.stdout.write('\r{0}%'.format(k * 100. / (.5 * M *
                                                                 (M - 1))))
                    sys.stdout.flush()
                    SD[i, j] = SD[j, i] = stat_dist(D[i, :], D[j, :])
            print('\n')

            # seriate SD matrix if at least one pair of GTs have SD overlap
            if True:
                mask = np.ones(SD.shape, dtype=bool)
                np.fill_diagonal(mask, 0)
                if np.min(SD[mask]) > 0:
                    print('seriating SD matrix...')
                    try:
                        print('\tattempting {0}'.format(self.i4.text()))
                        new_inds = seriate(SD, method=str(self.i4.text()))
                    except:
                        print('\tfailed, using default method')
                        new_inds = seriate(SD, method='MDS')
                    print(new_inds)

                    SD = SD[new_inds, :][:, new_inds]
                else:
                    print(
                        'No overlap between GT distributions. Seriation not possible.'
                    )

            plt.imshow(SD, aspect='auto', interpolation='none')
            plt.colorbar()
            plt.show(block=True)

            if False:
                avg_pdf = np.sum(D, axis=0) * 1. / np.sum(D)

                # look at number of random GT needed to estimate avg_pdf
                sd_est = {k: match_dist(D, avg_pdf, k) for k in range(1, M)}

                pprint(sd_est)

                K = sorted(sd_est.keys())

                plt.figure('SD v K')
                plt.plot(K, [sd_est[x][0] for x in K], 'x')
                plt.xlabel('Number of samples', fontsize=FS)
                plt.ylabel('Statistical Distance', fontsize=FS)
                plt.show(block=False)

                plt.figure('stdev(SD) v K')
                plt.plot(K, [sd_est[x][1] for x in K])
                plt.xlabel('Number of samples', fontsize=FS)
                plt.ylabel('$\sigma_{SD}$', fontsize=FS)
                plt.show(block=True)
Example #2
0
    def run_processing(self):
        ''' '''

        sol_fname = os.path.normpath(str(self.i1.text()))
        coef_fname = os.path.normpath(str(self.i2.text()))

        # confirm fname  format
        if not re.match('.+[.]json', sol_fname):
            print('Invalid filename format...')
            return

        if not coef_fname:
            print('Missing coef file')
            return

        try:
            sol = Solution(sol_fname)
        except IOError:
            print('Failed to read solution file')
            return

        try:
            # load coef file
            h, J = self.load_coef_file(coef_fname)
            efunc = lambda s: compute_E(h, J, s)
        except:
            print('Invalid coef file given...')

        # outcome statistics
        cell_occ = sol.cell_occ

        N = max(x[0] for x in cell_occ)
        M = max(x[1] for x in cell_occ)
        D = np.zeros([M, N], dtype=int)

        print(D.shape)

        for i, j, v in cell_occ:
            D[j - 1, i - 1] = v

        # reject rare outcomes
        if False:
            inds = np.nonzero(np.max(D, axis=0) > 2)[0]
            D = D[:, inds]

        if False:
            if True:
                stack_plot = stackPlot(label='GT')
                stack_plot.set_data(D)
                stack_plot.plot(block=True)
            else:
                plt.figure('GT')
                plt.clf()
                plt.plot(D.transpose(), 'x')
                plt.show(block=True)

        # statistical distance
        SD = np.zeros([M, M], dtype=float)

        print('Computing statistical distances...')
        k = 0
        for i in range(M - 1):
            for j in range(i + 1, M):
                k += 1
                sys.stdout.write('\r{0}%'.format(k * 100. / (.5 * M *
                                                             (M - 1))))
                sys.stdout.flush()
                SD[i, j] = SD[j, i] = stat_dist(D[i, :], D[j, :])
        print('\n')

        # seriate SD matrix if at least one pair of GTs have SD overlap
        mask = np.ones(SD.shape, dtype=bool)
        np.fill_diagonal(mask, 0)
        if np.min(SD[mask]) > 0:
            print('seriating SD matrix...')
            try:
                print('\tattempting {0}'.format(self.i3.text()))
                new_inds = seriate(SD, method=str(self.i3.text()))
            except:
                print('\tfailed, using default method')
                new_inds = seriate(SD, method='MDS')
            print(new_inds)

            SD = SD[new_inds, :][:, new_inds]
        else:
            print(
                'No overlap between GT distributions. Seriation not possible.')

        plt.imshow(SD, aspect='auto', interpolation='none', vmin=0, vmax=1.0)
        plt.colorbar()
        plt.show(block=True)
        return

        avg_pdf = np.sum(D, axis=0) * 1. / np.sum(D)

        # look at number of random GT needed to estimate avg_pdf
        sd_est = {k: match_dist(D, avg_pdf, k) for k in range(1, M)}

        pprint(sd_est)

        K = sorted(sd_est.keys())

        plt.figure('SD v K')
        plt.plot(K, [sd_est[x][0] for x in K], 'x')
        plt.xlabel('Number of samples', fontsize=FS)
        plt.ylabel('Statistical Distance', fontsize=FS)
        plt.show(block=False)

        plt.figure('stdev(SD) v K')
        plt.plot(K, [sd_est[x][1] for x in K])
        plt.xlabel('Number of samples', fontsize=FS)
        plt.ylabel('$\sigma_{SD}$', fontsize=FS)
        plt.show(block=True)
Example #3
0
    def run_processing(self):
        ''' '''

        sol_fname = os.path.normpath(str(self.i1.text()))
        coef_fname = os.path.normpath(str(self.i2.text()))
        summ_fname = os.path.normpath(str(self.i3.text()))

         # confirm fname  format
        if not re.match('.+[.]json', sol_fname):
            print('Invalid filename format...')
            return

        if not coef_fname:
            print('Missing coef file')
            return

        try:
            sol = DWAVE_Sol(sol_fname)
        except IOError:
            print('Failed to read solution file')
            return

        try:
            # load coef file
            h, J = self.load_coef_file(coef_fname)
            efunc = lambda s: compute_E(h, J, s)
        except:
            print('Invalid coef file given...')
            return

        try:
            # load summary file
            embeds = self.process_summ_file(summ_fname)
            subsols = {}
            params = {}
            for k, embed in embeds.items():
                qca_file = os.path.basename(embed['qca_file'])
                if not qca_file == EMBED_FILTER:
                    continue
                qbits = list(reduce(lambda x,y:x+y, embed['models'].values()))
                qbits = [tuple_to_linear(qb, M=12, N=12, L=4, index0=False) for qb in qbits]
                subsols[k] = sol.get_reduced_solution(qbits, efunc)
                h_, J_ = reduce_coefs(h, J, qbits)
                params[k] = {'h': h_, 'J': J_, 'qca_file': qca_file}
        except:
            print('Failed to read embed summary...')
            return

        for k, subsol in subsols.items():
            self.export_subsol(subsol, params[k])

        return
        for k, subsol in subsols.items():
            print(k)
            keys, spins, cell_occ, occ = subsol.model_reduction(embeds[k]['models'], \
                ind_map=lambda qb: tuple_to_linear(qb, 12, 12, index0=False))

        # outcome statistics
#        cell_occ = sol.cell_occ
#            cell_occ = subsols[k].cell_occ
            M = max(cell_occ)   # number fo gauge transformation
            N = max(max(x) for k, x in cell_occ.items())    # number of states
            D = np.zeros([M, N], dtype=int)

            for g, co in cell_occ.items():
                for s, o in co.items():
                    D[g-1,s-1] = o

            # reject rare outcomes
            if False:
                inds = np.nonzero(np.max(D, axis=0) > 2)[0]
                D = D[:, inds]

            if False:
                if True:
                    stack_plot = stackPlot(label='GT')
                    stack_plot.set_data(D)
                    stack_plot.plot(block=True)
                else:
                    plt.figure('GT')
                    plt.clf()
                    plt.plot(D.transpose(), 'x')
                    plt.show(block=True)

            # statistical distance
            SD = np.zeros([M, M], dtype=float)

            print('Computing statistical distances...')
            k = 0
            for i in range(M-1):
                for j in range(i+1, M):
                    k += 1
                    sys.stdout.write('\r{0}%'.format(k*100./(.5*M*(M-1))))
                    sys.stdout.flush()
                    SD[i,j] = SD[j,i] = stat_dist(D[i,:], D[j,:])
            print('\n')

            # seriate SD matrix if at least one pair of GTs have SD overlap
            if True:
                mask = np.ones(SD.shape, dtype=bool)
                np.fill_diagonal(mask, 0)
                if np.min(SD[mask])>0:
                    print('seriating SD matrix...')
                    try:
                        print('\tattempting {0}'.format(self.i4.text()))
                        new_inds = seriate(SD, method=str(self.i4.text()))
                    except:
                        print('\tfailed, using default method')
                        new_inds = seriate(SD, method='MDS')
                    print(new_inds)

                    SD = SD[new_inds, :][:, new_inds]
                else:
                    print('No overlap between GT distributions. Seriation not possible.')

            plt.imshow(SD, aspect='auto', interpolation='none')
            plt.colorbar()
            plt.show(block=True)

            if False:
                avg_pdf = np.sum(D,axis=0)*1./np.sum(D)

                # look at number of random GT needed to estimate avg_pdf
                sd_est = {k: match_dist(D, avg_pdf, k) for k in range(1, M)}

                pprint(sd_est)

                K = sorted(sd_est.keys())

                plt.figure('SD v K')
                plt.plot(K, [sd_est[x][0] for x in K], 'x')
                plt.xlabel('Number of samples', fontsize=FS)
                plt.ylabel('Statistical Distance', fontsize=FS)
                plt.show(block=False)

                plt.figure('stdev(SD) v K')
                plt.plot(K, [sd_est[x][1] for x in K])
                plt.xlabel('Number of samples', fontsize=FS)
                plt.ylabel('$\sigma_{SD}$', fontsize=FS)
                plt.show(block=True)
Example #4
0
    def run_processing(self):
        ''' '''
        
        sol_fname = os.path.normpath(str(self.i1.text()))
        coef_fname = os.path.normpath(str(self.i2.text()))
        
         # confirm fname  format
        if not re.match('.+[.]json', sol_fname):
            print('Invalid filename format...')
            return
            
        if not coef_fname:
            print('Missing coef file')
            return
        
        try:
            sol = Solution(sol_fname)
        except IOError:
            print('Failed to read solution file')
            return

        try:
            # load coef file
            h, J = self.load_coef_file(coef_fname)
            efunc = lambda s: compute_E(h, J, s)
        except:
            print('Invalid coef file given...')
        
        # outcome statistics
        cell_occ = sol.cell_occ
        
        N = max(x[0] for x in cell_occ)
        M = max(x[1] for x in cell_occ)
        D = np.zeros([M, N], dtype=int)
        
        print(D.shape)
        
        for i, j, v in cell_occ:
            D[j-1,i-1] = v
        
        # reject rare outcomes
        if False:
            inds = np.nonzero(np.max(D, axis=0) > 2)[0]
            D = D[:, inds]
        
        if False:
            if True:
                stack_plot = stackPlot(label='GT')
                stack_plot.set_data(D)
                stack_plot.plot(block=True)
            else:
                plt.figure('GT')
                plt.clf()
                plt.plot(D.transpose(), 'x')
                plt.show(block=True)
                
        # statistical distance
        SD = np.zeros([M, M], dtype=float)
        
        print('Computing statistical distances...')
        k = 0
        for i in range(M-1):
            for j in range(i+1, M):
                k += 1
                sys.stdout.write('\r{0}%'.format(k*100./(.5*M*(M-1))))
                sys.stdout.flush()
                SD[i,j] = SD[j,i] = stat_dist(D[i,:], D[j,:])
        print('\n')
        
        # seriate SD matrix if at least one pair of GTs have SD overlap
        mask = np.ones(SD.shape, dtype=bool)
        np.fill_diagonal(mask, 0)
        if np.min(SD[mask])>0:
            print('seriating SD matrix...')
            try:
                print('\tattempting {0}'.format(self.i3.text()))
                new_inds = seriate(SD, method=str(self.i3.text()))
            except:
                print('\tfailed, using default method')
                new_inds = seriate(SD, method='MDS')
            print(new_inds)
            
            SD = SD[new_inds, :][:, new_inds]
        else:
            print('No overlap between GT distributions. Seriation not possible.')
        
        plt.imshow(SD, aspect='auto', interpolation='none', vmin=0, vmax=1.0)
        plt.colorbar()
        plt.show(block=True)
        return
        
        avg_pdf = np.sum(D,axis=0)*1./np.sum(D)
        
        # look at number of random GT needed to estimate avg_pdf
        sd_est = {k: match_dist(D, avg_pdf, k) for k in range(1, M)}
        
        pprint(sd_est)
    
        K = sorted(sd_est.keys())
        
        plt.figure('SD v K')
        plt.plot(K, [sd_est[x][0] for x in K], 'x')
        plt.xlabel('Number of samples', fontsize=FS)
        plt.ylabel('Statistical Distance', fontsize=FS)
        plt.show(block=False)
        
        plt.figure('stdev(SD) v K')
        plt.plot(K, [sd_est[x][1] for x in K])
        plt.xlabel('Number of samples', fontsize=FS)
        plt.ylabel('$\sigma_{SD}$', fontsize=FS)
        plt.show(block=True)