Пример #1
0
 def to_alignment(self):
     """Converts the profile to alignment format"""
     aln = alignment.alignment(self.env)
     _modeller.mod_profile_to_aln(prf=self.modpt,
                                  aln=aln.modpt,
                                  libs=self.env.libs.modpt)
     return aln
Пример #2
0
	def compute_matrices(self): #change to compute_matrixes
		#self.TDMA_MODE = 0;
		#eps_vec = .5*np.ones(self.N)
		self.eps_vec = 0*np.ones(self.N)
		#self.i = 1
		#while np.any(np.isnan(self.final_messages)):
		Kprime = len(self.map);
		if self.verbose:
			print 'Remaining ', Kprime, ' nodes are: '
			print self.map
			## special case for one remaining node
		if Kprime == 1:
			self.TOTAL_TRANSMISSIONS += 1
			while not transmit_messages(1, self.eps_vec[self.map]):
				self.TOTAL_TRANSMISSIONS += 1
			self.final_messages[self.map] = self.W[self.map]
		else:
			## Generate next m transmissions
			(self.V, U) = alignment('mixed', self.J, 1e-4, 100, False)
			#try changing it here??
			self.V = np.asarray(self.V)
			print 'new V is '
			print self.V
			#print 'type of new V is ', type(self.V)
			#print 'type of an element of new V is ', type(self.V[0,0])
			self.m = np.shape(self.V)[0]
			if self.verbose:
				print 'Minimum rank is ', self.m
			# generate next symbol based on current V
			L = len(self.tx_symbols);
			if self.i == 1:
				L = 0
			self.unsolved = np.ones(Kprime) > 0
Пример #3
0
def test_model_pathway():
    m = test_model1()
    bd = bigg_data.bigg_data(m.model.id)
    bd.load_metabolite_kegg_ids()
    bd.load_reaction_ECs()
    
    
    p = test_pathway1()
    p.load_ECs("test_pathway")
    
    align = alignment.alignment()
    align.load_model_pathway(p, m, bd)#, max_metabolite_occurance, compartment = "c")
    #align.load_pathway_pathway(p, p)
        
    distance_to_remove = 3
    #b, score = align.make_alignment(alignment_type = "Greedy", K_m = 3, to_many = 0, from_many = 0, remove_borders = 1, topological_weight = 0, homological_weight_EC = 10, homological_weight_KEGG = 10, homological_additive = 0, remove_too_distant = 1, remove_too_close = 1, distance_to_remove = distance_to_remove)
    b, score = align.make_alignment(alignment_type = "Greedy", K_m = 3, to_many = 0, from_many = 0, remove_borders = 1, topological_weight = 10, homological_weight_EC = 10, homological_weight_KEGG = 10, homological_additive = 0, remove_too_distant = 1, remove_too_close = 0, distance_to_remove = distance_to_remove)    
    align.print_alignment(b)
    #print()
    #print(align.KEGG_metabolites_dst)    
    #print()
    #print(align.KEGG_metabolites_src)
    #print()
    #print()
    align.print_T_sim()
    
    c = align.get_anchors()
    align.print_alignment(c)


    
    #align.T_sim[:,:] = 1
    #align.print_T_sim()
    #align.adjust_similarities(align.T_sim, 0, 5, 0, 0, remove_too_close=0, remove_too_distant=1, distance_to_remove=distance_to_remove)
    #align.print_T_sim()

        
    
    print()

    #paths.get_reaction_connectivity(m)
    #paths.shortest_path(m, "r1", "r_out_5", compartment = "c")
    
    #align.print_matrix("dst", "in")
    #align.print_matrix("dst", "out")
    
    #align.print_matrix("src", "in")
    #align.print_matrix("src", "out")
    
    #print(align.reactions_dst)
    #print()
    #for i in align.M_dst:
    #    for j in i:
    #        print(j, end= " ")
    #    print()
    return (align, m)
    def _makeAlignment(self):
        ''' Construct a pseudo-FASTA string to create a new alignment from. '''

        pseudofasta = ''
        sequences = self.database.iterRecords('alignment')
        for sequence in sequences:
            _, name, seq = sequence
            if name != '' and seq != '':
                pseudofasta += '>%s\n%s\n' % (name, seq)
        if pseudofasta != '':
            self.alignment = alignment(str(pseudofasta))
Пример #5
0
 def build_sequence(self, sequence, special_patches=None,
                    patch_default=True):
     """Build an extended chain from a string of one-letter residue codes"""
     a = alignment.alignment(self.env)
     a.append_sequence(sequence)
     self.clear_topology()
     self.generate_topology(a[0], patch_default=patch_default)
     if special_patches:
         special_patches(self)
     self.build(initialize_xyz=True, build_method='INTERNAL_COORDINATES')
     self.prottyp = 'structure'
