<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import time <EOL> import socket <EOL> from bisect import insort , bisect <EOL> from copy import deepcopy , copy <EOL> import cPickle <EOL> import os . path <EOL> import shutil <EOL> import tempfile <EOL> from numpy import exp <EOL> try : <EOL> from matplotlib . backends . backend_agg import FigureCanvasAgg <EOL> from matplotlib . figure import Figure <EOL> import simplejson <EOL> from pkg_resources import resource_filename <EOL> _can_create_html = True <EOL> except : <EOL> _can_create_html = False <EOL> from pebl import posterior , config <EOL> from pebl . util import flatten , rescale_logvalues <EOL> from pebl . network import Network <EOL> class _ScoredNetwork ( Network ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , edgelist , score ) : <EOL> self . edges = edgelist <EOL> self . score = score <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( self . score , other . score ) <EOL> def __eq__ ( self , other ) : <EOL> return self . score == other . score and self . edges == other . edges <EOL> def __hash__ ( self ) : <EOL> return hash ( self . edges ) <EOL> class LearnerRunStats : <EOL> def __init__ ( self , start ) : <EOL> self . start = start <EOL> self . end = None <EOL> self . host = socket . gethostname ( ) <EOL> class LearnerResult : <EOL> """<STR_LIT>""" <EOL> _params = ( <EOL> config . StringParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> config . StringParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> config . oneof ( '<STR_LIT>' , '<STR_LIT:html>' ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> config . StringParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT:result>' <EOL> ) , <EOL> config . IntParameter ( <EOL> '<STR_LIT>' , <EOL> """<STR_LIT>""" , <EOL> default = <NUM_LIT:1000> <EOL> ) <EOL> ) <EOL> def __init__ ( self , learner_ = None , size = None ) : <EOL> self . data = learner_ . data if learner_ else None <EOL> self . nodes = self . data . variables if self . data else None <EOL> self . size = size or config . get ( '<STR_LIT>' ) <EOL> self . networks = [ ] <EOL> self . nethashes = { } <EOL> self . runs = [ ] <EOL> def start_run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . runs . append ( LearnerRunStats ( time . time ( ) ) ) <EOL> def stop_run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . runs [ - <NUM_LIT:1> ] . end = time . time ( ) <EOL> def add_network ( self , net , score ) : <EOL> """<STR_LIT>""" <EOL> nets = self . networks <EOL> nethashes = self . nethashes <EOL> nethash = hash ( net . edges ) <EOL> if self . size == <NUM_LIT:0> or len ( nets ) < self . size : <EOL> if nethash not in nethashes : <EOL> snet = _ScoredNetwork ( copy ( net . edges ) , score ) <EOL> insort ( nets , snet ) <EOL> nethashes [ nethash ] = <NUM_LIT:1> <EOL> elif score > nets [ <NUM_LIT:0> ] . score and nethash not in nethashes : <EOL> nethashes . pop ( hash ( nets [ <NUM_LIT:0> ] . edges ) ) <EOL> nets . remove ( nets [ <NUM_LIT:0> ] ) <EOL> snet = _ScoredNetwork ( copy ( net . edges ) , score ) <EOL> insort ( nets , snet ) <EOL> nethashes [ nethash ] = <NUM_LIT:1> <EOL> def tofile ( self , filename = None ) : <EOL> """<STR_LIT>""" <EOL> filename = filename or config . get ( '<STR_LIT>' ) <EOL> with open ( filename , '<STR_LIT:w>' ) as fp : <EOL> cPickle . dump ( self , fp ) <EOL> def tohtml ( self , outdir = None ) : <EOL> """<STR_LIT>""" <EOL> if _can_create_html : <EOL> HtmlFormatter ( ) . htmlreport ( <EOL> self , <EOL> outdir or config . get ( '<STR_LIT>' ) <EOL> ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> @ property <EOL> def posterior ( self ) : <EOL> """<STR_LIT>""" <EOL> return posterior . from_sorted_scored_networks ( <EOL> self . nodes , <EOL> list ( reversed ( self . networks ) ) <EOL> ) <EOL> class HtmlFormatter : <EOL> def htmlreport ( self , result_ , outdir , numnetworks = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> def jsonize_run ( r ) : <EOL> return { <EOL> '<STR_LIT:start>' : time . asctime ( time . localtime ( r . start ) ) , <EOL> '<STR_LIT:end>' : time . asctime ( time . localtime ( r . end ) ) , <EOL> '<STR_LIT>' : round ( ( r . end - r . start ) / <NUM_LIT> , <NUM_LIT:3> ) , <EOL> '<STR_LIT:host>' : r . host <EOL> } <EOL> pjoin = os . path . join <EOL> if not os . path . exists ( outdir ) : <EOL> os . makedirs ( outdir ) <EOL> staticdir = resource_filename ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> shutil . copy2 ( pjoin ( staticdir , '<STR_LIT>' ) , outdir ) <EOL> shutil . copytree ( pjoin ( staticdir , '<STR_LIT>' ) , pjoin ( outdir , '<STR_LIT>' ) ) <EOL> outdir = pjoin ( outdir , '<STR_LIT:data>' ) <EOL> os . mkdir ( outdir ) <EOL> post = result_ . posterior <EOL> numnetworks = numnetworks if len ( post ) >= numnetworks else len ( post ) <EOL> topscores = post . scores [ : numnetworks ] <EOL> norm_topscores = exp ( rescale_logvalues ( topscores ) ) <EOL> resultsdata = { <EOL> '<STR_LIT>' : [ round ( s , <NUM_LIT:3> ) for s in norm_topscores ] , <EOL> '<STR_LIT>' : [ round ( s , <NUM_LIT:3> ) for s in topscores ] , <EOL> '<STR_LIT>' : [ jsonize_run ( r ) for r in result_ . runs ] , <EOL> } <EOL> open ( pjoin ( outdir , '<STR_LIT>' ) , '<STR_LIT:w>' ) . write ( <EOL> "<STR_LIT>" + simplejson . dumps ( resultsdata ) <EOL> ) <EOL> top = post [ <NUM_LIT:0> ] <EOL> top . layout ( ) <EOL> for i , net in enumerate ( post [ : numnetworks ] ) : <EOL> self . network_image ( <EOL> net , <EOL> pjoin ( outdir , "<STR_LIT>" % i ) , <EOL> pjoin ( outdir , "<STR_LIT>" % i ) , <EOL> top . node_positions <EOL> ) <EOL> cm = post . consensus_matrix <EOL> for threshold in xrange ( <NUM_LIT:10> ) : <EOL> self . consensus_network_image ( <EOL> post . consensus_network ( threshold / <NUM_LIT> ) , <EOL> pjoin ( outdir , "<STR_LIT>" % threshold ) , <EOL> cm , top . node_positions <EOL> ) <EOL> self . plot ( post . scores , pjoin ( outdir , "<STR_LIT>" ) ) <EOL> def plot ( self , values , outfile ) : <EOL> fig = Figure ( figsize = ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> ax = fig . add_axes ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> ax . scatter ( range ( len ( values ) ) , values , edgecolors = '<STR_LIT:None>' , s = <NUM_LIT:10> ) <EOL> ax . set_title ( "<STR_LIT>" ) <EOL> ax . set_xlabel ( "<STR_LIT>" ) <EOL> ax . set_ylabel ( "<STR_LIT>" ) <EOL> ax . set_xbound ( - <NUM_LIT:20> , len ( values ) + <NUM_LIT:20> ) <EOL> canvas = FigureCanvasAgg ( fig ) <EOL> canvas . print_figure ( outfile , dpi = <NUM_LIT> ) <EOL> def network_image ( self , net , outfile1 , outfile2 , node_positions , <EOL> dot = "<STR_LIT>" , neato = "<STR_LIT>" ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> net . as_dotfile ( fname ) <EOL> os . system ( "<STR_LIT>" % ( dot , outfile1 , fname ) ) <EOL> os . remove ( fname ) <EOL> net . node_positions = node_positions <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> net . as_dotfile ( fname ) <EOL> os . system ( "<STR_LIT>" % ( neato , outfile2 , fname ) ) <EOL> os . remove ( fname ) <EOL> def consensus_network_image ( self , net , outfile , cm , node_positions ) : <EOL> def colorize_edge ( weight ) : <EOL> colors = "<STR_LIT>" <EOL> breakpoints = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> return "<STR_LIT:#>" + str ( colors [ bisect ( breakpoints , weight ) ] ) * <NUM_LIT:6> <EOL> def node ( n , position ) : <EOL> s = "<STR_LIT>" % n . name <EOL> if position : <EOL> x , y = position <EOL> s += "<STR_LIT>" % ( x , y ) <EOL> return s + "<STR_LIT:;>" <EOL> nodes = net . nodes <EOL> positions = node_positions <EOL> dotstr = "<STR_LIT:\n>" . join ( <EOL> [ "<STR_LIT>" ] + <EOL> [ node ( n , pos ) for n , pos in zip ( nodes , positions ) ] + <EOL> [ "<STR_LIT>" % ( nodes [ src ] . name , nodes [ dest ] . name , colorize_edge ( cm [ src ] [ dest ] ) ) for src , dest in net . edges <EOL> ] + <EOL> [ "<STR_LIT:}>" ] <EOL> ) <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> open ( fname , '<STR_LIT:w>' ) . write ( dotstr ) <EOL> os . system ( "<STR_LIT>" % ( outfile , fname ) ) <EOL> os . remove ( fname ) <EOL> def merge ( * args ) : <EOL> """<STR_LIT>""" <EOL> results = flatten ( args ) <EOL> if len ( results ) is <NUM_LIT:1> : <EOL> return results [ <NUM_LIT:0> ] <EOL> newresults = LearnerResult ( ) <EOL> newresults . data = results [ <NUM_LIT:0> ] . data <EOL> newresults . nodes = results [ <NUM_LIT:0> ] . nodes <EOL> allnets = list ( set ( [ net for net in flatten ( r . networks for r in results ) ] ) ) <EOL> allnets . sort ( ) <EOL> newresults . networks = allnets <EOL> newresults . nethashes = dict ( [ ( net , <NUM_LIT:1> ) for net in allnets ] ) <EOL> if hasattr ( results [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> newresults . runs = flatten ( [ r . runs for r in results ] ) <EOL> else : <EOL> newresults . runs = [ ] <EOL> return newresults <EOL> def fromfile ( filename ) : <EOL> """<STR_LIT>""" <EOL> return cPickle . load ( open ( filename ) ) </s>
