Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: replace progressbar with tqdm #202

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage*
.cache
nosetests.xml
coverage.xml
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install:
- eval "$(conda shell.bash hook)"
- conda activate test-env
- travis_retry conda install -c conda-forge python=$TRAVIS_PYTHON_VERSION
- travis_retry conda install -c conda-forge numpy scipy python-igraph h5netcdf
- travis_retry conda install -c conda-forge numpy scipy python-igraph h5netcdf tqdm
- travis_retry conda update -c conda-forge --all

# testing dependencies
Expand Down
3 changes: 0 additions & 3 deletions CONTRIBUTIONS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
- `Numpy <http://www.numpy.org/>`_
- `Cython <http://cython.org/>`_

**Included**
- `progressbar <http://pypi.python.org/pypi/progressbar/>`_

**To Do**
- A lot - See current product backlog.

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ build-backend = "setuptools.build_meta"

[tool.pylint.main]
ignore = [
"CVS", ".cache", ".tox", ".ropeproject", "build", "mpi.py", "progressbar"
"CVS", ".cache", ".tox", ".ropeproject", "build", "mpi.py"
]
ignore-patterns = ["navigator", "progressbar", "numerics"]
ignore-patterns = ["navigator", "numerics"]
persistent = false
jobs = 0

Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ install_requires =
scipy >= 1.10
igraph >= 0.10
h5netcdf >= 1.1
tqdm >= 4.66
python_requires = >=3.8
packages = find:
package_dir =
Expand Down Expand Up @@ -125,7 +126,7 @@ commands =

[flake8]
extend-exclude =
.git, .cache, .tox, .ropeproject, build, progressbar,
.git, .cache, .tox, .ropeproject, build,
docs/source/conf.py
extend-ignore =
E121, E123, E126, E226, E24, E704, E731, F401, F403, F405, F812, F841, W503
Expand All @@ -139,6 +140,6 @@ testpaths =
python_files =
test*.py Test*.py
norecursedirs =
.git .cache .tox .ropeproject build progressbar
.git .cache .tox .ropeproject build
addopts =
-v -r a -n auto
19 changes: 5 additions & 14 deletions src/pyunicorn/climate/havlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
# Import NumPy for the array object and fast numerics
import numpy as np

# Import progress bar for easy progress bar handling
from ..utils import progressbar
# Import tqdm for easy progress bar handling
from tqdm import trange

# Import cnNetwork for Network base class
from .climate_network import ClimateNetwork
Expand Down Expand Up @@ -172,19 +172,13 @@ def _calculate_correlation_strength(self, anomaly, max_delay, gamma=0.2):
correlation_strength = np.empty((N, N))
max_lag_matrix = np.empty((N, N))

# Initialize progress bar
if self.silence_level <= 1:
progress = progressbar.ProgressBar(maxval=N).start()

# Calculate the inverse Fourier transform of all time series
ifft = np.fft.ifft(anomaly, axis=0)

for i in range(N):
# Update progress bar every 10 steps
if self.silence_level <= 1:
if (i % 10) == 0:
progress.update(i)
# toggle progress bar
silence = self.silence_level > 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would inline this into the trange() call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This obviously applies equally to the other changes.


for i in trange(N, disable=silence, leave=False):
# Calculate the cross correlation function of node i to all other
# nodes which is not normalized yet.
# The real part has to be taken to get rid of small imaginary
Expand All @@ -210,9 +204,6 @@ def _calculate_correlation_strength(self, anomaly, max_delay, gamma=0.2):
# Store time delays at maximum cross correlation
max_lag_matrix[i, :] = cc_one_to_all.argmax(axis=0) - max_delay

if self.silence_level <= 1:
progress.finish()

return (correlation_strength, max_lag_matrix)

def get_max_delay(self):
Expand Down
21 changes: 6 additions & 15 deletions src/pyunicorn/climate/mutual_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
# array object and fast numerics
import numpy as np

# easy progress bar handling
from tqdm import trange