Пример #6
0
 def _makeAlignment(self):
     
     ''' Construct a pseudo-FASTA string to create a new alignment from. '''
     
     pseudofasta = ''
     sequences = self.database.iterRecords('alignment')
     for sequence in sequences:
         _,name,seq = sequence
         if name != '' and seq != '':
             pseudofasta += '>%s\n%s\n' % (name,seq)
     if pseudofasta != '':
         self.alignment = alignment(str(pseudofasta))
Пример #7
0
 def compute_matrices(self):  #change to compute_matrixes
     #self.TDMA_MODE = 0;
     #eps_vec = .5*np.ones(self.N)
     self.eps_vec = 0 * np.ones(self.N)
     #self.i = 1
     #while np.any(np.isnan(self.final_messages)):
     Kprime = len(self.map)
     if self.verbose:
         print 'Remaining ', Kprime, ' nodes are: '
         print self.map
         ## special case for one remaining node
         #doesnt make sense anymore, must change
     if Kprime == 1:
         #smsgx.set_V_row(self.V[self.m_i,:])
         #self.TOTAL_TRANSMISSIONS += 1
         #while not transmit_messages(1, self.eps_vec[self.map]):
         #self.TOTAL_TRANSMISSIONS += 1
         #print 'map is ', self.map
         #print 'W is ', self.W
         #print 'final messages are', self.final_messages
         #self.final_messages[self.map] = self.W[self.map]
         #make a new V to transmit instead, just what W needs
         self.V = np.zeros((1, self.N))
         self.V[0, self.map] = 1
         self.m = 1
     else:
         ## Generate next m transmissions
         #(self.V, U) = alignment('mixed', self.J, 1e-4, 100, True)
         (self.V, U) = alignment('greedy', self.J, 1e-4, 100, False)
         #try changing it here??
         self.V = np.asarray(self.V)
         #RANDOM INSTEAD??? even easier, change right before transmission!
         #self.V = np.random.
         print 'new V is '
         print self.V
         #print 'type of new V is ', type(self.V)
         #print 'type of an element of new V is ', type(self.V[0,0])
         self.m = np.shape(self.V)[0]
         if self.verbose:
             print 'Minimum rank is ', self.m
         # generate next symbol based on current V
         L = len(self.tx_symbols)
         if self.i == 1:
             L = 0
         self.unsolved = np.ones(Kprime) > 0
Пример #8
0
	def compute_matrices(self): #change to compute_matrixes
		#self.TDMA_MODE = 0;
		#eps_vec = .5*np.ones(self.N)
		self.eps_vec = 0*np.ones(self.N)
		#self.i = 1
		#while np.any(np.isnan(self.final_messages)):
		Kprime = len(self.map);
		if self.verbose:
			print 'Remaining ', Kprime, ' nodes are: '
			print self.map
			## special case for one remaining node
			#doesnt make sense anymore, must change
		if Kprime == 1:
		#smsgx.set_V_row(self.V[self.m_i,:])
			#self.TOTAL_TRANSMISSIONS += 1
			#while not transmit_messages(1, self.eps_vec[self.map]):
				#self.TOTAL_TRANSMISSIONS += 1
			#print 'map is ', self.map
			#print 'W is ', self.W
			#print 'final messages are', self.final_messages
			#self.final_messages[self.map] = self.W[self.map]
			#make a new V to transmit instead, just what W needs
			self.V = np.zeros((1,self.N))
			self.V[0,self.map] = 1
			self.m = 1
		else:
			## Generate next m transmissions
			#(self.V, U) = alignment('mixed', self.J, 1e-4, 100, True)
			(self.V, U) = alignment('greedy', self.J, 1e-4, 100, False)
			#try changing it here??
			self.V = np.asarray(self.V)
			#RANDOM INSTEAD??? even easier, change right before transmission!
			#self.V = np.random.
			print 'new V is '
			print self.V
			#print 'type of new V is ', type(self.V)
			#print 'type of an element of new V is ', type(self.V[0,0])
			self.m = np.shape(self.V)[0]
			if self.verbose:
				print 'Minimum rank is ', self.m
			# generate next symbol based on current V
			L = len(self.tx_symbols);
			if self.i == 1:
				L = 0
			self.unsolved = np.ones(Kprime) > 0
 def align_sequence(self, other_sample):
     '''Sets maximum length of sequence and returns algorithm score'''
     seq_1 = self.sequence[:MAX_ALIGN_LENGTH]
     seq_2 = other_sample.sequence[:MAX_ALIGN_LENGTH]
     max_score = alignment.alignment(seq_1, seq_2)
     return max_score
