/
ops.py
940 lines (682 loc) · 27.3 KB
/
ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
"""
Miscellaneous operations.
"""
import collections.abc
import datetime
import itertools
import math
import os
import re
import socket
import time
import types
import urllib.parse
import numpy as np
import pandas as pd
""" General use -------------------------------------------------------------------- """
def confirmed(prompt=None, resp=False, confirmation_required=True):
"""
Type to confirm whether to proceed or not.
See also
[`C-1 <http://sensitivecities.com/
so-youd-like-to-make-a-map-using-python-EN.html#.WbpP0T6GNQB>`_].
:param prompt: a message that prompts an response (Yes/No), defaults to ``None``
:type prompt: str or None
:param resp: default response, defaults to ``False``
:type resp: bool
:param confirmation_required: whether to require users to confirm and proceed,
defaults to ``True``
:type confirmation_required: bool
:return: a response
:rtype: bool
**Example**::
>>> from pyhelpers.ops import confirmed
>>> if confirmed(prompt="Create Directory?", resp=True):
... print(True)
Create Directory? [No]|Yes: yes
True
"""
if confirmation_required:
if prompt is None:
prompt = "Confirmed? "
if resp is True: # meaning that default response is True
prompt = "{} [{}]|{}: ".format(prompt, "Yes", "No")
else:
prompt = "{} [{}]|{}: ".format(prompt, "No", "Yes")
ans = input(prompt)
if not ans:
return resp
if re.match('[Yy](es)?', ans):
return True
if re.match('[Nn](o)?', ans):
return False
else:
return True
""" Basic data manipulation -------------------------------------------------------- """
# Iterable
def split_list_by_size(lst, sub_len):
"""
Split a list into (evenly sized) sub-lists.
See also [`SLBS-1 <https://stackoverflow.com/questions/312443/>`_].
:param lst: a list of any
:type lst: list
:param sub_len: length of a sub-list
:type sub_len: int
:return: a sequence of ``sub_len``-sized sub-lists from ``lst``
:rtype: types.GeneratorType
**Example**::
>>> from pyhelpers.ops import split_list_by_size
>>> lst_ = list(range(0, 10))
>>> sub_lst_len = 3
>>> lists = split_list_by_size(lst_, sub_lst_len)
>>> print(list(lists))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
"""
for i in range(0, len(lst), sub_len):
yield lst[i:i + sub_len]
def split_list(lst, num_of_sub):
"""
Split a list into a number of equally-sized sub-lists.
See also [`SL-1 <https://stackoverflow.com/questions/312443/>`_].
:param lst: a list of any
:type lst: list
:param num_of_sub: number of sub-lists
:type num_of_sub: int
:return: a total of ``num_of_sub`` sub-lists from ``lst``
:rtype: types.GeneratorType
**Example**::
>>> from pyhelpers.ops import split_list
>>> lst_ = list(range(0, 10))
>>> num_of_sub_lists = 3
>>> lists = list(split_list(lst_, num_of_sub_lists))
>>> print(list(lists))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
"""
chunk_size = math.ceil(len(lst) / num_of_sub)
for i in range(0, len(lst), chunk_size):
yield lst[i:i + chunk_size]
def split_iterable(iterable, chunk_size):
"""
Split a list into (evenly sized) chunks.
See also [`SI-1 <https://stackoverflow.com/questions/24527006/>`_].
:param iterable: iterable object
:type iterable: list or tuple or collections.abc.Iterable
:param chunk_size: length of a chunk
:type chunk_size: int
:return: a sequence of equally-sized chunks from ``iterable``
:rtype: types.GeneratorType
**Examples**::
>>> import pandas as pd_
>>> from pyhelpers.ops import split_iterable
>>> lst = list(range(0, 10))
>>> size_of_chunk = 3
>>> for lst_ in split_iterable(lst, size_of_chunk):
... print(list(lst_))
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]
>>> lst = pd_.Series(range(0, 20))
>>> size_of_chunk = 5
>>> for lst_ in split_iterable(lst, size_of_chunk):
... print(list(lst_))
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11, 12, 13, 14]
[15, 16, 17, 18, 19]
"""
iterator = iter(iterable)
for x in iterator:
yield itertools.chain([x], itertools.islice(iterator, chunk_size - 1))
def update_nested_dict(source_dict, updates):
"""
Update a nested dictionary or similar mapping.
See also [`UND-1 <https://stackoverflow.com/questions/3232943/>`_].
:param source_dict: a dictionary that needs to be updated
:type source_dict: dict
:param updates: a dictionary with new data
:type updates: dict
:return: an updated dictionary
:rtype: dict
**Examples**::
>>> from pyhelpers.ops import update_nested_dict
>>> source_dict_ = {'key_1': 1}
>>> updates_ = {'key_2': 2}
>>> source_dict_ = update_nested_dict(source_dict_, updates_)
>>> print(source_dict_)
{'key_1': 1, 'key_2': 2}
>>> source_dict_ = {'key': 'val_old'}
>>> updates_ = {'key': 'val_new'}
>>> source_dict_ = update_nested_dict(source_dict_, updates_)
>>> print(source_dict_)
{'key': 'val_new'}
>>> source_dict_ = {'key': {'k1': 'v1_old', 'k2': 'v2'}}
>>> updates_ = {'key': {'k1': 'v1_new'}}
>>> source_dict_ = update_nested_dict(source_dict_, updates_)
>>> print(source_dict_)
{'key': {'k1': 'v1_new', 'k2': 'v2'}}
>>> source_dict_ = {'key': {'k1': {}, 'k2': 'v2'}}
>>> updates_ = {'key': {'k1': 'v1'}}
>>> source_dict_ = update_nested_dict(source_dict_, updates_)
>>> print(source_dict_)
{'key': {'k1': 'v1', 'k2': 'v2'}}
>>> source_dict_ = {'key': {'k1': 'v1', 'k2': 'v2'}}
>>> updates_ = {'key': {'k1': {}}}
>>> source_dict_ = update_nested_dict(source_dict_, updates_)
>>> print(source_dict_)
{'key': {'k1': 'v1', 'k2': 'v2'}}
"""
for key, val in updates.items():
if isinstance(val, collections.abc.Mapping) or isinstance(val, dict):
source_dict[key] = update_nested_dict(source_dict.get(key, {}), val)
elif isinstance(val, list):
source_dict[key] = (source_dict.get(key, []) + val)
else:
source_dict[key] = updates[key]
return source_dict
def get_all_values_from_nested_dict(key, target_dict):
"""
Get all values in a nested dictionary.
See also
[`GAVFND-1 <https://gist.github.com/douglasmiranda/5127251>`_] and
[`GAVFND-2 <https://stackoverflow.com/questions/9807634/>`_].
:param key: any that can be the key of a dictionary
:type key: any
:param target_dict: a (nested) dictionary
:type target_dict: dict
:return: all values of the ``key`` within the given ``target_dict``
:rtype: types.GeneratorType
**Examples**::
>>> from pyhelpers.ops import get_all_values_from_nested_dict
>>> key_ = 'key'
>>> target_dict_ = {'key': 'val'}
>>> val = get_all_values_from_nested_dict(key_, target_dict_)
>>> print(list(val))
[['val']]
>>> key_ = 'k1'
>>> target_dict_ = {'key': {'k1': 'v1', 'k2': 'v2'}}
>>> val = get_all_values_from_nested_dict(key_, target_dict_)
>>> print(list(val))
[['v1']]
>>> key_ = 'k1'
>>> target_dict_ = {'key': {'k1': ['v1', 'v1_1']}}
>>> val = get_all_values_from_nested_dict(key_, target_dict_)
>>> print(list(val))
[['v1', 'v1_1']]
>>> key_ = 'k2'
>>> target_dict_ = {'key': {'k1': 'v1', 'k2': ['v2', 'v2_1']}}
>>> val = get_all_values_from_nested_dict(key_, target_dict_)
>>> print(list(val))
[['v2', 'v2_1']]
"""
for k, v in target_dict.items():
if key == k:
yield [v] if isinstance(v, str) else v
elif isinstance(v, dict):
for x in get_all_values_from_nested_dict(key, v):
yield x
elif isinstance(v, collections.abc.Iterable):
for d in v:
if isinstance(d, dict):
for y in get_all_values_from_nested_dict(key, d):
yield y
def remove_multiple_keys_from_dict(target_dict, *keys):
"""
Remove multiple keys from a dictionary.
:param target_dict: a dictionary
:type target_dict: dict
:param keys: (a sequence of) any that can be the key of a dictionary
:type keys: any
**Example**::
>>> from pyhelpers.ops import remove_multiple_keys_from_dict
>>> target_dict_ = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4', 'k5': 'v5'}
>>> remove_multiple_keys_from_dict(target_dict_, 'k1', 'k3', 'k4')
>>> print(target_dict_)
{'k2': 'v2', 'k5': 'v5'}
"""
# assert isinstance(dictionary, dict)
for k in keys:
if k in target_dict.keys():
target_dict.pop(k)
def merge_dicts(*dicts):
"""
Merge multiple dictionaries.
:param dicts: (one or) multiple dictionaries
:type dicts: dict
:return: a single dictionary containing all elements of the input
:rtype: dict
**Example**::
>>> from pyhelpers.ops import merge_dicts
>>> dict_a = {'a': 1}
>>> dict_b = {'b': 2}
>>> dict_c = {'c': 3}
>>> merged_dict = merge_dicts(dict_a, dict_b, dict_c)
>>> print(merged_dict)
{'a': 1, 'b': 2, 'c': 3}
"""
super_dict = {}
for d in dicts:
super_dict.update(d)
return super_dict
# Tabular data
def detect_nan_for_str_column(data_frame, column_names=None):
"""
Detect if a str type column contains ``NaN`` when reading csv files.
:param data_frame: a data frame to be examined
:type data_frame: pandas.DataFrame
:param column_names: a sequence of column names, if ``None`` (default), all columns
:type column_names: None or collections.abc.Iterable
:return: position index of the column that contains ``NaN``
:rtype: types.GeneratorType
**Example**::
>>> import numpy as np_
>>> import pandas as pd_
>>> from pyhelpers.ops import detect_nan_for_str_column
>>> df = pd_.DataFrame(np_.resize(range(10), (10, 2)), columns=['a', 'b'])
>>> df.iloc[3, 1] = np.nan
>>> nan_col_pos = detect_nan_for_str_column(df, column_names=None)
>>> print(list(nan_col_pos))
[1]
"""
if column_names is None:
column_names = data_frame.columns
for x in column_names:
temp = [str(v) for v in data_frame[x].unique() if isinstance(v, str)
or np.isnan(v)]
if 'nan' in temp:
yield data_frame.columns.get_loc(x)
def create_rotation_matrix(theta):
"""
Create a rotation matrix (counterclockwise).
:param theta: rotation angle (in radian)
:type theta: int or float
:return: a rotation matrix of shape (2, 2)
:rtype: numpy.ndarray
**Example**::
>>> from pyhelpers.ops import create_rotation_matrix
>>> rot_mat = create_rotation_matrix(theta=30)
>>> print(rotation_mat)
# [[-0.98803162 0.15425145]
# [-0.15425145 -0.98803162]]
"""
sin_theta, cos_theta = np.sin(theta), np.cos(theta)
rotation_mat = np.array([[sin_theta, cos_theta], [-cos_theta, sin_theta]])
return rotation_mat
def dict_to_dataframe(input_dict, k='key', v='value'):
"""
Convert a dictionary to a data frame.
:param input_dict: a dictionary to be converted to a data frame
:type input_dict: dict
:param k: column name for keys
:type k: str
:param v: column name for values
:type v: str
:return: a data frame converted from the ``input_dict``
:rtype: pandas.DataFrame
**Example**::
>>> from pyhelpers.ops import dict_to_dataframe
>>> input_dict_ = {'a': 1, 'b': 2}
>>> df = dict_to_dataframe(input_dict_)
>>> print(df)
# key value
# 0 a 1
# 1 b 2
"""
dict_keys = list(input_dict.keys())
dict_vals = list(input_dict.values())
data_frame = pd.DataFrame({k: dict_keys, v: dict_vals})
return data_frame
def parse_csr_matrix(path_to_csr, verbose=False, **kwargs):
"""
Load in a compressed sparse row (CSR) or compressed row storage (CRS).
:param path_to_csr: path where a CSR (e.g. .npz) file is saved
:type path_to_csr: str
:param verbose: whether to print relevant information in console
as the function runs, defaults to ``False``
:type verbose: bool or int
:param kwargs: optional parameters of
`numpy.load <https://numpy.org/doc/stable/reference/generated/numpy.load>`_
:return: a compressed sparse row
:rtype: scipy.sparse.csr.csr_matrix
**Example**::
>>> import numpy as np_
>>> import scipy.sparse
>>> from pyhelpers.dir import cd
>>> from pyhelpers.ops import parse_csr_matrix
>>> data_ = np_.array([1, 2, 3, 4, 5, 6])
>>> indices_ = np_.array([0, 2, 2, 0, 1, 2])
>>> indptr_ = np_.array([0, 2, 3, 6])
>>> csr_m = scipy.sparse.csr_matrix((data_, indices_, indptr_), shape=(3, 3))
>>> path_to_csr_npz = cd("tests\\data", "csr_mat.npz")
>>> np_.savez_compressed(path_to_csr_npz, indptr=csr_m.indptr,
... indices=csr_m.indices, data=csr_m.data,
... shape=csr_m.shape)
>>> csr_mat_ = parse_csr_matrix(path_to_csr_npz, verbose=True)
Loading "\\tests\\data\\csr_mat.npz" ... Done.
>>> # .nnz gets the count of explicitly-stored values (non-zeros)
>>> print((csr_mat_ != csr_m).count_nonzero() == 0)
True
>>> print((csr_mat_ != csr_m).nnz == 0)
True
"""
if verbose:
print("Loading \"\\{}\"".format(os.path.relpath(path_to_csr)),
end=" ... ")
try:
csr_loader = np.load(path_to_csr, **kwargs)
data = csr_loader['data']
indices = csr_loader['indices']
indptr = csr_loader['indptr']
shape = csr_loader['shape']
import scipy.sparse
csr_mat = scipy.sparse.csr_matrix((data, indices, indptr), shape)
print("Done.") if verbose else ""
return csr_mat
except Exception as e:
print("Failed. {}".format(e)) if verbose else ""
""" Basic computation -------------------------------------------------------------- """
def get_extreme_outlier_bounds(num_dat, k=1.5):
"""
Get upper and lower bounds for extreme outliers.
:param num_dat: an array of numbers
:type num_dat: array-like
:param k: a scale coefficient associated with interquartile range, defaults to ``1.5``
:type k: float, int
:return: lower and upper bound
:rtype: tuple
**Example**::
>>> import pandas as pd_
>>> from pyhelpers.ops import get_extreme_outlier_bounds
>>> data = pd_.DataFrame(range(100), columns=['col'])
>>> lo_bound, up_bound = get_extreme_outlier_bounds(data, k=1.5)
>>> print((lo_bound, up_bound))
(0.0, 148.5)
"""
q1, q3 = np.percentile(num_dat, 25), np.percentile(num_dat, 75)
iqr = q3 - q1
lower_bound = np.max([0, q1 - k * iqr])
upper_bound = q3 + k * iqr
return lower_bound, upper_bound
def interquartile_range(num_dat):
"""
Calculate interquartile range.
This function may be an alternative to
`scipy.stats.iqr
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.iqr.html>`_.
:param num_dat: an array of numbers
:type num_dat: array-like
:return: interquartile range of ``num_dat``
:rtype: float
**Example**::
>>> import pandas as pd_
>>> from pyhelpers.ops import interquartile_range
>>> data = pd_.DataFrame(range(100), columns=['col'])
>>> iqr_ = interquartile_range(data)
>>> print(iqr_)
49.5
"""
iqr = np.subtract(*np.percentile(num_dat, [75, 25]))
return iqr
def find_closest_date(date, lookup_dates, as_datetime=False, fmt='%Y-%m-%d %H:%M:%S.%f'):
"""
Find the closest date of a given one from a list of dates.
:param date: a date
:type date: str or datetime.datetime
:param lookup_dates: an array of dates
:type lookup_dates: list or tuple or collections.abc.Iterable
:param as_datetime: whether to return a datetime.datetime-formatted date,
defaults to ``False``
:type as_datetime: bool
:param fmt: datetime format, defaults to ``'%Y-%m-%d %H:%M:%S.%f'``
:type fmt: str
:return: the date that is closest to the given ``date``
:rtype: str or datetime.datetime
**Examples**::
>>> import pandas as pd_
>>> from pyhelpers.ops import find_closest_date
>>> date_list = pd_.date_range('2019-01-02', '2019-12-31')
>>> date_ = '2019-01-01'
>>> closest_date_ = find_closest_date(date_, date_list, as_datetime=True)
>>> print(closest_date_) # Timestamp('2019-01-02 00:00:00', freq='D')
2019-01-02 00:00:00
>>> date_ = pd_.to_datetime('2019-01-01')
>>> closest_date_ = find_closest_date(date_, date_list, as_datetime=False)
>>> print(closest_date_) # '2019-01-02 00:00:00.000000'
2019-01-02 00:00:00.000000
"""
closest_date = min(lookup_dates,
key=lambda x: abs(pd.to_datetime(x) - pd.to_datetime(date)))
if as_datetime:
if isinstance(closest_date, str):
closest_date = pd.to_datetime(closest_date)
else:
if isinstance(closest_date, datetime.datetime):
closest_date = closest_date.strftime(fmt)
return closest_date
""" Graph plotting ----------------------------------------------------------------- """
def cmap_discretisation(cmap, n_colours):
"""
Create a discrete colour ramp.
See also [`CD-1 <http://sensitivecities.com/
so-youd-like-to-make-a-map-using-python-EN.html#.WbpP0T6GNQB>`_].
:param cmap: a colormap instance,
such as built-in `colormaps`_ accessible via `matplotlib.cm.get_cmap`_
:type cmap: matplotlib.colors.ListedColormap
:param n_colours: number of colours
:type n_colours: int
:return: a discrete colormap from (the continuous) ``cmap``
:rtype: matplotlib.colors.LinearSegmentedColormap
.. _`colormaps`: https://matplotlib.org/tutorials/colors/colormaps.html
.. _`matplotlib.cm.get_cmap`:
https://matplotlib.org/api/cm_api.html#matplotlib.cm.get_cmap
**Example**::
>>> import matplotlib.cm
>>> import matplotlib.pyplot as plt_
>>> import numpy as np_
>>> from pyhelpers.ops import cmap_discretisation
>>> cm_accent = cmap_discretisation(matplotlib.cm.get_cmap('Accent'), n_colours=5)
>>> fig, ax = plt_.subplots(figsize=(10, 2))
>>> ax.imshow(np_.resize(range(100), (5, 100)), cmap=cm_accent,
... interpolation='nearest')
>>> plt_.axis('off')
>>> plt_.tight_layout()
>>> plt_.show()
.. image:: ../_images/cmap-discretisation.*
:width: 400pt
"""
if isinstance(cmap, str):
import matplotlib.cm
cmap = matplotlib.cm.get_cmap(cmap)
colours_i = np.concatenate((np.linspace(0, 1., n_colours), (0., 0., 0., 0.)))
colours_rgba = cmap(colours_i)
indices = np.linspace(0, 1., n_colours + 1)
c_dict = {}
for ki, key in enumerate(('red', 'green', 'blue')):
c_dict[key] = [(indices[x], colours_rgba[x - 1, ki], colours_rgba[x, ki])
for x in range(n_colours + 1)]
import matplotlib.colors
colour_map = matplotlib.colors.LinearSegmentedColormap(
cmap.name + '_%d' % n_colours, c_dict, 1024)
return colour_map
def colour_bar_index(cmap, n_colours, labels=None, **kwargs):
"""
Create a colour bar.
To stop making off-by-one errors. Takes a standard colour ramp, and discretizes it,
then draws a colour bar with correctly aligned labels.
See also [`CBI-1 <http://sensitivecities.com/
so-youd-like-to-make-a-map-using-python-EN.html#.WbpP0T6GNQB>`_].
:param cmap: a colormap instance,
such as built-in `colormaps`_ accessible via `matplotlib.cm.get_cmap`_
:type cmap: matplotlib.colors.ListedColormap
:param n_colours: number of colours
:type n_colours: int
:param labels: a list of labels for the colour bar, defaults to ``None``
:type labels: list or None
:param kwargs: optional parameters of `matplotlib.pyplot.colorbar`_
:return: a colour bar object
:rtype: matplotlib.colorbar.Colorbar
.. _`colormaps`: https://matplotlib.org/tutorials/colors/colormaps.html
.. _`matplotlib.cm.get_cmap`:
https://matplotlib.org/api/cm_api.html#matplotlib.cm.get_cmap
.. _`matplotlib.pyplot.colorbar`:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html
**Examples**::
>>> import matplotlib.cm
>>> import matplotlib.pyplot as plt_
>>> from pyhelpers.ops import colour_bar_index
>>> plt_.figure(figsize=(2, 6))
>>> cbar = colour_bar_index(cmap=matplotlib.cm.get_cmap('Accent'), n_colours=5)
>>> cbar.ax.tick_params(labelsize=18)
>>> plt_.axis('off')
>>> plt_.tight_layout()
>>> plt_.show()
.. image:: ../_images/colour-bar-index-1.*
:width: 120pt
.. code-block:: python
>>> plt_.figure(figsize=(2, 6))
>>> cbar = colour_bar_index(matplotlib.cm.get_cmap('Accent'), n_colours=5,
... labels=list('abcde'))
>>> cbar.ax.tick_params(labelsize=18)
>>> plt_.axis('off')
>>> plt_.tight_layout()
>>> plt_.show()
.. image:: ../_images/colour-bar-index-2.*
:width: 120pt
"""
cmap = cmap_discretisation(cmap, n_colours)
import matplotlib.cm
mappable = matplotlib.cm.ScalarMappable(cmap=cmap)
mappable.set_array(np.array([]))
mappable.set_clim(-0.5, n_colours + 0.5)
import matplotlib.pyplot as plt
colour_bar = plt.colorbar(mappable, **kwargs)
colour_bar.set_ticks(np.linspace(0, n_colours, n_colours))
colour_bar.set_ticklabels(range(n_colours))
if labels:
colour_bar.set_ticklabels(labels)
return colour_bar
""" Web scraping ------------------------------------------------------------------- """
def is_network_connected():
"""
Check if the current machine can connect to the Internet.
:return: whether the Internet connection is currently working
:rtype: bool
**Examples**::
>>> from pyhelpers.ops import is_network_connected
>>> is_network_connected()
"""
host_name = socket.gethostname()
ip_address = socket.gethostbyname(host_name)
return False if ip_address == "127.0.0.1" else True
def is_url_connectable(url):
"""
Check if the current machine can connect to a given URL.
:param url: a URL
:type url: str
:return: whether the machine can currently connect to the given URL
:rtype: bool
**Examples**::
>>> from pyhelpers.ops import is_url_connectable
>>> url_0 = 'https://www.python.org/'
>>> is_url_connectable(url_0)
True
>>> url_1 = 'https://www.python.org1/'
>>> is_url_connectable(url_1)
False
"""
try:
netloc = urllib.parse.urlparse(url).netloc
host = socket.gethostbyname(netloc)
s = socket.create_connection((host, 80))
s.close()
return True
except (socket.gaierror, OSError):
return False
def fake_requests_headers(random=False):
"""
Make a fake HTTP headers for
`requests.get <https://requests.readthedocs.io/en/master/user/
advanced/#request-and-response-objects>`_.
:param random: whether to go for a random agent, defaults to ``False``
:type random: bool
:return: fake HTTP headers
:rtype: dict
**Examples**::
>>> from pyhelpers.ops import fake_requests_headers
>>> fake_headers_ = fake_requests_headers()
>>> print(fake_headers_)
{'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'}
>>> fake_headers_ = fake_requests_headers(random=True)
>>> print(fake_headers_)
{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36'}
.. note::
The above ``fake_headers_`` may be different every time we run the examples.
"""
import fake_useragent
try:
fake_user_agent = fake_useragent.UserAgent(verify_ssl=False)
ua = fake_user_agent.random if random else fake_user_agent['google chrome']
except fake_useragent.FakeUserAgentError:
fake_user_agent = fake_useragent.UserAgent(verify_ssl=False)
if random:
import random
ua = random.choice(fake_user_agent.data_browsers['internetexplorer'])
else:
ua = fake_user_agent['Internet Explorer']
fake_headers = {'User-Agent': ua}
return fake_headers
def download_file_from_url(url, path_to_file, wait_to_retry=3600, random_header=False,
**kwargs):
"""
Download an object available at a given URL.
See also [`DFFU-1 <https://stackoverflow.com/questions/37573483/>`_].
:param url: a URL
:type url: str
:param path_to_file: a full path to which the downloaded object is saved as
:type path_to_file: str
:param wait_to_retry: a wait time to retry downloading,
defaults to ``3600`` (in second)
:type wait_to_retry: int or float
:param random_header: whether to go for a random agent, defaults to ``False``
:type random_header: bool
:param kwargs: optional parameters of
`open <https://docs.python.org/3/library/functions.html#open>`_
**Example**::
>>> from pyhelpers.dir import cd
>>> from pyhelpers.ops import download_file_from_url
>>> url_to_python_logo = 'https://www.python.org/static/community_logos/' \
... 'python-logo-master-v3-TM.png'
>>> img_dir = cd("tests\\images")
>>> path_to_python_logo_png = cd(img_dir, "python-logo.png")
>>> download_file_from_url(url_to_python_logo, path_to_python_logo_png)
"""
import requests
headers = fake_requests_headers(random_header)
# Streaming, so we can iterate over the response
resp = requests.get(url, stream=True, headers=headers)
if resp.status_code == 429:
time.sleep(wait_to_retry)
total_size = int(resp.headers.get('content-length')) # Total size in bytes
block_size = 1024 * 1024
wrote = 0
directory = os.path.dirname(path_to_file)
if directory == "":
path_to_file = os.path.join(os.getcwd(), path_to_file)
else:
if not os.path.exists(directory):
os.makedirs(directory)
import tqdm
with open(path_to_file, mode='wb', **kwargs) as f:
for data in tqdm.tqdm(resp.iter_content(block_size, decode_unicode=True),
total=total_size // block_size, unit='MB'):
wrote = wrote + len(data)
try:
f.write(data)
except TypeError:
f.write(data.encode())
f.close()
resp.close()
if total_size != 0 and wrote != total_size:
print("ERROR, something went wrong!")