[822] | 1 | |
---|
| 2 | from __future__ import print_function |
---|
| 3 | import os |
---|
| 4 | import subprocess |
---|
| 5 | import sys |
---|
[826] | 6 | import shutil |
---|
| 7 | import random |
---|
[822] | 8 | |
---|
| 9 | |
---|
| 10 | def short_path(name): |
---|
| 11 | abs_path = os.path.abspath(name) |
---|
| 12 | rel_path = os.path.relpath(name) |
---|
| 13 | if len(abs_path) < len(rel_path): |
---|
| 14 | return abs_path |
---|
| 15 | else: |
---|
| 16 | return rel_path |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | def check_pyconf_file(pyconf_file): |
---|
| 20 | if not os.path.isfile(pyconf_file): |
---|
| 21 | help_str = ''' |
---|
| 22 | You should create a file named %s in this directory with the following definitions: |
---|
| 23 | - ... (to complete) |
---|
| 24 | Optional definitions: |
---|
| 25 | - dsk_dir: path to a local storage (typically in /dsk/l1/misc) to store generated data and images |
---|
| 26 | *** Stopping execution |
---|
| 27 | ''' % (short_path(pyconf_file)) |
---|
| 28 | print(help_str) |
---|
| 29 | sys.exit() |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | def my_mkdir(abs_dirname): |
---|
| 34 | dirname = short_path(abs_dirname) |
---|
| 35 | try: |
---|
| 36 | print("mkdir %s" % (dirname)) |
---|
| 37 | os.mkdir(dirname) |
---|
| 38 | except OSError: |
---|
| 39 | print("*** Error: impossible to create directory %s" % (dirname), file = sys.stderr) |
---|
| 40 | sys.exit(1) |
---|
| 41 | |
---|
| 42 | def my_chdir(abs_dirname): |
---|
| 43 | dirname = short_path(abs_dirname) |
---|
| 44 | print("cd %s" % (dirname)); |
---|
| 45 | os.chdir(dirname) |
---|
| 46 | |
---|
| 47 | def my_cp(abs_src, abs_dst): |
---|
| 48 | src = short_path(abs_src) |
---|
| 49 | dst = short_path(abs_dst) |
---|
| 50 | print("cp %s %s" % (src, dst)) |
---|
| 51 | shutil.copy(src, dst) |
---|
| 52 | |
---|
[826] | 53 | def my_symlink(target, link_name): |
---|
| 54 | if not os.path.exists(link_name): |
---|
| 55 | print("ln -s %s %s" % (short_path(target), short_path(link_name))) |
---|
| 56 | os.symlink(target, link_name) |
---|
[822] | 57 | |
---|
[826] | 58 | |
---|
[822] | 59 | def get_random_img_file(density, x, y, granularity, seed): |
---|
| 60 | density_str = str(density) |
---|
| 61 | if density < 10: |
---|
| 62 | density_str = "0" + density_str |
---|
| 63 | if density < 100: |
---|
| 64 | density_str = "0" + density_str |
---|
| 65 | |
---|
| 66 | return "rand_" + str(x) + "_" + str(y) + "_" + density_str + "_G" + str(granularity) + ".pgm" |
---|
| 67 | |
---|
| 68 | |
---|
[826] | 69 | # for the HDD constraint of 8 chars |
---|
| 70 | def get_short_random_img_file(density, granularity): |
---|
| 71 | density_str = str(density) |
---|
| 72 | granularity_str = str(granularity) |
---|
| 73 | if density < 10: |
---|
| 74 | density_str = "0" + density_str |
---|
| 75 | if density < 100: |
---|
| 76 | density_str = "0" + density_str |
---|
| 77 | if granularity < 10: |
---|
| 78 | granularity_str = "0" + granularity_str |
---|
| 79 | |
---|
| 80 | return "G" + granularity_str + "_D" + density_str + ".pgm" |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | def get_dirname(dir, nthreads, config, ftrs, granularity, start_den, end_den, basename): |
---|
| 85 | start_den_str = str(start_den) |
---|
| 86 | end_den_str = str(end_den) |
---|
| 87 | if start_den < 10: |
---|
| 88 | start_den_str = "0" + start_den_str |
---|
| 89 | if start_den < 100: |
---|
| 90 | start_den_str = "0" + start_den_str |
---|
| 91 | if end_den < 10: |
---|
| 92 | end_den_str = "0" + end_den_str |
---|
| 93 | if end_den < 100: |
---|
| 94 | end_den_str = "0" + end_den_str |
---|
| 95 | |
---|
[822] | 96 | if ftrs: |
---|
| 97 | ftrs_str = "_FEATURES_1" |
---|
| 98 | else: |
---|
| 99 | ftrs_str = "_FEATURES_0" |
---|
| 100 | |
---|
| 101 | config_keys = list(config.keys()) # duplicates list |
---|
| 102 | try: |
---|
| 103 | config_keys.remove('FEATURES') |
---|
| 104 | except: |
---|
| 105 | pass |
---|
| 106 | config_keys = sorted(config_keys) |
---|
[826] | 107 | dirname = os.path.join(dir, basename + "_T" + str(nthreads) + "_G" + str(granularity) + "_D" + start_den_str + "-" + end_den_str + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ftrs_str) |
---|
[822] | 108 | |
---|
[826] | 109 | return dirname |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | def get_graph_filename(dir, nthreads, granularity, config, ext): |
---|
| 113 | config_keys = list(config.keys()) # duplicates list |
---|
| 114 | config_keys = sorted(config_keys) |
---|
| 115 | filename = os.path.join(dir, "random_T" + str(nthreads) + "_G" + str(granularity) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ext) |
---|
| 116 | |
---|
[822] | 117 | return filename |
---|
| 118 | |
---|
| 119 | |
---|
[826] | 120 | |
---|
| 121 | def get_filename(dir, nthreads, config, ftrs, basename): |
---|
| 122 | if ftrs: |
---|
| 123 | ftrs_str = "_FEATURES_1" |
---|
| 124 | else: |
---|
| 125 | ftrs_str = "_FEATURES_0" |
---|
| 126 | |
---|
[822] | 127 | config_keys = list(config.keys()) # duplicates list |
---|
[826] | 128 | try: |
---|
| 129 | config_keys.remove('FEATURES') |
---|
| 130 | except: |
---|
| 131 | pass |
---|
[822] | 132 | config_keys = sorted(config_keys) |
---|
[826] | 133 | filename = os.path.join(dir, basename + "_T" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ftrs_str + ".txt") |
---|
[822] | 134 | |
---|
| 135 | return filename |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | def print_and_call(cmd): |
---|
| 139 | print(subprocess.list2cmdline(cmd)) |
---|
| 140 | retval = subprocess.call(cmd) |
---|
| 141 | return retval |
---|
| 142 | |
---|
[826] | 143 | |
---|
| 144 | def print_call_and_check(cmd): |
---|
| 145 | retval = print_and_call(cmd) |
---|
| 146 | if retval != 0: |
---|
| 147 | print("*** Error: command '%s' returned %d\n" % (subprocess.list2cmdline(cmd), retval)) |
---|
| 148 | sys.exit(1) |
---|
| 149 | |
---|
| 150 | |
---|
[822] | 151 | def print_and_popen(cmd, abs_outfile): |
---|
| 152 | outfile = short_path(abs_outfile) |
---|
| 153 | print(subprocess.list2cmdline(cmd), end = "") |
---|
| 154 | print(" >", outfile) |
---|
| 155 | child = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
---|
| 156 | output = child.communicate()[0] |
---|
| 157 | retval = child.returncode |
---|
| 158 | |
---|
| 159 | # Write simulation results to log file |
---|
| 160 | file = open(outfile, 'w') |
---|
| 161 | file.write(output) |
---|
| 162 | file.close() |
---|
| 163 | |
---|
| 164 | if retval != 0: |
---|
| 165 | print("*** Error while executing command '%s'" % subprocess.list2cmdline(cmd)) |
---|
| 166 | sys.exit(1) |
---|
| 167 | |
---|
| 168 | return output |
---|
| 169 | |
---|
| 170 | |
---|
[826] | 171 | def gen_random_image(filename, x, y, granularity, density, seed): |
---|
| 172 | random.seed(seed) |
---|
| 173 | img = [[0 for a in range(x)] for b in range(y)] |
---|
| 174 | for i in range(0, x, granularity): |
---|
| 175 | for j in range(0, y, granularity): |
---|
| 176 | r = random.random(); |
---|
| 177 | if r < density: |
---|
| 178 | px = 255 |
---|
| 179 | else: |
---|
| 180 | px = 0 |
---|
| 181 | |
---|
| 182 | for di in range(0, granularity): |
---|
| 183 | for dj in range(0, granularity): |
---|
| 184 | if i + di < x and j + dj < y: |
---|
| 185 | img[i + di][j + dj] = px; |
---|
| 186 | |
---|
| 187 | f = open(filename, 'wb') |
---|
| 188 | f.write("P5\n%d %d\n255\n" % (x, y)) |
---|
| 189 | for j in range(0, y): |
---|
| 190 | bimg = bytearray(img[j]) |
---|
| 191 | f.write(bimg) |
---|
| 192 | f.close() |
---|
| 193 | |
---|
| 194 | |
---|