class CLIProgressBar(object): widgets = [' ', Bar(marker=RotatingMarker()), ' ', Percentage()] def __init__(self, size=100, label="", color=None, fd=sys.stdout): self.progress_bar = PB(widgets=[label] + self.widgets, maxval=size, fd=fd) self.progress = 0 def start(self, width='50%'): self.progress_bar.start() self.progress = 0 def increment(self, i=1): p = self.progress + i self.progress_bar.update(p) def update(self, progress): self.progress_bar.update(progress) def reset(self): self.progress_bar.start() self.progress = 0 def end(self): self.progress_bar.finish() def __call__(self, iterable): return self.progress_bar(iterable)
def example3(): widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish()
def example1(): widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=10000000).start() for i in range(1000000): # do something pbar.update(10 * i + 1) pbar.finish()
def example18(): widgets = [Percentage(), ' ', Bar(), ' ', ETA(), ' ', AdaptiveETA()] pbar = ProgressBar(widgets=widgets, maxval=500) pbar.start() for i in range(500): time.sleep(0.01 + (i < 100) * 0.01 + (i > 400) * 0.9) pbar.update(i + 1) pbar.finish()
def example17(): widgets = [FormatLabel('Animated Bouncer: value %(value)d - '), BouncingBar(marker=RotatingMarker())] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(180))): time.sleep(0.05)
def example4(): widgets = ['Test: ', Percentage(), ' ', Bar(marker='0', left='[', right=']'), ' ', ETA(), ' ', FileTransferSpeed()] pbar = ProgressBar(widgets=widgets, maxval=500) pbar.start() for i in range(100, 500 + 1, 50): time.sleep(0.2) pbar.update(i) pbar.finish()
def example15(): # You may need python 3.x to see this correctly try: widgets = ['Wheels: ', AnimatedMarker(markers='◐◓◑◒')] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(24))): time.sleep(0.3) except UnicodeError: sys.stdout.write('Unicode error: skipping example')
def example2(): class CrazyFileTransferSpeed(FileTransferSpeed): """It's bigger between 45 and 80 percent.""" def update(self, pbar): if 45 < pbar.percentage() < 80: return 'Bigger Now ' + FileTransferSpeed.update(self, pbar) else: return FileTransferSpeed.update(self, pbar) widgets = [CrazyFileTransferSpeed(), ' <<<', Bar(), '>>> ', Percentage(), ' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=10000000) # maybe do something pbar.start() for i in range(2000000): # do something pbar.update(5 * i + 1) pbar.finish()
def optimize_strains(self, pathways, view, aerobic=True): """ Optimize targets for the identified pathways. The optimization will only run if the pathway can be optimized. Arguments --------- pathways: list A list of dictionaries to optimize ([Host, Model] -> PredictedPathways). view: object A view for multi, single os distributed processing. aerobic: bool If True, it will set `model.reactions.EX_o2_e.lower_bound` to 0. Returns ------- pandas.DataFrame A data frame with strain designs processed and ranked. """ opt_gene_runner = _OptGeneRunner(self.debug) differential_fva_runner = _DifferentialFVARunner(self.debug) strategies = [ (host, model, pathway, aerobic) for (host, model) in pathways for pathway in pathways[host, model] if pathway.needs_optimization(model, objective=model.biomass) ] print("Optimizing %i pathways" % len(strategies)) results = DataFrame(columns=[ "host", "model", "manipulations", "heterologous_pathway", "fitness", "yield", "product", "biomass", "method" ]) mapped_designs1 = view.map(opt_gene_runner, strategies) mapped_designs2 = view.map(differential_fva_runner, strategies) progress = ProgressBar( maxval=len(mapped_designs1) + len(mapped_designs2), widgets=["Processing solutions: ", Bar(), ETA()]) progress.start() results = self.build_results_data(mapped_designs1, strategies, results, progress) results = self.build_results_data(mapped_designs2, strategies, results, progress, offset=len(mapped_designs1)) progress.finish() return results
def example10(): widgets = ['Processed: ', Counter(), ' lines (', Timer(), ')'] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(150))): time.sleep(0.1)
def example9(): pbar = ProgressBar(widgets=['Working: ', AnimatedMarker()]) for i in pbar((i for i in range(50))): time.sleep(.08)
def example8(): pbar = ProgressBar(maxval=80) # Progressbar can't guess maxval. for i in pbar((i for i in range(80))): time.sleep(0.01)
def run(self, surface_only=True, improvements_only=True, progress=True, view=None, fraction_of_optimum=1.0): """Run the differential flux variability analysis. Parameters ---------- surface_only : bool, optional If only the surface of the n-dimensional production envelope should be scanned (defaults to True). improvements_only : bool, optional If only grid points should should be scanned that constitute and improvement in production over the reference state (defaults to True). progress : bool, optional If a progress bar should be shown. view : SequentialView or MultiprocessingView or ipython.cluster.DirectView, optional A parallelization view (defaults to SequentialView). fraction_of_optimum : float, optional A value between zero and one that determines the width of the flux ranges of the reference solution. The lower the value, the larger the ranges. Returns ------- pandas.Panel A pandas Panel containing a results DataFrame for every grid point scanned. """ # Calculate the reference state. self.reference_flux_dist = pfba( self.reference_model, fraction_of_optimum=fraction_of_optimum) self.reference_flux_ranges = flux_variability_analysis( self.reference_model, reactions=self.included_reactions, view=view, remove_cycles=False, fraction_of_optimum=fraction_of_optimum).data_frame self.reference_flux_ranges[ self.reference_flux_ranges.abs() < non_zero_flux_threshold] = 0.0 reference_intervals = self.reference_flux_ranges.loc[ self.included_reactions, ['lower_bound', 'upper_bound']].values if self.normalize_ranges_by is not None: logger.debug( self.reference_flux_ranges.loc[self.normalize_ranges_by, ]) # The most obvious flux to normalize by is the biomass reaction # flux. This is probably always greater than zero. Just in case # the model is defined differently or some other normalizing # reaction is chosen, we use the absolute value. norm = abs(self.reference_flux_ranges.at[self.normalize_ranges_by, "lower_bound"]) if norm > non_zero_flux_threshold: normalized_reference_intervals = reference_intervals / norm else: raise ValueError( "The reaction that you have chosen for normalization '{}' " "has zero flux in the reference state. Please choose another " "one.".format(self.normalize_ranges_by)) with TimeMachine() as tm: # Make sure that the design_space_model is initialized to its original state later for variable in self.variables: reaction = self.design_space_model.reactions.get_by_id( variable) tm(do=int, undo=partial(setattr, reaction, 'lower_bound', reaction.lower_bound)) tm(do=int, undo=partial(setattr, reaction, 'upper_bound', reaction.upper_bound)) target_reaction = self.design_space_model.reactions.get_by_id( self.objective) tm(do=int, undo=partial(setattr, target_reaction, 'lower_bound', target_reaction.lower_bound)) tm(do=int, undo=partial(setattr, target_reaction, 'upper_bound', target_reaction.upper_bound)) if view is None: view = config.default_view else: view = view self._init_search_grid(surface_only=surface_only, improvements_only=improvements_only) func_obj = _DifferentialFvaEvaluator(self.design_space_model, self.variables, self.objective, self.included_reactions) if progress: progress = ProgressBar(len(self.grid)) results = list( progress(view.imap(func_obj, self.grid.iterrows()))) else: results = list(view.map(func_obj, self.grid.iterrows())) solutions = dict((tuple(point.iteritems()), fva_result) for (point, fva_result) in results) for sol in solutions.values(): sol[sol.abs() < non_zero_flux_threshold] = 0.0 intervals = sol.loc[self.included_reactions, ['lower_bound', 'upper_bound']].values gaps = [ self._interval_gap(interval1, interval2) for interval1, interval2 in zip(reference_intervals, intervals) ] sol['gaps'] = gaps if self.normalize_ranges_by is not None: # See comment above regarding normalization. normalizer = abs(sol.lower_bound[self.normalize_ranges_by]) if normalizer > non_zero_flux_threshold: normalized_intervals = sol.loc[ self.included_reactions, ['lower_bound', 'upper_bound']].values / normalizer sol['normalized_gaps'] = [ self._interval_gap(interval1, interval2) for interval1, interval2 in zip( normalized_reference_intervals, normalized_intervals) ] else: sol['normalized_gaps'] = numpy.nan else: sol['normalized_gaps'] = gaps # Determine where the reference flux range overlaps with zero. zero_overlap_mask = numpy.asarray([ self._interval_overlap(interval1, (0, 0)) > 0 for interval1 in reference_intervals ], dtype=bool) collection = list() for key, df in solutions.items(): df['biomass'] = key[0][1] df['production'] = key[1][1] df['KO'] = False df['flux_reversal'] = False df['suddenly_essential'] = False df['free_flux'] = False df.loc[(df.lower_bound == 0) & (df.upper_bound == 0) & (~zero_overlap_mask), 'KO'] = True df.loc[((self.reference_flux_ranges.upper_bound < 0) & (df.lower_bound > 0) | ((self.reference_flux_ranges.lower_bound > 0) & (df.upper_bound < 0))), 'flux_reversal'] = True df.loc[(zero_overlap_mask & (df.lower_bound > 0)) | (zero_overlap_mask & (df.upper_bound < 0)), 'suddenly_essential'] = True is_reversible = numpy.asarray([ self.design_space_model.reactions.get_by_id(i).reversibility for i in df.index ], dtype=bool) not_reversible = ~is_reversible df.loc[((df.lower_bound == -1000) & (df.upper_bound == 1000) & is_reversible) | ((df.lower_bound == 0) & (df.upper_bound == 1000) & not_reversible) | ((df.lower_bound == -1000) & (df.upper_bound == 0) & not_reversible), 'free_flux'] = True df['reaction'] = df.index df['excluded'] = df['reaction'].isin(self.exclude) collection.append(df) # multi_index = [(key[0][1], key[1][1]) for key in solutions] # solutions_multi_index = pandas.concat(list(solutions.values()), # axis=0, keys=multi_index)# # solutions_multi_index.index.set_names(['biomass', 'production', # 'reaction'], inplace=True) total = pandas.concat(collection, ignore_index=True, copy=False) total.sort_values(['biomass', 'production', 'reaction'], inplace=True) total.index = total['reaction'] return DifferentialFVAResult(total, self.envelope, self.reference_flux_ranges)
def example0(): pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=300).start() for i in range(300): time.sleep(0.01) pbar.update(i + 1) pbar.finish()
def example19(): pbar = ProgressBar() for i in pbar([]): pass pbar.finish()
def run(self, surface_only=True, improvements_only=True, view=None): """Run the differential flux variability analysis. Parameters ---------- surface_only : bool, optional If only the surface of the n-dimensional production envelope should be scanned (defaults to True). improvements_only : bool, optional If only grid points should should be scanned that constitute and improvement in production over the reference state (defaults to True). view : SequentialView or MultiprocessingView or ipython.cluster.DirectView, optional A parallelization view (defaults to SequentialView). Returns ------- pandas.Panel A pandas Panel containing a results DataFrame for every grid point scanned. """ with TimeMachine() as tm: # Make sure that the design_space_model is initialized to its original state later for variable in self.variables: reaction = self.design_space_model.reactions.get_by_id( variable) tm(do=int, undo=partial(setattr, reaction, 'lower_bound', reaction.lower_bound)) tm(do=int, undo=partial(setattr, reaction, 'upper_bound', reaction.upper_bound)) target_reaction = self.design_space_model.reactions.get_by_id( self.objective) tm(do=int, undo=partial(setattr, target_reaction, 'lower_bound', target_reaction.lower_bound)) tm(do=int, undo=partial(setattr, target_reaction, 'upper_bound', target_reaction.upper_bound)) if view is None: view = config.default_view else: view = view included_reactions = [ reaction.id for reaction in self.reference_model.reactions if reaction.id not in self.exclude ] self.reference_flux_ranges = flux_variability_analysis( self.reference_model, reactions=included_reactions, view=view, remove_cycles=False).data_frame self._init_search_grid(surface_only=surface_only, improvements_only=improvements_only) progress = ProgressBar(len(self.grid)) func_obj = _DifferentialFvaEvaluator(self.design_space_model, self.variables, self.objective, included_reactions) results = list(progress(view.imap(func_obj, self.grid.iterrows()))) solutions = dict((tuple(point.iteritems()), fva_result.data_frame) for (point, fva_result) in results) reference_intervals = self.reference_flux_ranges[[ 'lower_bound', 'upper_bound' ]].values for sol in six.itervalues(solutions): intervals = sol[['lower_bound', 'upper_bound']].values gaps = [ self._interval_gap(interval1, interval2) for interval1, interval2 in my_zip(reference_intervals, intervals) ] sol['gaps'] = gaps if self.normalize_ranges_by is not None: for sol in six.itervalues(solutions): normalized_intervals = sol[[ 'lower_bound', 'upper_bound' ]].values / sol.lower_bound[self.normalize_ranges_by] normalized_gaps = [ self._interval_gap(interval1, interval2) for interval1, interval2 in my_zip(reference_intervals, normalized_intervals) ] sol['normalized_gaps'] = normalized_gaps for df in six.itervalues(solutions): ko_selection = df[(df.lower_bound == 0) & (df.upper_bound == 0) & (self.reference_flux_ranges.lower_bound != 0) & self.reference_flux_ranges.upper_bound != 0] df['KO'] = False df.loc[ko_selection.index]['KO'] = True for df in six.itervalues(solutions): flux_reversal_selection = df[( (self.reference_flux_ranges.upper_bound < 0) & (df.lower_bound > 0) | ((self.reference_flux_ranges.lower_bound > 0) & (df.upper_bound < 0)))] df['flux_reversal'] = False df.loc[flux_reversal_selection.index]['flux_reversal'] = True for df in six.itervalues(solutions): flux_reversal_selection = df[( (self.reference_flux_ranges.lower_bound <= 0) & (df.lower_bound > 0)) | ( (self.reference_flux_ranges.upper_bound >= 0) & (df.upper_bound <= 0))] df['suddenly_essential'] = False df.loc[flux_reversal_selection.index]['suddenly_essential'] = True # solutions['reference_flux_ranges'] = self.reference_flux_ranges return DifferentialFVAResult(pandas.Panel(solutions), self.envelope, self.variables, self.objective)
def run(self, surface_only=True, improvements_only=True, progress=True, view=None): """Run the differential flux variability analysis. Parameters ---------- surface_only : bool, optional If only the surface of the n-dimensional production envelope should be scanned (defaults to True). improvements_only : bool, optional If only grid points should should be scanned that constitute and improvement in production over the reference state (defaults to True). progress : bool, optional If a progress bar should be shown. view : SequentialView or MultiprocessingView or ipython.cluster.DirectView, optional A parallelization view (defaults to SequentialView). Returns ------- pandas.Panel A pandas Panel containing a results DataFrame for every grid point scanned. """ with TimeMachine() as tm: # Make sure that the design_space_model is initialized to its original state later for variable in self.variables: reaction = self.design_space_model.reactions.get_by_id( variable) tm(do=int, undo=partial(setattr, reaction, 'lower_bound', reaction.lower_bound)) tm(do=int, undo=partial(setattr, reaction, 'upper_bound', reaction.upper_bound)) target_reaction = self.design_space_model.reactions.get_by_id( self.objective) tm(do=int, undo=partial(setattr, target_reaction, 'lower_bound', target_reaction.lower_bound)) tm(do=int, undo=partial(setattr, target_reaction, 'upper_bound', target_reaction.upper_bound)) if view is None: view = config.default_view else: view = view included_reactions = [ reaction.id for reaction in self.reference_model.reactions if reaction.id not in self.exclude ] + self.variables + [self.objective] self.reference_flux_dist = pfba(self.reference_model, fraction_of_optimum=0.99) self.reference_flux_ranges = flux_variability_analysis( self.reference_model, reactions=included_reactions, view=view, remove_cycles=False, fraction_of_optimum=0.75).data_frame self._init_search_grid(surface_only=surface_only, improvements_only=improvements_only) func_obj = _DifferentialFvaEvaluator(self.design_space_model, self.variables, self.objective, included_reactions) if progress: progress = ProgressBar(len(self.grid)) results = list( progress(view.imap(func_obj, self.grid.iterrows()))) else: results = list(view.map(func_obj, self.grid.iterrows())) solutions = dict((tuple(point.iteritems()), fva_result) for (point, fva_result) in results) reference_intervals = self.reference_flux_ranges[[ 'lower_bound', 'upper_bound' ]].values for sol in six.itervalues(solutions): intervals = sol[['lower_bound', 'upper_bound']].values gaps = [ self._interval_gap(interval1, interval2) for interval1, interval2 in my_zip(reference_intervals, intervals) ] sol['gaps'] = gaps if self.normalize_ranges_by is not None: normalizer = sol.lower_bound[self.normalize_ranges_by] if normalizer > non_zero_flux_threshold: normalized_intervals = sol[['lower_bound', 'upper_bound' ]].values / normalizer sol['normalized_gaps'] = [ self._interval_gap(interval1, interval2) for interval1, interval2 in my_zip( reference_intervals, normalized_intervals) ] else: sol['normalized_gaps'] = [numpy.nan] * len(sol.lower_bound) else: sol['normalized_gaps'] = gaps ref_upper_bound = self.reference_flux_ranges.upper_bound.apply( lambda v: 0 if abs(v) < non_zero_flux_threshold else v) ref_lower_bound = self.reference_flux_ranges.lower_bound.apply( lambda v: 0 if abs(v) < non_zero_flux_threshold else v) collection = list() for key, df in six.iteritems(solutions): df['biomass'] = key[0][1] df['production'] = key[1][1] df['KO'] = False df['flux_reversal'] = False df['suddenly_essential'] = False df['free_flux'] = False df.loc[(df.lower_bound == 0) & (df.upper_bound == 0) & (ref_upper_bound != 0) & (ref_lower_bound != 0), 'KO'] = True df.loc[((ref_upper_bound < 0) & (df.lower_bound > 0) | ((ref_lower_bound > 0) & (df.upper_bound < 0))), 'flux_reversal'] = True df.loc[((df.lower_bound <= 0) & (df.lower_bound > 0)) | ((ref_lower_bound >= 0) & (df.upper_bound <= 0)), 'suddenly_essential'] = True is_reversible = numpy.asarray([ self.design_space_model.reactions.get_by_id(i).reversibility for i in df.index ], dtype=bool) not_reversible = numpy.logical_not(is_reversible) df.loc[((df.lower_bound == -1000) & (df.upper_bound == 1000) & is_reversible) | ((df.lower_bound == 0) & (df.upper_bound == 1000) & not_reversible) | ((df.lower_bound == -1000) & (df.upper_bound == 0) & not_reversible), 'free_flux'] = True df['reaction'] = df.index df['excluded'] = df['reaction'].isin(self.exclude) collection.append(df) # multi_index = [(key[0][1], key[1][1]) for key in solutions] # solutions_multi_index = pandas.concat(list(solutions.values()), # axis=0, keys=multi_index)# # solutions_multi_index.index.set_names(['biomass', 'production', # 'reaction'], inplace=True) total = pandas.concat(collection, ignore_index=True, copy=False) total.sort_values(['biomass', 'production', 'reaction'], inplace=True) total.index = total['reaction'] return DifferentialFVAResult(total, self.envelope, self.reference_flux_ranges, self.reference_flux_dist)
def example11(): widgets = [FormatLabel('Processed: %(value)d lines (in: %(elapsed)s)')] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(150))): time.sleep(0.1)
def example12(): widgets = ['Balloon: ', AnimatedMarker(markers='.oO@* ')] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(24))): time.sleep(0.3)
def __init__(self, size=100, label="", color=None, fd=sys.stdout): self.progress_bar = PB(widgets=[label] + self.widgets, maxval=size, fd=fd) self.progress = 0
def example16(): widgets = [FormatLabel('Bouncer: value %(value)d - '), BouncingBar()] pbar = ProgressBar(widgets=widgets) for i in pbar((i for i in range(180))): time.sleep(0.05)
def example5(): pbar = ProgressBar(widgets=[SimpleProgress()], maxval=17).start() for i in range(17): time.sleep(0.2) pbar.update(i + 1) pbar.finish()
def example6(): pbar = ProgressBar().start() for i in range(100): time.sleep(0.01) pbar.update(i + 1) pbar.finish()
def example7(): pbar = ProgressBar() # Progressbar can guess maxval automatically. for i in pbar(range(80)): time.sleep(0.01)