Python tqdm.auto.tqdm.auto() Examples

The following are 11 code examples of tqdm.auto.tqdm.auto(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tqdm.auto.tqdm , or try the search function .
Example #1
Source File: readwrite.py    From scanpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _download(url: str, path: Path):
    try:
        import ipywidgets
        from tqdm.auto import tqdm
    except ModuleNotFoundError:
        from tqdm import tqdm
    from urllib.request import urlretrieve

    path.parent.mkdir(parents=True, exist_ok=True)
    with tqdm(unit='B', unit_scale=True, miniters=1, desc=path.name) as t:

        def update_to(b=1, bsize=1, tsize=None):
            if tsize is not None:
                t.total = tsize
            t.update(b * bsize - t.n)

        try:
            urlretrieve(url, str(path), reporthook=update_to)
        except Exception:
            # Make sure file doesn’t exist half-downloaded
            if path.is_file():
                path.unlink()
            raise 
Example #2
Source File: estimation.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, k: int = 10, return_value: str = 'k_skewness',
                 hub_size: float = 2., metric='euclidean',
                 store_k_neighbors: bool = False, store_k_occurrence: bool = False,
                 algorithm: str = 'auto', algorithm_params: dict = None,
                 hubness: str = None, hubness_params: dict = None,
                 verbose: int = 0, n_jobs: int = 1, random_state=None,
                 shuffle_equal: bool = True):
        self.k = k
        self.return_value = return_value
        self.hub_size = hub_size
        self.metric = metric
        self.store_k_neighbors = store_k_neighbors
        self.store_k_occurrence = store_k_occurrence
        self.algorithm = algorithm
        self.algorithm_params = algorithm_params
        self.hubness = hubness
        self.hubness_params = hubness_params
        self.verbose = verbose
        self.n_jobs = n_jobs
        self.random_state = random_state
        self.shuffle_equal = shuffle_equal 
Example #3
Source File: classification.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, n_neighbors: int = 5, weights: str = 'uniform',
                 algorithm: str = 'auto', algorithm_params: dict = None,
                 hubness: str = None, hubness_params: dict = None,
                 leaf_size: int = 30, p=2, metric='minkowski', metric_params=None,
                 n_jobs=None, verbose: int = 0, **kwargs):

        super().__init__(
            n_neighbors=n_neighbors,
            algorithm=algorithm,
            algorithm_params=algorithm_params,
            hubness=hubness,
            hubness_params=hubness_params,
            leaf_size=leaf_size, metric=metric, p=p,
            metric_params=metric_params,
            n_jobs=n_jobs,
            verbose=verbose,
            **kwargs)
        self.weights = _check_weights(weights) 
Example #4
Source File: classification.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, radius=1.0, weights='uniform',
                 algorithm: str = 'auto', algorithm_params: dict = None,
                 hubness: str = None, hubness_params: dict = None,
                 leaf_size=30, p=2, metric='minkowski',
                 outlier_label=None, metric_params=None, n_jobs=None,
                 **kwargs):
        super().__init__(
            radius=radius,
            algorithm=algorithm,
            algorithm_params=algorithm_params,
            hubness=hubness,
            hubness_params=hubness_params,
            leaf_size=leaf_size,
            metric=metric, p=p, metric_params=metric_params,
            n_jobs=n_jobs, **kwargs)
        self.weights = _check_weights(weights)
        self.outlier_label = outlier_label 
Example #5
Source File: base.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, n_neighbors=None, radius=None,
                 algorithm='auto', algorithm_params: dict = None,
                 hubness: str = None, hubness_params: dict = None,
                 leaf_size=30, metric='minkowski', p=2, metric_params=None,
                 n_jobs=None, verbose: int = 0, **kwargs):
        super().__init__(n_neighbors=n_neighbors,
                         radius=radius,
                         algorithm=algorithm,
                         leaf_size=leaf_size,
                         metric=metric, p=p, metric_params=metric_params,
                         n_jobs=n_jobs)
        if algorithm_params is None:
            n_candidates = 1 if hubness is None else 100
            algorithm_params = {'n_candidates': n_candidates,
                                'metric': metric}
        if n_jobs is not None and 'n_jobs' not in algorithm_params:
            algorithm_params['n_jobs'] = self.n_jobs
        if 'verbose' not in algorithm_params:
            algorithm_params['verbose'] = verbose
        hubness_params = hubness_params if hubness_params is not None else {}
        if 'verbose' not in hubness_params:
            hubness_params['verbose'] = verbose

        self.algorithm_params = algorithm_params
        self.hubness_params = hubness_params
        self.hubness = hubness
        self.verbose = verbose
        self.kwargs = kwargs 
