idx int64 0 251k | question stringlengths 53 3.53k | target stringlengths 5 1.23k | len_question int64 20 893 | len_target int64 3 238 |
|---|---|---|---|---|
5,200 | def fn_max ( self , a , axis = None ) : return numpy . nanmax ( self . _to_ndarray ( a ) , axis = axis ) | Return the maximum of an array ignoring any NaNs . | 36 | 11 |
5,201 | def fn_median ( self , a , axis = None ) : return numpy . nanmedian ( self . _to_ndarray ( a ) , axis = axis ) | Compute the median of an array ignoring NaNs . | 38 | 11 |
5,202 | def fn_mean ( self , a , axis = None ) : return numpy . nanmean ( self . _to_ndarray ( a ) , axis = axis ) | Compute the arithmetic mean of an array ignoring NaNs . | 36 | 12 |
5,203 | def fn_std ( self , a , axis = None ) : return numpy . nanstd ( self . _to_ndarray ( a ) , axis = axis ) | Compute the standard deviation of an array ignoring NaNs . | 36 | 12 |
5,204 | def fn_var ( self , a , axis = None ) : return numpy . nanvar ( self . _to_ndarray ( a ) , axis = axis ) | Compute the variance of an array ignoring NaNs . | 36 | 11 |
5,205 | def fn_ceil ( self , value ) : if is_ndarray ( value ) or isinstance ( value , ( list , tuple ) ) : return numpy . ceil ( self . _to_ndarray ( value ) ) else : return math . ceil ( value ) | Return the ceiling of a number . | 60 | 7 |
5,206 | def fn_int ( self , value ) : if is_ndarray ( value ) or isinstance ( value , ( list , tuple ) ) : return self . _to_ndarray ( value ) . astype ( 'int' ) else : return int ( value ) | Return the value cast to an int . | 57 | 8 |
5,207 | def fn_float ( self , value ) : if is_ndarray ( value ) or isinstance ( value , ( list , tuple ) ) : return self . _to_ndarray ( value ) . astype ( 'float' ) else : return float ( value ) | Return the value cast to a float . | 57 | 8 |
5,208 | def make_datetime ( dt , date_parser = parse_date ) : if ( isinstance ( dt , ( datetime . datetime , datetime . date , datetime . time , pd . Timestamp , np . datetime64 ) ) or dt in ( float ( 'nan' ) , float ( 'inf' ) , float ( '-inf' ) , None , '' ) ) : return dt if isinstance ( dt , ( float , int ) ) : return datetime_from_ordinal_float ( dt ) if isinstance ( dt , datetime . date ) : return datetime . datetime ( dt . year , dt . month , dt . day ) if isinstance ( dt , datetime . time ) : return datetime . datetime ( 1 , 1 , 1 , dt . hour , dt . minute , dt . second , dt . microsecond ) if not dt : return datetime . datetime ( 1970 , 1 , 1 ) if isinstance ( dt , basestring ) : try : return date_parser ( dt ) except ValueError : print ( 'Unable to make_datetime({})' . format ( dt ) ) raise try : return datetime . datetime ( * dt . timetuple ( ) [ : 7 ] ) except AttributeError : try : dt = list ( dt ) if 0 < len ( dt ) < 7 : try : return datetime . datetime ( * dt [ : 7 ] ) except ( TypeError , IndexError , ValueError ) : pass except ( TypeError , IndexError , ValueError , AttributeError ) : # dt is not iterable return dt return [ make_datetime ( val , date_parser = date_parser ) for val in dt ] | Coerce a datetime or string into datetime . datetime object | 394 | 15 |
5,209 | def quantize_datetime ( dt , resolution = None ) : # FIXME: this automatically truncates off microseconds just because timtuple() only goes out to sec resolution = int ( resolution or 6 ) if hasattr ( dt , 'timetuple' ) : dt = dt . timetuple ( ) # strips timezone info if isinstance ( dt , time . struct_time ) : # strip last 3 fields (tm_wday, tm_yday, tm_isdst) dt = list ( dt ) [ : 6 ] # struct_time has no microsecond, but accepts float seconds dt += [ int ( ( dt [ 5 ] - int ( dt [ 5 ] ) ) * 1000000 ) ] dt [ 5 ] = int ( dt [ 5 ] ) return datetime . datetime ( * ( dt [ : resolution ] + [ 1 ] * max ( 3 - resolution , 0 ) ) ) if isinstance ( dt , tuple ) and len ( dt ) <= 9 and all ( isinstance ( val , ( float , int ) ) for val in dt ) : dt = list ( dt ) + [ 0 ] * ( max ( 6 - len ( dt ) , 0 ) ) # if the 6th element of the tuple looks like a float set of seconds need to add microseconds if len ( dt ) == 6 and isinstance ( dt [ 5 ] , float ) : dt = list ( dt ) + [ 1000000 * ( dt [ 5 ] - int ( dt [ 5 ] ) ) ] dt [ 5 ] = int ( dt [ 5 ] ) dt = tuple ( int ( val ) for val in dt ) return datetime . datetime ( * ( dt [ : resolution ] + [ 1 ] * max ( resolution - 3 , 0 ) ) ) return [ quantize_datetime ( value ) for value in dt ] | Quantize a datetime to integer years months days hours minutes seconds or microseconds | 419 | 16 |
5,210 | def timetag_str ( dt = None , sep = '-' , filler = '0' , resolution = 6 ) : resolution = int ( resolution or 6 ) if sep in ( None , False ) : sep = '' sep = str ( sep ) dt = datetime . datetime . now ( ) if dt is None else dt # FIXME: don't use timetuple which truncates microseconds return sep . join ( ( '{0:' + filler + ( '2' if filler else '' ) + 'd}' ) . format ( i ) for i in tuple ( dt . timetuple ( ) [ : resolution ] ) ) | Generate a date - time tag suitable for appending to a file name . | 138 | 16 |
5,211 | def make_tz_aware ( dt , tz = 'UTC' , is_dst = None ) : # make sure dt is a datetime, time, or list of datetime/times dt = make_datetime ( dt ) if not isinstance ( dt , ( list , datetime . datetime , datetime . date , datetime . time , pd . Timestamp ) ) : return dt # TODO: deal with sequence of timezones try : tz = dt . tzinfo or tz except ( ValueError , AttributeError , TypeError ) : pass try : tzstr = str ( tz ) . strip ( ) . upper ( ) if tzstr in TZ_ABBREV_NAME : is_dst = is_dst or tzstr . endswith ( 'DT' ) tz = TZ_ABBREV_NAME . get ( tzstr , tz ) except ( ValueError , AttributeError , TypeError ) : pass try : tz = pytz . timezone ( tz ) except ( ValueError , AttributeError , TypeError ) : # from traceback import print_exc # print_exc() pass try : return tz . localize ( dt , is_dst = is_dst ) except ( ValueError , AttributeError , TypeError ) : # from traceback import print_exc # print_exc() # TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta' pass # could be datetime.time, which can't be localized. Insted `replace` the TZ # don't try/except in case dt is not a datetime or time type -- should raise an exception if not isinstance ( dt , list ) : return dt . replace ( tzinfo = tz ) return [ make_tz_aware ( dt0 , tz = tz , is_dst = is_dst ) for dt0 in dt ] | Add timezone information to a datetime object only if it is naive . | 446 | 15 |
5,212 | def translate_addresstype ( f ) : @ wraps ( f ) def wr ( r , pc ) : at = r [ "addressType" ] try : r . update ( { "addressType" : POSTCODE_API_TYPEDEFS_ADDRESS_TYPES [ at ] } ) except : logger . warning ( "Warning: {}: " "unknown 'addressType': {}" . format ( pc , at ) ) return f ( r , pc ) return wr | decorator to translate the addressType field . | 106 | 10 |
5,213 | def translate_purposes ( f ) : @ wraps ( f ) def wr ( r , pc ) : tmp = [ ] for P in r [ "purposes" ] : try : tmp . append ( POSTCODE_API_TYPEDEFS_PURPOSES [ P ] ) except : logger . warning ( "Warning: {}: " "cannot translate 'purpose': {}" . format ( pc , P ) ) tmp . append ( P ) r . update ( { "purposes" : tmp } ) return f ( r , pc ) return wr | decorator to translate the purposes field . | 120 | 9 |
5,214 | def quantile ( data , num_breaks ) : def scipy_mquantiles ( a , prob = list ( [ .25 , .5 , .75 ] ) , alphap = .4 , betap = .4 , axis = None , limit = ( ) ) : """ function copied from scipy 0.13.3::scipy.stats.mstats.mquantiles """ def _quantiles1D ( data , m , p ) : x = numpy . sort ( data . compressed ( ) ) n = len ( x ) if n == 0 : return numpy . ma . array ( numpy . empty ( len ( p ) , dtype = float ) , mask = True ) elif n == 1 : return numpy . ma . array ( numpy . resize ( x , p . shape ) , mask = numpy . ma . nomask ) aleph = ( n * p + m ) k = numpy . floor ( aleph . clip ( 1 , n - 1 ) ) . astype ( int ) gamma = ( aleph - k ) . clip ( 0 , 1 ) return ( 1. - gamma ) * x [ ( k - 1 ) . tolist ( ) ] + gamma * x [ k . tolist ( ) ] # Initialization & checks --------- data = numpy . ma . array ( a , copy = False ) if data . ndim > 2 : raise TypeError ( "Array should be 2D at most !" ) # if limit : condition = ( limit [ 0 ] < data ) & ( data < limit [ 1 ] ) data [ ~ condition . filled ( True ) ] = numpy . ma . masked # p = numpy . array ( prob , copy = False , ndmin = 1 ) m = alphap + p * ( 1. - alphap - betap ) # Computes quantiles along axis (or globally) if ( axis is None ) : return _quantiles1D ( data , m , p ) return numpy . ma . apply_along_axis ( _quantiles1D , axis , data , m , p ) return scipy_mquantiles ( data , numpy . linspace ( 1.0 / num_breaks , 1 , num_breaks ) ) | Calculate quantile breaks . | 485 | 7 |
5,215 | def equal ( data , num_breaks ) : step = ( numpy . amax ( data ) - numpy . amin ( data ) ) / num_breaks return numpy . linspace ( numpy . amin ( data ) + step , numpy . amax ( data ) , num_breaks ) | Calculate equal interval breaks . | 67 | 7 |
5,216 | def add_column ( filename , column , formula , force = False ) : columns = parse_formula ( formula ) logger . info ( "Running file: %s" % filename ) logger . debug ( " Reading columns: %s" % columns ) data = fitsio . read ( filename , columns = columns ) logger . debug ( ' Evaluating formula: %s' % formula ) col = eval ( formula ) col = np . asarray ( col , dtype = [ ( column , col . dtype ) ] ) insert_columns ( filename , col , force = force ) return True | Add a column to a FITS file . | 125 | 9 |
5,217 | def load_files ( filenames , multiproc = False , * * kwargs ) : filenames = np . atleast_1d ( filenames ) logger . debug ( "Loading %s files..." % len ( filenames ) ) kwargs = [ dict ( filename = f , * * kwargs ) for f in filenames ] if multiproc : from multiprocessing import Pool processes = multiproc if multiproc > 0 else None p = Pool ( processes , maxtasksperchild = 1 ) out = p . map ( load_file , kwargs ) else : out = [ load_file ( kw ) for kw in kwargs ] dtype = out [ 0 ] . dtype for i , d in enumerate ( out ) : if d . dtype != dtype : # ADW: Not really safe... logger . warn ( "Casting input data to same type." ) out [ i ] = d . astype ( dtype , copy = False ) logger . debug ( 'Concatenating arrays...' ) return np . concatenate ( out ) | Load a set of FITS files with kwargs . | 241 | 12 |
5,218 | def applyFracdet ( self , lon , lat ) : self . loadFracdet ( ) fracdet_core = meanFracdet ( self . m_fracdet , lon , lat , np . tile ( 0.1 , len ( lon ) ) ) fracdet_wide = meanFracdet ( self . m_fracdet , lon , lat , np . tile ( 0.5 , len ( lon ) ) ) return ( fracdet_core >= self . config [ self . algorithm ] [ 'fracdet_core_threshold' ] ) & ( fracdet_core >= self . config [ self . algorithm ] [ 'fracdet_core_threshold' ] ) | We want to enforce minimum fracdet for a satellite to be considered detectable | 154 | 15 |
5,219 | def applyHotspot ( self , lon , lat ) : self . loadRealResults ( ) cut_detect_real = ( self . data_real [ 'SIG' ] >= self . config [ self . algorithm ] [ 'sig_threshold' ] ) lon_real = self . data_real [ 'RA' ] [ cut_detect_real ] lat_real = self . data_real [ 'DEC' ] [ cut_detect_real ] cut_hotspot = np . tile ( True , len ( lon ) ) for ii in range ( 0 , len ( lon ) ) : cut_hotspot [ ii ] = ~ np . any ( angsep ( lon [ ii ] , lat [ ii ] , lon_real , lat_real ) < self . config [ self . algorithm ] [ 'hotspot_angsep_threshold' ] ) return cut_hotspot | Exclude objects that are too close to hotspot | 200 | 10 |
5,220 | def predict ( self , lon , lat , * * kwargs ) : assert self . classifier is not None , 'ERROR' pred = np . zeros ( len ( lon ) ) cut_geometry , flags_geometry = self . applyGeometry ( lon , lat ) x_test = [ ] for key , operation in self . config [ 'operation' ] [ 'params_intrinsic' ] : assert operation . lower ( ) in [ 'linear' , 'log' ] , 'ERROR' if operation . lower ( ) == 'linear' : x_test . append ( kwargs [ key ] ) else : x_test . append ( np . log10 ( kwargs [ key ] ) ) x_test = np . vstack ( x_test ) . T #import pdb; pdb.set_trace() pred [ cut_geometry ] = self . classifier . predict_proba ( x_test [ cut_geometry ] ) [ : , 1 ] self . validatePredict ( pred , flags_geometry , lon , lat , kwargs [ 'r_physical' ] , kwargs [ 'abs_mag' ] , kwargs [ 'distance' ] ) return pred , flags_geometry | distance abs_mag r_physical | 274 | 7 |
5,221 | def catalogFactory ( name , * * kwargs ) : fn = lambda member : inspect . isclass ( member ) and member . __module__ == __name__ catalogs = odict ( inspect . getmembers ( sys . modules [ __name__ ] , fn ) ) if name not in list ( catalogs . keys ( ) ) : msg = "%s not found in catalogs:\n %s" % ( name , list ( kernels . keys ( ) ) ) logger . error ( msg ) msg = "Unrecognized catalog: %s" % name raise Exception ( msg ) return catalogs [ name ] ( * * kwargs ) | Factory for various catalogs . | 136 | 6 |
5,222 | def write_results ( filename , config , srcfile , samples ) : results = createResults ( config , srcfile , samples = samples ) results . write ( filename ) | Package everything nicely | 35 | 3 |
5,223 | def estimate ( self , param , burn = None , clip = 10.0 , alpha = 0.32 ) : # FIXME: Need to add age and metallicity to composite isochrone params (currently properties) if param not in list ( self . samples . names ) + list ( self . source . params ) + [ 'age' , 'metallicity' ] : msg = 'Unrecognized parameter: %s' % param raise KeyError ( msg ) # If the parameter is in the samples if param in self . samples . names : if param . startswith ( 'position_angle' ) : return self . estimate_position_angle ( param , burn = burn , clip = clip , alpha = alpha ) return self . samples . peak_interval ( param , burn = burn , clip = clip , alpha = alpha ) mle = self . get_mle ( ) errors = [ np . nan , np . nan ] # Set default value to the MLE value if param in self . source . params : err = self . source . params [ param ] . errors if err is not None : errors = err # For age and metallicity from composite isochrone return [ float ( mle [ param ] ) , errors ] | Estimate parameter value and uncertainties | 263 | 6 |
5,224 | def estimate_params ( self , burn = None , clip = 10.0 , alpha = 0.32 ) : mle = self . get_mle ( ) out = odict ( ) for param in mle . keys ( ) : out [ param ] = self . estimate ( param , burn = burn , clip = clip , alpha = alpha ) return out | Estimate all source parameters | 76 | 5 |
5,225 | def estimate_position_angle ( self , param = 'position_angle' , burn = None , clip = 10.0 , alpha = 0.32 ) : # Transform so peak in the middle of the distribution pa = self . samples . get ( param , burn = burn , clip = clip ) peak = ugali . utils . stats . kde_peak ( pa ) shift = 180. * ( ( pa + 90 - peak ) > 180 ) pa -= shift # Get the kde interval ret = ugali . utils . stats . peak_interval ( pa , alpha ) if ret [ 0 ] < 0 : ret [ 0 ] += 180. ret [ 1 ] [ 0 ] += 180. ret [ 1 ] [ 1 ] += 180. return ret | Estimate the position angle from the posterior dealing with periodicity . | 162 | 13 |
5,226 | def date_range_for_webtrends ( cls , start_at = None , end_at = None ) : if start_at and end_at : start_date = cls . parse_standard_date_string_to_date ( start_at ) end_date = cls . parse_standard_date_string_to_date ( end_at ) return [ ( cls . parse_date_for_query ( start_date ) , cls . parse_date_for_query ( end_date ) ) ] else : return [ ( "current_hour-1" , "current_hour-1" ) ] | Get the start and end formatted for query or the last hour if none specified . Unlike reports this does not aggregate periods and so it is possible to just query a range and parse out the individual hours . | 141 | 40 |
5,227 | def get_ugali_dir ( ) : dirname = os . getenv ( 'UGALIDIR' ) # Get the HOME directory if not dirname : dirname = os . path . join ( os . getenv ( 'HOME' ) , '.ugali' ) if not os . path . exists ( dirname ) : from ugali . utils . logger import logger msg = "Creating UGALIDIR:\n%s" % dirname logger . warning ( msg ) return mkdir ( dirname ) | Get the path to the ugali data directory from the environment | 112 | 13 |
5,228 | def get_iso_dir ( ) : dirname = os . path . join ( get_ugali_dir ( ) , 'isochrones' ) if not os . path . exists ( dirname ) : from ugali . utils . logger import logger msg = "Isochrone directory not found:\n%s" % dirname logger . warning ( msg ) return dirname | Get the ugali isochrone directory . | 83 | 10 |
5,229 | def registerParser ( self , parser ) : if not isinstance ( parser , Subparser ) : raise TypeError ( "%s is not an instance of a subparser." % parser ) self . parsers . append ( parser ) | Registers a parser to parse configuration inputs . | 47 | 9 |
5,230 | def addConfig ( self , name , default = None , cast = None , required = False , description = None ) : # Validate the name if not self . configNameRE . match ( name ) : raise InvalidConfigurationException ( "Invalid configuration name: %s" % name ) self . configs [ self . _sanitizeName ( name ) ] = { 'default' : default , 'cast' : cast , 'required' : required , 'description' : description } | Adds the given configuration option to the ConfigManager . | 101 | 10 |
5,231 | def parse ( self ) : self . _config = _Config ( ) self . _setDefaults ( ) for parser in self . parsers : for key , value in parser . parse ( self , self . _config ) . items ( ) : key = self . _sanitizeName ( key ) if key not in self . configs : raise UnknownConfigurationException ( key ) if value is not None : self . _setConfig ( key , value ) self . _ensureRequired ( ) self . _cast ( ) return self . _config | Executes the registered parsers to parse input configurations . | 115 | 11 |
5,232 | def _setDefaults ( self ) : for configName , configDict in self . configs . items ( ) : self . _setConfig ( configName , configDict [ 'default' ] ) | Sets all the expected configuration options on the config object as either the requested default value or None . | 44 | 20 |
5,233 | def _cast ( self ) : for configName , configDict in self . configs . items ( ) : if configDict [ 'cast' ] is not None : configValue = getattr ( self . _config , configName ) if configValue is not None : try : self . _setConfig ( configName , configDict [ 'cast' ] ( configValue ) ) except : raise InvalidConfigurationException ( "%s: %r" % ( configName , configValue ) ) | Iterates through our parsed configuration options and cast any options with marked cast types . | 104 | 16 |
5,234 | def list_models ( self , macaroons ) : return make_request ( "{}model" . format ( self . url ) , timeout = self . timeout , client = self . _client , cookies = self . cookies ) | Get the logged in user s models from the JIMM controller . | 48 | 14 |
5,235 | def write ( self , novel_title = 'novel' , filetype = 'txt' ) : self . _compose_chapters ( ) self . _write_to_file ( novel_title , filetype ) | Composes chapters and writes the novel to a text file | 48 | 11 |
5,236 | def _compose_chapters ( self ) : for count in range ( self . chapter_count ) : chapter_num = count + 1 c = Chapter ( self . markov , chapter_num ) self . chapters . append ( c ) | Creates a chapters and appends them to list | 51 | 10 |
5,237 | def valid_address ( address ) : if not address : return False components = str ( address ) . split ( ':' ) if len ( components ) > 2 or not valid_hostname ( components [ 0 ] ) : return False if len ( components ) == 2 and not valid_port ( components [ 1 ] ) : return False return True | Determines whether the specified address string is valid . | 71 | 11 |
5,238 | def valid_hostname ( host ) : if len ( host ) > 255 : return False if host [ - 1 : ] == '.' : host = host [ : - 1 ] return all ( _hostname_re . match ( c ) for c in host . split ( '.' ) ) | Returns whether the specified string is a valid hostname . | 62 | 11 |
5,239 | def sample ( self , n , mass_min = 0.1 , mass_max = 10. , steps = 10000 , seed = None ) : if seed is not None : np . random . seed ( seed ) d_mass = ( mass_max - mass_min ) / float ( steps ) mass = np . linspace ( mass_min , mass_max , steps ) cdf = np . insert ( np . cumsum ( d_mass * self . pdf ( mass [ 1 : ] , log_mode = False ) ) , 0 , 0. ) cdf = cdf / cdf [ - 1 ] f = scipy . interpolate . interp1d ( cdf , mass ) return f ( np . random . uniform ( size = n ) ) | Sample initial mass values between mass_min and mass_max following the IMF distribution . | 166 | 17 |
5,240 | def pdf ( cls , mass , log_mode = True ) : log_mass = np . log10 ( mass ) # From Eq 2 mb = mbreak = [ 0.08 , 0.5 ] # Msun a = alpha = [ 0.3 , 1.3 , 2.3 ] # alpha # Normalization set from 0.1 -- 100 Msun norm = 0.27947743949440446 b = 1. / norm c = b * mbreak [ 0 ] ** ( alpha [ 1 ] - alpha [ 0 ] ) d = c * mbreak [ 1 ] ** ( alpha [ 2 ] - alpha [ 1 ] ) dn_dm = b * ( mass < 0.08 ) * mass ** ( - alpha [ 0 ] ) dn_dm += c * ( 0.08 <= mass ) * ( mass < 0.5 ) * mass ** ( - alpha [ 1 ] ) dn_dm += d * ( 0.5 <= mass ) * mass ** ( - alpha [ 2 ] ) if log_mode : # Number per logarithmic mass range, i.e., dN/dlog(M) return dn_dm * ( mass * np . log ( 10 ) ) else : # Number per linear mass range, i.e., dN/dM return dn_dm | PDF for the Kroupa IMF . | 285 | 8 |
5,241 | def pdf ( cls , mass , log_mode = True ) : alpha = 2.35 a = 0.060285569480482866 dn_dm = a * mass ** ( - alpha ) if log_mode : # Number per logarithmic mass range, i.e., dN/dlog(M) return dn_dm * ( mass * np . log ( 10 ) ) else : # Number per linear mass range, i.e., dN/dM return dn_dm | PDF for the Salpeter IMF . | 111 | 8 |
5,242 | def _getConfigFile ( self , config ) : joinPath = lambda p : ( os . path . join ( p ) if isinstance ( p , ( tuple , list ) ) else p ) if self . filepathConfig is not None and self . filenameConfig is not None : if hasattr ( config , self . filepathConfig ) and hasattr ( config , self . filenameConfig ) : path = joinPath ( getattr ( config , self . filepathConfig ) ) name = getattr ( config , self . filenameConfig ) if os . path . isfile ( os . path . join ( path , name ) ) : return open ( os . path . join ( path , name ) , 'r' ) if self . filepath is not None and self . filename is not None : path = joinPath ( self . filepath ) name = self . filename if os . path . isfile ( os . path . join ( path , name ) ) : return open ( os . path . join ( path , name ) , 'r' ) | Retrieves a file descriptor to a configuration file to process . | 219 | 13 |
5,243 | def _count_citations ( aux_file ) : counter = defaultdict ( int ) with open ( aux_file ) as fobj : content = fobj . read ( ) for match in CITE_PATTERN . finditer ( content ) : name = match . groups ( ) [ 0 ] counter [ name ] += 1 return counter | Counts the citations in an aux - file . | 72 | 10 |
5,244 | def _setup_logger ( self ) : log = logging . getLogger ( 'latexmk.py' ) handler = logging . StreamHandler ( ) log . addHandler ( handler ) if self . opt . verbose : log . setLevel ( logging . INFO ) return log | Set up a logger . | 60 | 5 |
5,245 | def _parse_texlipse_config ( self ) : # If Eclipse's workspace refresh, the # ".texlipse"-File will be newly created, # so try again after short sleep if # the file is still missing. if not os . path . isfile ( '.texlipse' ) : time . sleep ( 0.1 ) if not os . path . isfile ( '.texlipse' ) : self . log . error ( '! Fatal error: File .texlipse is missing.' ) self . log . error ( '! Exiting...' ) sys . exit ( 1 ) with open ( '.texlipse' ) as fobj : content = fobj . read ( ) match = TEXLIPSE_MAIN_PATTERN . search ( content ) if match : project_name = match . groups ( ) [ 0 ] self . log . info ( 'Found inputfile in ".texlipse": %s.tex' % project_name ) return project_name else : self . log . error ( '! Fatal error: Parsing .texlipse failed.' ) self . log . error ( '! Exiting...' ) sys . exit ( 1 ) | Read the project name from the texlipse config file . texlipse . | 253 | 16 |
5,246 | def _read_latex_files ( self ) : if os . path . isfile ( '%s.aux' % self . project_name ) : cite_counter = self . generate_citation_counter ( ) self . read_glossaries ( ) else : cite_counter = { '%s.aux' % self . project_name : defaultdict ( int ) } fname = '%s.toc' % self . project_name if os . path . isfile ( fname ) : with open ( fname ) as fobj : toc_file = fobj . read ( ) else : toc_file = '' gloss_files = dict ( ) for gloss in self . glossaries : ext = self . glossaries [ gloss ] [ 1 ] filename = '%s.%s' % ( self . project_name , ext ) if os . path . isfile ( filename ) : with open ( filename ) as fobj : gloss_files [ gloss ] = fobj . read ( ) return cite_counter , toc_file , gloss_files | Check if some latex output files exist before first latex run process them and return the generated data . | 231 | 19 |
5,247 | def read_glossaries ( self ) : filename = '%s.aux' % self . project_name with open ( filename ) as fobj : main_aux = fobj . read ( ) pattern = r'\\@newglossary\{(.*)\}\{.*\}\{(.*)\}\{(.*)\}' for match in re . finditer ( pattern , main_aux ) : name , ext_i , ext_o = match . groups ( ) self . glossaries [ name ] = ( ext_i , ext_o ) | Read all existing glossaries in the main aux - file . | 121 | 12 |
5,248 | def check_errors ( self ) : errors = ERROR_PATTTERN . findall ( self . out ) # "errors" is a list of tuples if errors : self . log . error ( '! Errors occurred:' ) self . log . error ( '\n' . join ( [ error . replace ( '\r' , '' ) . strip ( ) for error in chain ( * errors ) if error . strip ( ) ] ) ) self . log . error ( '! See "%s.log" for details.' % self . project_name ) if self . opt . exit_on_error : self . log . error ( '! Exiting...' ) sys . exit ( 1 ) | Check if errors occured during a latex run by scanning the output . | 148 | 14 |
5,249 | def generate_citation_counter ( self ) : cite_counter = dict ( ) filename = '%s.aux' % self . project_name with open ( filename ) as fobj : main_aux = fobj . read ( ) cite_counter [ filename ] = _count_citations ( filename ) for match in re . finditer ( r'\\@input\{(.*.aux)\}' , main_aux ) : filename = match . groups ( ) [ 0 ] try : counter = _count_citations ( filename ) except IOError : pass else : cite_counter [ filename ] = counter return cite_counter | Generate dictionary with the number of citations in all included files . If this changes after the first latex run you have to run bibtex . | 135 | 29 |
5,250 | def latex_run ( self ) : self . log . info ( 'Running %s...' % self . latex_cmd ) cmd = [ self . latex_cmd ] cmd . extend ( LATEX_FLAGS ) cmd . append ( '%s.tex' % self . project_name ) try : with open ( os . devnull , 'w' ) as null : Popen ( cmd , stdout = null , stderr = null ) . wait ( ) except OSError : self . log . error ( NO_LATEX_ERROR % self . latex_cmd ) self . latex_run_counter += 1 fname = '%s.log' % self . project_name with codecs . open ( fname , 'r' , 'utf-8' , 'replace' ) as fobj : self . out = fobj . read ( ) self . check_errors ( ) | Start latex run . | 195 | 4 |
5,251 | def bibtex_run ( self ) : self . log . info ( 'Running bibtex...' ) try : with open ( os . devnull , 'w' ) as null : Popen ( [ 'bibtex' , self . project_name ] , stdout = null ) . wait ( ) except OSError : self . log . error ( NO_LATEX_ERROR % 'bibtex' ) sys . exit ( 1 ) shutil . copy ( '%s.bib' % self . bib_file , '%s.bib.old' % self . bib_file ) | Start bibtex run . | 136 | 6 |
5,252 | def makeindex_runs ( self , gloss_files ) : gloss_changed = False for gloss in self . glossaries : make_gloss = False ext_i , ext_o = self . glossaries [ gloss ] fname_in = '%s.%s' % ( self . project_name , ext_i ) fname_out = '%s.%s' % ( self . project_name , ext_o ) if re . search ( 'No file %s.' % fname_in , self . out ) : make_gloss = True if not os . path . isfile ( fname_out ) : make_gloss = True else : with open ( fname_out ) as fobj : try : if gloss_files [ gloss ] != fobj . read ( ) : make_gloss = True except KeyError : make_gloss = True if make_gloss : self . log . info ( 'Running makeindex (%s)...' % gloss ) try : cmd = [ 'makeindex' , '-q' , '-s' , '%s.ist' % self . project_name , '-o' , fname_in , fname_out ] with open ( os . devnull , 'w' ) as null : Popen ( cmd , stdout = null ) . wait ( ) except OSError : self . log . error ( NO_LATEX_ERROR % 'makeindex' ) sys . exit ( 1 ) gloss_changed = True return gloss_changed | Check for each glossary if it has to be regenerated with makeindex . | 331 | 16 |
5,253 | def open_preview ( self ) : self . log . info ( 'Opening preview...' ) if self . opt . pdf : ext = 'pdf' else : ext = 'dvi' filename = '%s.%s' % ( self . project_name , ext ) if sys . platform == 'win32' : try : os . startfile ( filename ) except OSError : self . log . error ( 'Preview-Error: Extension .%s is not linked to a ' 'specific application!' % ext ) elif sys . platform == 'darwin' : call ( [ 'open' , filename ] ) else : self . log . error ( 'Preview-Error: Preview function is currently not ' 'supported on Linux.' ) | Try to open a preview of the generated document . Currently only supported on Windows . | 159 | 16 |
5,254 | def need_latex_rerun ( self ) : for pattern in LATEX_RERUN_PATTERNS : if pattern . search ( self . out ) : return True return False | Test for all rerun patterns if they match the output . | 42 | 12 |
5,255 | def run ( self ) : # store files self . old_dir = [ ] if self . opt . clean : self . old_dir = os . listdir ( '.' ) cite_counter , toc_file , gloss_files = self . _read_latex_files ( ) self . latex_run ( ) self . read_glossaries ( ) gloss_changed = self . makeindex_runs ( gloss_files ) if gloss_changed or self . _is_toc_changed ( toc_file ) : self . latex_run ( ) if self . _need_bib_run ( cite_counter ) : self . bibtex_run ( ) self . latex_run ( ) while ( self . latex_run_counter < MAX_RUNS ) : if not self . need_latex_rerun ( ) : break self . latex_run ( ) if self . opt . check_cite : cites = set ( ) with open ( '%s.aux' % self . project_name ) as fobj : aux_content = fobj . read ( ) for match in BIBCITE_PATTERN . finditer ( aux_content ) : name = match . groups ( ) [ 0 ] cites . add ( name ) with open ( '%s.bib' % self . bib_file ) as fobj : bib_content = fobj . read ( ) for match in BIBENTRY_PATTERN . finditer ( bib_content ) : name = match . groups ( ) [ 0 ] if name not in cites : self . log . info ( 'Bib entry not cited: "%s"' % name ) if self . opt . clean : ending = '.dvi' if self . opt . pdf : ending = '.pdf' for fname in os . listdir ( '.' ) : if not ( fname in self . old_dir or fname . endswith ( ending ) ) : try : os . remove ( fname ) except IOError : pass if self . opt . preview : self . open_preview ( ) | Run the LaTeX compilation . | 450 | 6 |
5,256 | def command ( self , outfile , configfile , pix ) : params = dict ( script = self . config [ 'scan' ] [ 'script' ] , config = configfile , outfile = outfile , nside = self . nside_likelihood , pix = pix , verbose = '-v' if self . verbose else '' ) cmd = '%(script)s %(config)s %(outfile)s --hpx %(nside)i %(pix)i %(verbose)s' % params return cmd | Generate the command for running the likelihood scan . | 123 | 10 |
5,257 | def submit_all ( self , coords = None , queue = None , debug = False ) : if coords is None : pixels = np . arange ( hp . nside2npix ( self . nside_likelihood ) ) else : coords = np . asarray ( coords ) if coords . ndim == 1 : coords = np . array ( [ coords ] ) if coords . shape [ 1 ] == 2 : lon , lat = coords . T radius = np . zeros ( len ( lon ) ) elif coords . shape [ 1 ] == 3 : lon , lat , radius = coords . T else : raise Exception ( "Unrecognized coords shape:" + str ( coords . shape ) ) #ADW: targets is still in glon,glat if self . config [ 'coords' ] [ 'coordsys' ] . lower ( ) == 'cel' : lon , lat = gal2cel ( lon , lat ) vec = ang2vec ( lon , lat ) pixels = np . zeros ( 0 , dtype = int ) for v , r in zip ( vec , radius ) : pix = query_disc ( self . nside_likelihood , v , r , inclusive = True , fact = 32 ) pixels = np . hstack ( [ pixels , pix ] ) #pixels = np.unique(pixels) inside = ugali . utils . skymap . inFootprint ( self . config , pixels ) if inside . sum ( ) != len ( pixels ) : logger . warning ( "Ignoring pixels outside survey footprint:\n" + str ( pixels [ ~ inside ] ) ) if inside . sum ( ) == 0 : logger . warning ( "No pixels inside footprint." ) return # Only write the configfile once outdir = mkdir ( self . config [ 'output' ] [ 'likedir' ] ) # Actually copy config instead of re-writing shutil . copy ( self . config . filename , outdir ) configfile = join ( outdir , os . path . basename ( self . config . filename ) ) pixels = pixels [ inside ] self . submit ( pixels , queue = queue , debug = debug , configfile = configfile ) | Submit likelihood analyses on a set of coordinates . If coords is None submit all coordinates in the footprint . | 485 | 21 |
5,258 | def check ( cls ) : if cls == AppSettings : return None exceptions = [ ] for setting in cls . settings . values ( ) : try : setting . check ( ) # pylama:ignore=W0703 except Exception as e : exceptions . append ( str ( e ) ) if exceptions : raise ImproperlyConfigured ( "\n" . join ( exceptions ) ) | Class method to check every settings . | 82 | 7 |
5,259 | def parse_args ( name = "" , args = None ) : def _load_json_file ( path ) : with open ( path ) as f : json_data = json . load ( f ) json_data [ 'path_to_json_file' ] = path return json_data parser = argparse . ArgumentParser ( description = "%s collector for sending" " data to the performance" " platform" % name ) parser . add_argument ( '-c' , '--credentials' , dest = 'credentials' , type = _load_json_file , help = 'JSON file containing credentials ' 'for the collector' , required = True ) group = parser . add_mutually_exclusive_group ( required = True ) group . add_argument ( '-l' , '--collector' , dest = 'collector_slug' , type = str , help = 'Collector slug to query the API for the ' 'collector config' ) group . add_argument ( '-q' , '--query' , dest = 'query' , type = _load_json_file , help = 'JSON file containing details ' 'about the query to make ' 'against the source API ' 'and the target data-set' ) parser . add_argument ( '-t' , '--token' , dest = 'token' , type = _load_json_file , help = 'JSON file containing token ' 'for the collector' , required = True ) parser . add_argument ( '-b' , '--performanceplatform' , dest = 'performanceplatform' , type = _load_json_file , help = 'JSON file containing the Performance Platform ' 'config for the collector' , required = True ) parser . add_argument ( '-s' , '--start' , dest = 'start_at' , type = parse_date , help = 'Date to start collection from' ) parser . add_argument ( '-e' , '--end' , dest = 'end_at' , type = parse_date , help = 'Date to end collection' ) parser . add_argument ( '--console-logging' , dest = 'console_logging' , action = 'store_true' , help = 'Output logging to the console rather than file' ) parser . add_argument ( '--dry-run' , dest = 'dry_run' , action = 'store_true' , help = 'Instead of pushing to the Performance Platform ' 'the collector will print out what would have ' 'been pushed' ) parser . set_defaults ( console_logging = False , dry_run = False ) args = parser . parse_args ( args ) return args | Parse command line argument for a collector | 585 | 8 |
5,260 | def hash ( self ) : renderer_str = "{}|{}|{}|{}" . format ( self . renderer . __class__ . __name__ , self . renderer . colormap , self . renderer . fill_value , self . renderer . background_color ) if isinstance ( self . renderer , StretchedRenderer ) : renderer_str = "{}|{}|{}" . format ( renderer_str , self . renderer . method , self . renderer . colorspace ) elif isinstance ( self . renderer , UniqueValuesRenderer ) : renderer_str = "{}|{}" . format ( renderer_str , self . renderer . labels ) return hash ( "{}/{}/{}" . format ( self . variable . pk , renderer_str , self . time_index ) ) | Returns a hash of this render configuration from the variable renderer and time_index parameters . Used for caching the full - extent native projection render so that subsequent requests can be served by a warp operation only . | 192 | 41 |
5,261 | def factory ( type , module = None , * * kwargs ) : cls = type if module is None : module = __name__ fn = lambda member : inspect . isclass ( member ) and member . __module__ == module classes = odict ( inspect . getmembers ( sys . modules [ module ] , fn ) ) members = odict ( [ ( k . lower ( ) , v ) for k , v in classes . items ( ) ] ) lower = cls . lower ( ) if lower not in list ( members . keys ( ) ) : msg = "Unrecognized class: %s.%s" % ( module , cls ) raise KeyError ( msg ) return members [ lower ] ( * * kwargs ) | Factory for creating objects . Arguments are passed directly to the constructor of the chosen class . | 157 | 18 |
5,262 | def get_definition_from_renderer ( renderer ) : config = { 'colors' : [ [ x [ 0 ] , x [ 1 ] . to_hex ( ) ] for x in renderer . colormap ] , 'options' : { } } if renderer . fill_value : config [ 'options' ] [ 'fill_value' ] = renderer . fill_value if isinstance ( renderer , StretchedRenderer ) : config [ 'type' ] = 'stretched' config [ 'options' ] [ 'color_space' ] = renderer . colorspace elif isinstance ( renderer , UniqueValuesRenderer ) : config [ 'type' ] = 'unique' if renderer . labels : config [ 'options' ] [ 'labels' ] = renderer . labels elif isinstance ( renderer , ClassifiedRenderer ) : config [ 'type' ] = 'classified' else : raise ValueError ( '{0} is not a valid renderer type' . format ( renderer . __class__ . __name__ ) ) return config | Returns a dictionary definition of the given renderer | 237 | 9 |
5,263 | def set_model ( self , name , model ) : # Careful to not use `hasattr` # https://hynek.me/articles/hasattr/ try : self . __getattribute__ ( 'models' ) except AttributeError : object . __setattr__ ( self , 'models' , odict ( ) ) self . models [ name ] = model | Set a model . | 80 | 4 |
5,264 | def set_params ( self , * * kwargs ) : for key , value in list ( kwargs . items ( ) ) : setattr ( self , key , value ) | Set the parameter values | 39 | 4 |
5,265 | def get_params ( self ) : return odict ( [ ( key , param . value ) for key , param in self . params . items ( ) ] ) | Get an odict of the parameter names and values | 34 | 10 |
5,266 | def get_free_params ( self ) : return odict ( [ ( key , param . value ) for key , param in self . params . items ( ) if param . free ] ) | Get an odict of free parameter names and values | 40 | 10 |
5,267 | def iter_finds ( regex_obj , s ) : if isinstance ( regex_obj , str ) : for m in re . finditer ( regex_obj , s ) : yield m . group ( ) else : for m in regex_obj . finditer ( s ) : yield m . group ( ) | Generate all matches found within a string for a regex and yield each match as a string | 66 | 18 |
5,268 | def composite_decorator ( func ) : @ wraps ( func ) def wrapper ( self , * args , * * kwargs ) : total = [ ] for weight , iso in zip ( self . weights , self . isochrones ) : subfunc = getattr ( iso , func . __name__ ) total . append ( weight * subfunc ( * args , * * kwargs ) ) return np . sum ( total , axis = 0 ) return wrapper | Decorator for wrapping functions that calculate a weighted sum | 98 | 11 |
5,269 | def mergeCatalogs ( catalog_list ) : # Check the columns for c in catalog_list : if c . data . dtype . names != catalog_list [ 0 ] . data . dtype . names : msg = "Catalog data columns not the same." raise Exception ( msg ) data = np . concatenate ( [ c . data for c in catalog_list ] ) config = catalog_list [ 0 ] . config return Catalog ( config , data = data ) | Merge a list of Catalogs . | 99 | 8 |
5,270 | def applyCut ( self , cut ) : return Catalog ( self . config , data = self . data [ cut ] ) | Return a new catalog which is a subset of objects selected using the input cut array . | 25 | 17 |
5,271 | def bootstrap ( self , mc_bit = 0x10 , seed = None ) : if seed is not None : np . random . seed ( seed ) data = copy . deepcopy ( self . data ) idx = np . random . randint ( 0 , len ( data ) , len ( data ) ) data [ self . config [ 'catalog' ] [ 'mag_1_field' ] ] [ : ] = self . mag_1 [ idx ] data [ self . config [ 'catalog' ] [ 'mag_err_1_field' ] ] [ : ] = self . mag_err_1 [ idx ] data [ self . config [ 'catalog' ] [ 'mag_2_field' ] ] [ : ] = self . mag_2 [ idx ] data [ self . config [ 'catalog' ] [ 'mag_err_2_field' ] ] [ : ] = self . mag_err_2 [ idx ] data [ self . config [ 'catalog' ] [ 'mc_source_id_field' ] ] [ : ] |= mc_bit return Catalog ( self . config , data = data ) | Return a random catalog by boostrapping the colors of the objects in the current catalog . | 252 | 18 |
5,272 | def project ( self , projector = None ) : msg = "'%s.project': ADW 2018-05-05" % self . __class__ . __name__ DeprecationWarning ( msg ) if projector is None : try : self . projector = ugali . utils . projector . Projector ( self . config [ 'coords' ] [ 'reference' ] [ 0 ] , self . config [ 'coords' ] [ 'reference' ] [ 1 ] ) except KeyError : logger . warning ( 'Projection reference point is median (lon, lat) of catalog objects' ) self . projector = ugali . utils . projector . Projector ( np . median ( self . lon ) , np . median ( self . lat ) ) else : self . projector = projector self . x , self . y = self . projector . sphereToImage ( self . lon , self . lat ) | Project coordinates on sphere to image plane using Projector class . | 194 | 12 |
5,273 | def spatialBin ( self , roi ) : if hasattr ( self , 'pixel_roi_index' ) and hasattr ( self , 'pixel' ) : logger . warning ( 'Catalog alread spatially binned' ) return # ADW: Not safe to set index = -1 (since it will access last entry); # np.inf would be better... self . pixel = ang2pix ( self . config [ 'coords' ] [ 'nside_pixel' ] , self . lon , self . lat ) self . pixel_roi_index = roi . indexROI ( self . lon , self . lat ) logger . info ( "Found %i objects outside ROI" % ( self . pixel_roi_index < 0 ) . sum ( ) ) | Calculate indices of ROI pixels corresponding to object locations . | 172 | 13 |
5,274 | def write ( self , outfile , clobber = True , * * kwargs ) : fitsio . write ( outfile , self . data , clobber = True , * * kwargs ) | Write the current object catalog to FITS file . | 45 | 10 |
5,275 | def _parse ( self , roi = None , filenames = None ) : if ( roi is not None ) and ( filenames is not None ) : msg = "Cannot take both roi and filenames" raise Exception ( msg ) if roi is not None : pixels = roi . getCatalogPixels ( ) filenames = self . config . getFilenames ( ) [ 'catalog' ] [ pixels ] elif filenames is None : filenames = self . config . getFilenames ( ) [ 'catalog' ] . compressed ( ) else : filenames = np . atleast_1d ( filenames ) if len ( filenames ) == 0 : msg = "No catalog files found." raise Exception ( msg ) # Load the data self . data = load_infiles ( filenames ) # Apply a selection cut self . _applySelection ( ) # Cast data to recarray (historical reasons) self . data = self . data . view ( np . recarray ) | Parse catalog FITS files into recarray . | 226 | 10 |
5,276 | def _defineVariables ( self ) : logger . info ( 'Catalog contains %i objects' % ( len ( self . data ) ) ) mc_source_id_field = self . config [ 'catalog' ] [ 'mc_source_id_field' ] if mc_source_id_field is not None : if mc_source_id_field not in self . data . dtype . names : array = np . zeros ( len ( self . data ) , dtype = '>i8' ) # FITS byte-order convention self . data = mlab . rec_append_fields ( self . data , names = mc_source_id_field , arrs = array ) logger . info ( 'Found %i simulated objects' % ( np . sum ( self . mc_source_id > 0 ) ) ) | Helper funtion to define pertinent variables from catalog data . | 180 | 11 |
5,277 | def add_node ( self , node_id , task , inputs ) : if node_id in self . nodes_by_id : raise ValueError ( 'The node {0} already exists in this workflow.' . format ( node_id ) ) node = WorkflowNode ( node_id , task , inputs ) self . nodes_by_id [ node_id ] = node for source , value in six . itervalues ( inputs ) : if source == 'dependency' : dependents = self . dependents_by_node_id . get ( value [ 0 ] , set ( ) ) dependents . add ( node_id ) self . dependents_by_node_id [ value [ 0 ] ] = dependents | Adds a node to the workflow . | 157 | 7 |
5,278 | def map_output ( self , node_id , node_output_name , parameter_name ) : self . output_mapping [ parameter_name ] = ( node_id , node_output_name ) dependents = self . dependents_by_node_id . get ( node_id , set ( ) ) dependents . add ( 'output_{}' . format ( parameter_name ) ) self . dependents_by_node_id [ node_id ] = dependents | Maps the output from a node to a workflow output . | 105 | 11 |
5,279 | def to_json ( self , indent = None ) : inputs = ParameterCollection ( self . inputs ) d = { 'meta' : { 'name' : self . name , 'description' : self . description } , 'inputs' : [ ] , 'workflow' : [ ] , 'outputs' : [ { 'name' : k , 'node' : v } for k , v in six . iteritems ( self . output_mapping ) ] } for parameter in self . inputs : input_info = { 'name' : parameter . name , 'type' : parameter . id } args , kwargs = parameter . serialize_args ( ) args = list ( args ) args . pop ( 0 ) # 'name' is already taken care of kwargs . pop ( 'required' , None ) # 'required' is assumed True for workflow inputs if args or kwargs : input_info [ 'args' ] = [ args , kwargs ] d [ 'inputs' ] . append ( input_info ) for node in sorted ( six . itervalues ( self . nodes_by_id ) , key = lambda x : x . id ) : task_name = node . task . name if not task_name : raise ValueError ( 'The task {0} does not have a name and therefore cannot be serialized.' . format ( node . task . __class__ . __name__ ) ) node_inputs = { } for input_name , ( source , value ) in six . iteritems ( node . inputs ) : input_info = { 'source' : source } if source == 'input' : input_info [ 'input' ] = inputs . by_name [ value ] . name else : input_info [ 'node' ] = value node_inputs [ input_name ] = input_info d [ 'workflow' ] . append ( { 'id' : node . id , 'task' : task_name , 'inputs' : node_inputs } ) return json . dumps ( d , indent = indent ) | Serialize this workflow to JSON | 446 | 6 |
5,280 | def from_json ( cls , text ) : d = json . loads ( text ) meta = d . get ( 'meta' , { } ) workflow = cls ( name = meta . get ( 'name' ) , description = meta . get ( 'description' ) ) for workflow_input in d . get ( 'inputs' , [ ] ) : parameter_cls = Parameter . by_id ( workflow_input [ 'type' ] ) args = [ workflow_input [ 'name' ] ] kwargs = { 'required' : True } if workflow_input . get ( 'args' ) : args = workflow_input [ 'args' ] [ 0 ] + args kwargs . update ( workflow_input [ 'args' ] [ 1 ] ) args , kwargs = parameter_cls . deserialize_args ( args , kwargs ) workflow . inputs . append ( parameter_cls ( * args , * * kwargs ) ) for node in d . get ( 'workflow' , [ ] ) : node_inputs = { } for k , v in six . iteritems ( node . get ( 'inputs' , { } ) ) : node_inputs [ k ] = ( v [ 'source' ] , v . get ( 'input' ) or v . get ( 'node' ) ) workflow . add_node ( node [ 'id' ] , Task . by_name ( node [ 'task' ] ) ( ) , node_inputs ) for output in d . get ( 'outputs' , [ ] ) : node = output [ 'node' ] node_parameters = ParameterCollection ( workflow . nodes_by_id [ node [ 0 ] ] . task . outputs ) # Add parameter to workflow output output_param = copy . copy ( node_parameters . by_name [ node [ 1 ] ] ) output_param . name = output [ 'name' ] workflow . outputs . append ( output_param ) workflow . map_output ( node [ 0 ] , node [ 1 ] , output [ 'name' ] ) return workflow | Return a new workflow deserialized from a JSON string | 452 | 11 |
5,281 | def get_handler ( self , * args , * * options ) : handler = super ( ) . get_handler ( * args , * * options ) use_static_handler = options [ 'use_static_handler' ] insecure_serving = options [ 'insecure_serving' ] if use_static_handler and ( settings . DEBUG or insecure_serving ) : return CRAStaticFilesHandler ( handler ) return handler | Return the static files serving handler wrapping the default handler if static files should be served . Otherwise return the default handler . | 88 | 23 |
5,282 | def surveyPixel ( lon , lat , nside_pix , nside_subpix = None ) : pix = np . unique ( ang2pix ( nside_pix , lon , lat ) ) if nside_subpix is None : return pix else : subpix_array = [ ] for ii in range ( 0 , len ( pix ) ) : subpix = subpixel ( pix [ ii ] , nside_pix , nside_subpix ) subpix_array . append ( subpix ) return pix , np . array ( subpix_array ) | Return the set of HEALPix pixels that cover the given coordinates at resolution nside . Optionally return the set of subpixels within those pixels at resolution nside_subpix | 133 | 38 |
5,283 | def allSkyCoordinates ( nside ) : lon , lat = pix2ang ( nside , np . arange ( 0 , hp . nside2npix ( nside ) ) ) return lon , lat | Generate a set of coordinates at the centers of pixels of resolutions nside across the full sky . | 49 | 20 |
5,284 | def randomPositionsMask ( mask , nside_pix , n ) : npix = len ( mask ) nside = hp . npix2nside ( npix ) # Estimate the number of points that need to be thrown based off # coverage fraction of the HEALPix mask coverage_fraction = float ( np . sum ( mask ) ) / len ( mask ) n_throw = int ( n / coverage_fraction ) lon , lat = [ ] , [ ] latch = True count = 0 while len ( lon ) < n : lon_throw = np . random . uniform ( 0. , 360. , n_throw ) lat_throw = np . degrees ( np . arcsin ( np . random . uniform ( - 1. , 1. , n_throw ) ) ) pix = ugali . utils . healpix . angToPix ( nside , lon_throw , lat_throw ) cut = mask [ pix ] . astype ( bool ) lon = np . append ( lon , lon_throw [ cut ] ) lat = np . append ( lat , lat_throw [ cut ] ) count += 1 if count > 10 : raise RuntimeError ( 'Too many loops...' ) return lon [ 0 : n ] , lat [ 0 : n ] | Generate n random positions within a HEALPix mask of booleans . | 283 | 17 |
5,285 | def embed_ising ( source_linear , source_quadratic , embedding , target_adjacency , chain_strength = 1.0 ) : # store variables in the target graph that the embedding hasn't used unused = { v for v in target_adjacency } - set ( ) . union ( * embedding . values ( ) ) # ok, let's begin with the linear biases. # we spread the value of h evenly over the chain target_linear = { v : 0. for v in target_adjacency } for v , bias in iteritems ( source_linear ) : try : chain_variables = embedding [ v ] except KeyError : # if our embedding doesn't deal with this variable, assume it's an isolated vertex and embed it to one of # the unused variables. if this turns out to not be an isolated vertex, it will be caught below when # handling quadratic biases try : embedding [ v ] = { unused . pop ( ) } except KeyError : raise ValueError ( 'no embedding provided for source variable {}' . format ( v ) ) chain_variables = embedding [ v ] b = bias / len ( chain_variables ) for s in chain_variables : try : target_linear [ s ] += b except KeyError : raise ValueError ( 'chain variable {} not in target_adjacency' . format ( s ) ) # next up the quadratic biases. # We spread the quadratic biases evenly over the edges target_quadratic = { } for ( u , v ) , bias in iteritems ( source_quadratic ) : edges = set ( ) if u not in embedding : raise ValueError ( 'no embedding provided for source variable {}' . format ( u ) ) if v not in embedding : raise ValueError ( 'no embedding provided for source variable {}' . format ( v ) ) for s in embedding [ u ] : for t in embedding [ v ] : try : if s in target_adjacency [ t ] and ( t , s ) not in edges : edges . add ( ( s , t ) ) except KeyError : raise ValueError ( 'chain variable {} not in target_adjacency' . format ( s ) ) if not edges : raise ValueError ( "no edges in target graph between source variables {}, {}" . format ( u , v ) ) b = bias / len ( edges ) # in some cases the logical J can have (u, v) and (v, u) as inputs, so make # sure we are not doubling them up with our choice of ordering for s , t in edges : if ( s , t ) in target_quadratic : target_quadratic [ ( s , t ) ] += b elif ( t , s ) in target_quadratic : target_quadratic [ ( t , s ) ] += b else : target_quadratic [ ( s , t ) ] = b # finally we need to connect the nodes in the chains chain_quadratic = { } for chain in itervalues ( embedding ) : chain_quadratic . update ( chain_to_quadratic ( chain , target_adjacency , chain_strength ) ) return target_linear , target_quadratic , chain_quadratic | Embeds a logical Ising model onto another graph via an embedding . | 713 | 15 |
5,286 | def chain_break_frequency ( samples , embedding ) : counts = { v : 0 for v in embedding } total = 0 for sample in samples : for v , chain in iteritems ( embedding ) : vals = [ sample [ u ] for u in chain ] if not _all_equal ( vals ) : counts [ v ] += 1 total += 1 return { v : counts [ v ] / total for v in embedding } | Determines the frequency of chain breaks in the given samples . | 94 | 13 |
5,287 | def unembed_samples ( samples , embedding , chain_break_method = None ) : if chain_break_method is None : chain_break_method = majority_vote return list ( itertools . chain ( * ( chain_break_method ( sample , embedding ) for sample in samples ) ) ) | Return samples over the variables in the source graph . | 68 | 10 |
5,288 | def discard ( sample , embedding ) : unembeded = { } for v , chain in iteritems ( embedding ) : vals = [ sample [ u ] for u in chain ] if _all_equal ( vals ) : unembeded [ v ] = vals . pop ( ) else : return yield unembeded | Discards the sample if broken . | 70 | 7 |
5,289 | def majority_vote ( sample , embedding ) : unembeded = { } for v , chain in iteritems ( embedding ) : vals = [ sample [ u ] for u in chain ] if _all_equal ( vals ) : unembeded [ v ] = vals . pop ( ) else : unembeded [ v ] = _most_common ( vals ) yield unembeded | Determines the sample values by majority vote . | 86 | 10 |
5,290 | def weighted_random ( sample , embedding ) : unembeded = { } for v , chain in iteritems ( embedding ) : vals = [ sample [ u ] for u in chain ] # pick a random element uniformly from all vals, this weights them by # the proportion of each unembeded [ v ] = random . choice ( vals ) yield unembeded | Determines the sample values by weighed random choice . | 80 | 11 |
5,291 | def _all_equal ( iterable ) : iterator = iter ( iterable ) first = next ( iterator ) return all ( first == rest for rest in iterator ) | True if all values in iterable are equal else False . | 34 | 12 |
5,292 | def _most_common ( iterable ) : data = Counter ( iterable ) return max ( data , key = data . __getitem__ ) | Returns the most common element in iterable . | 31 | 9 |
5,293 | def lnlike ( self , theta ) : params , loglike = self . params , self . loglike kwargs = dict ( list ( zip ( params , theta ) ) ) try : lnlike = loglike . value ( * * kwargs ) except ValueError as AssertionError : lnlike = - np . inf return lnlike | Logarithm of the likelihood | 79 | 7 |
5,294 | def lnprior ( self , theta ) : params , priors = self . params , self . priors kwargs = dict ( list ( zip ( params , theta ) ) ) err = np . seterr ( invalid = 'raise' ) try : lnprior = np . sum ( np . log ( [ priors [ k ] ( v ) for k , v in list ( kwargs . items ( ) ) ] ) ) except ( FloatingPointError , ValueError ) : lnprior = - np . inf np . seterr ( * * err ) return lnprior | Logarithm of the prior | 130 | 7 |
5,295 | def lnprob ( self , theta ) : global niter params , priors , loglike = self . params , self . priors , self . loglike # Avoid extra likelihood calls with bad priors _lnprior = self . lnprior ( theta ) if np . isfinite ( _lnprior ) : _lnlike = self . lnlike ( theta ) else : _lnprior = - np . inf _lnlike = - np . inf _lnprob = _lnprior + _lnlike if ( niter % 100 == 0 ) : msg = "%i function calls ...\n" % niter msg += ', ' . join ( '%s: %.3f' % ( k , v ) for k , v in zip ( params , theta ) ) msg += '\nlog(like): %.3f, log(prior): %.3f' % ( _lnprior , _lnlike ) logger . debug ( msg ) niter += 1 return _lnprob | Logarithm of the probability | 226 | 7 |
5,296 | def write_membership ( filename , config , srcfile , section = None ) : source = Source ( ) source . load ( srcfile , section = section ) loglike = createLoglike ( config , source ) loglike . write_membership ( filename ) | Top level interface to write the membership from a config and source model . | 55 | 14 |
5,297 | def createCatalog ( config , roi = None , lon = None , lat = None ) : import ugali . observation . catalog if roi is None : roi = createROI ( config , lon , lat ) catalog = ugali . observation . catalog . Catalog ( config , roi = roi ) return catalog | Create a catalog object | 71 | 4 |
5,298 | def simulateCatalog ( config , roi = None , lon = None , lat = None ) : import ugali . simulation . simulator if roi is None : roi = createROI ( config , lon , lat ) sim = ugali . simulation . simulator . Simulator ( config , roi ) return sim . catalog ( ) | Simulate a catalog object . | 72 | 6 |
5,299 | def calc_observable_fraction ( self , distance_modulus ) : # This is the observable fraction after magnitude cuts in each # pixel of the ROI. observable_fraction = self . isochrone . observableFraction ( self . mask , distance_modulus ) if not observable_fraction . sum ( ) > 0 : msg = "No observable fraction" msg += ( "\n" + str ( self . source . params ) ) logger . error ( msg ) raise ValueError ( msg ) return observable_fraction | Calculated observable fraction within each pixel of the target region . | 113 | 12 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.