def heightmap_to_psf(hyps, height_map): resolution = hyps['resolution'] focal_length = hyps['focal_length'] wavelength = hyps['wavelength'] pixel_pitch = hyps['pixel_pitch'] refractive_idc = hyps['refractive_idc'] r_cutoff = hyps['r_cutoff'] input_field = torch.ones((resolution, resolution)) phase_delay = utils.heightmap_to_phase(height_map, wavelength, refractive_idc) field = propagate_through_lens(input_field, phase_delay) field = circular_aperture(field, r_cutoff) # propagate field from aperture to sensor element = Propagation(kernel_type='fresnel', propagation_distances=focal_length, slm_resolution=[resolution, resolution], slm_pixel_pitch=[pixel_pitch, pixel_pitch], wavelength=wavelength) field = element.forward(field) psf = utils.field_to_intensity(field) psf /= psf.sum() return psf.to(DEVICE)
def __init__(self, seed: int, alpha: float, std: float, grid_length: int, cell_length: int, sensor_density: int, noise_floor: int): self.seed = seed self.alpha = alpha self.std = std self.grid_length = grid_length self.cell_length = cell_length self.sensor_density = sensor_density self.noise_floor = noise_floor self.propagation = Propagation(self.alpha, self.std)
def forward(self, x): # model point from infinity input_field = torch.ones((self.resolution, self.resolution)) phase_delay = utils.heightmap_to_phase(self.heightmap, self.wavelength, self.refractive_idc) field = optics.propagate_through_lens(input_field, phase_delay) field = optics.circular_aperture(field, self.r_cutoff) # kernel_type = 'fresnel_conv' leads to nans element = Propagation(kernel_type='fresnel', propagation_distances=self.focal_length, slm_resolution=[self.resolution, self.resolution], slm_pixel_pitch=[self.pixel_pitch, self.pixel_pitch], wavelength=self.wavelength) field = element.forward(field) psf = utils.field_to_intensity(field) psf /= psf.sum() final = optics.convolve_img(x, psf) if not self.use_wiener: return final.to(DEVICE) else: # perform Wiener filtering final = final.to(DEVICE) imag = torch.zeros(final.shape).to(DEVICE) img = utils.stack_complex(final, imag) img_fft = torch.fft(utils.ifftshift(img), 2) otf = optics.psf2otf(psf, output_size=img.shape[2:4]) otf = torch.stack((otf, otf, otf), 0) otf = torch.unsqueeze(otf, 0) conj_otf = utils.conj(otf) otf_img = utils.mul_complex(img_fft, conj_otf) denominator = optics.abs_complex(otf) denominator[:, :, :, :, 0] += self.K product = utils.div_complex(otf_img, denominator) filtered = utils.ifftshift(torch.ifft(product, 2)) filtered = torch.clamp(filtered, min=1e-5) return filtered[:, :, :, :, 0]
import json import falcon from execution import Execution from termination import Termination from propagation import Propagation from addition import Addition from deletion import Deletion from getting_list import GettingList import os with open('dejima_config.json') as f: dejima_config_dict = json.load(f) peer_name = os.environ['PEER_NAME'] db_conn_dict={} # key: xid, value: database connection for each xid transaction. child_peer_dict = {} # key: xid, value: set of child peers for each xid transaction. app = falcon.API() app.add_route("/post_transaction", Execution(peer_name, db_conn_dict, child_peer_dict, dejima_config_dict)) app.add_route("/add_student", Addition(peer_name, db_conn_dict, child_peer_dict, dejima_config_dict)) app.add_route("/delete_student", Deletion(peer_name, db_conn_dict, child_peer_dict, dejima_config_dict)) app.add_route("/get_student_list", GettingList(peer_name, db_conn_dict, child_peer_dict, dejima_config_dict)) app.add_route("/_propagate", Propagation(peer_name, db_conn_dict, child_peer_dict, dejima_config_dict)) app.add_route("/_terminate_transaction", Termination(db_conn_dict, child_peer_dict, dejima_config_dict)) if __name__ == "__main__": from wsgiref import simple_server httpd = simple_server.make_server("0.0.0.0", 8000, app) httpd.serve_forever()
def setValues(): """Default settings :Returns: - Default settings for the probabilistic models for degradation of concrete. """ # Concrete settings concrete = Concrete('C25/30') concrete.setWCratio(0.4) # values: (0.3),0.4,(0.45),0.5 concrete.setCuringPeriod(1) # values: 1,3,7,28 concrete.setGrade(45) # values: 45,40,25,35 # Reinforcement settings reinforcement = Reinforcement('S500') reinforcement.setYieldStress(500) # values: all reinforcement.setDiameter(16) # values: (8),10,16,27 reinforcement.setBars(1) # values: all # Geometry settings geometrie = Geometrie('Beam') geometrie.setCover(30) # values: all geometrie.setBeamWidth(350) geometrie.setBeamHeight(550) geometrie.setBeamLength(5000) # Environment settings environment = Environment() environment.setZone('Submerged') # values: 'Submerged','Tidal','Splash','Atmospheric' environment.setHumidity(80) # values: 50,65,80,95,100 # for the simplified corrosion rate: # environment.setExposure('Wet-Dry') # values: 'Wet','Wet-Dry','Airborne sea water','Tidal' environment.setTemperature(20) # values: all environment.setShelter('Unsheltered') # 'Sheltered','Unsheltered' # Chloride chloride = Chloride(concrete, geometrie, environment) # Carbonation carbonation = Carbonation(concrete, geometrie, environment) # Propagation rate = Propagation(environment) # Corrosion pitting = Pitting(reinforcement, rate) # pitting.setDeltaTime(50) # values: all # Resistance resistance = Resistance(concrete, reinforcement, geometrie, rate, pitting) return concrete, reinforcement, geometrie, environment, chloride, carbonation, rate, pitting, resistance
class GenerateData: '''generate training data using a propagation model ''' def __init__(self, seed: int, alpha: float, std: float, grid_length: int, cell_length: int, sensor_density: int, noise_floor: int): self.seed = seed self.alpha = alpha self.std = std self.grid_length = grid_length self.cell_length = cell_length self.sensor_density = sensor_density self.noise_floor = noise_floor self.propagation = Propagation(self.alpha, self.std) def log(self, power, cell_percentage, sample_per_label, sensor_file, root_dir, num_tx, num_tx_upper, min_dist, max_dist): '''the meta data of the data ''' with open(root_dir + '.txt', 'w') as f: f.write(f'seed = {self.seed}\n') f.write(f'alpha = {self.alpha}\n') f.write(f'std = {self.std}\n') f.write(f'grid length = {self.grid_length}\n') f.write(f'cell length = {self.cell_length}\n') f.write(f'sensor density = {self.sensor_density}\n') f.write(f'noise floor = {self.noise_floor}\n') f.write(f'power = {power}\n') f.write(f'cell percentage = {cell_percentage}\n') f.write(f'sample per label = {sample_per_label}\n') f.write(f'sensor file = {sensor_file}\n') f.write(f'root file = {root_dir}\n') f.write(f'number of TX = {num_tx}\n') f.write(f'num TX upperbound = {num_tx_upper}\n') f.write(f'min distance = {min_dist}\n') f.write(f'max distance = {max_dist}\n') def generate(self, power: float, cell_percentage: float, sample_per_label: int, sensor_file: str, root_dir: str, num_tx: int, num_tx_upper: bool, min_dist: int, max_dist: int): ''' The generated input data is not images, but instead matrix. Because saving as images will loss some accuracy Args: power -- the power of the transmitter cell_percentage -- percentage of cells being label (see if all discrete location needs to be labels) sample_per_label -- samples per cell sensor_file -- sensor location file root_dir -- the output directory ''' Utility.remove_make(root_dir) self.log(power, cell_percentage, sample_per_label, sensor_file, root_dir, num_tx, num_tx_upper, min_dist, max_dist) random.seed(self.seed) np.random.seed(self.seed) # 1 read the sensor file, do a checking if str(self.grid_length) not in sensor_file[:sensor_file.find('-')]: print( f'grid length {self.grid_length} and sensor file {sensor_file} not match' ) sensors = [] with open(sensor_file, 'r') as f: indx = 0 for line in f: x, y = line.split() sensors.append(Sensor(int(x), int(y), indx)) indx += 1 # 2 start from (0, 0), generate data, might skip some locations label_count = int(self.grid_length * self.grid_length * cell_percentage) population = [(i, j) for j in range(self.grid_length) for i in range(self.grid_length)] labels = random.sample(population, label_count) counter = 0 for label in sorted(labels): tx = label # each label create a directory tx_float = (tx[0] + random.uniform(0, 1), tx[1] + random.uniform(0, 1)) if counter % 100 == 0: print(f'{counter/len(labels)*100}%') folder = f'{root_dir}/{counter:06d}' os.mkdir( folder ) # update on Aug. 27, change the name of the folder from label to counter index for i in range(sample_per_label): targets = [tx_float] grid = np.zeros((self.grid_length, self.grid_length)) grid.fill(Default.noise_floor) for sensor in sensors: dist = Utility.distance_propagation( tx_float, (sensor.x, sensor.y)) * Default.cell_length pathloss = self.propagation.pathloss(dist) rssi = power - pathloss grid[sensor.x][ sensor. y] = rssi if rssi > Default.noise_floor else Default.noise_floor # the other TX population_set = set(population) if num_tx_upper is False: num_tx_copy = num_tx else: num_tx_copy = random.randint(1, num_tx) intru = tx while num_tx_copy > 1: # get one new TX at a time self.update_population(population_set, intru, Default.grid_length, min_dist, max_dist) ntx = random.sample(population_set, 1)[0] ntx = (ntx[0] + random.uniform(0, 1), ntx[1] + random.uniform(0, 1) ) # TX is not at the center of grid cell targets.append(ntx) for sensor in sensors: dist = Utility.distance_propagation( ntx, (sensor.x, sensor.y)) * Default.cell_length pathloss = self.propagation.pathloss(dist) rssi = power - pathloss exist_rssi = grid[sensor.x][sensor.y] grid[sensor.x][sensor.y] = Utility.linear2db( Utility.db2linear(exist_rssi) + Utility.db2linear(rssi)) num_tx_copy -= 1 intru = ntx np.save(f'{folder}/{i}.npy', grid.astype(np.float32)) np.save(f'{folder}/{i}.target', np.array(targets).astype(np.float32)) if i == 0: imageio.imwrite(f'{folder}/{tx}.png', grid) counter += 1 def update_population(self, population_set, intruder, grid_len, min_dist, max_dist): '''Update the population (the TX candidate locations) Args: population_set -- set -- the TX candidate locations intruder -- tuple<int, int> -- the new selected TX grid_len -- int -- grid length min_dist -- int -- minimum distance between two TX. for far away sensors max_dist -- int -- maximum distance between two TX. for close by sensors ''' if max_dist is None: cur_x = intruder[0] cur_y = intruder[1] for x in range(-min_dist, min_dist + 1): for y in range(-min_dist, min_dist + 1): nxt_x = cur_x + x nxt_y = cur_y + y if 0 <= nxt_x < grid_len and 0 <= nxt_y < grid_len and Utility.distance( intruder, (nxt_x, nxt_y)) < min_dist: if (nxt_x, nxt_y) in population_set: population_set.remove((nxt_x, nxt_y)) else: cur_x = intruder[0] cur_y = intruder[1] for x, y in population_set.copy(): if not (min_dist <= Utility.distance(intruder, (x, y)) <= max_dist): population_set.remove((x, y))
class GenerateData: '''generate training data using a propagation model ''' def __init__(self, seed: int, alpha: float, std: float, grid_length: int, cell_length: int, sensor_density: int, noise_floor: int): self.seed = seed self.alpha = alpha self.std = std self.grid_length = grid_length self.cell_length = cell_length self.sensor_density = sensor_density self.noise_floor = noise_floor self.propagation = Propagation(self.alpha, self.std) def log(self, power, cell_percentage, sample_per_label, sensor_file, root_dir, num_tx, num_tx_upper, min_dist, max_dist, edge, splat): '''the meta data of the data ''' with open(root_dir + '.txt', 'w') as f: f.write(f'seed = {self.seed}\n') f.write(f'alpha = {self.alpha}\n') f.write(f'std = {self.std}\n') f.write(f'grid length = {self.grid_length}\n') f.write(f'cell length = {self.cell_length}\n') f.write(f'sensor density = {self.sensor_density}\n') f.write(f'noise floor = {self.noise_floor}\n') f.write(f'power = {power}\n') f.write(f'cell percentage = {cell_percentage}\n') f.write(f'sample per label = {sample_per_label}\n') f.write(f'sensor file = {sensor_file}\n') f.write(f'root file = {root_dir}\n') f.write(f'number of TX = {num_tx}\n') f.write(f'num TX upperbound = {num_tx_upper}\n') f.write(f'min distance = {min_dist}\n') f.write(f'max distance = {max_dist}\n') f.write(f'edge = {edge}\n') f.write(f'splat = {splat}\n') def check_edge(self, tx, edge): '''if tx is at the edge, return false Args: tx -- tuple<int, int> ''' if edge <= tx[0] < self.grid_length-edge and edge <= tx[1] < self.grid_length-edge: return True return False def generate(self, power: float, cell_percentage: float, sample_per_label: int, sensor_file: str,\ root_dir: str, num_tx: int, num_tx_upper: bool, min_dist: int, max_dist: int, edge: int, vary_power: int, splat: bool): ''' The generated input data is not images, but instead matrix. Because saving as images will loss some accuracy Args: power -- the power of the transmitter cell_percentage -- percentage of cells being label (see if all discrete location needs to be labels) sample_per_label -- samples per cell sensor_file -- sensor location file root_dir -- the output directory ''' Utility.remove_make(root_dir) self.log(power, cell_percentage, sample_per_label, sensor_file, root_dir, num_tx, num_tx_upper, min_dist, max_dist, edge, splat) # random.seed(self.seed) # need to comment this line when running simulate_data.py, or they will generate the same TX locations # 1 read the sensor file, do a checking if str(self.grid_length) not in sensor_file[:sensor_file.find('-')]: print(f'grid length {self.grid_length} and sensor file {sensor_file} not match') sensors = [] with open(sensor_file, 'r') as f: indx = 0 for line in f: x, y = line.split() sensors.append(Sensor(int(x), int(y), indx)) indx += 1 # 2 start from (0, 0), generate data, might skip some locations population = [(i, j) for i in range(self.grid_length) for j in range(self.grid_length) if self.check_edge((i, j), edge)] # Tx is not at edge label_count = int(len(population)*cell_percentage) labels = random.sample(population, label_count) counter = 0 for label in sorted(labels): tx = label # each label create a directory tx_float = (tx[0] + random.uniform(0, 1), tx[1] + random.uniform(0, 1)) if counter % 100 == 0: print(f'{counter/len(labels)*100}%') folder = f'{root_dir}/{counter:06d}' os.mkdir(folder) # update on Aug. 27, change the name of the folder from label to counter index for i in range(sample_per_label): power_delta = random.uniform(-vary_power, vary_power) targets = [tx_float] # ground truth location power_deltas = [power_delta] # ground truth power grid = np.zeros((self.grid_length, self.grid_length)) grid.fill(Default.noise_floor) for sensor in sensors: if not splat: dist = Utility.distance_propagation(tx_float, (sensor.x, sensor.y)) * Default.cell_length pathloss = self.propagation.pathloss(dist) else: pathloss = mysplat.pathloss(tx_float[0], tx_float[1], sensor.x, sensor.y) + random.uniform(-0.25, 0.25) rssi = (power + power_delta) - pathloss grid[sensor.x][sensor.y] = rssi if rssi > Default.noise_floor else Default.noise_floor # the other TX population_set = set(population) if num_tx_upper is False: num_tx_copy = num_tx else: num_tx_copy = random.randint(1, num_tx) intru = tx while num_tx_copy > 1: # get one new TX at a time self.update_population(population_set, intru, Default.grid_length, min_dist, max_dist) ntx = random.sample(population_set, 1)[0] ntx = (ntx[0] + random.uniform(0, 1), ntx[1] + random.uniform(0, 1)) # TX is not at the center of grid cell power_delta = random.uniform(-vary_power, vary_power) targets.append(ntx) power_deltas.append(power_delta) for sensor in sensors: if not splat: dist = Utility.distance_propagation(ntx, (sensor.x, sensor.y)) * Default.cell_length pathloss = self.propagation.pathloss(dist) else: pathloss = mysplat.pathloss(ntx[0], ntx[1], sensor.x, sensor.y) + random.uniform(-0.25, 0.25) rssi = (power + power_delta) - pathloss exist_rssi = grid[sensor.x][sensor.y] grid[sensor.x][sensor.y] = Utility.linear2db(Utility.db2linear(exist_rssi) + Utility.db2linear(rssi)) num_tx_copy -= 1 intru = ntx np.save(f'{folder}/{i}.npy', grid.astype(np.float32)) np.save(f'{folder}/{i}.target', np.array(targets).astype(np.float32)) np.savetxt(f'{folder}/{i}.txt', np.array(power_deltas).astype(np.float32)) # Dec. 12, 2020: save in txt format (currently not used in the CNN) self.save_ipsn_input(grid, targets, power_deltas, sensors, f'{folder}/{i}.json') # if i == 0: # imageio.imwrite(f'{folder}/{tx}.png', grid) counter += 1 def save_ipsn_input(self, grid, targets, power_deltas, sensors, output_file): ''' Args: grid -- np.ndarray, n = 2 targets -- list<tuple<float, float>> power_deltas -- list<float> -- power of the TX, varying sensors -- list<Sensors> output_file -- str ''' tx_data = {} for i, tx in enumerate(targets): tx_data[str(i)] = { "location": (round(tx[0], 3), round(tx[1], 3)), "gain": round(power_deltas[i], 3) } sensor_data = {} for i, sen in enumerate(sensors): sensor_data[str(i)] = round(grid[sen.x][sen.y], 3) ipsn_input = IpsnInput(tx_data, sensor_data) with open(output_file, 'w') as f: f.write(ipsn_input.to_json_str()) def generate_ipsn(self, power: str, sensor_file: str, root_dir: str, splat: bool): '''generate the training data for the IPSN20 localization algorithm ''' # sensors Utility.remove_make(root_dir) sensors = [] with open(sensor_file, 'r') as f, open(root_dir + '/sensors', 'w') as f_out: indx = 0 for line in f: x, y = line.split() f_out.write('{} {} {} {}\n'.format(x, y, 1, 1)) # uniform cost sensors.append(Sensor(int(x), int(y), indx)) indx += 1 # covariance matrix sen_num = len(sensors) with open(root_dir + '/cov', 'w') as f: cov = np.zeros((sen_num, sen_num)) for i in range(sen_num): for j in range(sen_num): if i == j: cov[i, j] = 1 # assume the std is 1 f.write('{} '.format(cov[i, j])) f.write('\n') # hypothesis total_loc = self.grid_length*self.grid_length with open(root_dir + '/hypothesis', 'w') as f: for tx_1dindex in range(total_loc): t_x = tx_1dindex // self.grid_length t_y = tx_1dindex % self.grid_length for sen in sensors: if not splat: dist = Utility.distance_propagation((t_x, t_y), (sen.x, sen.y)) * Default.cell_length pathloss = self.propagation.pathloss(dist) else: pathloss = mysplat.pathloss(t_x, t_y, sen.x, sen.y) + random.uniform(-0.25, 0.25) f.write('{} {} {} {} {:.3f} {}\n'.format(t_x, t_y, sen.x, sen.y, power - pathloss, 1)) def update_population(self, population_set, intruder, grid_len, min_dist, max_dist): '''Update the population (the TX candidate locations) Args: population_set -- set -- the TX candidate locations intruder -- tuple<int, int> -- the new selected TX grid_len -- int -- grid length min_dist -- int -- minimum distance between two TX. for far away sensors max_dist -- int -- maximum distance between two TX. for close by sensors ''' if max_dist is None: cur_x = intruder[0] cur_y = intruder[1] for x in range(-min_dist, min_dist+1): for y in range(-min_dist, min_dist+1): nxt_x = cur_x + x nxt_y = cur_y + y if 0 <= nxt_x < grid_len and 0 <= nxt_y < grid_len and Utility.distance(intruder, (nxt_x, nxt_y)) < min_dist: if (nxt_x, nxt_y) in population_set: population_set.remove((nxt_x, nxt_y)) else: cur_x = intruder[0] cur_y = intruder[1] for x, y in population_set.copy(): if not (min_dist <= Utility.distance(intruder, (x, y)) <= max_dist): population_set.remove((x, y))
from primaryproc import PrimaryProc from propagation import Propagation, SimpleTarget, coords, coord_sys #, CloudTarget, EPRGen import numpy as np #from tqdm import tqdm st = SimpleTarget( 100, coords(75000, 0, 180 * math.pi / 180, coord_sys["spherical"]), coords(200, 0, 0.1, coord_sys["spherical"])) #st1 = SimpleTarget(10, coords(100000, 0, 45 * math.pi / 180, coord_sys["spherical"]), # coords(0, 0, 0.1, coord_sys["spherical"])) #cl = create_cloud(50000, 80000, 0, 0) #gen = EPRGen() pr = Propagation() pr.targets = [st] #+ gen.get() pp = PrimaryProc() class SpiralWidget(QGLWidget): ''' Widget for drawing two spirals. ''' def __init__(self, parent): QGLWidget.__init__(self, parent) self.setMinimumSize(500, 500) self.timerticks = 0 self.targets = [] self.settings = { "IKO": {