Ejemplo n.º 1
0
def computeMI_cond(model, node, minDist, maxDist, neighbours_G, snapshots,
                   nTrials, nSamples, modelSettings):
    MIs = []
    HXs = []
    subgraph_nodes = [node]
    for d in range(1, maxDist + 1):
        # get subgraph and outer neighbourhood at distance d
        if len(neighbours_G[d]) > 0:

            subgraph = nx.ego_graph(model.graph, node, d)

            if d >= minDist:
                print(
                    f'------------------- distance d={d}, num neighbours = {len(neighbours_G[d])}, subgraph size = {len(subgraph)}, num states = {len(snapshots[d-1])} -----------------------'
                )

                model_subgraph = fastIsing.Ising(subgraph, **modelSettings)

                threads = nthreads if len(subgraph_nodes) > 20 else 1

                initState = 1  #if args.fixMag else -1


                _, _, MI, HX, HXgiveny, keys, probs = simulation.neighbourhoodMI(model_subgraph, node, \
                                d, neighbours_G, snapshots[d-1], nTrials, \
                                args.burninSteps, nSamples, \
                                args.distSamples, threads=threads, \
                                initStateIdx=initState, uniformPDF=1, out='MI')
                print(HXgiveny)

                MIs.append(MI)
                HXs.append(HX)
                print(MIs)

    return MIs, HXs
Ejemplo n.º 2
0
def computeMI_cond(model, node, minDist, maxDist, neighbours_G, snapshots,
                   nTrials, nSamples, modelSettings, corrTimeSettings):

    all_MI = np.full(maxDist - minDist + 1, np.nan)
    all_HX = np.full(maxDist - minDist + 1, np.nan)

    stop = False

    subgraph_nodes = [node]
    for idx, d in enumerate(minDist, maxDist + 1):

        # get subgraph and outer neighbourhood at distance d
        if len(neighbours_G[d]) > 0:

            subgraph = nx.ego_graph(model.graph, node, d)

            print(f'------------------- distance d={d}, num neighbours = \
                {len(neighbours_G[d])}, subgraph size = {len(subgraph)}, \
                num states = {len(snapshots[d-1])} -----------------------')

            model_subgraph = fastIsing.Ising(subgraph, **modelSettings)

            # determine correlation time for subgraph Ising model
            if args.maxCorrTime == args.minCorrTime:
                distSamples_subgraph = args.maxCorrTime
                mixingTime_subgraph = corrTimeSettings['burninSteps']
            else:
                mixingTime_subgraph, meanMag, distSamples_subgraph, _ = \
                    simulation.determineCorrTime(model_subgraph, nodeG=node, **corrTimeSettings)
                if args.maxCorrTime > 0:
                    distSamples_subgraph = min(distSamples_subgraph,
                                               args.maxCorrTime)
                distSamples_subgraph = max(distSamples_subgraph,
                                           args.minCorrTime)
            print(f'correlation time = {distSamples_subgraph}')
            print(f'mixing time      = {mixingTime_subgraph}')

            threads = nthreads if len(subgraph) > 20 else 1

            _, _, MI, HX, HXgiveny, keys, probs = \
                    simulation.neighbourhoodMI(model_subgraph, node, \
                        d, neighbours_G, snapshots[d-1], nTrials=nTrials, \
                        burninSteps=mixingTime_subgraph, nSamples=nSamples, \
                        distSamples=distSamples_subgraph, threads=threads, \
                        initStateIdx=args.initState, uniformPDF=args.uniformPDF, out='MI')

            all_MI[idx] = MI
            all_HX[idx] = HX

            if MI < args.threshold: break

    return all_MI, all_HX
    graph = nx.read_gpickle(args.graph)
    N = len(graph)
    maxDist = args.maxDist if args.maxDist > 0 else nx.diameter(graph)
    networkSettings = dict( \
        path = args.graph, \
        size = N, \
        node = node
    )

    # setup Ising model with N=networkSize spin flip attempts per simulation step
    modelSettings = dict( \
        temperature     = T, \
        updateType      = 'async' ,\
        magSide         = args.magSide if args.magSide in ['pos', 'neg'] else ''
    )
    model = fastIsing.Ising(graph, **modelSettings)

    try:
        mixingResults = IO.loadResults(targetDirectory, 'mixingResults')
        corrTimeSettings = IO.loadResults(targetDirectory, 'corrTimeSettings')
        burninSteps = mixingResults['burninSteps']
        distSamples = mixingResults['distSamples']

    except:
        subprocess.call(['python3', 'run_mixing.py', f'{T}', f'{args.dir}', f'{args.graph}', \
                        '--maxcorrtime', '10000', \
                        '--maxmixing', '10000'])
        mixingResults = IO.loadResults(targetDirectory, 'mixingResults')
        corrTimeSettings = IO.loadResults(targetDirectory, 'corrTimeSettings')
        burninSteps = mixingResults['burninSteps']
        distSamples = mixingResults['distSamples']
Ejemplo n.º 4
0
        burninSteps      = burninSteps, \
        updateMethod     = updateType
        )
    IO.saveSettings(targetDirectory, settings)

    for i, g in enumerate(ensemble):

        graph = nx.read_gpickle(g)
        filename = os.path.split(g)[-1].strip('.gpickle')

        modelSettings = dict(\
                             graph       = graph,\
                             updateType  = updateType,\
                             magSide     = magSide
                             )
        model = fastIsing.Ising(**modelSettings)

        Tc = Tc_idx = -1
        while Tc < 0:
            mags, sus, binder, abs_mags = simulation.magnetizationParallel(model, \
                                temps        = temps,        \
                                n            = nSamples,     \
                                burninSteps  = burninSteps)

            Tc, Tc_idx = find_Tc_gaussian(sus, binder, temps)
            if Tc < 0:
                print(f'failed to estimate T_c. Another estimation will be performed, using a larger temperature range')
                temps = np.linspace(args.minT, temps[-1] + args.maxT, args.numT)

        #a, b = optimize.curve_fit(sig, temps, abs_mags)
        #mag_Tc = sig(Tc, *a)
Ejemplo n.º 5
0
from scipy.ndimage import gaussian_filter1d

nudges = np.logspace(-1, 1, 10)
out = np.zeros((N, nudges.size, deltas))
mis = np.zeros((N, graph.number_of_nodes(), deltas))

fig, ax = plt.subplots()
tax = ax.twinx()


modelsettings = dict(\
                 graph = graph, \
                 updateType = 'single', \
                 nudgeType  = 'constant',\
                 magSide = 'neg')
m = fastIsing.Ising(\
                **modelsettings)
temps = np.logspace(-6, np.log10(20), 30)
samps = [m.matchMagnetization(temps, 100) for i in range(1)]

samps = np.array(samps)
samps = gaussian_filter1d(samps, 3, axis=-1)
samps = samps.mean(0)
mag, sus = samps
ax.plot(temps, mag)
tax.plot(temps, sus)

fig.show()
IDX = np.argmin(abs(mag - .8 * mag.max()))
#    IDX = np.argmax(sus)
m.t = temps[IDX]