Beispiel #1
0
def mergeSort(arr: list,
              comp: Comparator,
              statParams: list = None,
              n: int = 2,
              combGroups: bool = True,
              sortGroups: bool = False) -> list:
    """Sorts the array with the given comparator.
	
	statParams must be the format ((D0, D1), dist, target AUC)
	combGroups determins if the returned array is one list or each group as its own list.
	sortGroups determins if groups will be sorted by size in the sort.
	yields the arr after each pass through
	also yields the stats if given statParams"""
    groups: list = list([arr[i]] for i in range(len(arr)))
    mergers: list = []
    currLayer: int = -1
    # while there are partitions
    while len(groups) != 1:
        currLayer += 1
        i: int = 0
        while len(groups) >= n:
            # last group, odd one out
            # get n arrays
            # feed the MultiMergers with them
            arrays: list = [groups.pop(0) for _ in range(n)]
            mergers.append(MultiMerger(arrays, comp, i, 0))
            i += 1
        #while we have active mergers
        while mergers:
            for merger in mergers:
                res = merger.inc()
                if res:  #if that merger is done
                    validate(merger.output)
                    comp.learn(merger.output)
                    if sortGroups:
                        groups.append(merger.output)
                    else:
                        groups.insert(0, merger.output)
                    mergers.remove(merger)
        if combGroups:
            arr: list = []
            for group in groups:
                arr.extend(group)
        else:
            arr: list = groups
        # run dem stats
        if statParams:
            stats: list = runStats(groups, statParams +
                                   [n, currLayer, len(mergers)], comp)
            yield arr, stats
        else:
            yield arr
Beispiel #2
0
def analyzeAFCStudies(log: str, results: str, n0: int, n1: int) -> tuple:
	"""Extracts the times out of the log file generated from DylAFC
	extracts the x0 and x1 vectors and the ranks from the results file from DylComp"""
	times = list()
	with open(log) as f:
		for line in f:
			line: list = line.strip().split()
			times.append(float(line[-1]))

	data, D0, D1 = continuousScale(n0, n1)
	comp = Comparator(data, rand=True)
	comp.learn(results)
	for arr in treeMergeSort(data[:], comp):
		pass
	indeciesAFC: list = [arr.index(i) for i in range(256)]
	x0, x1 = genX0X1(arr, D1, D0)
	x0: np.ndarray = np.array([indeciesAFC[i] for i in range(128)])
	x1: np.ndarray = np.array([indeciesAFC[i] for i in range(128, 256)])
	return times, x0, x1, indeciesAFC
Beispiel #3
0
def sort(args) -> list:
    """Performs a sort based on the given args.
	Args is of the format (dist, auc, n0, n1) and is one tuple/list.
	Throws an error if the array did not sort correctly.
	Returns the results."""
    dist, auc, n0, n1 = args
    results = list()
    data, D0, D1 = continuousScale(n0, n1)
    comp = Comparator(data, level=0, rand=True)
    sep = genSep(dist, auc)
    comp.genRand(n0, n1, sep, dist)
    for arr, stats in treeMergeSort(data, comp, [(D0, D1), dist, auc], n=2):
        stats.extend([len(comp), comp.genSeps(), comp.pc[-1]])
        comp.resetPC()
        results.append(stats)
    if arr != sorted(arr, key=lambda x: comp.getLatentScore(x)[0]):
        print(arr)
        print(sorted(arr, key=lambda x: comp.getLatentScore(x)[0]))
        raise AssertionError("did not sort")
    return results
Beispiel #4
0
     anim = FuncAnimation(fig,
                          update,
                          frames=np.arange(0, frames),
                          interval=100)
     anim.save("rocs.gif", writer=PillowWriter(fps=10))
     pbar.close()
 else:
     import matplotlib.pyplot as plt
     from apng import APNG
     from DylSort import treeMergeSort
     from DylComp import Comparator
     from DylData import continuousScale
     from DylMath import genROC, avROC
     seed = 15
     data, D0, D1 = continuousScale(128, 128)
     comp = Comparator(data, level=0, rand=True, seed=seed)
     comp.genRand(len(D0), len(D1), 7.72, 'exponential')
     np.random.seed(seed)
     im = APNG()
     fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
     fig.suptitle("Pass number, MSE true, MSE empirical")
     x = np.linspace(0, 1, num=200)
     y = x**(1 / 7.72)
     ax1.set_aspect('equal', 'box')
     ax2.set_aspect('equal', 'box')
     elo = simulation_ELO_targetAUC(True)
     merge = treeMergeSort(data,
                           comp,
                           statParams=[(D0, D1)],
                           combGroups=False)
     plt.tight_layout()
