idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
22,800
def get_translation_cache_key ( translated_model , master_id , language_code ) : return 'parler.{0}.{1}.{2}.{3}' . format ( translated_model . _meta . app_label , translated_model . __name__ , master_id , language_code )
The low - level function to get the cache key for a translation .
22,801
def get_cached_translation ( instance , language_code = None , related_name = None , use_fallback = False ) : if language_code is None : language_code = instance . get_current_language ( ) translated_model = instance . _parler_meta . get_model_by_related_name ( related_name ) values = _get_cached_values ( instance , tr...
Fetch an cached translation .
22,802
def _cache_translation ( translation , timeout = cache . default_timeout ) : if not appsettings . PARLER_ENABLE_CACHING : return if translation . master_id is None : raise ValueError ( "Can't cache unsaved translation" ) fields = translation . get_translated_fields ( ) values = { 'id' : translation . id } for name in f...
Store a new translation in the cache .
22,803
def _cache_translation_needs_fallback ( instance , language_code , related_name , timeout = cache . default_timeout ) : if not appsettings . PARLER_ENABLE_CACHING or not instance . pk or instance . _state . adding : return tr_model = instance . _parler_meta . get_model_by_related_name ( related_name ) key = get_transla...
Store the fact that a translation doesn t exist and the fallback should be used .
22,804
def select_template_name ( template_name_list , using = None ) : if not isinstance ( template_name_list , tuple ) : template_name_list = tuple ( template_name_list ) try : return _cached_name_lookups [ template_name_list ] except KeyError : for template_name in template_name_list : try : get_template ( template_name , ...
Given a list of template names find the first one that exists .
22,805
def get_view_url ( self ) : if not self . view_url_name : raise ImproperlyConfigured ( "Missing `view_url_name` attribute on {0}" . format ( self . __class__ . __name__ ) ) return reverse ( self . view_url_name , args = self . args , kwargs = self . kwargs )
This method is used by the get_translated_url template tag .
22,806
def get_object ( self , queryset = None ) : if queryset is None : queryset = self . get_queryset ( ) slug = self . kwargs [ self . slug_url_kwarg ] choices = self . get_language_choices ( ) obj = None using_fallback = False prev_choices = [ ] for lang_choice in choices : try : filters = self . get_translated_filters ( ...
Fetch the object using a translated slug .
22,807
def get_object ( self , queryset = None ) : object = super ( LanguageChoiceMixin , self ) . get_object ( queryset ) if isinstance ( object , TranslatableModelMixin ) : object . set_current_language ( self . get_language ( ) , initialize = True ) return object
Assign the language for the retrieved object .
22,808
def get_form_class ( self ) : super_method = super ( TranslatableModelFormMixin , self ) . get_form_class default_method = getattr ( ModelFormMixin . get_form_class , '__func__' , ModelFormMixin . get_form_class ) if not ( super_method . __func__ is default_method ) : return super_method ( ) else : if self . form_class...
Return a TranslatableModelForm by default if no form_class is set .
22,809
def get_parler_languages_from_django_cms ( cms_languages = None ) : valid_keys = [ 'code' , 'fallbacks' , 'hide_untranslated' , 'redirect_on_fallback' ] if cms_languages : if sys . version_info < ( 3 , 0 , 0 ) : int_types = ( int , long ) else : int_types = int parler_languages = copy . deepcopy ( cms_languages ) for s...
Converts django CMS setting CMS_LANGUAGES into PARLER_LANGUAGES . Since CMS_LANGUAGES is a strict superset of PARLER_LANGUAGES we do a bit of cleansing to remove irrelevant items .
22,810
def get_first_language ( self , site_id = None ) : if site_id is None : site_id = getattr ( settings , 'SITE_ID' , None ) try : return self [ site_id ] [ 0 ] [ 'code' ] except ( KeyError , IndexError ) : return self [ 'default' ] [ 'code' ]
Return the first language for the current site . This can be used for user interfaces where the languages are displayed in tabs .
22,811
def _get_model_form_field ( model , name , formfield_callback = None , ** kwargs ) : field = model . _meta . get_field ( name ) if not field . editable : return None if formfield_callback is None : formfield = field . formfield ( ** kwargs ) elif not callable ( formfield_callback ) : raise TypeError ( 'formfield_callba...
Utility to create the formfield from a model field . When a field is not editable a None will be returned .
22,812
def save_translated_fields ( self ) : fields = { } for field in self . _translated_fields : try : value = self . cleaned_data [ field ] except KeyError : continue fields [ field ] = value translations = self . instance . _set_translated_fields ( ** fields ) non_translated_fields = set ( ( 'id' , 'master_id' , 'language...
Save all translated fields .
22,813
def create_translations_model ( shared_model , related_name , meta , ** fields ) : if not meta : meta = { } if shared_model . _meta . abstract : raise TypeError ( "Can't create TranslatedFieldsModel for abstract class {0}" . format ( shared_model . __name__ ) ) meta [ 'app_label' ] = shared_model . _meta . app_label me...
Dynamically create the translations model . Create the translations model for the shared model model .
22,814
def _set_translated_fields ( self , language_code = None , ** fields ) : objects = [ ] for parler_meta , model_fields in self . _parler_meta . _split_fields ( ** fields ) : translation = self . _get_translated_model ( language_code = language_code , auto_create = True , meta = parler_meta ) for field , value in six . i...
Assign fields to the translated models .
22,815
def create_translation ( self , language_code , ** fields ) : if language_code is None : raise ValueError ( get_null_language_error ( ) ) meta = self . _parler_meta if self . _translations_cache [ meta . root_model ] . get ( language_code , None ) : raise ValueError ( "Translation already exists: {0}" . format ( langua...
Add a translation to the model .
22,816
def delete_translation ( self , language_code , related_name = None ) : if language_code is None : raise ValueError ( get_null_language_error ( ) ) if related_name is None : metas = self . _parler_meta else : metas = [ self . _parler_meta [ related_name ] ] num_deleted = 0 for meta in metas : try : translation = self ....
Delete a translation from a model .
22,817
def set_current_language ( self , language_code , initialize = False ) : self . _current_language = normalize_language_code ( language_code or get_language ( ) ) if initialize : self . _get_translated_model ( use_fallback = False , auto_create = True )
Switch the currently activate language of the object .
22,818
def get_fallback_languages ( self ) : lang_dict = get_language_settings ( self . _current_language ) fallbacks = [ lang for lang in lang_dict [ 'fallbacks' ] if lang != self . _current_language ] return fallbacks or [ ]
Return the fallback language codes which are used in case there is no translation for the currently active language .
22,819
def has_translation ( self , language_code = None , related_name = None ) : if language_code is None : language_code = self . _current_language if language_code is None : raise ValueError ( get_null_language_error ( ) ) meta = self . _parler_meta . _get_extension_by_related_name ( related_name ) try : return not is_mis...
Return whether a translation for the given language exists . Defaults to the current language code .
22,820
def get_available_languages ( self , related_name = None , include_unsaved = False ) : meta = self . _parler_meta . _get_extension_by_related_name ( related_name ) prefetch = self . _get_prefetched_translations ( meta = meta ) if prefetch is not None : db_languages = sorted ( obj . language_code for obj in prefetch ) e...
Return the language codes of all translated variations .
22,821
def get_translation ( self , language_code , related_name = None ) : meta = self . _parler_meta . _get_extension_by_related_name ( related_name ) return self . _get_translated_model ( language_code , meta = meta )
Fetch the translated model
22,822
def _get_translated_model ( self , language_code = None , use_fallback = False , auto_create = False , meta = None ) : if self . _parler_meta is None : raise ImproperlyConfigured ( "No translation is assigned to the current model!" ) if self . _translations_cache is None : raise RuntimeError ( "Accessing translated fie...
Fetch the translated fields model .
22,823
def _get_any_translated_model ( self , meta = None ) : if meta is None : meta = self . _parler_meta . root tr_model = meta . model local_cache = self . _translations_cache [ tr_model ] if local_cache : check_languages = [ self . _current_language ] + self . get_fallback_languages ( ) try : for fallback_lang in check_la...
Return any available translation . Returns None if there are no translations at all .
22,824
def _get_translated_queryset ( self , meta = None ) : if meta is None : meta = self . _parler_meta . root accessor = getattr ( self , meta . rel_name ) return accessor . get_queryset ( )
Return the queryset that points to the translated model . If there is a prefetch it can be read from this queryset .
22,825
def _get_prefetched_translations ( self , meta = None ) : if meta is None : meta = self . _parler_meta . root related_name = meta . rel_name try : return self . _prefetched_objects_cache [ related_name ] except ( AttributeError , KeyError ) : return None
Return the queryset with prefetch results .
22,826
def validate_unique ( self , exclude = None ) : errors = { } try : super ( TranslatableModelMixin , self ) . validate_unique ( exclude = exclude ) except ValidationError as e : errors = e . message_dict for local_cache in six . itervalues ( self . _translations_cache ) : for translation in six . itervalues ( local_cach...
Also validate the unique_together of the translated model .
22,827
def save_translation ( self , translation , * args , ** kwargs ) : if self . pk is None or self . _state . adding : raise RuntimeError ( "Can't save translations when the master object is not yet saved." ) if translation . pk is None or translation . is_modified : if not translation . master_id : translation . _state ....
Save the translation when it s modified or unsaved .
22,828
def safe_translation_getter ( self , field , default = None , language_code = None , any_language = False ) : meta = self . _parler_meta . _get_extension_by_field ( field ) if language_code and language_code != self . _current_language : try : tr_model = self . _get_translated_model ( language_code , meta = meta , use_...
Fetch a translated property and return a default value when both the translation and fallback language are missing .
22,829
def _get_extension_by_field ( self , name ) : if name is None : raise TypeError ( "Expected field name" ) tr_model = self . get_model_by_field ( name ) for meta in self . _extensions : if meta . model == tr_model : return meta
Find the ParlerOptions object that corresponds with the given translated field .
22,830
def _validate_master ( new_class ) : if not new_class . master or not isinstance ( new_class . master , ForwardManyToOneDescriptor ) : raise ImproperlyConfigured ( "{0}.master should be a ForeignKey to the shared table." . format ( new_class . __name__ ) ) remote_field = new_class . master . field . remote_field shared...
Check whether the master field on a TranslatedFieldsModel is correctly configured .
22,831
def short_description ( self ) : translations_model = self . field . meta . model if translations_model is None : return pretty_name ( self . field . name ) field = translations_model . _meta . get_field ( self . field . name ) return field . verbose_name
Ensure that the admin list_display renders the correct verbose name for translated fields .
22,832
def get_queryset ( self , request ) : qs = super ( BaseTranslatableAdmin , self ) . get_queryset ( request ) if self . _has_translatable_model ( ) : if not isinstance ( qs , TranslatableQuerySet ) : raise ImproperlyConfigured ( "{0} class does not inherit from TranslatableQuerySet" . format ( qs . __class__ . __name__ ...
Make sure the current language is selected .
22,833
def all_languages_column ( self , object ) : all_languages = [ code for code , __ in settings . LANGUAGES ] return mark_safe ( self . _languages_column ( object , all_languages , span_classes = 'all-languages' ) )
The language column which can be included in the list_display . It also shows untranslated languages
22,834
def get_available_languages ( self , obj ) : if obj : return obj . get_available_languages ( ) else : return self . model . _parler_meta . root_model . objects . none ( )
Fetching the available languages as queryset .
22,835
def get_object ( self , request , object_id , * args , ** kwargs ) : obj = super ( TranslatableAdmin , self ) . get_object ( request , object_id , * args , ** kwargs ) if obj is not None and self . _has_translatable_model ( ) : obj . set_current_language ( self . _language ( request , obj ) , initialize = True ) return...
Make sure the object is fetched in the correct language .
22,836
def get_urls ( self ) : urlpatterns = super ( TranslatableAdmin , self ) . get_urls ( ) if not self . _has_translatable_model ( ) : return urlpatterns else : opts = self . model . _meta info = opts . app_label , opts . model_name return [ url ( r'^(.+)/change/delete-translation/(.+)/$' , self . admin_site . admin_view ...
Add a delete - translation view .
22,837
def render_change_form ( self , request , context , add = False , change = False , form_url = '' , obj = None ) : if self . _has_translatable_model ( ) : lang_code = self . get_form_language ( request , obj ) lang = get_language_title ( lang_code ) available_languages = self . get_available_languages ( obj ) language_t...
Insert the language tabs .
22,838
def deletion_not_allowed ( self , request , obj , language_code ) : opts = self . model . _meta context = { 'object' : obj . master , 'language_code' : language_code , 'opts' : opts , 'app_label' : opts . app_label , 'language_name' : get_language_title ( language_code ) , 'object_name' : force_text ( opts . verbose_na...
Deletion - not - allowed view .
22,839
def get_translation_objects ( self , request , language_code , obj = None , inlines = True ) : if obj is not None : for translations_model in obj . _parler_meta . get_all_models ( ) : try : translation = translations_model . objects . get ( master = obj , language_code = language_code ) except translations_model . Does...
Return all objects that should be deleted when a translation is deleted . This method can yield all QuerySet objects or lists for the objects .
22,840
def _get_inline_translations ( self , request , language_code , obj = None ) : inline_instances = self . get_inline_instances ( request , obj = obj ) for inline in inline_instances : if issubclass ( inline . model , TranslatableModelMixin ) : fk = inline . get_formset ( request , obj ) . fk rel_name = 'master__{0}' . f...
Fetch the inline translations
22,841
def default_change_form_template ( self ) : opts = self . model . _meta app_label = opts . app_label return select_template_name ( ( "admin/{0}/{1}/change_form.html" . format ( app_label , opts . object_name . lower ( ) ) , "admin/{0}/change_form.html" . format ( app_label ) , "admin/change_form.html" ) )
Determine what the actual change_form_template should be .
22,842
def get_formset ( self , request , obj = None , ** kwargs ) : FormSet = super ( TranslatableInlineModelAdmin , self ) . get_formset ( request , obj , ** kwargs ) FormSet . language_code = self . get_form_language ( request , obj ) if self . inline_tabs : available_languages = self . get_available_languages ( obj , Form...
Return the formset and provide the language information to the formset .
22,843
def get_available_languages ( self , obj , formset ) : if obj : filter = { 'master__{0}' . format ( formset . fk . name ) : obj } return self . model . _parler_meta . root_model . objects . using ( obj . _state . db ) . filter ( ** filter ) . values_list ( 'language_code' , flat = True ) . distinct ( ) . order_by ( 'la...
Fetching the available inline languages as queryset .
22,844
def language ( self , language_code = None ) : if language_code is None : language_code = appsettings . PARLER_LANGUAGES . get_default_language ( ) self . _language = language_code return self
Set the language code to assign to objects retrieved using this QuerySet .
22,845
def translated ( self , * language_codes , ** translated_fields ) : relname = self . model . _parler_meta . root_rel_name if not language_codes : language_codes = ( get_language ( ) , ) filters = { } for field_name , val in six . iteritems ( translated_fields ) : if field_name . startswith ( 'master__' ) : filters [ fi...
Only return translated objects which of the given languages .
22,846
def active_translations ( self , language_code = None , ** translated_fields ) : language_codes = get_active_language_choices ( language_code ) return self . translated ( * language_codes , ** translated_fields )
Only return objects which are translated or have a fallback that should be displayed .
22,847
def get_language_title ( language_code ) : from parler import appsettings if not language_code : raise ValueError ( "Missing language_code in get_language_title()" ) if appsettings . PARLER_SHOW_EXCLUDED_LANGUAGE_TABS : languages = ALL_LANGUAGES_DICT else : languages = LANGUAGES_DICT try : return _ ( languages [ langua...
Return the verbose_name for a language code .
22,848
def is_multilingual_project ( site_id = None ) : from parler import appsettings if site_id is None : site_id = getattr ( settings , 'SITE_ID' , None ) return appsettings . PARLER_SHOW_EXCLUDED_LANGUAGE_TABS or site_id in appsettings . PARLER_LANGUAGES
Whether the current Django project is configured for multilingual support .
22,849
def __timestamp ( ) : today = time . time ( ) ret = struct . pack ( b'=L' , int ( today ) ) return ret
Generate timestamp data for pyc header .
22,850
def _get_scripts_resource ( pe ) : res = None for entry in pe . DIRECTORY_ENTRY_RESOURCE . entries : if entry . name and entry . name . string == b"PYTHONSCRIPT" : res = entry . directory . entries [ 0 ] . directory . entries [ 0 ] break return res
Return the PYTHONSCRIPT resource entry .
22,851
def _resource_dump ( pe , res ) : rva = res . data . struct . OffsetToData size = res . data . struct . Size dump = pe . get_data ( rva , size ) return dump
Return the dump of the given resource .
22,852
def _get_co_from_dump ( data ) : current = struct . calcsize ( b'iiii' ) metadata = struct . unpack ( b'iiii' , data [ : current ] ) logging . info ( "Magic value: %x" , metadata [ 0 ] ) logging . info ( "Code bytes length: %d" , metadata [ 3 ] ) arcname = '' while six . indexbytes ( data , current ) != 0 : arcname += ...
Return the code objects from the dump .
22,853
def check_py2exe_file ( pe ) : py2exe_resource = _get_scripts_resource ( pe ) if py2exe_resource is None : logging . info ( 'This is not a py2exe executable.' ) if pe . __data__ . find ( b'pyi-windows-manifest-filename' ) : logging . info ( 'This seems a pyinstaller executable (unsupported).' ) return bool ( py2exe_res...
Check file is a py2exe executable .
22,854
def extract_code_objects ( pe ) : script_res = _get_scripts_resource ( pe ) dump = _resource_dump ( pe , script_res ) return _get_co_from_dump ( dump )
Extract Python code objects from a py2exe executable .
22,855
def dump_to_pyc ( co , python_version , output_dir ) : pyc_basename = ntpath . basename ( co . co_filename ) pyc_name = pyc_basename + '.pyc' if pyc_name not in IGNORE : logging . info ( "Extracting %s" , pyc_name ) pyc_header = _generate_pyc_header ( python_version , len ( co . co_code ) ) destination = os . path . jo...
Save given code_object as a . pyc file .
22,856
def unpy2exe ( filename , python_version = None , output_dir = None ) : if output_dir is None : output_dir = '.' elif not os . path . exists ( output_dir ) : os . makedirs ( output_dir ) pe = pefile . PE ( filename ) is_py2exe = check_py2exe_file ( pe ) if not is_py2exe : raise ValueError ( 'Not a py2exe executable.' )...
Process input params and produce output pyc files .
22,857
def are_all_nodes_discovered ( self ) : undiscovered = self . find_all ( lambda e : not e . discovered ) return len ( list ( undiscovered ) ) == 0
Reports whether there are nodes whose node info is still unknown .
22,858
def _path ( self , attrpath ) : attr , _ , subpath = attrpath . partition ( "." ) if attr not in self . __dict__ : self . __dict__ [ attr ] = Namespace ( ) self . __namespaces . add ( attr ) if subpath : return self . __dict__ [ attr ] . _path ( subpath ) else : return self . __dict__ [ attr ]
Returns the namespace object at the given . - separated path creating any namespaces in the path that don t already exist .
22,859
def add ( self , data_bytes ) : try : if isinstance ( data_bytes , basestring ) : data_bytes = map ( ord , data_bytes ) except NameError : if isinstance ( data_bytes , str ) : data_bytes = map ( ord , data_bytes ) for b in data_bytes : self . _crc ^= ( b << 56 ) & Signature . MASK64 for _ in range ( 8 ) : if self . _cr...
Feed ASCII string or bytes to the signature function
22,860
def get_max_bitlen ( self ) : payload_max_bitlen = self . max_size * self . value_type . get_max_bitlen ( ) return { self . MODE_DYNAMIC : payload_max_bitlen + self . max_size . bit_length ( ) , self . MODE_STATIC : payload_max_bitlen } [ self . mode ]
Returns total maximum bit length of the array including length field if applicable .
22,861
def get_dsdl_signature_source_definition ( self ) : txt = StringIO ( ) txt . write ( self . full_name + '\n' ) def adjoin ( attrs ) : return txt . write ( '\n' . join ( x . get_normalized_definition ( ) for x in attrs ) + '\n' ) if self . kind == CompoundType . KIND_SERVICE : if self . request_union : txt . write ( '\n...
Returns normalized DSDL definition text . Please refer to the specification for details about normalized DSDL definitions .
22,862
def get_data_type_signature ( self ) : if self . _data_type_signature is None : sig = Signature ( self . get_dsdl_signature ( ) ) fields = self . request_fields + self . response_fields if self . kind == CompoundType . KIND_SERVICE else self . fields for field in fields : field_sig = field . type . get_data_type_signat...
Computes data type signature of this type . The data type signature is guaranteed to match only if all nested data structures are compatible . Please refer to the specification for details about signatures .
22,863
def close ( self ) : self . _handle . remove ( ) self . _node_monitor_event_handle . remove ( ) self . _allocation_table . close ( )
Stops the instance and closes the allocation table storage .
22,864
def pretty_filename ( filename ) : try : a = os . path . abspath ( filename ) r = os . path . relpath ( filename ) except ValueError : a = r = filename return a if '..' in r else r
Returns a nice human readable path to filename .
22,865
def write_event ( self , event ) : if not isinstance ( event , event_pb2 . Event ) : raise TypeError ( "expected an event_pb2.Event proto, " " but got %s" % type ( event ) ) return self . _write_serialized_event ( event . SerializeToString ( ) )
Appends event to the file .
22,866
def flush ( self ) : if self . _num_outstanding_events == 0 or self . _recordio_writer is None : return self . _recordio_writer . flush ( ) if self . _logger is not None : self . _logger . info ( 'wrote %d %s to disk' , self . _num_outstanding_events , 'event' if self . _num_outstanding_events == 1 else 'events' ) self...
Flushes the event file to disk .
22,867
def close ( self ) : self . flush ( ) if self . _recordio_writer is not None : self . _recordio_writer . close ( ) self . _recordio_writer = None
Flushes the pending events and closes the writer after it is done .
22,868
def close ( self ) : if not self . _closed : self . add_event ( self . _sentinel_event ) self . flush ( ) self . _worker . join ( ) self . _ev_writer . close ( ) self . _closed = True
Flushes the event file to disk and close the file . Call this method when you do not need the summary writer anymore .
22,869
def write_record ( self , event_str ) : header = struct . pack ( 'Q' , len ( event_str ) ) header += struct . pack ( 'I' , masked_crc32c ( header ) ) footer = struct . pack ( 'I' , masked_crc32c ( event_str ) ) self . _writer . write ( header + event_str + footer )
Writes a serialized event to file .
22,870
def close ( self ) : if self . _writer is not None : self . flush ( ) self . _writer . close ( ) self . _writer = None
Closes the record writer .
22,871
def add_summary ( self , summary , global_step = None ) : if isinstance ( summary , bytes ) : summ = summary_pb2 . Summary ( ) summ . ParseFromString ( summary ) summary = summ for value in summary . value : if not value . metadata : continue if value . tag in self . _seen_summary_tags : value . ClearField ( "metadata"...
Adds a Summary protocol buffer to the event file . This method wraps the provided summary in an Event protocol buffer and adds it to the event file .
22,872
def add_graph ( self , graph ) : event = event_pb2 . Event ( graph_def = graph . SerializeToString ( ) ) self . _add_event ( event , None )
Adds a Graph protocol buffer to the event file .
22,873
def add_scalar ( self , tag , value , global_step = None ) : if isinstance ( value , ( tuple , list , dict ) ) : if isinstance ( value , ( tuple , list ) ) : if len ( value ) != 2 : raise ValueError ( 'expected two elements in value, while received %d' % len ( value ) ) value = { value [ 0 ] : value [ 1 ] } self . _add...
Adds scalar data to the event file .
22,874
def _add_scalars ( self , tag , scalar_dict , global_step = None ) : timestamp = time . time ( ) fw_logdir = self . _file_writer . get_logdir ( ) for scalar_name , scalar_value in scalar_dict . items ( ) : fw_tag = fw_logdir + '/' + tag + '/' + scalar_name if fw_tag in self . _all_writers . keys ( ) : fw = self . _all_...
Adds multiple scalars to summary . This enables drawing multiple curves in one plot .
22,875
def add_histogram ( self , tag , values , global_step = None , bins = 'default' ) : if bins == 'default' : bins = self . _get_default_bins ( ) self . _file_writer . add_summary ( histogram_summary ( tag , values , bins ) , global_step )
Add histogram data to the event file .
22,876
def add_image ( self , tag , image , global_step = None ) : self . _file_writer . add_summary ( image_summary ( tag , image ) , global_step )
Add image data to the event file . This function supports input as a 2D 3D or 4D image . If the input image is 2D a channel axis is prepended as the first dimension and image will be replicated three times and concatenated along the channel axis . If the input image is 3D it will be replicated three times and concatena...
22,877
def add_audio ( self , tag , audio , sample_rate = 44100 , global_step = None ) : self . _file_writer . add_summary ( audio_summary ( tag , audio , sample_rate = sample_rate ) , global_step )
Add audio data to the event file .
22,878
def add_text ( self , tag , text , global_step = None ) : self . _file_writer . add_summary ( text_summary ( tag , text ) , global_step ) if tag not in self . _text_tags : self . _text_tags . append ( tag ) extension_dir = self . get_logdir ( ) + '/plugins/tensorboard_text/' if not os . path . exists ( extension_dir ) ...
Add text data to the event file .
22,879
def add_pr_curve ( self , tag , labels , predictions , num_thresholds , global_step = None , weights = None ) : if num_thresholds < 2 : raise ValueError ( 'num_thresholds must be >= 2' ) labels = _make_numpy_array ( labels ) predictions = _make_numpy_array ( predictions ) self . _file_writer . add_summary ( pr_curve_su...
Adds precision - recall curve .
22,880
def _rectangular ( n ) : for i in n : if len ( i ) != len ( n [ 0 ] ) : return False return True
Checks to see if a 2D list is a valid 2D matrix
22,881
def _is_2D_matrix ( matrix ) : return ( ( isinstance ( matrix [ 0 ] , list ) and _rectangular ( matrix ) and not isinstance ( matrix [ 0 ] [ 0 ] , list ) ) or ( not isinstance ( matrix , list ) and matrix . shape == 2 ) )
Checks to see if a ndarray is 2D or a list of lists is 2D
22,882
def _save_image ( image , filename , nrow = 8 , padding = 2 , square_image = True ) : if not isinstance ( image , NDArray ) : raise TypeError ( 'MXNet NDArray expected, received {}' . format ( str ( type ( image ) ) ) ) image = _prepare_image ( image , nrow = nrow , padding = padding , square_image = square_image ) if ...
Saves a given Tensor into an image file . If the input tensor contains multiple images a grid of images will be saved .
22,883
def _save_embedding_tsv ( data , file_path ) : if isinstance ( data , np . ndarray ) : data_list = data . tolist ( ) elif isinstance ( data , NDArray ) : data_list = data . asnumpy ( ) . tolist ( ) else : raise TypeError ( 'expected NDArray of np.ndarray, while received type {}' . format ( str ( type ( data ) ) ) ) wit...
Given a 2D NDarray or a numpy . ndarray as embeding save it in tensors . tsv under the path provided by the user .
22,884
def _make_image ( tensor ) : assert isinstance ( tensor , NDArray ) if Image is None : raise ImportError ( 'need to install PIL for visualizing images' ) height , width , channel = tensor . shape tensor = _make_numpy_array ( tensor ) image = Image . fromarray ( tensor ) output = io . BytesIO ( ) image . save ( output ,...
Converts an NDArray type image to Image protobuf
22,885
def pr_curve_summary ( tag , labels , predictions , num_thresholds , weights = None ) : if num_thresholds > 127 : logging . warning ( 'num_thresholds>127 would result in failure of creating pr_curve protobuf,' ' clipping it at 127' ) num_thresholds = 127 labels = _make_numpy_array ( labels ) predictions = _make_numpy_a...
Outputs a precision - recall curve Summary protocol buffer .
22,886
def _get_nodes_from_symbol ( sym ) : if not isinstance ( sym , Symbol ) : raise TypeError ( 'sym must be an `mxnet.symbol.Symbol`,' ' received type {}' . format ( str ( type ( sym ) ) ) ) conf = json . loads ( sym . tojson ( ) ) nodes = conf [ 'nodes' ] data2op = { } for i , node in enumerate ( nodes ) : if node [ 'op'...
Given a symbol and shapes return a list of NodeDef s for visualizing the the graph in TensorBoard .
22,887
def temporal_derivatives ( order , variables , data ) : variables_deriv = OrderedDict ( ) data_deriv = OrderedDict ( ) if 0 in order : data_deriv [ 0 ] = data [ variables ] variables_deriv [ 0 ] = variables order = set ( order ) - set ( [ 0 ] ) for o in order : variables_deriv [ o ] = [ '{}_derivative{}' . format ( v ,...
Compute temporal derivative terms by the method of backwards differences .
22,888
def exponential_terms ( order , variables , data ) : variables_exp = OrderedDict ( ) data_exp = OrderedDict ( ) if 1 in order : data_exp [ 1 ] = data [ variables ] variables_exp [ 1 ] = variables order = set ( order ) - set ( [ 1 ] ) for o in order : variables_exp [ o ] = [ '{}_power{}' . format ( v , o ) for v in vari...
Compute exponential expansions .
22,889
def _order_as_range ( order ) : order = order . split ( '-' ) order = [ int ( o ) for o in order ] if len ( order ) > 1 : order = range ( order [ 0 ] , ( order [ - 1 ] + 1 ) ) return order
Convert a hyphenated string representing order for derivative or exponential terms into a range object that can be passed as input to the appropriate expansion function .
22,890
def _check_and_expand_exponential ( expr , variables , data ) : if re . search ( r'\^\^[0-9]+$' , expr ) : order = re . compile ( r'\^\^([0-9]+)$' ) . findall ( expr ) order = range ( 1 , int ( * order ) + 1 ) variables , data = exponential_terms ( order , variables , data ) elif re . search ( r'\^[0-9]+[\-]?[0-9]*$' ,...
Check if the current operation specifies exponential expansion . ^^6 specifies all powers up to the 6th ^5 - 6 the 5th and 6th powers ^6 the 6th only .
22,891
def _check_and_expand_derivative ( expr , variables , data ) : if re . search ( r'^dd[0-9]+' , expr ) : order = re . compile ( r'^dd([0-9]+)' ) . findall ( expr ) order = range ( 0 , int ( * order ) + 1 ) ( variables , data ) = temporal_derivatives ( order , variables , data ) elif re . search ( r'^d[0-9]+[\-]?[0-9]*' ...
Check if the current operation specifies a temporal derivative . dd6x specifies all derivatives up to the 6th d5 - 6x the 5th and 6th d6x the 6th only .
22,892
def _check_and_expand_subformula ( expression , parent_data , variables , data ) : grouping_depth = 0 for i , char in enumerate ( expression ) : if char == '(' : if grouping_depth == 0 : formula_delimiter = i + 1 grouping_depth += 1 elif char == ')' : grouping_depth -= 1 if grouping_depth == 0 : expr = expression [ for...
Check if the current operation contains a suboperation and parse it where appropriate .
22,893
def parse_expression ( expression , parent_data ) : variables = None data = None variables , data = _check_and_expand_subformula ( expression , parent_data , variables , data ) variables , data = _check_and_expand_exponential ( expression , variables , data ) variables , data = _check_and_expand_derivative ( expression...
Parse an expression in a model formula .
22,894
def _expand_shorthand ( model_formula , variables ) : wm = 'white_matter' gsr = 'global_signal' rps = 'trans_x + trans_y + trans_z + rot_x + rot_y + rot_z' fd = 'framewise_displacement' acc = _get_matches_from_data ( 'a_comp_cor_[0-9]+' , variables ) tcc = _get_matches_from_data ( 't_comp_cor_[0-9]+' , variables ) dv =...
Expand shorthand terms in the model formula .
22,895
def _unscramble_regressor_columns ( parent_data , data ) : matches = [ '_power[0-9]+' , '_derivative[0-9]+' ] var = OrderedDict ( ( c , deque ( ) ) for c in parent_data . columns ) for c in data . columns : col = c for m in matches : col = re . sub ( m , '' , col ) if col == c : var [ col ] . appendleft ( c ) else : va...
Reorder the columns of a confound matrix such that the columns are in the same order as the input data with any expansion columns inserted immediately after the originals .
22,896
def parse_formula ( model_formula , parent_data , unscramble = False ) : variables = { } data = { } expr_delimiter = 0 grouping_depth = 0 model_formula = _expand_shorthand ( model_formula , parent_data . columns ) for i , char in enumerate ( model_formula ) : if char == '(' : grouping_depth += 1 elif char == ')' : grou...
Recursively parse a model formula by breaking it into additive atoms and tracking grouping symbol depth .
22,897
def mask ( in_file , mask_file , new_name ) : import nibabel as nb import os in_nii = nb . load ( in_file ) mask_nii = nb . load ( mask_file ) data = in_nii . get_data ( ) data [ mask_nii . get_data ( ) == 0 ] = 0 new_nii = nb . Nifti1Image ( data , in_nii . affine , in_nii . header ) new_nii . to_filename ( new_name )...
Apply a binary mask to an image .
22,898
def create_cfm ( in_file , lesion_mask = None , global_mask = True , out_path = None ) : import os import numpy as np import nibabel as nb from nipype . utils . filemanip import fname_presuffix if out_path is None : out_path = fname_presuffix ( in_file , suffix = '_cfm' , newpath = os . getcwd ( ) ) else : out_path = o...
Create a mask to constrain registration .
22,899
def _get_settings ( self ) : if isdefined ( self . inputs . settings ) : NIWORKFLOWS_LOG . info ( 'User-defined settings, overriding defaults' ) return self . inputs . settings filestart = '{}-mni_registration_{}_' . format ( self . inputs . moving . lower ( ) , self . inputs . flavor ) filenames = [ i for i in pkgr . ...
Return any settings defined by the user as well as any pre - defined settings files that exist for the image modalities to be registered .