|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import numpy as np |
| 4 | +import random |
| 5 | +import yaml |
| 6 | + |
| 7 | +from scipy.cluster.vq import vq, kmeans2 |
| 8 | +from typing import Tuple |
| 9 | +from benchmark.datasets import DATASETS |
| 10 | + |
| 11 | +def cluster_and_permute( |
| 12 | + data, num_clusters |
| 13 | +) -> Tuple[np.ndarray[int], np.ndarray[int]]: |
| 14 | + """ |
| 15 | + Cluster the data and return permutation of row indices |
| 16 | + that would group indices of the same cluster together |
| 17 | + """ |
| 18 | + npts = np.shape(data)[0] |
| 19 | + sample_size = min(100000, npts) |
| 20 | + sample_indices = np.random.choice(range(npts), size=sample_size, replace=False) |
| 21 | + sampled_data = data[sample_indices, :] |
| 22 | + centroids, sample_labels = kmeans2(sampled_data, num_clusters, minit="++", iter=10) |
| 23 | + labels, dist = vq(data, centroids) |
| 24 | + |
| 25 | + count = np.zeros(num_clusters) |
| 26 | + for i in range(npts): |
| 27 | + count[labels[i]] += 1 |
| 28 | + print("Cluster counts") |
| 29 | + print(count) |
| 30 | + |
| 31 | + offsets = np.zeros(num_clusters + 1, dtype=int) |
| 32 | + for i in range(0, num_clusters, 1): |
| 33 | + offsets[i + 1] = offsets[i] + count[i] |
| 34 | + |
| 35 | + permutation = np.zeros(npts, dtype=int) |
| 36 | + counters = np.zeros(num_clusters, dtype=int) |
| 37 | + for i in range(npts): |
| 38 | + label = labels[i] |
| 39 | + row = offsets[label] + counters[label] |
| 40 | + counters[label] += 1 |
| 41 | + permutation[row] = i |
| 42 | + |
| 43 | + return offsets, permutation |
| 44 | + |
| 45 | + |
| 46 | +def write_permuated_data( |
| 47 | + data, |
| 48 | + permutation:np.ndarray[int], |
| 49 | + output_data_file:str |
| 50 | +): |
| 51 | + permuted_data = data[permutation,:] |
| 52 | + |
| 53 | + shape = np.shape(permuted_data) |
| 54 | + with open(output_data_file, 'wb') as df: |
| 55 | + df.write(shape[0].to_bytes(4, 'little')) |
| 56 | + df.write(shape[1].to_bytes(4, 'little')) |
| 57 | + df.write(permuted_data) |
| 58 | + |
| 59 | + |
| 60 | +def create_runbook( |
| 61 | + dataset_str:str, |
| 62 | + offsets:np.ndarray[int], |
| 63 | + permutation:np.ndarray[int], |
| 64 | + num_clusters:int, |
| 65 | + output_yaml_file:str |
| 66 | +): |
| 67 | + ins_cursor_start = offsets.copy() |
| 68 | + ins_cursor_end = offsets.copy() |
| 69 | + |
| 70 | + del_cursor_start = offsets.copy() |
| 71 | + del_cursor_end = offsets.copy() |
| 72 | + |
| 73 | + operation_list = [] |
| 74 | + num_operations = 1 |
| 75 | + active_points = 0 |
| 76 | + max_pts = 0 |
| 77 | + active_points_in_cluster = np.zeros(num_clusters) |
| 78 | + |
| 79 | + num_rounds = 5 |
| 80 | + sample = np.random.default_rng().dirichlet((100,15,10,5,3), num_clusters) |
| 81 | + for c in range(num_clusters): |
| 82 | + np.random.default_rng().shuffle(sample[c]) |
| 83 | + print(sample) |
| 84 | + |
| 85 | + for round in range(num_rounds): |
| 86 | + #insertions |
| 87 | + for c in range(num_clusters): |
| 88 | + delta = (int)((offsets[c+1]-offsets[c]) * sample[c,round]) |
| 89 | + ins_cursor_end[c] = ins_cursor_start[c] + delta |
| 90 | + active_points += delta |
| 91 | + max_pts = max(max_pts, active_points) |
| 92 | + active_points_in_cluster[c] += delta |
| 93 | + print('ins [', ins_cursor_start[c], ', ', ins_cursor_end[c], |
| 94 | + ') active:', int(active_points_in_cluster[c]), |
| 95 | + 'total:', active_points) |
| 96 | + entry = [{'operation': 'insert'}, {'start': int(ins_cursor_start[c])}, {'end': int(ins_cursor_end[c])}] |
| 97 | + operation_list.append((num_operations, entry)) |
| 98 | + num_operations += 1 |
| 99 | + operation_list.append((num_operations, [{'operation': str('search')}])) |
| 100 | + num_operations += 1 |
| 101 | + ins_cursor_start[c] = ins_cursor_end[c] |
| 102 | + |
| 103 | + #deletions |
| 104 | + for c in range(num_clusters): |
| 105 | + fraction = random.uniform(0.5,0.9) |
| 106 | + delta = (int)(fraction*(ins_cursor_end[c]-del_cursor_start[c])) |
| 107 | + del_cursor_end[c] = del_cursor_start[c] + delta |
| 108 | + active_points -= delta |
| 109 | + active_points_in_cluster[c] -= delta |
| 110 | + print('del [', del_cursor_start[c], ',', del_cursor_end[c], |
| 111 | + ') active:', int(active_points_in_cluster[c]), |
| 112 | + 'total:', active_points) |
| 113 | + entry = [{'operation': 'delete'}, {'start': int(del_cursor_start[c])}, {'end': int(del_cursor_end[c])}] |
| 114 | + operation_list.append((num_operations, entry)) |
| 115 | + num_operations += 1 |
| 116 | + operation_list.append((num_operations, [{'operation': 'search'}])) |
| 117 | + num_operations += 1 |
| 118 | + del_cursor_start[c] = del_cursor_end[c] |
| 119 | + |
| 120 | + |
| 121 | + with open(output_yaml_file, 'w') as yf: |
| 122 | + operation_list.sort(key = lambda x: x[0]) |
| 123 | + sorted_dict = {} |
| 124 | + sorted_dict['max_pts'] = int(max_pts) |
| 125 | + for (k, v) in operation_list: |
| 126 | + sorted_dict[k]=v |
| 127 | + yaml_object = {} |
| 128 | + yaml_object[dataset_str] = sorted_dict |
| 129 | + yaml.dump(yaml_object, yf) |
| 130 | + |
| 131 | + |
| 132 | +def main(): |
| 133 | + parser = argparse.ArgumentParser( |
| 134 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 135 | + |
| 136 | + parser.add_argument( |
| 137 | + '--dataset', |
| 138 | + choices=DATASETS.keys(), |
| 139 | + required=True) |
| 140 | + parser.add_argument( |
| 141 | + '-c', '--num_clusters', |
| 142 | + type=int, |
| 143 | + required=True |
| 144 | + ) |
| 145 | + parser.add_argument( |
| 146 | + '-o', '--output_data_file', |
| 147 | + required=True |
| 148 | + ) |
| 149 | + parser.add_argument( |
| 150 | + '-y', '--output_yaml_file', |
| 151 | + required=True |
| 152 | + ) |
| 153 | + args = parser.parse_args() |
| 154 | + |
| 155 | + ds = DATASETS[args.dataset]() |
| 156 | + if ds.nb <= 10**7: |
| 157 | + data = ds.get_dataset() |
| 158 | + else: |
| 159 | + data = next(ds.get_dataset_iterator(bs=ds.nb)) |
| 160 | + print(np.shape(data)) |
| 161 | + |
| 162 | + offsets, permutation = cluster_and_permute(data, args.num_clusters) |
| 163 | + print(permutation) |
| 164 | + |
| 165 | + write_permuated_data(data=data, |
| 166 | + permutation=permutation, |
| 167 | + output_data_file=args.output_data_file) |
| 168 | + |
| 169 | + create_runbook(dataset_str=args.dataset, |
| 170 | + offsets=offsets, |
| 171 | + permutation=permutation, |
| 172 | + num_clusters=args.num_clusters, |
| 173 | + output_yaml_file=args.output_yaml_file) |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == '__main__': |
| 177 | + main() |
0 commit comments