def __init__(self,base_dir, cal=None, cal_each_mask=False): ''' A Decoder for a Vector Coded Aperture Measurment System Parameters ----------- base_dir : str, or `unipath.Path` the base directory which holds all data cal: `skrf.Calibration` object or None the calibration template that is copied for each mask's calibration. The `measurements` attribute provided by the calibraition template is never used, only the `ideals`. if None, then no calibration is performed cal_each_mask : bool should a calibration be performed for each mask? If True, this requires that `cal` represents a calibration template, for which the `measurements` are provided for each mask dir ''' self.base_dir = Path( base_dir) self.cal = cal self.cal_each_mask = cal_each_mask # determine rank max_dec = max([int(d) for d in self.decs.keys()]) self.rank = int(sqrt(len('{0:b}'.format(max_dec)))) self.frequency = rf.ran(str(self.decs.values()[0])).values()[0].frequency
def decode_with_rotation(dir_, f="634ghz", cal=None, averaging=True): """ decode a hadamard-encoded dataset at a given frequency Examples ---------- f = '634ghz' dir_= '../CAI/Bar Image/hadamard_2/Primary' a = decode(dir_=dir_, f = f,averaging =True) matshow(rf.complex_2_db(a)) cb = colorbar() #clim(-26,-25) grid(0) """ hexs = os.listdir(dir_) # determine resolution and rank res = int(sqrt(len(hexs))) rank = int(log2(res)) # calculate inverse hadamard-delta transform`T` # this can be pre-computed bins = [hex2bin(k, rank=rank) for k in hexs] T = array([array(list(k), dtype=int) for k in bins]) T_inv = inv(T) # create measurement frame `M` in hadamard space. M = [] for k in hexs: b = array(list(hex2bin(k, rank=rank)), dtype=int) n = rf.ran(dir_ + "/" + k) if cal is not None: n = cal.apply_cal_to_list(n) if averaging: n = rf.average(n.values()) else: n = n[sorted(n.keys())[-1]] s = n[f].s[0, 0, 0] # pull out single complex number m = s * b M.append(m) M = array(M) # transform the frame `M` in hadamard space to # frame `A` in delta space A = T_inv.dot(M) # pixels lay along the diagonal. A_diag = array([A[k, k] for k in range(res ** 2)]) # reshape the diagonal matrix into the image a = A_diag.reshape(res, res) return a, A
def decode_with_rotation(dir_, f='634ghz', cal=None, averaging=True): ''' decode a hadamard-encoded dataset at a given frequency Examples ---------- f = '634ghz' dir_= '../CAI/Bar Image/hadamard_2/Primary' a = decode(dir_=dir_, f = f,averaging =True) matshow(rf.complex_2_db(a)) cb = colorbar() #clim(-26,-25) grid(0) ''' hexs = os.listdir(dir_) #determine resolution and rank res = int(sqrt(len(hexs))) rank = int(log2(res)) # calculate inverse hadamard-delta transform`T` # this can be pre-computed bins = [hex2bin(k, rank=rank) for k in hexs] T = array([array(list(k), dtype=int) for k in bins]) T_inv = inv(T) # create measurement frame `M` in hadamard space. M = [] for k in hexs: b = array(list(hex2bin(k, rank=rank)), dtype=int) n = rf.ran(dir_ + '/' + k) if cal is not None: n = cal.apply_cal_to_list(n) if averaging: n = rf.average(n.values()) else: n = n[sorted(n.keys())[-1]] s = n[f].s[0, 0, 0] # pull out single complex number m = s * b M.append(m) M = array(M) # transform the frame `M` in hadamard space to # frame `A` in delta space A = T_inv.dot(M) # pixels lay along the diagonal. A_diag = array([A[k, k] for k in range(res**2)]) #reshape the diagonal matrix into the image a = A_diag.reshape(res, res) return a, A
def raw_ntwk_of(self,dec,name): ''' raw ntwk for a given mask, or pixel ''' if isinstance(dec, tuple): ntwks = [self.raw_ntwk_of(k,name) for k in self.pixel2decs(*dec)] return rf.average(ntwks) ntwk = rf.ran(str(self.decs[dec]), contains=name).values()[0] return ntwk
def da(self): ''' a xarray.DataArray object representing the entire data-set ''' # return cached value if it exists if self._da is not None: return self._da hexs= self.hexs #determine resolution and rank res = int(sqrt(len(hexs))) rank = int(log2(res)) M=[] # will hold weighted masks for k in hexs: n = rf.ran(self.dir_+'/'+k) if self.cal is not None: n = self.cal.apply_cal_to_list(n) if self.averaging: n = rf.average(n.values()) else: n = n[sorted(n.keys())[-1]] s = n.s[:,0,0].reshape(-1,1,1) # pull out single complex number m = hex2mask(k,rank=rank) #copy mask allong frequency dimension m = expand_dims(m,0).repeat(s.shape[0],0) m = m*s M.append(m) M= array(M) n.frequency.unit='ghz' da = DataArray(M, coords=[('mask_hex',hexs), ('f_ghz',n.frequency.f_scaled), ('row',range(res)), ('col',range(res))]) self._da = da # cache it return da
def da(self): ''' a xarray.DataArray object representing the entire data-set ''' # return cached value if it exists if self._da is not None and caching: return self._da hexs = self.hexs res = self.res rank = self.rank M = [] # will hold weighted masks for k in hexs: n = rf.ran(self.dir_ + '/' + k) if self.cal is not None: n = self.cal.apply_cal_to_list(n) if self.averaging: n = rf.average(n.values()) else: n = n[sorted(n.keys())[-1]] s = n.s[:, 0, 0].reshape(-1, 1, 1) # pull out single complex number m = hex2mask(k, rank=rank) #copy mask allong frequency dimension m = expand_dims(m, 0).repeat(s.shape[0], 0) m = m * s M.append(m) M = array(M) n.frequency.unit = 'ghz' da = DataArray(M, coords=[('mask_hex', hexs), ('f_ghz', n.frequency.f_scaled), ('row', range(res)), ('col', range(res))]) if caching: self._da = da return da
def cal_of(self,dec): ''' Calibration for a given mask, or pixel ''' ##TODO: for no-cal or static cal this could be only calculated # once to improve performance if self.cal is None: freq = self.frequency n = len(freq) coefs ={'directivity':zeros(n), 'source match': zeros(n), 'reflection tracking':ones(n)} cal =OnePort.from_coefs(frequency=freq, coefs=coefs) return cal if not self.cal_each_mask: return self.cal else: # we want a calibration for each mask, so create the calbration # for this mask,or pixel cal = deepcopy(self.cal) ideals = cal.ideals if isinstance(dec, tuple): # decode the measurements measured =[] for ideal in ideals: m = self.raw_ntwk_of(dec,ideal.name) measured.append(m) else: measured = rf.ran(self.decs[dec]).values() cal.measured, cal.ideals = rf.align_measured_ideals(measured,ideals) cal.name = str(dec) return cal
def frequency(self): return rf.ran(self.dir_+'/'+self.hexs[0]).values()[0].frequency
def _get_ntwk_dict(self): try: return rf.ran(self.input_dir) except(OSError): return {}
def frequency(self): return rf.ran(self.dir_ + '/' + self.hexs[0]).values()[0].frequency