Example #6
Source File: base.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _check_algorithm_metric(self):
        if self.algorithm not in ['auto', *EXACT_ALG, *ANN_ALG]:
            raise ValueError("unrecognized algorithm: '%s'" % self.algorithm)

        if self.algorithm == 'auto':
            if self.metric == 'precomputed':
                alg_check = 'brute'
            elif (callable(self.metric) or
                  self.metric in VALID_METRICS['ball_tree']):
                alg_check = 'ball_tree'
            else:
                alg_check = 'brute'
        else:
            alg_check = self.algorithm

        if callable(self.metric):
            if self.algorithm in ['kd_tree', *ANN_ALG]:
                # callable metric is only valid for brute force and ball_tree
                raise ValueError(f"{self.algorithm} algorithm does not support callable metric '{self.metric}'")
        elif self.metric not in VALID_METRICS[alg_check]:
            raise ValueError(f"Metric '{self.metric}' not valid. Use "
                             f"sorted(skhubness.neighbors.VALID_METRICS['{alg_check}']) "
                             f"to get valid options. "
                             f"Metric can also be a callable function.")

        if self.metric_params is not None and 'p' in self.metric_params:
            warnings.warn("Parameter p is found in metric_params. "
                          "The corresponding parameter from __init__ "
                          "is ignored.", SyntaxWarning, stacklevel=3)
            effective_p = self.metric_params['p']
        else:
            effective_p = self.p

        if self.metric in ['wminkowski', 'minkowski'] and effective_p <= 0:
            raise ValueError("p must be greater than zero for minkowski metric") 
Example #7
Source File: nng.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, n_candidates: int = 5,
                 metric: str = 'euclidean',
                 index_dir: str = 'auto',
                 optimize: bool = False,
                 edge_size_for_creation: int = 80,
                 edge_size_for_search: int = 40,
                 num_incoming: int = -1,
                 num_outgoing: int = -1,
                 epsilon: float = 0.1,
                 n_jobs: int = 1,
                 verbose: int = 0):

        if ngtpy is None:  # pragma: no cover
            raise ImportError(f'Please install the `ngt` package, before using this class.\n'
                              f'$ pip3 install ngt') from None

        super().__init__(n_candidates=n_candidates,
                         metric=metric,
                         n_jobs=n_jobs,
                         verbose=verbose,
                         )
        self.index_dir = index_dir
        self.optimize = optimize
        self.edge_size_for_creation = edge_size_for_creation
        self.edge_size_for_search = edge_size_for_search
        self.num_incoming = num_incoming
        self.num_outgoing = num_outgoing
        self.epsilon = epsilon 
Example #8
Source File: tqdm_progress_bar.py    From addons with Apache License 2.0 4 votes vote down vote up
def __init__(
        self,
        metrics_separator: str = " - ",
        overall_bar_format: str = "{l_bar}{bar} {n_fmt}/{total_fmt} ETA: "
        "{remaining}s,  {rate_fmt}{postfix}",
        epoch_bar_format: str = "{n_fmt}/{total_fmt}{bar} ETA: "
        "{remaining}s - {desc}",
        metrics_format: str = "{name}: {value:0.4f}",
        update_per_second: int = 10,
        leave_epoch_progress: bool = True,
        leave_overall_progress: bool = True,
        show_epoch_progress: bool = True,
        show_overall_progress: bool = True,
    ):

        try:
            # import tqdm here because tqdm is not a required package
            # for addons
            import tqdm

            version_message = "Please update your TQDM version to >= 4.36.1, "
            "you have version {}. To update, run !pip install -U tqdm"
            assert tqdm.__version__ >= "4.36.1", version_message.format(
                tqdm.__version__
            )
            from tqdm.auto import tqdm

            self.tqdm = tqdm
        except ImportError:
            raise ImportError("Please install tqdm via pip install tqdm")

        self.metrics_separator = metrics_separator
        self.overall_bar_format = overall_bar_format
        self.epoch_bar_format = epoch_bar_format
        self.leave_epoch_progress = leave_epoch_progress
        self.leave_overall_progress = leave_overall_progress
        self.show_epoch_progress = show_epoch_progress
        self.show_overall_progress = show_overall_progress
        self.metrics_format = metrics_format

        # compute update interval (inverse of update per second)
        self.update_interval = 1 / update_per_second

        self.last_update_time = time.time()
        self.overall_progress_tqdm = None
        self.epoch_progress_tqdm = None
        self.is_training = False
        self.num_epochs = None
        self.logs = None
        super().__init__() 