Пример #10
0
def main():
    """Run the entry point."""
    parser = argparse.ArgumentParser()
    parser.add_argument("output", help="Report output file.")
    parser.add_argument("alignment_stats", help="Alignment stats file.")
    parser.add_argument("quality", help="Fastcat quality results.")
    parser.add_argument(
        "threshold",
        help="Threashold percentage expected for consensus accuracy")
    parser.add_argument("references", help="Reference file input at start")
    parser.add_argument("--revision",
                        default='unknown',
                        help="git branch/tag of the executed workflow")
    parser.add_argument("--commit",
                        default='unknown',
                        help="git commit of the executed workflow")
    parser.add_argument("--summaries", nargs='+', help="Read summary file.")
    parser.add_argument("--flagstats", nargs='+', help="Flag stat summaries")
    parser.add_argument("--consensus",
                        nargs='+',
                        help="consensus fasta sequences")
    parser.add_argument("--bedFiles",
                        nargs='+',
                        help="bed files for sequence depth")
    parser.add_argument("--unmapped", nargs='+', help="unmapped fastcat stats")
    parser.add_argument(
        "--versions",
        required=True,
        help="directory containing CSVs containing name,version.")
    args = parser.parse_args()
    report_doc = report.WFReport("Transcript target report",
                                 "wf-transcript-target",
                                 revision=args.revision,
                                 commit=args.commit)

    flag_stats = read_flag_stat_files(args.flagstats)
    # Retrieve flag and consensus stats and create df
    seq_summary = read_files(args.summaries)
    statsdf = seq_summary
    statsdf = statsdf.drop(['rstart', 'rend'], 1)
    ref_names, accuracy_list = list(statsdf['ref']), list(statsdf['acc'])
    accuracy_list = list(np.around(np.array(accuracy_list), 2))
    align_stats = args.alignment_stats
    align_stats = pd.read_csv(align_stats, sep='\t', header=None)
    align_stats.reset_index()
    # Retrieve total flag stats
    total_seq = int(align_stats.iat[0, 0])
    mapped = int(align_stats.iat[4, 0])
    percent_mapped = (mapped / total_seq) * 100
    percentage_aligned = list(
        map((lambda x: (x / total_seq) * 100), flag_stats))
    percentage_aligned = list(np.around(np.array(percentage_aligned), 2))
    # Output all in a table
    threshold = int(args.threshold)
    table_consensus = {
        'Reference name': ref_names,
        'Consensus Accuracy %': accuracy_list,
        'Number of reads aligned': flag_stats,
        'Total Aligned %': percentage_aligned
    }
    consensus_df = pd.DataFrame(table_consensus)
    consensus_df[''] = np.where(
        consensus_df['Consensus Accuracy %'] < threshold, 'Warning', '')
    section = report_doc.add_section()
    section.markdown("## Summary")
    section.markdown(" This table summarises the consensus accuracy",
                     "and alignment stats for each reference.")
    section.table(consensus_df, index=False)
    if consensus_df[''].isin({'': ['Warning']}).any():
        section.markdown('**Warning: Some references < threshold accuracy**')
    else:
        pass
    # Exec Summary infographic
    unmapped = args.unmapped
    print(unmapped)
    unmapped_df = pd.read_csv(unmapped[0], delimiter='\t')
    unmapped_read_qual = unmapped_df["mean_quality"].mean()
    unmapped_read_length = unmapped_df["read_length"].mean()
    section = report_doc.add_section()
    section.markdown("## Total aligned reads")
    exec_summary = aplanat.graphics.InfoGraphItems()
    exec_summary.append("Total reads", str(total_seq), "calculator", '')
    exec_summary.append("On target reads",
                        str("%.2f" % round(percent_mapped, 2)) + '%',
                        "percent", '')
    exec_summary.append("Unmapped mean Q",
                        str("%.2f" % round(unmapped_read_qual, 2)),
                        "clipboard-check", '')
    exec_summary.append("Unmapped mean len",
                        str("%.0f" % round(unmapped_read_length, 0)),
                        "calculator", '')
    exec_plot = aplanat.graphics.infographic(exec_summary.values(), ncols=4)
    section.plot(exec_plot, key="exec-plot")
    # Quality
    quality = args.quality
    quality_df = pd.read_csv(quality, sep='\t')
    read_qual = fastcat.read_quality_plot(quality_df)
    read_length = fastcat.read_length_plot(quality_df)
    section = report_doc.add_section()
    section.markdown("## Read Quality Control")
    section.markdown("This sections displays basic QC"
                     " metrics indicating read data quality.")
    section.plot(
        layout([[read_length, read_qual]], sizing_mode="stretch_width"))
    # Depth Coverage
    section = report_doc.add_section()
    section.markdown("## Depth of coverage")
    section.markdown("The depth of coverage of alignments "
                     "across the reference.")
    depth = args.bedFiles
    depth_graphs = depth_graph(depth)
    section.plot(
        gridplot(depth_graphs, ncols=2, plot_width=300, plot_height=300))
    # Assembly
    section = report_doc.add_section()
    section.markdown("## Consensus Alignment Statistics")
    section.markdown(
        "The following summarises the statistics from the consensus"
        "aligned with the reference")
    seq_summary = statsdf.drop(['name'], 1)
    seq_summary = seq_summary.rename(columns={'ref': 'Name'})
    seq_summary = seq_summary.round({'ref_coverage': 4, 'iden': 4, 'acc': 4})
    section.table(seq_summary)
    # Reference and consensus alignments
    section = report_doc.add_section()
    section.markdown("## Alignment of Consensus and Reference")
    section.markdown("Sequence alignment using Levenshtein (edit) distance.")
    ref_file = args.references
    refseq = alignment.referenceSeq(str(ref_file))
    cons = read_consensus(args.consensus)
    for item in (cons.keys()):
        section.markdown("###" + str(item))
        p = alignment.alignment(cons[str(item)], refseq[str(item)], 50)
        section.markdown(str(p[1]))
        section.markdown('Length of Consensus: ' + str(p[2]))
        section.markdown('Length of Reference: ' + str(p[3]))
        section.markdown("<pre>" + p[0] + "</pre>")
    section = report_doc.add_section()
    section.markdown("## Software versions")
    section.markdown('''The table below highlights versions
                    of key software used within the analysis''')
    section = report_doc.add_section(
        section=scomponents.version_table(args.versions))
    # write report
    report_doc.write(args.output)