from ..core._ext.types import to_cy, FIELD
from ._ext.numerics import mutual_information

# Import progress bar for easy progress bar handling
from ..utils import progressbar

# Import cnNetwork for Network base class
from .climate_network import ClimateNetwork

Expand Down Expand Up @@ -216,18 +216,12 @@
# Compute the information entropies of each time series
H = - (p * log(p)).sum(axis=1)

# Initialize progress bar
if self.silence_level <= 1:
progress = progressbar.ProgressBar(maxval=self.N**2).start()
# Toggle progress bar
silence = self.silence_level > 1

Check warning on line 220 in src/pyunicorn/climate/mutual_info.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/climate/mutual_info.py#L220

Added line #L220 was not covered by tests

# Calculate only the lower half of the MI matrix, since MI is
# symmetric with respect to X and Y.
for i in range(self.N):
# Update progress bar every 10 steps
if self.silence_level <= 1:
if (i % 10) == 0:
progress.update(i**2)

for i in trange(self.N, disable=silence):

Check warning on line 224 in src/pyunicorn/climate/mutual_info.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/climate/mutual_info.py#L224

Added line #L224 was not covered by tests
for j in range(i):
# Calculate the joint probability distribution
pxy = histogram2d(
Expand All @@ -246,9 +240,6 @@
mi.itemset((i, j), H.item(i) + H.item(j) - HXY)
mi.itemset((j, i), mi.item((i, j)))

if self.silence_level <= 1:
progress.finish()

return mi

def calculate_similarity_measure(self, anomaly):
Expand Down
65 changes: 23 additions & 42 deletions src/pyunicorn/core/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from scipy.linalg import expm
from scipy import sparse as sp # fast sparse matrices
from scipy.sparse.linalg import eigsh, inv, splu
from tqdm import tqdm, trange # easy progress bar handling

import igraph # high performance graph theory tools

Expand All @@ -53,8 +54,6 @@
_nsi_betweenness, _mpi_newman_betweenness, _mpi_nsi_newman_betweenness, \
_do_nsi_clustering_I, _do_nsi_clustering_II, _do_nsi_hamming_clustering

from ..utils import progressbar # easy progress bar handling


# =============================================================================

Expand Down Expand Up @@ -1153,8 +1152,7 @@
cum += link_prob[i]
return i

progress = progressbar.ProgressBar(maxval=N).start()
for j in range(n_initials, N):
for j in trange(n_initials, N):

Check warning on line 1155 in src/pyunicorn/core/network.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/core/network.py#L1155

Added line #L1155 was not covered by tests
# add node j with unit weight:
link_prob[j] = kstar[j] = w[j] = 1
total_link_prob += 1
Expand Down Expand Up @@ -1216,11 +1214,6 @@
total_link_prob += link_prob[i] + link_prob[j2]
# print(total_link_prob, link_prob.sum())

if j % 10:
progress.update(j)

progress.finish()

else:
link_target = []

Expand Down Expand Up @@ -1321,30 +1314,28 @@
return i

this_N = n_initials
progress = progressbar.ProgressBar(maxval=N).start()
it = 0
while this_N < N and it < n_increases:
it += 1
i = _inc_target()
total_inc_prob -= inc_prob[i]
w[i] += 1
inc_prob[i] = w[i]**exponent
total_inc_prob += inc_prob[i]
if (mode == "exp" and random.uniform() > hold_prob**w[i]) or \
(mode == "rec" and random.uniform()
< w[i]*1.0/(split_weight+w[i])): # reciprocal
# split i into i,this_N:
with tqdm(total=N) as pbar:
while this_N < N and it < n_increases:
it += 1
i = _inc_target()

Check warning on line 1321 in src/pyunicorn/core/network.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/core/network.py#L1318-L1321

Added lines #L1318 - L1321 were not covered by tests
total_inc_prob -= inc_prob[i]
w[this_N] = w[i]*random.beta(beta, beta)
w[i] -= w[this_N]
inc_prob[this_N] = w[this_N]**exponent
w[i] += 1

Check warning on line 1323 in src/pyunicorn/core/network.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/core/network.py#L1323

Added line #L1323 was not covered by tests
inc_prob[i] = w[i]**exponent
total_inc_prob += inc_prob[this_N] + inc_prob[i]
this_N += 1
if this_N % 10:
progress.update(this_N)
total_inc_prob += inc_prob[i]
if (mode == "exp" and random.uniform() > hold_prob**w[i]) or \

Check warning on line 1326 in src/pyunicorn/core/network.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/core/network.py#L1325-L1326

Added lines #L1325 - L1326 were not covered by tests
(mode == "rec" and random.uniform()
< w[i]*1.0/(split_weight+w[i])): # reciprocal
# split i into i,this_N:
total_inc_prob -= inc_prob[i]
w[this_N] = w[i]*random.beta(beta, beta)
w[i] -= w[this_N]
inc_prob[this_N] = w[this_N]**exponent
inc_prob[i] = w[i]**exponent
total_inc_prob += inc_prob[this_N] + inc_prob[i]
this_N += 1
pbar.update()

Check warning on line 1337 in src/pyunicorn/core/network.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/core/network.py#L1330-L1337

Added lines #L1330 - L1337 were not covered by tests

progress.finish()
return w

def randomly_rewire(self, iterations):
Expand Down Expand Up @@ -4440,16 +4431,10 @@
if self.silence_level <= 1:
print("Calculating (weighted) node vulnerabilities...")

# Initialize progress bar
if self.silence_level <= 1:
progress = progressbar.ProgressBar(maxval=self.N).start()

for i in range(self.N):
# Update progress bar every 10 steps
if self.silence_level <= 1:
if (i % 10) == 0:
progress.update(i)
# Toggle progress bar
silence = self.silence_level > 1

for i in trange(self.N, disable=silence):
# Remove vertex i from graph
graph = self.graph - i

Expand All @@ -4467,10 +4452,6 @@
# Clean up
del graph, network

# Terminate progress bar
if self.silence_level <= 1:
progress.finish()

return vulnerability

#
Expand Down
22 changes: 7 additions & 15 deletions src/pyunicorn/timeseries/surrogates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import numpy as np
from numpy import random

# easy progress bar handling
from tqdm import trange

from ..core._ext.types import to_cy, ADJ, DEGREE, FIELD, DFIELD
from ._ext.numerics import _embed_time_series_array, _recurrence_plot, \
_twins_s, _twin_surrogates_s, _test_pearson_correlation, \
_test_mutual_information

# easy progress bar handling
from ..utils import progressbar


#
# Define class Surrogates
#


class Surrogates:

"""
Expand Down Expand Up @@ -704,15 +704,10 @@
# Initialize density estimate
density_estimate = np.zeros(n_bins)

# Initialize progress bar
if self.silence_level <= 2:
progress = progressbar.ProgressBar(maxval=realizations).start()

for i in range(realizations):
# Update progress bar
if self.silence_level <= 2:
progress.update(i)
# Toggle progress bar
silence = self.silence_level > 2

Check warning on line 708 in src/pyunicorn/timeseries/surrogates.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/timeseries/surrogates.py#L708

Added line #L708 was not covered by tests

for _ in trange(realizations, disable=silence):

Check warning on line 710 in src/pyunicorn/timeseries/surrogates.py

View check run for this annotation

Codecov / codecov/patch

src/pyunicorn/timeseries/surrogates.py#L710

Added line #L710 was not covered by tests
# Get the surrogate
# Mean and variance are conserved by all surrogates
surrogates = surrogate_function(original_data)
Expand All @@ -738,9 +733,6 @@
# but you never know...)
del surrogates, correlation_measure_test

if self.silence_level <= 2:
progress.finish()

# Normalize density estimate
density_estimate /= density_estimate.sum()

Expand Down
46 changes: 0 additions & 46 deletions src/pyunicorn/utils/progressbar/__init__.py

This file was deleted.

Loading