idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
39,700
def save_gui_settings ( self , * a ) : if self . _autosettings_path : gui_settings_dir = _os . path . join ( _cwd , 'egg_settings' ) if not _os . path . exists ( gui_settings_dir ) : _os . mkdir ( gui_settings_dir ) path = _os . path . join ( gui_settings_dir , self . _autosettings_path ) d = _d . databox ( ) for x in self . _autosettings_controls : self . _store_gui_setting ( d , x ) d . save_file ( path , force_overwrite = True )
Saves just the current configuration of the controls if the autosettings_path is set .
39,701
def _store_gui_setting ( self , databox , name ) : try : databox . insert_header ( name , eval ( name + ".get_value()" ) ) except : print ( "ERROR: Could not store gui setting " + repr ( name ) )
Stores the gui setting in the header of the supplied databox . hkeys in the file are set to have the format self . controlname
39,702
def place_object ( self , object , column = None , row = None , column_span = 1 , row_span = 1 , alignment = 1 ) : if column == None : column = self . _auto_column self . _auto_column += 1 if row == None : row = self . _auto_row self . objects . append ( object ) try : object . _widget widget = object . _widget except : widget = object self . _layout . addWidget ( widget , row , column , row_span , column_span , _g . Qt . QtCore . Qt . Alignment ( alignment ) ) try : object . set_parent ( self ) except : None return object
This adds either one of our simplified objects or a QWidget to the grid at the specified position appends the object to self . objects .
39,703
def remove_object ( self , object = 0 , delete = True ) : if type ( object ) in [ int , int ] : n = object else : n = self . objects . index ( object ) object = self . objects . pop ( n ) if hasattr_safe ( object , '_widget' ) : self . _layout . removeWidget ( object . _widget ) object . _widget . hide ( ) if delete : object . _widget . deleteLater ( ) else : self . _layout . removeWidget ( object ) object . hide ( ) if delete : object . deleteLater ( )
Removes the supplied object from the grid . If object is an integer it removes the n th object .
39,704
def set_column_stretch ( self , column = 0 , stretch = 10 ) : self . _layout . setColumnStretch ( column , stretch ) return self
Sets the column stretch . Larger numbers mean it will expand more to fill space .
39,705
def set_row_stretch ( self , row = 0 , stretch = 10 ) : self . _layout . setRowStretch ( row , stretch ) return self
Sets the row stretch . Larger numbers mean it will expand more to fill space .
39,706
def new_autorow ( self , row = None ) : if row == None : self . _auto_row += 1 else : self . _auto_row = row self . _auto_column = 0 return self
Sets the auto - add row . If row = None increments by 1
39,707
def _save_settings ( self ) : if self . _autosettings_path == None : return gui_settings_dir = _os . path . join ( _cwd , 'egg_settings' ) if not _os . path . exists ( gui_settings_dir ) : _os . mkdir ( gui_settings_dir ) path = _os . path . join ( gui_settings_dir , self . _autosettings_path ) settings = _g . QtCore . QSettings ( path , _g . QtCore . QSettings . IniFormat ) settings . clear ( ) if hasattr_safe ( self . _window , "saveState" ) : settings . setValue ( 'State' , self . _window . saveState ( ) ) settings . setValue ( 'Geometry' , self . _window . saveGeometry ( ) )
Saves all the parameters to a text file .
39,708
def _load_settings ( self ) : if self . _autosettings_path == None : return gui_settings_dir = _os . path . join ( _cwd , 'egg_settings' ) path = _os . path . join ( gui_settings_dir , self . _autosettings_path ) if not _os . path . exists ( path ) : return settings = _g . QtCore . QSettings ( path , _g . QtCore . QSettings . IniFormat ) if settings . contains ( 'State' ) and hasattr_safe ( self . _window , "restoreState" ) : x = settings . value ( 'State' ) if hasattr ( x , "toByteArray" ) : x = x . toByteArray ( ) self . _window . restoreState ( x ) if settings . contains ( 'Geometry' ) : x = settings . value ( 'Geometry' ) if hasattr ( x , "toByteArray" ) : x = x . toByteArray ( ) self . _window . restoreGeometry ( x )
Loads all the parameters from a databox text file . If path = None loads from self . _autosettings_path .
39,709
def show ( self , block_command_line = False , block_timing = 0.05 ) : self . _is_open = True self . _window . show ( ) self . _window . raise_ ( ) if block_command_line : while self . _is_open : _a . processEvents ( ) _t . sleep ( block_timing ) _t . sleep ( 0.5 ) return self
Shows the window and raises it .
39,710
def set_checked ( self , value = True , block_events = False ) : if block_events : self . _widget . blockSignals ( True ) self . _widget . setChecked ( value ) if block_events : self . _widget . blockSignals ( False ) return self
This will set whether the button is checked .
39,711
def click ( self ) : if self . is_checkable ( ) : if self . is_checked ( ) : self . set_checked ( False ) else : self . set_checked ( True ) self . signal_clicked . emit ( self . is_checked ( ) ) else : self . signal_clicked . emit ( True ) return self
Pretends to user clicked it sending the signal and everything .
39,712
def set_value ( self , value , block_events = False ) : if block_events : self . block_events ( ) self . _widget . setValue ( value ) if block_events : self . unblock_events ( )
Sets the current value of the number box .
39,713
def set_step ( self , value , block_events = False ) : if block_events : self . block_events ( ) self . _widget . setSingleStep ( value ) if block_events : self . unblock_events ( )
Sets the step of the number box .
39,714
def get_all_items ( self ) : return [ self . _widget . itemText ( k ) for k in range ( self . _widget . count ( ) ) ]
Returns all items in the combobox dictionary .
39,715
def add_tab ( self , title = "Yeah!" , block_events = True ) : self . _widget . blockSignals ( block_events ) tab = GridLayout ( ) self . tabs . append ( tab ) tab . set_parent ( self ) self . _widget . addTab ( tab . _widget , title ) if 'self' in self . _lazy_load and self . get_tab_count ( ) > self . _lazy_load [ 'self' ] : v = self . _lazy_load . pop ( 'self' ) self . set_current_tab ( v ) self . _widget . blockSignals ( False ) return tab
Adds a tab to the area and creates the layout for this tab .
39,716
def remove_tab ( self , tab = 0 ) : t = self . tabs . pop ( tab ) self . _widget . removeTab ( tab ) return t
Removes the tab by index .
39,717
def get_value ( self , column = 0 , row = 0 ) : x = self . _widget . item ( row , column ) if x == None : return x else : return str ( self . _widget . item ( row , column ) . text ( ) )
Returns a the value at column row .
39,718
def set_value ( self , column = 0 , row = 0 , value = '' , block_events = False ) : if block_events : self . block_events ( ) while column > self . _widget . columnCount ( ) - 1 : self . _widget . insertColumn ( self . _widget . columnCount ( ) ) while row > self . _widget . rowCount ( ) - 1 : self . _widget . insertRow ( self . _widget . rowCount ( ) ) self . _widget . setItem ( row , column , _g . Qt . QtGui . QTableWidgetItem ( str ( value ) ) ) if block_events : self . unblock_events ( ) return self
Sets the value at column row . This will create elements dynamically and in a totally stupid while - looping way .
39,719
def set_column_width ( self , n = 0 , width = 120 ) : self . _widget . setColumnWidth ( n , width ) return self
Sets the n th column width in pixels .
39,720
def set_header_visibility ( self , column = False , row = False ) : if row : self . _widget . verticalHeader ( ) . show ( ) else : self . _widget . verticalHeader ( ) . hide ( ) if column : self . _widget . horizontalHeader ( ) . show ( ) else : self . _widget . horizontalHeader ( ) . hide ( ) return self
Sets whether we can see the column and row headers .
39,721
def set_row_height ( self , n = 0 , height = 18 ) : self . _widget . setRowHeight ( n , height ) return self
Sets the n th row height in pixels .
39,722
def _clean_up_key ( self , key ) : for n in self . naughty : key = key . replace ( n , '_' ) return key
Returns the key string with no naughty characters .
39,723
def _cell_changed ( self , * a ) : self . block_events ( ) for n in range ( self . get_row_count ( ) ) : key = self . get_value ( 0 , n ) value = self . get_value ( 1 , n ) if key == None : key = '' self . set_value ( 0 , n , '' ) if value == None : value = '' self . set_value ( 1 , n , '' ) if not key == '' : key = self . _clean_up_key ( key ) try : eval ( value ) self . _widget . item ( n , 1 ) . setData ( _g . QtCore . Qt . BackgroundRole , _g . Qt . QtGui . QColor ( 'white' ) ) except : self . _widget . item ( n , 1 ) . setData ( _g . QtCore . Qt . BackgroundRole , _g . Qt . QtGui . QColor ( 'pink' ) ) self . unblock_events ( )
Called whenever a cell is changed . Updates the dictionary .
39,724
def get_item ( self , key ) : keys = list ( self . keys ( ) ) if not key in keys : self . print_message ( "ERROR: '" + str ( key ) + "' not found." ) return None try : x = eval ( self . get_value ( 1 , keys . index ( key ) ) ) return x except : self . print_message ( "ERROR: '" + str ( self . get_value ( 1 , keys . index ( key ) ) ) + "' cannot be evaluated." ) return None
Returns the value associated with the key .
39,725
def keys ( self ) : keys = list ( ) for n in range ( len ( self ) ) : key = self . get_value ( ) if not key in [ '' , None ] : keys . append ( key ) return keys
Returns a sorted list of keys
39,726
def set_item ( self , key , value ) : keys = list ( self . keys ( ) ) if key in keys : self . set_value ( 1 , keys . index ( key ) , str ( value ) ) else : self . set_value ( 0 , len ( self ) , str ( key ) ) self . set_value ( 1 , len ( self ) - 1 , str ( value ) )
Sets the item by key and refills the table sorted .
39,727
def get_text ( self ) : if self . _multiline : return str ( self . _widget . toPlainText ( ) ) else : return str ( self . _widget . text ( ) )
Returns the current text .
39,728
def set_text ( self , text = "YEAH." ) : s = str ( text ) if not s == self . get_text ( ) : self . _widget . setText ( str ( text ) ) return self
Sets the current value of the text box .
39,729
def set_colors ( self , text = 'black' , background = 'white' ) : if self . _multiline : self . _widget . setStyleSheet ( "QTextEdit {background-color: " + str ( background ) + "; color: " + str ( text ) + "}" ) else : self . _widget . setStyleSheet ( "QLineEdit {background-color: " + str ( background ) + "; color: " + str ( text ) + "}" )
Sets the colors of the text area .
39,730
def block_events ( self ) : BaseObject . block_events ( self ) for i in range ( self . _widget . topLevelItemCount ( ) ) : self . _widget . topLevelItem ( i ) . param . blockSignals ( True ) return self
Special version of block_events that loops over all tree elements .
39,731
def unblock_events ( self ) : BaseObject . unblock_events ( self ) for i in range ( self . _widget . topLevelItemCount ( ) ) : self . _widget . topLevelItem ( i ) . param . blockSignals ( False ) return self
Special version of unblock_events that loops over all tree elements as well .
39,732
def connect_signal_changed ( self , name , function ) : x = self . _find_parameter ( name . split ( "/" ) ) if x == None : return None x . sigValueChanged . connect ( function ) if name in self . _connection_lists : self . _connection_lists [ name ] . append ( function ) else : self . _connection_lists [ name ] = [ function ] return self
Connects a changed signal from the parameters of the specified name to the supplied function .
39,733
def _find_parameter ( self , name_list , create_missing = False , quiet = False ) : s = list ( name_list ) if len ( s ) == 0 : return self . _widget r = self . _clean_up_name ( s . pop ( 0 ) ) result = self . _widget . findItems ( r , _g . QtCore . Qt . MatchCaseSensitive | _g . QtCore . Qt . MatchFixedString ) if len ( result ) == 0 and not create_missing : if not quiet : self . print_message ( "ERROR: Could not find '" + r + "'" ) return None elif len ( result ) : x = result [ 0 ] . param else : x = _g . parametertree . Parameter . create ( name = r , type = 'group' , children = [ ] ) self . _widget . addParameters ( x ) for n in s : n = self . _clean_up_name ( n ) try : x = x . param ( n ) except : if create_missing : x = x . addChild ( _g . parametertree . Parameter . create ( name = n , type = 'group' , children = [ ] ) ) else : if not quiet : self . print_message ( "ERROR: Could not find '" + n + "' in '" + x . name ( ) + "'" ) return None return x
Tries to find and return the parameter of the specified name . The name should be of the form
39,734
def _clean_up_name ( self , name ) : for n in self . naughty : name = name . replace ( n , '_' ) return name
Cleans up the name according to the rules specified in this exact function . Uses self . naughty a list of naughty characters .
39,735
def send_to_databox_header ( self , destination_databox ) : k , d = self . get_dictionary ( ) destination_databox . update_headers ( d , k )
Sends all the information currently in the tree to the supplied databox s header in alphabetical order . If the entries already exists just updates them .
39,736
def get_value ( self , name ) : name = self . _clean_up_name ( name ) x = self . _find_parameter ( name . split ( '/' ) ) if x == None : return None value = x . value ( ) bounds = None if x . opts [ 'type' ] == 'list' : if not value in x . opts [ 'values' ] : if len ( x . opts ( 'values' ) ) : self . set_value ( name , x . opts [ 'values' ] [ 0 ] ) return x . opts [ 'values' ] [ 0 ] else : return None elif x . opts [ 'type' ] in [ 'str' ] : return str ( value ) else : if 'limits' in x . opts : bounds = x . opts [ 'limits' ] elif 'bounds' in x . opts : bounds = x . opts [ 'bounds' ] if not bounds == None : if not bounds [ 1 ] == None and value > bounds [ 1 ] : value = bounds [ 1 ] if not bounds [ 0 ] == None and value < bounds [ 0 ] : value = bounds [ 0 ] return value
Returns the value of the parameter with the specified name .
39,737
def get_list_values ( self , name ) : if not self . get_type ( name ) in [ 'list' ] : self . print_message ( 'ERROR: "' + name + '" is not a list.' ) return return list ( self . get_widget ( name ) . opts [ 'values' ] )
Returns the values for a list item of the specified name .
39,738
def set_value ( self , name , value , ignore_error = False , block_user_signals = False ) : name = self . _clean_up_name ( name ) if block_user_signals : self . block_user_signals ( name , ignore_error ) x = self . _find_parameter ( name . split ( '/' ) , quiet = ignore_error ) if x == None : return None if x . type ( ) in [ 'list' ] : if str ( value ) in list ( x . forward . keys ( ) ) : x . setValue ( str ( value ) ) else : x . setValue ( list ( x . forward . keys ( ) ) [ 0 ] ) else : x . setValue ( eval ( x . opts [ 'type' ] ) ( value ) ) if block_user_signals : self . unblock_user_signals ( name , ignore_error ) return self
Sets the variable of the supplied name to the supplied value .
39,739
def save ( self , path = None ) : if path == None : if self . _autosettings_path == None : return self gui_settings_dir = _os . path . join ( _cwd , 'egg_settings' ) if not _os . path . exists ( gui_settings_dir ) : _os . mkdir ( gui_settings_dir ) path = _os . path . join ( gui_settings_dir , self . _autosettings_path ) d = _d . databox ( ) keys , dictionary = self . get_dictionary ( ) for k in keys : d . insert_header ( k , dictionary [ k ] ) try : d . save_file ( path , force_overwrite = True , header_only = True ) except : print ( 'Warning: could not save ' + path . __repr__ ( ) + ' once. Could be that this is being called too rapidly.' ) return self
Saves all the parameters to a text file using the databox functionality . If path = None saves to self . _autosettings_path . If self . _autosettings_path = None does not save .
39,740
def _set_value_safe ( self , k , v , ignore_errors = False , block_user_signals = False ) : try : self . set_value ( k , v , ignore_error = ignore_errors , block_user_signals = block_user_signals ) except : print ( "TreeDictionary ERROR: Could not set '" + k + "' to '" + v + "'" )
Actually sets the value first by trying it directly then by
39,741
def _button_autosave_clicked ( self , checked ) : if checked : path = _spinmob . dialogs . save ( filters = self . file_type ) if not path : self . button_autosave . set_checked ( False ) return self . _autosave_directory , filename = _os . path . split ( path ) self . _label_path . set_text ( filename ) self . save_gui_settings ( )
Called whenever the button is clicked .
39,742
def save_file ( self , path = None , force_overwrite = False , just_settings = False , ** kwargs ) : if not 'binary' in kwargs : kwargs [ 'binary' ] = self . combo_binary . get_text ( ) if just_settings : d = _d . databox ( ) else : d = self for x in self . _autosettings_controls : self . _store_gui_setting ( d , x ) _d . databox . save_file ( d , path , self . file_type , self . file_type , force_overwrite , ** kwargs )
Saves the data in the databox to a file .
39,743
def autosave ( self ) : if self . button_autosave . is_checked ( ) : self . save_file ( _os . path . join ( self . _autosave_directory , "%04d " % ( self . number_file . get_value ( ) ) + self . _label_path . get_text ( ) ) ) self . number_file . increment ( )
Autosaves the currently stored data but only if autosave is checked!
39,744
def autozoom ( self , n = None ) : if n == None : for p in self . plot_widgets : p . autoRange ( ) else : self . plot_widgets [ n ] . autoRange ( ) return self
Auto - scales the axes to fit all the data in plot index n . If n == None auto - scale everyone .
39,745
def _synchronize_controls ( self ) : self . grid_script . _widget . setVisible ( self . button_script . get_value ( ) ) if not self . combo_autoscript . get_index ( ) == 0 : self . script . disable ( ) else : self . script . enable ( )
Updates the gui based on button configs .
39,746
def _set_number_of_plots ( self , n ) : if self . button_multi . is_checked ( ) and len ( self . _curves ) == len ( self . plot_widgets ) and len ( self . _curves ) == n : return if not self . button_multi . is_checked ( ) and len ( self . plot_widgets ) == 1 and len ( self . _curves ) == n : return self . grid_plot . block_events ( ) while len ( self . _curves ) > n : self . _curves . pop ( - 1 ) while len ( self . _curves ) < n : self . _curves . append ( _g . PlotCurveItem ( pen = ( len ( self . _curves ) , n ) ) ) if self . button_multi . is_checked ( ) : n_plots = n else : n_plots = min ( n , 1 ) while len ( self . plot_widgets ) : p = self . plot_widgets . pop ( - 1 ) p . clear ( ) self . grid_plot . remove_object ( p ) for i in range ( n_plots ) : self . plot_widgets . append ( self . grid_plot . place_object ( _g . PlotWidget ( ) , 0 , i , alignment = 0 ) ) for i in range ( n ) : self . plot_widgets [ min ( i , len ( self . plot_widgets ) - 1 ) ] . addItem ( self . _curves [ i ] ) if self . ROIs is not None : for i in range ( len ( self . ROIs ) ) : ROIs = self . ROIs [ i ] if not _spinmob . fun . is_iterable ( ROIs ) : ROIs = [ ROIs ] for ROI in ROIs : m = min ( i , len ( self . plot_widgets ) - 1 ) if m >= 0 : self . plot_widgets [ m ] . addItem ( ROI ) self . grid_plot . unblock_events ( )
Adjusts number of plots & curves to the desired value the gui .
39,747
def resolve_const_spec ( self , name , lineno ) : if name in self . const_specs : return self . const_specs [ name ] . link ( self ) if '.' in name : include_name , component = name . split ( '.' , 1 ) if include_name in self . included_scopes : return self . included_scopes [ include_name ] . resolve_const_spec ( component , lineno ) raise ThriftCompilerError ( 'Unknown constant "%s" referenced at line %d%s' % ( name , lineno , self . __in_path ( ) ) )
Finds and links the ConstSpec with the given name .
39,748
def resolve_type_spec ( self , name , lineno ) : if name in self . type_specs : return self . type_specs [ name ] . link ( self ) if '.' in name : include_name , component = name . split ( '.' , 1 ) if include_name in self . included_scopes : return self . included_scopes [ include_name ] . resolve_type_spec ( component , lineno ) raise ThriftCompilerError ( 'Unknown type "%s" referenced at line %d%s' % ( name , lineno , self . __in_path ( ) ) )
Finds and links the TypeSpec with the given name .
39,749
def resolve_service_spec ( self , name , lineno ) : if name in self . service_specs : return self . service_specs [ name ] . link ( self ) if '.' in name : include_name , component = name . split ( '.' , 2 ) if include_name in self . included_scopes : return self . included_scopes [ include_name ] . resolve_service_spec ( component , lineno ) raise ThriftCompilerError ( 'Unknown service "%s" referenced at line %d%s' % ( name , lineno , self . __in_path ( ) ) )
Finds and links the ServiceSpec with the given name .
39,750
def add_include ( self , name , included_scope , module ) : assert name not in self . included_scopes self . included_scopes [ name ] = included_scope self . add_surface ( name , module )
Register an imported module into this scope .
39,751
def add_service_spec ( self , service_spec ) : assert service_spec is not None if service_spec . name in self . service_specs : raise ThriftCompilerError ( 'Cannot define service "%s". That name is already taken.' % service_spec . name ) self . service_specs [ service_spec . name ] = service_spec
Registers the given ServiceSpec into the scope .
39,752
def add_const_spec ( self , const_spec ) : if const_spec . name in self . const_specs : raise ThriftCompilerError ( 'Cannot define constant "%s". That name is already taken.' % const_spec . name ) self . const_specs [ const_spec . name ] = const_spec
Adds a ConstSpec to the compliation scope .
39,753
def add_surface ( self , name , surface ) : assert surface is not None if hasattr ( self . module , name ) : raise ThriftCompilerError ( 'Cannot define "%s". The name has already been used.' % name ) setattr ( self . module , name , surface )
Adds a top - level attribute with the given name to the module .
39,754
def add_type_spec ( self , name , spec , lineno ) : assert type is not None if name in self . type_specs : raise ThriftCompilerError ( 'Cannot define type "%s" at line %d. ' 'Another type with that name already exists.' % ( name , lineno ) ) self . type_specs [ name ] = spec
Adds the given type to the scope .
39,755
def coarsen_matrix ( Z , xlevel = 0 , ylevel = 0 , method = 'average' ) : if not ylevel : Z_coarsened = Z else : temp = [ ] for z in Z : temp . append ( coarsen_array ( z , ylevel , method ) ) Z_coarsened = _n . array ( temp ) if xlevel : Z_coarsened = Z_coarsened . transpose ( ) temp = [ ] for z in Z_coarsened : temp . append ( coarsen_array ( z , xlevel , method ) ) Z_coarsened = _n . array ( temp ) . transpose ( ) return Z_coarsened if ylevel : Z_ycoarsened = [ ] for c in Z : Z_ycoarsened . append ( coarsen_array ( c , ylevel , method ) ) Z_ycoarsened = _n . array ( Z_ycoarsened ) if xlevel : return coarsen_array ( Z_ycoarsened , xlevel , method ) else : return _n . array ( Z_ycoarsened )
This returns a coarsened numpy matrix .
39,756
def erange ( start , end , steps ) : if start == 0 : print ( "Nothing you multiply zero by gives you anything but zero. Try picking something small." ) return None if end == 0 : print ( "It takes an infinite number of steps to get to zero. Try a small number?" ) return None x = ( 1.0 * end / start ) ** ( 1.0 / ( steps - 1 ) ) ns = _n . array ( list ( range ( 0 , steps ) ) ) a = start * _n . power ( x , ns ) a [ - 1 ] = end return a
Returns a numpy array over the specified range taking geometric steps .
39,757
def is_a_number ( s ) : if _s . fun . is_iterable ( s ) and not type ( s ) == str : return False try : float ( s ) return 1 except : try : complex ( s ) return 2 except : try : complex ( s . replace ( '(' , '' ) . replace ( ')' , '' ) . replace ( 'i' , 'j' ) ) return 2 except : return False
This takes an object and determines whether it s a number or a string representing a number .
39,758
def array_shift ( a , n , fill = "average" ) : new_a = _n . array ( a ) if n == 0 : return new_a fill_array = _n . array ( [ ] ) fill_array . resize ( _n . abs ( n ) ) if fill is "average" : fill_array = 0.0 * fill_array + _n . average ( a ) elif fill is "wrap" and n >= 0 : for i in range ( 0 , n ) : fill_array [ i ] = a [ i - n ] elif fill is "wrap" and n < 0 : for i in range ( 0 , - n ) : fill_array [ i ] = a [ i ] else : fill_array = 0.0 * fill_array + fill if n > 0 : for i in range ( n , len ( a ) ) : new_a [ i ] = a [ i - n ] for i in range ( 0 , n ) : new_a [ i ] = fill_array [ i ] else : for i in range ( 0 , len ( a ) + n ) : new_a [ i ] = a [ i - n ] for i in range ( 0 , - n ) : new_a [ - i - 1 ] = fill_array [ - i - 1 ] return new_a
This will return an array with all the elements shifted forward in index by n .
39,759
def assemble_covariance ( error , correlation ) : covariance = [ ] for n in range ( 0 , len ( error ) ) : covariance . append ( [ ] ) for m in range ( 0 , len ( error ) ) : covariance [ n ] . append ( correlation [ n ] [ m ] * error [ n ] * error [ m ] ) return _n . array ( covariance )
This takes an error vector and a correlation matrix and assembles the covariance
39,760
def combine_dictionaries ( a , b ) : c = { } for key in list ( b . keys ( ) ) : c [ key ] = b [ key ] for key in list ( a . keys ( ) ) : c [ key ] = a [ key ] return c
returns the combined dictionary . a s values preferentially chosen
39,761
def decompose_covariance ( c ) : c = _n . array ( c ) e = [ ] for n in range ( 0 , len ( c [ 0 ] ) ) : e . append ( _n . sqrt ( c [ n ] [ n ] ) ) for n in range ( 0 , len ( c [ 0 ] ) ) : for m in range ( 0 , len ( c [ 0 ] ) ) : c [ n ] [ m ] = c [ n ] [ m ] / ( e [ n ] * e [ m ] ) return [ _n . array ( e ) , _n . array ( c ) ]
This decomposes a covariance matrix into an error vector and a correlation matrix
39,762
def derivative_fit ( xdata , ydata , neighbors = 1 ) : x = [ ] dydx = [ ] nmax = len ( xdata ) - 1 for n in range ( nmax + 1 ) : i1 = max ( 0 , n - neighbors ) i2 = min ( nmax , n + neighbors ) xmini = _n . array ( xdata [ i1 : i2 + 1 ] ) ymini = _n . array ( ydata [ i1 : i2 + 1 ] ) slope , intercept = fit_linear ( xmini , ymini ) x . append ( float ( sum ( xmini ) ) / len ( xmini ) ) dydx . append ( slope ) return _n . array ( x ) , _n . array ( dydx )
loops over the data points performing a least - squares linear fit of the nearest neighbors at each point . Returns an array of x - values and slopes .
39,763
def dumbguy_minimize ( f , xmin , xmax , xstep ) : prev = f ( xmin ) this = f ( xmin + xstep ) for x in frange ( xmin + xstep , xmax , xstep ) : next = f ( x + xstep ) if this < prev and this < next : return x , this prev = this this = next return x , this
This just steps x and looks for a peak
39,764
def elements_are_numbers ( array ) : if len ( array ) == 0 : return 0 output_value = 1 for x in array : test = is_a_number ( x ) if not test : return False output_value = max ( output_value , test ) return output_value
Tests whether the elements of the supplied array are numbers .
39,765
def find_N_peaks ( array , N = 4 , max_iterations = 100 , rec_max_iterations = 3 , recursion = 1 ) : if recursion < 0 : return None ymin = min ( array ) ymax = max ( array ) for n in range ( max_iterations ) : y1 = ( ymin + ymax ) / 2.0 p , s , i = find_peaks ( array , y1 , True ) for n in range ( len ( i ) ) : p2 = find_N_peaks ( s [ n ] , 2 , rec_max_iterations , rec_max_iterations = rec_max_iterations , recursion = recursion - 1 ) if not p2 is None : for x in p2 : if not x in p : p . append ( x + i [ n ] ) if len ( p ) == N : return p if len ( p ) > N : ymin = y1 else : ymax = y1 return None
This will run the find_peaks algorythm adjusting the baseline until exactly N peaks are found .
39,766
def find_peaks ( array , baseline = 0.1 , return_subarrays = False ) : peaks = [ ] if return_subarrays : subarray_values = [ ] subarray_indices = [ ] n = 0 while n < len ( array ) : if array [ n ] > baseline : if return_subarrays : subarray_values . append ( [ ] ) subarray_indices . append ( n ) ymax = baseline nmax = n while n < len ( array ) and array [ n ] > baseline : if return_subarrays : subarray_values [ - 1 ] . append ( array [ n ] ) if array [ n ] > ymax : ymax = array [ n ] nmax = n n = n + 1 peaks . append ( nmax ) else : n = n + 1 if return_subarrays : return peaks , subarray_values , subarray_indices else : return peaks
This will try to identify the indices of the peaks in array returning a list of indices in ascending order .
39,767
def find_zero_bisect ( f , xmin , xmax , xprecision ) : if f ( xmax ) * f ( xmin ) > 0 : print ( "find_zero_bisect(): no zero on the range" , xmin , "to" , xmax ) return None temp = min ( xmin , xmax ) xmax = max ( xmin , xmax ) xmin = temp xmid = ( xmin + xmax ) * 0.5 while xmax - xmin > xprecision : y = f ( xmid ) if y > 0 : if f ( xmin ) < 0 : xmax = xmid else : xmin = xmid elif y < 0 : if f ( xmin ) > 0 : xmax = xmid else : xmin = xmid else : return xmid xmid = ( xmin + xmax ) * 0.5 return xmid
This will bisect the range and zero in on zero .
39,768
def frange ( start , end , inc = 1.0 ) : start = 1.0 * start end = 1.0 * end inc = 1.0 * inc if not inc : return _n . array ( [ start , end ] ) if 1.0 * ( end - start ) / inc < 0.0 : inc = - inc ns = _n . array ( list ( range ( 0 , int ( 1.0 * ( end - start ) / inc ) + 1 ) ) ) return start + ns * inc
A range function that accepts float increments and reversed direction .
39,769
def get_shell_history ( ) : if 'get_ipython' in globals ( ) : a = list ( get_ipython ( ) . history_manager . input_hist_raw ) a . reverse ( ) return a elif 'SPYDER_SHELL_ID' in _os . environ : try : p = _os . path . join ( _settings . path_user , ".spyder2" , "history.py" ) a = read_lines ( p ) a . reverse ( ) return a except : pass else : try : import wx for x in wx . GetTopLevelWindows ( ) : if type ( x ) in [ wx . py . shell . ShellFrame , wx . py . crust . CrustFrame ] : a = x . shell . GetText ( ) . split ( ">>>" ) a . reverse ( ) return a except : pass return [ 'shell history not available' ]
This only works with some shells .
39,770
def index ( value , array ) : i = array . searchsorted ( value ) if i == len ( array ) : return - 1 else : return i
Array search that behaves like I want it to . Totally dumb I know .
39,771
def index_next_crossing ( value , array , starting_index = 0 , direction = 1 ) : for n in range ( starting_index , len ( array ) - 1 ) : if ( value - array [ n ] ) * direction >= 0 and ( value - array [ n + 1 ] ) * direction < 0 : return n return - 1
starts at starting_index and walks through the array until it finds a crossing point with value
39,772
def insert_ordered ( value , array ) : index = 0 for n in range ( 0 , len ( array ) ) : if value >= array [ n ] : index = n + 1 array . insert ( index , value ) return index
This will insert the value into the array keeping it sorted and returning the index where it was inserted
39,773
def replace_in_files ( search , replace , depth = 0 , paths = None , confirm = True ) : if paths == None : paths = _s . dialogs . MultipleFiles ( 'DIS AND DAT|*.*' ) if paths == [ ] : return for path in paths : lines = read_lines ( path ) if depth : N = min ( len ( lines ) , depth ) else : N = len ( lines ) for n in range ( 0 , N ) : if lines [ n ] . find ( search ) >= 0 : lines [ n ] = lines [ n ] . replace ( search , replace ) print ( path . split ( _os . path . pathsep ) [ - 1 ] + ': "' + lines [ n ] + '"' ) if not confirm : _os . rename ( path , path + ".backup" ) write_to_file ( path , join ( lines , '' ) ) if confirm : if input ( "yes? " ) == "yes" : replace_in_files ( search , replace , depth , paths , False ) return
Does a line - by - line search and replace but only up to the depth line .
39,774
def replace_lines_in_files ( search_string , replacement_line ) : paths = _s . dialogs . MultipleFiles ( 'DIS AND DAT|*.*' ) if paths == [ ] : return for path in paths : _shutil . copy ( path , path + ".backup" ) lines = read_lines ( path ) for n in range ( 0 , len ( lines ) ) : if lines [ n ] . find ( search_string ) >= 0 : print ( lines [ n ] ) lines [ n ] = replacement_line . strip ( ) + "\n" write_to_file ( path , join ( lines , '' ) ) return
Finds lines containing the search string and replaces the whole line with the specified replacement string .
39,775
def reverse ( array ) : l = list ( array ) l . reverse ( ) return _n . array ( l )
returns a reversed numpy array
39,776
def submatrix ( matrix , i1 , i2 , j1 , j2 ) : new = [ ] for i in range ( i1 , i2 + 1 ) : new . append ( matrix [ i ] [ j1 : j2 + 1 ] ) return _n . array ( new )
returns the submatrix defined by the index bounds i1 - i2 and j1 - j2
39,777
def trim_data_uber ( arrays , conditions ) : if len ( conditions ) == 0 : return arrays if len ( arrays ) == 0 : return [ ] all_conditions = conditions [ 0 ] for n in range ( 1 , len ( conditions ) ) : all_conditions = all_conditions & conditions [ n ] ns = _n . argwhere ( all_conditions ) . transpose ( ) [ 0 ] output = [ ] for n in range ( len ( arrays ) ) : if not arrays [ n ] is None : output . append ( arrays [ n ] [ ns ] ) else : output . append ( None ) return output
Non - destructively selects data from the supplied list of arrays based on the supplied list of conditions . Importantly if any of the conditions are not met for the n th data point the n th data point is rejected for all supplied arrays .
39,778
def _fetch ( self , searchtype , fields , ** kwargs ) : fields [ 'vintage' ] = self . vintage fields [ 'benchmark' ] = self . benchmark fields [ 'format' ] = 'json' if 'layers' in kwargs : fields [ 'layers' ] = kwargs [ 'layers' ] returntype = kwargs . get ( 'returntype' , 'geographies' ) url = self . _geturl ( searchtype , returntype ) try : with requests . get ( url , params = fields , timeout = kwargs . get ( 'timeout' ) ) as r : content = r . json ( ) if "addressMatches" in content . get ( 'result' , { } ) : return AddressResult ( content ) if "geographies" in content . get ( 'result' , { } ) : return GeographyResult ( content ) raise ValueError ( ) except ( ValueError , KeyError ) : raise ValueError ( "Unable to parse response from Census" ) except RequestException as e : raise e
Fetch a response from the Geocoding API .
39,779
def address ( self , street , city = None , state = None , zipcode = None , ** kwargs ) : fields = { 'street' : street , 'city' : city , 'state' : state , 'zip' : zipcode , } return self . _fetch ( 'address' , fields , ** kwargs )
Geocode an address .
39,780
def set_name ( self , name = "My Colormap" ) : if not type ( name ) == str : print ( "set_name(): Name must be a string." ) return self . _name = name return self
Sets the name .
39,781
def set_image ( self , image = 'auto' ) : if image == "auto" : image = _pylab . gca ( ) . images [ 0 ] self . _image = image self . update_image ( )
Set which pylab image to tweak .
39,782
def update_image ( self ) : if self . _image : self . _image . set_cmap ( self . get_cmap ( ) ) _pylab . draw ( )
Set s the image s cmap .
39,783
def pop_colorpoint ( self , n = 0 ) : if len ( self . _colorpoint_list ) > 2 : x = self . _colorpoint_list . pop ( n ) self . _colorpoint_list [ 0 ] [ 0 ] = 0.0 self . _colorpoint_list [ - 1 ] [ 0 ] = 1.0 self . update_image ( ) return x else : return self [ n ]
Removes and returns the specified colorpoint . Will always leave two behind .
39,784
def insert_colorpoint ( self , position = 0.5 , color1 = [ 1.0 , 1.0 , 0.0 ] , color2 = [ 1.0 , 1.0 , 0.0 ] ) : L = self . _colorpoint_list if position <= 0.0 : L . insert ( 0 , [ 0.0 , color1 , color2 ] ) elif position >= 1.0 : L . append ( [ 1.0 , color1 , color2 ] ) else : for n in range ( len ( self . _colorpoint_list ) ) : if position <= L [ n + 1 ] [ 0 ] : L . insert ( n + 1 , [ position , color1 , color2 ] ) break self . update_image ( ) return self
Inserts the specified color into the list .
39,785
def modify_colorpoint ( self , n , position = 0.5 , color1 = [ 1.0 , 1.0 , 1.0 ] , color2 = [ 1.0 , 1.0 , 1.0 ] ) : if n == 0.0 : position = 0.0 elif n == len ( self . _colorpoint_list ) - 1 : position = 1.0 else : position = max ( self . _colorpoint_list [ n - 1 ] [ 0 ] , position ) self . _colorpoint_list [ n ] = [ position , color1 , color2 ] self . update_image ( ) self . save_colormap ( "Last Used" )
Changes the values of an existing colorpoint then updates the colormap .
39,786
def get_cmap ( self ) : r = [ ] g = [ ] b = [ ] for p in self . _colorpoint_list : r . append ( ( p [ 0 ] , p [ 1 ] [ 0 ] * 1.0 , p [ 2 ] [ 0 ] * 1.0 ) ) g . append ( ( p [ 0 ] , p [ 1 ] [ 1 ] * 1.0 , p [ 2 ] [ 1 ] * 1.0 ) ) b . append ( ( p [ 0 ] , p [ 1 ] [ 2 ] * 1.0 , p [ 2 ] [ 2 ] * 1.0 ) ) c = { 'red' : r , 'green' : g , 'blue' : b } return _mpl . colors . LinearSegmentedColormap ( 'custom' , c )
Generates a pylab cmap object from the colorpoint data .
39,787
def _signal_load ( self ) : self . set_name ( str ( self . _combobox_cmaps . currentText ( ) ) ) self . load_colormap ( ) self . _build_gui ( ) self . _button_save . setEnabled ( False )
Load the selected cmap .
39,788
def _color_dialog_changed ( self , n , top , c ) : self . _button_save . setEnabled ( True ) cp = self . _colorpoint_list [ n ] if self . _checkboxes [ n ] . isChecked ( ) : self . modify_colorpoint ( n , cp [ 0 ] , [ c . red ( ) / 255.0 , c . green ( ) / 255.0 , c . blue ( ) / 255.0 ] , [ c . red ( ) / 255.0 , c . green ( ) / 255.0 , c . blue ( ) / 255.0 ] ) self . _buttons_top_color [ n ] . setStyleSheet ( "background-color: rgb(" + str ( c . red ( ) ) + "," + str ( c . green ( ) ) + "," + str ( c . green ( ) ) + "); border-radius: 3px;" ) self . _buttons_bottom_color [ n ] . setStyleSheet ( "background-color: rgb(" + str ( c . red ( ) ) + "," + str ( c . green ( ) ) + "," + str ( c . green ( ) ) + "); border-radius: 3px;" ) elif top : self . modify_colorpoint ( n , cp [ 0 ] , cp [ 1 ] , [ c . red ( ) / 255.0 , c . green ( ) / 255.0 , c . blue ( ) / 255.0 ] ) self . _buttons_top_color [ n ] . setStyleSheet ( "background-color: rgb(" + str ( c . red ( ) ) + "," + str ( c . green ( ) ) + "," + str ( c . green ( ) ) + "); border-radius: 3px;" ) else : self . modify_colorpoint ( n , cp [ 0 ] , [ c . red ( ) / 255.0 , c . green ( ) / 255.0 , c . blue ( ) / 255.0 ] , cp [ 2 ] ) self . _buttons_bottom_color [ n ] . setStyleSheet ( "background-color: rgb(" + str ( c . red ( ) ) + "," + str ( c . green ( ) ) + "," + str ( c . green ( ) ) + "); border-radius: 3px;" )
Updates the color of the slider .
39,789
def _button_plus_clicked ( self , n ) : self . _button_save . setEnabled ( True ) self . insert_colorpoint ( self . _colorpoint_list [ n ] [ 0 ] , self . _colorpoint_list [ n ] [ 1 ] , self . _colorpoint_list [ n ] [ 2 ] ) self . _build_gui ( )
Create a new colorpoint .
39,790
def _button_minus_clicked ( self , n ) : self . _button_save . setEnabled ( True ) self . pop_colorpoint ( n ) self . _build_gui ( )
Remove a new colorpoint .
39,791
def _color_button_clicked ( self , n , top ) : self . _button_save . setEnabled ( True ) if top : self . _color_dialogs_top [ n ] . open ( ) else : self . _color_dialogs_bottom [ n ] . open ( )
Opens the dialog .
39,792
def _load_cmap_list ( self ) : name = self . get_name ( ) self . _combobox_cmaps . blockSignals ( True ) self . _combobox_cmaps . clear ( ) paths = _settings . ListDir ( 'colormaps' ) for path in paths : self . _combobox_cmaps . addItem ( _os . path . splitext ( path ) [ 0 ] ) self . _combobox_cmaps . setCurrentIndex ( self . _combobox_cmaps . findText ( name ) ) self . _combobox_cmaps . blockSignals ( False )
Searches the colormaps directory for all files populates the list .
39,793
def load ( filters = "*.*" , text = 'Select a file, FACEFACE!' , default_directory = 'default_directory' ) : if not '*' in filters . split ( ';' ) : filters = filters + ";;All files (*)" if default_directory in _settings . keys ( ) : default = _settings [ default_directory ] else : default = "" result = _qtw . QFileDialog . getOpenFileName ( None , text , default , filters ) if _s . _qt . VERSION_INFO [ 0 : 5 ] == "PyQt5" : result = result [ 0 ] result = str ( result ) if result == '' : return None else : _settings [ default_directory ] = _os . path . split ( result ) [ 0 ] return result
Pops up a dialog for opening a single file . Returns a string path or None .
39,794
def load_multiple ( filters = "*.*" , text = 'Select some files, FACEFACE!' , default_directory = 'default_directory' ) : if not '*' in filters . split ( ';' ) : filters = filters + ";;All files (*)" if default_directory in _settings . keys ( ) : default = _settings [ default_directory ] else : default = "" results = _qtw . QFileDialog . getOpenFileNames ( None , text , default , filters ) if _s . _qt . VERSION_INFO [ 0 : 5 ] == "PyQt5" : results = results [ 0 ] result = [ ] for r in results : result . append ( str ( r ) ) if len ( result ) == 0 : return else : _settings [ default_directory ] = _os . path . split ( result [ 0 ] ) [ 0 ] return result
Pops up a dialog for opening more than one file . Returns a list of string paths or None .
39,795
def Dump ( self ) : prefs_file = open ( self . prefs_path , 'w' ) for n in range ( 0 , len ( self . prefs ) ) : if len ( list ( self . prefs . items ( ) ) [ n ] ) > 1 : prefs_file . write ( str ( list ( self . prefs . items ( ) ) [ n ] [ 0 ] ) + ' = ' + str ( list ( self . prefs . items ( ) ) [ n ] [ 1 ] ) + '\n' ) prefs_file . close ( )
Dumps the current prefs to the preferences . txt file
39,796
def link ( self ) : type_specs = { } types = [ ] for name , type_spec in self . scope . type_specs . items ( ) : type_spec = type_spec . link ( self . scope ) type_specs [ name ] = type_spec if type_spec . surface is not None : self . scope . add_surface ( name , type_spec . surface ) types . append ( type_spec . surface ) self . scope . type_specs = type_specs self . scope . add_surface ( '__types__' , tuple ( types ) )
Resolve and link all types in the scope .
39,797
def install ( path , name = None ) : if name is None : name = os . path . splitext ( os . path . basename ( path ) ) [ 0 ] callermod = inspect . getmodule ( inspect . stack ( ) [ 1 ] [ 0 ] ) name = '%s.%s' % ( callermod . __name__ , name ) if name in sys . modules : return sys . modules [ name ] if not os . path . isabs ( path ) : callerfile = callermod . __file__ path = os . path . normpath ( os . path . join ( os . path . dirname ( callerfile ) , path ) ) sys . modules [ name ] = mod = load ( path , name = name ) return mod
Compiles a Thrift file and installs it as a submodule of the caller .
39,798
def loads ( self , name , document ) : return self . compiler . compile ( name , document ) . link ( ) . surface
Parse and compile the given Thrift document .
39,799
def load ( self , path , name = None ) : if name is None : name = os . path . splitext ( os . path . basename ( path ) ) [ 0 ] with open ( path , 'r' ) as f : document = f . read ( ) return self . compiler . compile ( name , document , path ) . link ( ) . surface
Load and compile the given Thrift file .