# Turn off default if some partial analysis is requested
if NO_ALN or ALN_ONLY or SUBSET is not None:
    DEF_RUN = False
else:
    DEF_RUN = True
    print(
        'Default: Running alignment on current workstation and carrying out SNP analysis:'
    )

# ALIGNMENT only run
if ALN_ONLY:

    print('This is an alignment only run of pySNPCAllingPipeline')
    if RUN_ENV:
        align = alignment(CONF_FILE)
        align.do_alignments()
    else:
        # Prepare SLURM files
        align = scinet_alignment(CONF_FILE)
        align.do_scinet_alignments(submit_JOB=SUBMIT)

# DEFAULT RUN: Alignmnet --> getHQSNPs --> checkSNPs --> FilterSNPs --> write VCF
if DEF_RUN:

    # 1. Do alignments
    if RUN_ENV:
        align = alignment(CONF_FILE)
        align.do_alignments()
    else:
        align = scinet_alignment(CONF_FILE)
Пример #12
0
    def main_loop(self):
        eps_vec = .5 * np.ones(self.N)
        i = 1
        while np.any(np.isnan(self.final_messages)):
            Kprime = len(self.map)
            if self.verbose:
                print 'Remaining ', Kprime, ' nodes are: '
                print self.map

            ## special case for one remaining node
            if Kprime == 1:
                self.TOTAL_TRANSMISSIONS += 1
                while not transmit_messages(1, eps_vec[self.map]):
                    self.TOTAL_TRANSMISSIONS += 1
                self.final_messages[self.map] = self.W[self.map]
            else:
                ## Generate next m transmissions
                (V, U) = alignment('mixed', self.J, 1e-4, 100, False)
                m = np.shape(V)[0]
                if self.verbose:
                    print 'Minimum rank is ', m

                # generate next symbol based on current V
                L = len(self.tx_symbols)
                if i == 1:
                    L = 0

                unsolved = np.ones(Kprime) > 0
                m_i = 0
                while np.all(unsolved) and m_i < m:
                    self.tx_symbols = np.append(
                        self.tx_symbols, Symbol(V[m_i, :], self.W, self.map))
                    R = transmit_messages(1, eps_vec[self.map])
                    if i == 1:
                        self.rx_symbols = R
                    else:
                        self.rx_symbols = np.bmat([self.rx_symbols, R])
                    if self.verbose:
                        print 'Transmission ', m_i + 1, '/', m
                        print self.rx_symbols.astype(int)
                    self.TOTAL_TRANSMISSIONS += 1
                    # solve for messages if possible
                    #SEND TO TARGET HERE
                    (unsolved, final_messages) = bs_decode_messages(
                        self.dest, Kprime, self.map, self.rx_symbols,
                        self.tx_symbols, self.A, self.I, self.J, self.W,
                        self.final_messages, self.verbose)
                    #RECEIVE FROM TARGET HERE
                    #INTERPRET ACKS HERE
                    m_i += 1
                    i += 1
                    self.final_messages = final_messages
                # update data structures
                self.map = np.nonzero(np.isnan(
                    self.final_messages.reshape(-1)))[0]
                self.rx_symbols = self.rx_symbols[unsolved, :]
                self.J = self.J[unsolved, :]
                self.I = self.I[unsolved, :]
                self.A = self.A[unsolved, :]
        if self.verbose:
            print 'Total number of transmissions: ', self.TOTAL_TRANSMISSIONS
        return self.TOTAL_TRANSMISSIONS