Beispiel #5
0
		arrays: list = [[0, 1, 4, 2, 5, 3, 6],
			[0, 1, 2, 4, 3, 5, 6],
			[0, 1, 2, 4, 3, 5, 6],
			[0, 1, 2, 3, 4, 5, 6]]
		graphROCs(arrays, D0=[0, 1, 2, 3], D1=[4, 5, 6])
	elif test == 5:
		graphROC([4, 1, 2, 3], [1, 2], [3, 4])
	elif test == 6:
		from DylSort import treeMergeSort
		from DylComp import Comparator
		from DylData import continuousScale
		import matplotlib
		font: dict = {'size' : 10}
		matplotlib.rc('font', **font)
		data, D0, D1 = continuousScale(128, 128)
		comp: Comparator = Comparator(data, rand=True, level=0, seed=15)
		for arr in treeMergeSort(data, comp=comp):
			pass
		D0.sort(key = comp.getLatentScore)
		D1.sort(key = comp.getLatentScore)
		roc: dict = ROC1.rocxy(comp.getLatentScore(D1), comp.getLatentScore(D0))
		graphROCs([arr], True, True, D0, D1)
	elif test == 7:
		roc1: list = [[0, 0], [0, 1], [1, 1]]
		roc3 = roc2 = roc1
		roc4: list = [[0, 0], [0.5, 0], [0.5, 0.5], [1, 1]]
		avgROC: tuple = avROC([roc1, roc2, roc3, roc4])
		fig = plt.figure(figsize=(4,4))
		ax = fig.add_subplot(111)
		ax.plot(*zip(*roc1), 'm', label='chunk1', ls='-')
		ax.plot(*zip(*roc2), 'b', label='chunk2', ls='--')
Beispiel #6
0
			for group in groups: arr.extend(group)
		else:
			arr: list = groups
		yield (arr, runStats(groups, statParams + [n, layer, len(mergerss)], comp)) if statParams else arr

if __name__ == "__main__":
	test: int = int(argv[1]) if len(argv) > 1 else 1
	if test == 1:
		if len(argv) > 5 or len(argv) < 4:
			print("Usage:")
			print(f"{__file__} 1 <n0> <n1> <directory to save file into (optional)>")
		else:
			import matplotlib.pyplot as plt
			plt.rcParams["font.size"]: int = 10
			data, D0, D1 = continuousScale(int(argv[2]), int(argv[3]))
			comp: Comparator = Comparator(data, rand=True)
			for arr in treeMergeSort(data, comp):
				pass
			arrays: list = [arr]
			D0.sort(key=arr.index)
			D1.sort(key=arr.index)
			plt = graphROCs(arrays, True, D0=D0, D1=D1)
			ax = plt.gca()
			ax.set_title("")
			plt.title("")
			plt.gcf().suptitle("")
			if len(argv) > 4:
				plt.savefig(argv[4] + "/patches.pdf", bbox_inches = 'tight', pad_inches = 0)
			else:
				plt.show()
	elif test == 3:
Beispiel #7
0
                indeciesRight.pop(i)
        if len(groups) == 1:
            for i in range(indecies[0], indeciesRight[0] + 1):
                output[OIndex] = groups[0][i]
                OIndex += 1
            yield output
        left ^= toggle  # toggle left if toggle == True
        yield False


if __name__ == '__main__':
    from DylComp import Comparator
    for test in range(1, 7):
        if test == 1:
            objs: list = [*range(8)]
            comp: Comparator = Comparator(objs, rand=False)
            m: MultiMerger = MultiMerger([[0, 4], [1, 5], [2, 6], [3, 7]],
                                         comp,
                                         toggle=True)
            while not m.inc():
                pass
            if m.output != [0, 1, 2, 3, 4, 5, 6, 7]:
                raise AssertionError("wasn't right")
            comp.clearHistory()
            m1: MultiMerger = MultiMerger([[0, 4], [1, 5]], comp, toggle=True)
            while not m1.inc():
                pass
            if m1.output != [0, 1, 4, 5]:
                raise AssertionError("wasn't right")
            m2: MultiMerger = MultiMerger([[2, 6], [3, 7]], comp, toggle=True)
            while not m2.inc():