def split_words(text): #split a string into array of words try: text = regex_sub(r'[^\w ]', '', text, flags=REGEX_UNICODE) # strip special chars return [x.strip('.').lower() for x in text.split()] except TypeError: return None
def get_response(self, connection=None, silent=False): """ See :meth:`BaseShell.get_response` for more information. """ # Get connection spawn = self._get_connection(connection) # Convert binary representation to unicode using encoding text = spawn.before.decode(self._encoding) # Remove leading and trailing whitespaces and normalize newlines text = text.strip().replace('\r', '') # Remove control codes text = regex_sub(TERM_CODES_REGEX, '', text) # Split text into lines lines = text.splitlines() # Delete buffer with output right now, as it can be very large del text # Remove echo command if it exists if self._try_filter_echo and \ lines and self._last_command is not None \ and lines[0].strip() == self._last_command.strip(): lines.pop(0) response = '\n'.join(lines) if not silent and self._response_logger is not None: self._response_logger(response, self._shell) return response
def split_words(text): #split a string into array of words try: text = regex_sub(r'[^\w ]', '', text) # strip special chars return [x.strip('.').lower() for x in text.split()] except TypeError: return None
def get_response(self, connection=None): """ See :meth:`BaseShell.get_response` for more information. """ # Get connection spawn = self._get_connection(connection) # Convert binary representation to unicode using encoding text = spawn.before.decode(self._encoding) # Remove leading and trailing whitespaces and normalize newlines text = text.strip().replace('\r', '') # Remove control codes text = regex_sub(TERM_CODES_REGEX, '', text) # Split text into lines lines = text.splitlines() # Delete buffer with output right now, as it can be very large del text # Remove echo command if it exists if self._try_filter_echo and \ lines and self._last_command is not None \ and lines[0].strip() == self._last_command.strip(): lines.pop(0) return '\n'.join(lines)
def c_format(color_string): # Automatic color formatting by replacing words global player temp_string = color_string for word in player.all_commands: temp_string = regex_sub(word, Back.GREEN + word + Back.RESET, temp_string) return temp_string
def Read(self, filename): if (path.exists(filename) or path.exists(self.FILENAME)) and (len(filename) > 0 or len(self.FILENAME) > 0): if filename and not self.FILENAME: self.FILENAME = filename with open(self.FILENAME, "r") as conffile: for line in conffile: if line[0] == '#' or len(line) == 0: continue else: self.CONFIG.update({ regex_sub('^([^ \t]+).*$', '\\1', str(line)).strip( ): regex_sub('^[^ \t]+[ \t]+(.*)$', '\\1', str(line)).strip() })
def split_words(text): #split a string into array of words try: text = regex_sub(r'[^\w ]', '', text, flags=REGEX_UNICODE) # strip special chars return [x.strip('.').lower() for x in text.split()] except TypeError: print "Error while splitting characters" return None
def split_words(text): """Split a string into array of words.""" try: # Strip special characters text = regex_sub(r'[^\w ]', '', text, flags=REGEX_UNICODE) return [x.strip('.').lower() for x in text.split()] except TypeError: print("Error while splitting characters.") return None
def split_words(text): """ This function splits sentences into an array of words """ try: text = regex_sub(r"[^\w ]", "", text, flags=REGEX_UNICODE) # strip special chars return [x.strip(".").lower() for x in text.split()] except TypeError: print("Error while splitting characters") return None
def split_words(text): ''' This function splits sentences into an array of words ''' try: text = regex_sub(r'[^\w ]', '', text, flags=REGEX_UNICODE) # strip special chars return [x.strip('.').lower() for x in text.split()] except TypeError: print("Error while splitting characters") return None
def to_decimal(value, formatting_chars=['$', ',']): """Convert a string value to a Decimal. Remove any $ characters.""" # Remove any formatting and/or special characters pattern = "[{0}]".format('|'.join(formatting_chars)) value = regex_sub(pattern, '', value) # Now, attempt a Decimal conversion. try: value = Decimal(value) except InvalidOperation: value = None return value
def split_sentences(text): ''' The regular expression matches all sentence ending punctuation and splits the string at those points. At this point in the code, the list looks like this ["Hello, world", "!" ... ]. The punctuation and all quotation marks are separated from the actual text. The first s_iter line turns each group of two items in the list into a tuple, excluding the last item in the list (the last item in the list does not need to have this performed on it). Then, the second s_iter line combines each tuple in the list into a single item and removes any whitespace at the beginning of the line. Now, the s_iter list is formatted correctly but it is missing the last item of the sentences list. The second to last line adds this item to the s_iter list and the last line returns the full list. ''' text = regex_sub(" (%s)\." % ("|".join(abbreviations)), r' \1', text) sentences = regex_split(u'(?<![A-ZА-ЯЁ])([.!?]"?)(?=\s+\"?[A-ZА-ЯЁ])', text, flags=REGEX_UNICODE) s_iter = zip(*[iter(sentences[:-1])] * 2) s_iter = [''.join(map(unicode,y)).lstrip() for y in s_iter] s_iter.append(sentences[-1]) return s_iter
def _get_output_filename(filename, extension): output_path, output_name = path.split(filename) output_name = path.splitext(output_name)[0] output_name = regex_sub('(_\d\d)$', '', output_name) if not output_name.startswith(OUTPUT_FILE_PREFIX): output_name = OUTPUT_FILE_PREFIX + output_name if not output_name.endswith(OUTPUT_FILE_SUFFIX): output_name = output_name + OUTPUT_FILE_SUFFIX file_count = 0 output_name = output_name + '_??' + extension while path.exists( path.join( output_path, output_name.replace('??', '{0:02d}'.format(file_count)))): file_count += 1 output_name = output_name.replace('??', '{0:02d}'.format(file_count)) return path.join(output_path, output_name)
def get_response(self, connection=None, silent=False): """ See :meth:`BaseShell.get_response` for more information. """ # Get connection spawn = self._get_connection(connection=connection) # Convert binary representation to unicode using encoding text = spawn.before.decode( encoding=self._encoding, errors=self._errors ) # Remove leading and trailing whitespaces and normalize newlines text = text.strip().replace('\r', '') term_codes_regex = getattr( self, 'TERM_CODES_REGEX', TERM_CODES_REGEX ) # Remove control codes text = regex_sub(term_codes_regex, '', text) # Split text into lines lines = text.splitlines() # Delete buffer with output right now, as it can be very large del text # Remove echo command if it exists if self._try_filter_echo and \ lines and self._last_command is not None \ and lines[0].strip() == self._last_command.strip(): lines.pop(0) response = '\n'.join(lines) # Log response if not silent: if self._testlog: self._testlog.log_get_response( self._node_identifier, self._shell_name, response) else: spawn._connection_logger.log_get_response(response) return response
def split_sentences(text): ''' The regular expression matches all sentence ending punctuation and splits the string at those points. At this point in the code, the list looks like this ["Hello, world", "!" ... ]. The punctuation and all quotation marks are separated from the actual text. The first s_iter line turns each group of two items in the list into a tuple, excluding the last item in the list (the last item in the list does not need to have this performed on it). Then, the second s_iter line combines each tuple in the list into a single item and removes any whitespace at the beginning of the line. Now, the s_iter list is formatted correctly but it is missing the last item of the sentences list. The second to last line adds this item to the s_iter list and the last line returns the full list. ''' text = regex_sub(" (%s)\." % ("|".join(abbreviations)), r' \1', text) sentences = regex_split(u'(?<![A-ZА-ЯЁ])([.!?]"?)(?=\s+\"?[A-ZА-ЯЁ])', text, flags=REGEX_UNICODE) s_iter = zip(*[iter(sentences[:-1])] * 2) s_iter = [''.join(map(unicode, y)).lstrip() for y in s_iter] s_iter.append(sentences[-1]) return s_iter
def _sort_list(self, l: list): l.sort( key=lambda name: int( regex_sub(r"[^0-9]*", '', name) ) )
def output_dir_url(self): if bool(self.output_file) is False: return None url = regex_sub('[^/]+$', '', self.output_file.url) return url
def set_outfile(validation_run, run_dir): outfile = first_file_in(run_dir, '.nc') if outfile is not None: outfile = regex_sub('/?' + OUTPUT_FOLDER + '/?', '', outfile) validation_run.output_file.name = outfile
def setup_filtering(reader, filters, param_filters, dataset, variable): # figure out which variables we have to load because we want to use them load_vars = get_used_variables(filters, dataset, variable) # restrict the variables that are read from file in the reader if hasattr(reader.cls, 'parameters'): __logger.debug("Replacing existing variables to read: {}".format(reader.cls.parameters)) reader.cls.parameters = load_vars if not filters and not param_filters: __logger.debug('No filters to apply for dataset {}.'.format(dataset)) return reader filtered_reader = reader for pfil in param_filters: __logger.debug("Setting up parametrised filter {} for dataset {} with parameter {}".format(pfil.filter.name, dataset, pfil.parameters)) inner_reader = filtered_reader while (hasattr(inner_reader, 'cls')): inner_reader = inner_reader.cls if(pfil.filter.name == "FIL_ISMN_NETWORKS" and pfil.parameters): if isinstance(inner_reader, ISMN_Interface): param = regex_sub(r'[ ]+,[ ]+', ',', pfil.parameters) # replace whitespace around commas param = regex_sub(r'(^[ ]+|[ ]+$)', '', param) # replace whitespace at start and end of string paramnetlist = param.split(',') networks = [ n for n in paramnetlist if n in inner_reader.list_networks() ] __logger.debug('Available networks: ' + ';'.join(inner_reader.list_networks())) __logger.debug('Selected networks: ' + ';'.join(networks)) inner_reader.activate_network(networks) continue masking_filters = [] for fil in filters: __logger.debug("Setting up filter {} for dataset {}.".format(fil.name, dataset)) if(fil.name == "FIL_ALL_VALID_RANGE"): masking_filters.append( (variable.pretty_name, '>=', variable.min_value) ) masking_filters.append( (variable.pretty_name, '<=', variable.max_value) ) continue if(fil.name == "FIL_ISMN_GOOD"): masking_filters.append( ('soil moisture_flag', '==', 'G') ) continue if(fil.name == "FIL_C3S_FLAG_0"): masking_filters.append( ('flag', '==', 0) ) continue if(fil.name == "FIL_C3S_NO_FLAG_1"): masking_filters.append( ('flag', '!=', 1) ) continue if(fil.name == "FIL_C3S_NO_FLAG_2"): masking_filters.append( ('flag', '!=', 2) ) continue if(fil.name == "FIL_C3S_MODE_ASC"): masking_filters.append( ('mode', '==', 1) ) continue if(fil.name == "FIL_C3S_MODE_DESC"): masking_filters.append( ('mode', '==', 2) ) continue if(fil.name == "FIL_GLDAS_UNFROZEN"): temp_variable = variable.pretty_name.replace("Moi", "TMP") masking_filters.append( ('SWE_inst', '<', 0.001) ) masking_filters.append( (variable.pretty_name, '>', 0.0) ) masking_filters.append( (temp_variable, '>', 1.) ) continue if(fil.name == "FIL_ASCAT_METOP_A"): masking_filters.append( ('sat_id', '==', 3) ) continue if(fil.name == "FIL_ASCAT_METOP_B"): masking_filters.append( ('sat_id', '==', 4) ) continue if(fil.name == "FIL_ASCAT_UNFROZEN_UNKNOWN"): masking_filters.append( ('ssf', '<=', 1) ) ## TODO: really should be == 0 or == 1 continue if(fil.name == "FIL_ASCAT_NO_CONF_FLAGS"): masking_filters.append( ('conf_flag', '==', 0) ) continue if(fil.name == "FIL_ASCAT_NO_PROC_FLAGS"): masking_filters.append( ('proc_flag', '==', 0) ) continue if(fil.name == "FIL_SMOS_QUAL_RECOMMENDED"): masking_filters.append( ('Quality_Flag', '==', 0) ) continue if(fil.name == "FIL_SMOS_TOPO_NO_MODERATE"): masking_filters.append( ('Processing_Flags', smos_exclude_bitmask, 0b00000001) ) continue if(fil.name == "FIL_SMOS_TOPO_NO_STRONG"): masking_filters.append( ('Processing_Flags', smos_exclude_bitmask, 0b00000010) ) continue if(fil.name == "FIL_SMOS_UNPOLLUTED"): masking_filters.append( ('Scene_Flags', smos_exclude_bitmask, 0b00000100) ) continue if(fil.name == "FIL_SMOS_UNFROZEN"): masking_filters.append( ('Scene_Flags', smos_exclude_bitmask, 0b00001000) ) continue if(fil.name == "FIL_SMOS_BRIGHTNESS"): masking_filters.append( ('Processing_Flags', smos_exclude_bitmask, 0b00000001) ) continue #snow depth in the nc file yet, this is the preliminary one. if(fil.name == "FIL_ERA5_TEMP_UNFROZEN"): era_temp_variable = variable.pretty_name.replace("wv", "t") masking_filters.append( (era_temp_variable, '>', 274.15) ) continue if(fil.name == "FIL_ERA5_LAND_TEMP_UNFROZEN"): era_temp_variable = variable.pretty_name.replace("wv", "t") masking_filters.append( (era_temp_variable, '>', 274.15) ) continue if len(masking_filters): filtered_reader = AdvancedMaskingAdapter(filtered_reader, masking_filters) return filtered_reader
def _remove_unicode(string): return regex_sub(r'[^\x00-\x7F]+', '', string)
from mesmerize.viewer.modules.nuset_segment import * import tifffile from glob import glob from re import sub as regex_sub from tqdm import tqdm if __name__ == '__main__': app = QtWidgets.QApplication([]) fnames = glob('/home/kushal/Sars_stuff/zfish_stds/*.tiff') fnames.sort(key=lambda name: int(regex_sub(r"[^0-9]*", '', name))) # img = tifffile.imread('/home/kushal/Sars_stuff/zfish_stds/5.tiff') imgs = [tifffile.imread(f) for f in tqdm(fnames)] w = ModuleGUI(None, None) w.show() w.widget.imgs_projected = imgs w.widget.z_max = len(imgs) w.widget.zslider.setMaximum(w.widget.z_max) w.widget.spinbox_zlevel.setMaximum(w.widget.z_max) w.widget.imgitem_raw.setImage(imgs[0]) # w.imgitem_raw.setImage(img) # w.image_projection = img app.exec_()