Пример #13
0
        imgArray.append(frame)
        maskArray.append(mask)

    alignedFolderNum = len(os.listdir(txtFolder))

    # save inpainting frames result
    outputInpaintPath = txtFolder + '/inpainting/'
    if not os.path.exists(outputInpaintPath):
        os.makedirs(outputInpaintPath)
    else:
        alignedFolderNum -= 1

    for i in trange(alignedFolderNum):

        # alignment
        curFrame = alignment(txtFolder, i, imgArray, maskArray)

        # inpainting
        if args.mode == 'N':
            newFrame, newMask = noWeightInpainting(txtFolder,
                                                   outputInpaintPath, i,
                                                   curFrame)
        elif args.mode == 'O':
            newFrame, newMask = oneLookInpainting(txtFolder, outputInpaintPath,
                                                  i, curFrame)
        elif args.mode == 'G':
            newFrame, newMask = gaussianInpainting(txtFolder,
                                                   outputInpaintPath, i,
                                                   curFrame)
        else:
            print("Undefined inpainting mode! Use default inpainting mode.")
    def main_loop(self):
        eps_vec = 0.5 * np.ones(self.N)
        i = 1
        while np.any(np.isnan(self.final_messages)):
            Kprime = len(self.map)
            if self.verbose:
                print "Remaining ", Kprime, " nodes are: "
                print self.map

                ## special case for one remaining node
            if Kprime == 1:
                self.TOTAL_TRANSMISSIONS += 1
                while not transmit_messages(1, eps_vec[self.map]):
                    self.TOTAL_TRANSMISSIONS += 1
                self.final_messages[self.map] = self.W[self.map]
            else:
                ## Generate next m transmissions
                (V, U) = alignment("mixed", self.J, 1e-4, 100, False)
                m = np.shape(V)[0]
                if self.verbose:
                    print "Minimum rank is ", m

                    # generate next symbol based on current V
                L = len(self.tx_symbols)
                if i == 1:
                    L = 0

                unsolved = np.ones(Kprime) > 0
                m_i = 0
                while np.all(unsolved) and m_i < m:
                    self.tx_symbols = np.append(self.tx_symbols, Symbol(V[m_i, :], self.W, self.map))
                    R = transmit_messages(1, eps_vec[self.map])
                    if i == 1:
                        self.rx_symbols = R
                    else:
                        self.rx_symbols = np.bmat([self.rx_symbols, R])
                    if self.verbose:
                        print "Transmission ", m_i + 1, "/", m
                        print self.rx_symbols.astype(int)
                    self.TOTAL_TRANSMISSIONS += 1
                    # solve for messages if possible
                    # SEND TO TARGET HERE
                    (unsolved, final_messages) = bs_decode_messages(
                        self.dest,
                        Kprime,
                        self.map,
                        self.rx_symbols,
                        self.tx_symbols,
                        self.A,
                        self.I,
                        self.J,
                        self.W,
                        self.final_messages,
                        self.verbose,
                    )
                    # RECEIVE FROM TARGET HERE
                    # INTERPRET ACKS HERE
                    m_i += 1
                    i += 1
                    self.final_messages = final_messages
                    # update data structures
                self.map = np.nonzero(np.isnan(self.final_messages.reshape(-1)))[0]
                self.rx_symbols = self.rx_symbols[unsolved, :]
                self.J = self.J[unsolved, :]
                self.I = self.I[unsolved, :]
                self.A = self.A[unsolved, :]
        if self.verbose:
            print "Total number of transmissions: ", self.TOTAL_TRANSMISSIONS
        return self.TOTAL_TRANSMISSIONS
Пример #15
0
import model_data as md
import pathway_data as pd
from alignment import alignment
from bigg_data import bigg_data
"""
model = md.model_data()
#model.load_model_sbml("iCHOv1_final.xml")
#model.load_model_cobra_sbml("iCHOv1_final.xml")
#model.load_model_mat("gimmeDG44.mat")
model.load_model_mat("gimmeS.mat")
#model.load_model_mat("gimmeK1.mat")

"""
align = alignment()
dummy = -1
reload = 0

if dummy == 1:
    align.load_dummy()