Example #9
Source File: algorithm.py    From spectre with Apache License 2.0 4 votes vote down vote up
def run_simulation_alg(self, alg, start, end, delay_factor=True):
        from tqdm.auto import tqdm

        alg.blotter.clear()
        # get factor data from algorithm
        run_engine = alg.run_engine
        data, _ = run_engine(start, end, delay_factor)
        ticks = self.get_data_ticks(data, start)
        if len(ticks) == 0:
            raise ValueError("No data returned, please set `start`, `end` time correctly")
        data = self.wrap_data(data, DataLoaderFastGetter)
        # mock CustomAlgorithm
        alg.run_engine = lambda *args: (self._mocked_data, self._mocked_last)
        if 'empty_cache_after_run' in alg.__dict__:
            for eng in alg._engines.values():
                eng.empty_cache()
            gc.collect()
            torch.cuda.empty_cache()

        # infer freq
        delta = min(ticks[1:] - ticks[:-1])
        data_freq = delta.resolution_string

        # loop factor data
        last_day = None
        for dt in tqdm(ticks):
            if self._stop:
                break
            # prepare data
            self.mock_data(data, dt)

            # if date changed
            if dt.day != last_day:
                if last_day is not None:
                    self.fire_market_close(alg)
            alg.set_datetime(dt)

            # fire daily data event
            if data_freq == 'D':
                self.fire_event(self, EveryBarData)

            # fire open event
            if dt.day != last_day:
                self.fire_market_open(alg)
                last_day = dt.day

            # fire intraday data event
            if data_freq != 'D':
                alg.blotter.set_price('close')
                self.fire_event(self, EveryBarData)

        self.fire_market_close(alg)
        alg.run_engine = run_engine 
Example #10
Source File: random_projection_trees.py    From scikit-hubness with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def fit(self, X, y=None) -> RandomProjectionTree:
        """ Build the annoy.Index and insert data from X.

        Parameters
        ----------
        X: np.array
            Data to be indexed
        y: any
            Ignored

        Returns
        -------
        self: RandomProjectionTree
            An instance of RandomProjectionTree with a built index
        """
        if y is None:
            X = check_array(X)
        else:
            X, y = check_X_y(X, y)
            self.y_train_ = y

        self.n_samples_fit_ = X.shape[0]
        self.n_features_ = X.shape[1]
        self.X_dtype_ = X.dtype
        if self.metric == 'minkowski':  # for compatibility
            self.metric = 'euclidean'
        metric = self.metric if self.metric != 'sqeuclidean' else 'euclidean'
        self.effective_metric_ = metric
        annoy_index = annoy.AnnoyIndex(X.shape[1], metric=metric)
        if self.mmap_dir == 'auto':
            self.annoy_ = create_tempfile_preferably_in_dir(prefix='skhubness_',
                                                            suffix='.annoy',
                                                            directory='/dev/shm')
            logging.warning(f'The index will be stored in {self.annoy_}. '
                            f'It will NOT be deleted automatically, when this instance is destructed.')
        elif isinstance(self.mmap_dir, str):
            self.annoy_ = create_tempfile_preferably_in_dir(prefix='skhubness_',
                                                            suffix='.annoy',
                                                            directory=self.mmap_dir)
        else:  # e.g. None
            self.mmap_dir = None

        for i, x in tqdm(enumerate(X),
                         desc='Build RPtree',
                         disable=False if self.verbose else True,
                         ):
            annoy_index.add_item(i, x.tolist())
        annoy_index.build(self.n_trees)

        if self.mmap_dir is None:
            self.annoy_ = annoy_index
        else:
            annoy_index.save(self.annoy_, )

        return self 
Example #11
Source File: monitoring.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def __init__(self, frontend="auto", **kwargs):
        """
        Parameters
        ----------
        frontend : {"auto", "console", "gui", "notebook"}, optional
            Selects a frontend for displaying the progress bar. By default ("auto"),
            the frontend is chosen by guessing in which environment the simulation
            is run. The "console" frontend displays an ascii progress bar, while the
            "gui" frontend is based on matplotlib and the "notebook" frontend is based
            on ipywidgets.
        **kwargs : dict, optional
            Arbitrary keyword arguments for progress bar customization.
            See https://tqdm.github.io/docs/tqdm/.

        """
        if frontend == "auto":
            from tqdm.auto import tqdm
        elif frontend == "console":
            from tqdm import tqdm
        elif frontend == "gui":
            from tqdm.gui import tqdm
        elif frontend == "notebook":
            from tqdm.notebook import tqdm
        else:
            raise ValueError(
                f"Frontend argument {frontend!r} not supported. "
                "Please select one of the following: "
                ", ".join(["auto", "console", "gui", "notebook"])
            )

        self.custom_description = False
        if "desc" in kwargs.keys():
            self.custom_description = True

        self.tqdm = tqdm
        self.tqdm_kwargs = {"bar_format": "{bar} {percentage:3.0f}% | {desc} "}
        self.tqdm_kwargs.update(kwargs)