elif dummy == -1:
    model = md.model_data()
    #model.load_model_mat("gimmeS.mat") # omogociti uporabniku, da izbere tip modela iz seznama modelov, ki ga pridobimo iz bigg-a
    #model.load_model_cobra_sbml("iCHOv1_bigg.xml")
    model.load_model_cobra_sbml("e_coli_core.xml")

    #if reload:
    #    model.get_ECs()
    #    model.save_ECs("ec.txt")
    #else:
    #    model.get_ECs_from_file("ec.txt")
Пример #16
0
def bs_index_coding(K, eps, verbose=True, dest=False):
	if dest == False:
		rand_dest = True
	else:
		rand_dest = False
	
	eps_vec = eps*np.ones(K)

	if verbose:
		print ' '
		print 'Number of receivers/nodes: ', K
		print 'Erasure prob is ', eps
		print '-----------'

	# generate random messages
	W = np.random.randint(1,257, (K,1))

	# store final received messages. goal is to "de-NaN" by the end
	final_messages = np.nan*np.zeros((K, 1))
	# keep track of all transmitted and received messages/symbols
	tx_symbols = np.array([]) # [1 -by- # of transmissions]

	# keep track of number of transmissions
	TOTAL_TRANSMISSIONS = 0

	## Base Station - to - cellular model
	# Uplink is assumed perfect, i.e. the BS has access to all messages and
	# their destinations
	if rand_dest:
		if verbose:
			print 'Message destinations chosen randomly'
		dest = singlerandperm(K); # receiver i wants message dest(i)
		print dest
	A = np.diag(W.reshape(-1)) # receiver (row) i has access to the message it plans to send
	mat_dest = (np.arange(K), np.array(dest))
	signal_space = np.zeros((K,K))>0
	signal_space[mat_dest] = True;
	I = compute_interferers(A, signal_space)
	J = I.astype(float)
	J[mat_dest] = -1
	map = np.arange(K)
	
	if verbose:
		print 'Interferer matrix is:'
		print J
	
	i = 1
	while np.any(np.isnan(final_messages)):
		Kprime = len(map);
		if verbose:
			print 'Remaining ', Kprime, ' nodes are: '
			print map

		## special case for one remaining node
		if Kprime == 1:
			TOTAL_TRANSMISSIONS = TOTAL_TRANSMISSIONS + 1
			while not transmit_messages(1, eps_vec[map]):
				TOTAL_TRANSMISSIONS = TOTAL_TRANSMISSIONS + 1
			final_messages[map] = W[map]
		else:
			## Generate next m transmissions
			(V, U) = alignment('mixed', J, 1e-4, 100, False)
			m = np.shape(V)[0]
			if verbose:
				print 'Minimum rank is ', m

			# generate next symbol based on current V
			L = len(tx_symbols);
			if i == 1:
				L = 0

			unsolved = np.ones(Kprime) > 0
			m_i = 0
			while np.all(unsolved) and m_i < m:
				tx_symbols = np.append(tx_symbols, Symbol(V[m_i,:], W, map))
				R = transmit_messages(1, eps_vec[map])
				if i == 1:
					rx_symbols = R
				else:
					rx_symbols = np.bmat([rx_symbols, R])
				if verbose:
					print 'Transmission ', m_i+1, '/', m
					print rx_symbols.astype(int)
				TOTAL_TRANSMISSIONS += 1
				# solve for messages if possible
				(unsolved, final_messages) = bs_decode_messages(dest, Kprime, map,
						rx_symbols, tx_symbols, A, I, J, W, final_messages, verbose)
				m_i += 1
				i += 1
			# update data structures
			map = np.nonzero(np.isnan(final_messages.reshape(-1)))[0]
			rx_symbols = rx_symbols[unsolved, :]
			J = J[unsolved, :]
			I = I[unsolved, :]
			A = A[unsolved, :]
	if verbose:
		print 'Total number of transmissions: ', TOTAL_TRANSMISSIONS
	return TOTAL_TRANSMISSIONS
	def main_loop(self): #change to compute_matrixes
		smsgx = TxSerialMsg()
		self.TDMA_MODE = 0;
		eps_vec = .5*np.ones(self.N)
		i = 1
		while np.any(np.isnan(self.final_messages)):
			Kprime = len(self.map);
			if self.verbose:
				print 'Remaining ', Kprime, ' nodes are: '
				print self.map

			## special case for one remaining node
			if Kprime == 1:
				self.TOTAL_TRANSMISSIONS += 1
				while not transmit_messages(1, eps_vec[self.map]):
					self.TOTAL_TRANSMISSIONS += 1
				self.final_messages[self.map] = self.W[self.map]
			else:
				## Generate next m transmissions
				(V, U) = alignment('mixed', self.J, 1e-4, 100, False)
				m = np.shape(V)[0]
				if self.verbose:
					print 'Minimum rank is ', m

				# generate next symbol based on current V
				L = len(self.tx_symbols);
				if i == 1:
					L = 0

				self.unsolved = np.ones(Kprime) > 0
				m_i = 0
				while np.all(self.unsolved) and m_i < m:
					self.tx_symbols = np.append(self.tx_symbols, Symbol(V[m_i,:], self.W, self.map))
					R = transmit_messages(1, eps_vec[self.map])
					if i == 1:
						self.rx_symbols = R
					else:
						self.rx_symbols = np.bmat([self.rx_symbols, R])
					if self.verbose:
						print 'Transmission ', m_i+1, '/', m
						print self.rx_symbols.astype(int)
					self.TOTAL_TRANSMISSIONS += 1
					# solve for messages if possible
					(self.unsolved, final_messages) = bs_decode_messages(self.dest, Kprime, self.map,
							self.rx_symbols, self.tx_symbols, self.A, self.I, self.J, self.W, self.final_messages, self.verbose)
					time.sleep(.2)
					print 'UNSOLVED: ', self.unsolved
					print 'MAP: ' , self.map
					#SEND TO TARGET HERE, rewrite these lines
					#smsg.set_crow
					print 'tx map ', self.map
					smsgx.set_messageid(255)  #now something to represent index coding, 255?
					smsgx.set_data(np.dot(V[m_i],self.W,))					
					##also send own message w[i] for comparison????
					smsgx.set_V_row(V[m_i]) 
					smsgx.set_current_transmission(self.TOTAL_TRANSMISSIONS)
					self.mif.sendMsg(self.source, 0xFFFF, smsgx.get_amType(), 0, smsgx)
					#time.sleep(1) #.125 too fast?
	
					#INTERPRET ACKS IN RECEIVE FUNCTION
					m_i += 1
					i += 1
					self.final_messages = final_messages #still need final messages??
				# update data structures
				self.map = np.nonzero(np.isnan(self.final_messages.reshape(-1)))[0]
				self.rx_symbols = self.rx_symbols[self.unsolved, :]
				self.J = self.J[self.unsolved, :]
				self.I = self.I[self.unsolved, :]
				self.A = self.A[self.unsolved, :]
		if self.verbose:
			print 'Total number of transmissions: ', self.TOTAL_TRANSMISSIONS
		return self.TOTAL_TRANSMISSIONS
Пример #18
0
def bs_index_coding(K, eps, verbose=True, dest=False):
    if dest == False:
        rand_dest = True
    else:
        rand_dest = False

    eps_vec = eps * np.ones(K)

    if verbose:
        print ' '
        print 'Number of receivers/nodes: ', K
        print 'Erasure prob is ', eps
        print '-----------'

    # generate random messages
    W = np.random.randint(1, 257, (K, 1))

    # store final received messages. goal is to "de-NaN" by the end
    final_messages = np.nan * np.zeros((K, 1))
    # keep track of all transmitted and received messages/symbols
    tx_symbols = np.array([])  # [1 -by- # of transmissions]

    # keep track of number of transmissions
    TOTAL_TRANSMISSIONS = 0

    ## Base Station - to - cellular model
    # Uplink is assumed perfect, i.e. the BS has access to all messages and
    # their destinations
    if rand_dest:
        if verbose:
            print 'Message destinations chosen randomly'
        dest = singlerandperm(K)
        # receiver i wants message dest(i)
        print dest
    A = np.diag(W.reshape(
        -1))  # receiver (row) i has access to the message it plans to send
    mat_dest = (np.arange(K), np.array(dest))
    signal_space = np.zeros((K, K)) > 0
    signal_space[mat_dest] = True
    I = compute_interferers(A, signal_space)
    J = I.astype(float)
    J[mat_dest] = -1
    map = np.arange(K)

    if verbose:
        print 'Interferer matrix is:'
        print J

    i = 1
    while np.any(np.isnan(final_messages)):
        Kprime = len(map)
        if verbose:
            print 'Remaining ', Kprime, ' nodes are: '
            print map

        ## special case for one remaining node
        if Kprime == 1:
            TOTAL_TRANSMISSIONS = TOTAL_TRANSMISSIONS + 1
            while not transmit_messages(1, eps_vec[map]):
                TOTAL_TRANSMISSIONS = TOTAL_TRANSMISSIONS + 1
            final_messages[map] = W[map]
        else:
            ## Generate next m transmissions
            (V, U) = alignment('mixed', J, 1e-4, 100, False)
            m = np.shape(V)[0]
            if verbose:
                print 'Minimum rank is ', m

            # generate next symbol based on current V
            L = len(tx_symbols)
            if i == 1:
                L = 0

            unsolved = np.ones(Kprime) > 0
            m_i = 0
            while np.all(unsolved) and m_i < m:
                tx_symbols = np.append(tx_symbols, Symbol(V[m_i, :], W, map))
                R = transmit_messages(1, eps_vec[map])
                if i == 1:
                    rx_symbols = R
                else:
                    rx_symbols = np.bmat([rx_symbols, R])
                if verbose:
                    print 'Transmission ', m_i + 1, '/', m
                    print rx_symbols.astype(int)
                TOTAL_TRANSMISSIONS += 1
                # solve for messages if possible
                (unsolved, final_messages) = bs_decode_messages(
                    dest, Kprime, map, rx_symbols, tx_symbols, A, I, J, W,
                    final_messages, verbose)
                m_i += 1
                i += 1
            # update data structures
            map = np.nonzero(np.isnan(final_messages.reshape(-1)))[0]
            rx_symbols = rx_symbols[unsolved, :]
            J = J[unsolved, :]
            I = I[unsolved, :]
            A = A[unsolved, :]
    if verbose:
        print 'Total number of transmissions: ', TOTAL_TRANSMISSIONS
    return TOTAL_TRANSMISSIONS
    def main_loop(self):  #change to compute_matrixes
        smsgx = TxSerialMsg()
        self.TDMA_MODE = 0
        eps_vec = .5 * np.ones(self.N)
        i = 1
        while np.any(np.isnan(self.final_messages)):
            Kprime = len(self.map)
            if self.verbose:
                print 'Remaining ', Kprime, ' nodes are: '
                print self.map

            ## special case for one remaining node
            if Kprime == 1:
                self.TOTAL_TRANSMISSIONS += 1
                while not transmit_messages(1, eps_vec[self.map]):
                    self.TOTAL_TRANSMISSIONS += 1
                self.final_messages[self.map] = self.W[self.map]
            else:
                ## Generate next m transmissions
                (V, U) = alignment('mixed', self.J, 1e-4, 100, False)
                m = np.shape(V)[0]
                if self.verbose:
                    print 'Minimum rank is ', m

                # generate next symbol based on current V
                L = len(self.tx_symbols)
                if i == 1:
                    L = 0

                self.unsolved = np.ones(Kprime) > 0
                m_i = 0
                while np.all(self.unsolved) and m_i < m:
                    self.tx_symbols = np.append(
                        self.tx_symbols, Symbol(V[m_i, :], self.W, self.map))
                    R = transmit_messages(1, eps_vec[self.map])
                    if i == 1:
                        self.rx_symbols = R
                    else:
                        self.rx_symbols = np.bmat([self.rx_symbols, R])
                    if self.verbose:
                        print 'Transmission ', m_i + 1, '/', m
                        print self.rx_symbols.astype(int)
                    self.TOTAL_TRANSMISSIONS += 1
                    # solve for messages if possible
                    (self.unsolved, final_messages) = bs_decode_messages(
                        self.dest, Kprime, self.map, self.rx_symbols,
                        self.tx_symbols, self.A, self.I, self.J, self.W,
                        self.final_messages, self.verbose)
                    time.sleep(.2)
                    print 'UNSOLVED: ', self.unsolved
                    print 'MAP: ', self.map
                    #SEND TO TARGET HERE, rewrite these lines
                    #smsg.set_crow
                    print 'tx map ', self.map
                    smsgx.set_messageid(
                        255)  #now something to represent index coding, 255?
                    smsgx.set_data(np.dot(
                        V[m_i],
                        self.W,
                    ))
                    ##also send own message w[i] for comparison????
                    smsgx.set_V_row(V[m_i])
                    smsgx.set_current_transmission(self.TOTAL_TRANSMISSIONS)
                    self.mif.sendMsg(self.source, 0xFFFF, smsgx.get_amType(),
                                     0, smsgx)
                    #time.sleep(1) #.125 too fast?

                    #INTERPRET ACKS IN RECEIVE FUNCTION
                    m_i += 1
                    i += 1
                    self.final_messages = final_messages  #still need final messages??
                # update data structures
                self.map = np.nonzero(np.isnan(
                    self.final_messages.reshape(-1)))[0]
                self.rx_symbols = self.rx_symbols[self.unsolved, :]
                self.J = self.J[self.unsolved, :]
                self.I = self.I[self.unsolved, :]
                self.A = self.A[self.unsolved, :]
        if self.verbose:
            print 'Total number of transmissions: ', self.TOTAL_TRANSMISSIONS
        return self.TOTAL_TRANSMISSIONS