FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/lib/matplotlib/cbook.py at v3.11.2 · matplotlib/matplotlib · GitHub
matplotlib
/
matplotlib
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
8.5k
Star
23.3k
Code
Issues
1.1k
Pull requests
429
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
cbook.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
2552 lines (2128 loc) · 83.5 KB
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
cbook.py
Copy path
File metadata and controls
2552 lines (2128 loc) · 83.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
A collection of utility functions and classes. Originally, many
(but not all) were from the Python Cookbook -- hence the name cbook.
"""
import
collections
import
collections
.
abc
import
contextlib
import
functools
import
gzip
import
itertools
import
math
import
operator
import
os
from
pathlib
import
Path
import
shlex
import
subprocess
import
sys
import
time
import
traceback
import
types
import
weakref
import
numpy
as
np
try
:
from
numpy
.
exceptions
import
VisibleDeprecationWarning
# numpy >= 1.25
except
ImportError
:
from
numpy
import
VisibleDeprecationWarning
import
matplotlib
from
matplotlib
import
_api
,
_c_internal_utils
,
mlab
class
_ExceptionInfo
:
"""
A class to carry exception information around.
This is used to store and later raise exceptions. It's an alternative to
directly storing Exception instances that circumvents traceback-related
issues: caching tracebacks can keep user's objects in local namespaces
alive indefinitely, which can lead to very surprising memory issues for
users and result in incorrect tracebacks.
"""
def
__init__
(
self
,
cls
,
*
args
,
notes
=
None
):
self
.
_cls
=
cls
self
.
_args
=
args
self
.
_notes
=
notes
if
notes
is
not
None
else
[]
@
classmethod
def
from_exception
(
cls
,
exc
):
return
cls
(
type
(
exc
),
*
exc
.
args
,
notes
=
getattr
(
exc
,
"__notes__"
, []))
def
to_exception
(
self
):
exc
=
self
.
_cls
(
*
self
.
_args
)
for
note
in
self
.
_notes
:
exc
.
add_note
(
note
)
return
exc
def
_get_running_interactive_framework
():
"""
Return the interactive framework whose event loop is currently running, if
any, or "headless" if no event loop can be started, or None.
Returns
-------
Optional[str]
One of the following values: "qt", "gtk3", "gtk4", "wx", "tk",
"macosx", "headless", ``None``.
"""
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
# entries can also have been explicitly set to None.
QtWidgets
=
(
sys
.
modules
.
get
(
"PyQt6.QtWidgets"
)
or
sys
.
modules
.
get
(
"PySide6.QtWidgets"
)
or
sys
.
modules
.
get
(
"PyQt5.QtWidgets"
)
or
sys
.
modules
.
get
(
"PySide2.QtWidgets"
)
)
if
QtWidgets
and
QtWidgets
.
QApplication
.
instance
():
return
"qt"
Gtk
=
sys
.
modules
.
get
(
"gi.repository.Gtk"
)
if
Gtk
:
if
Gtk
.
MAJOR_VERSION
==
4
:
from
gi
.
repository
import
GLib
if
GLib
.
main_depth
():
return
"gtk4"
if
Gtk
.
MAJOR_VERSION
==
3
and
Gtk
.
main_level
():
return
"gtk3"
wx
=
sys
.
modules
.
get
(
"wx"
)
if
wx
and
wx
.
GetApp
():
return
"wx"
tkinter
=
sys
.
modules
.
get
(
"tkinter"
)
if
tkinter
:
codes
=
{
tkinter
.
mainloop
.
__code__
,
tkinter
.
Misc
.
mainloop
.
__code__
}
for
frame
in
sys
.
_current_frames
().
values
():
while
frame
:
if
frame
.
f_code
in
codes
:
return
"tk"
frame
=
frame
.
f_back
# Preemptively break reference cycle between locals and the frame.
del
frame
macosx
=
sys
.
modules
.
get
(
"matplotlib.backends._macosx"
)
if
macosx
and
macosx
.
event_loop_is_running
():
return
"macosx"
if
not
_c_internal_utils
.
display_is_valid
():
return
"headless"
return
None
def
_exception_printer
(
exc
):
if
_get_running_interactive_framework
()
in
[
"headless"
,
None
]:
raise
exc
else
:
traceback
.
print_exc
()
class
_StrongRef
:
"""
Wrapper similar to a weakref, but keeping a strong reference to the object.
"""
def
__init__
(
self
,
obj
):
self
.
_obj
=
obj
def
__call__
(
self
):
return
self
.
_obj
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
_StrongRef
)
and
self
.
_obj
==
other
.
_obj
def
__hash__
(
self
):
return
hash
(
self
.
_obj
)
def
_weak_or_strong_ref
(
func
,
callback
):
"""
Return a `WeakMethod` wrapping *func* if possible, else a `_StrongRef`.
"""
try
:
return
weakref
.
WeakMethod
(
func
,
callback
)
except
TypeError
:
return
_StrongRef
(
func
)
class
_UnhashDict
:
"""
A minimal dict-like class that also supports unhashable keys, storing them
in a list of key-value pairs.
This class only implements the interface needed for `CallbackRegistry`, and
tries to minimize the overhead for the hashable case.
"""
def
__init__
(
self
,
pairs
):
self
.
_dict
=
{}
self
.
_pairs
=
[]
for
k
,
v
in
pairs
:
self
[
k
]
=
v
def
__setitem__
(
self
,
key
,
value
):
try
:
self
.
_dict
[
key
]
=
value
except
TypeError
:
for
i
, (
k
,
v
)
in
enumerate
(
self
.
_pairs
):
if
k
==
key
:
self
.
_pairs
[
i
]
=
(
key
,
value
)
break
else
:
self
.
_pairs
.
append
((
key
,
value
))
def
__getitem__
(
self
,
key
):
try
:
return
self
.
_dict
[
key
]
except
TypeError
:
pass
for
k
,
v
in
self
.
_pairs
:
if
k
==
key
:
return
v
raise
KeyError
(
key
)
def
pop
(
self
,
key
,
*
args
):
try
:
if
key
in
self
.
_dict
:
return
self
.
_dict
.
pop
(
key
)
except
TypeError
:
for
i
, (
k
,
v
)
in
enumerate
(
self
.
_pairs
):
if
k
==
key
:
del
self
.
_pairs
[
i
]
return
v
if
args
:
return
args
[
0
]
raise
KeyError
(
key
)
def
__iter__
(
self
):
yield
from
self
.
_dict
for
k
,
v
in
self
.
_pairs
:
yield
k
class
CallbackRegistry
:
"""
Handle registering, processing, blocking, and disconnecting
for a set of signals and callbacks:
>>> def oneat(x):
... print('eat', x)
>>> def ondrink(x):
... print('drink', x)
>>> from matplotlib.cbook import CallbackRegistry
>>> callbacks = CallbackRegistry()
>>> id_eat = callbacks.connect('eat', oneat)
>>> id_drink = callbacks.connect('drink', ondrink)
>>> callbacks.process('drink', 123)
drink 123
>>> callbacks.process('eat', 456)
eat 456
>>> callbacks.process('be merry', 456) # nothing will be called
>>> callbacks.disconnect(id_eat)
>>> callbacks.process('eat', 456) # nothing will be called
>>> with callbacks.blocked(signal='drink'):
... callbacks.process('drink', 123) # nothing will be called
>>> callbacks.process('drink', 123)
drink 123
>>> callbacks.disconnect(ondrink, signal='drink') # disconnect by func
>>> callbacks.process('drink', 123) # nothing will be called
In practice, one should always disconnect all callbacks when they are
no longer needed to avoid dangling references (and thus memory leaks).
However, real code in Matplotlib rarely does so, and due to its design,
it is rather difficult to place this kind of code. To get around this,
and prevent this class of memory leaks, we instead store weak references
to bound methods only, so when the destination object needs to die, the
CallbackRegistry won't keep it alive.
Parameters
----------
exception_handler : callable, optional
If not None, *exception_handler* must be a function that takes an
`Exception` as single parameter. It gets called with any `Exception`
raised by the callbacks during `CallbackRegistry.process`, and may
either re-raise the exception or handle it in another manner.
The default handler prints the exception (with `traceback.print_exc`) if
an interactive event loop is running; it re-raises the exception if no
interactive event loop is running.
signals : list, optional
If not None, *signals* is a list of signals that this registry handles:
attempting to `process` or to `connect` to a signal not in the list
throws a `ValueError`. The default, None, does not restrict the
handled signals.
"""
# We maintain two mappings:
# callbacks: signal -> {cid -> weakref-to-callback}
# _func_cid_map: {(signal, weakref-to-callback) -> cid}
def
__init__
(
self
,
exception_handler
=
_exception_printer
,
*
,
signals
=
None
):
self
.
_signals
=
None
if
signals
is
None
else
list
(
signals
)
# Copy it.
self
.
exception_handler
=
exception_handler
self
.
callbacks
=
{}
self
.
_cid_gen
=
itertools
.
count
()
self
.
_func_cid_map
=
_UnhashDict
([])
# A hidden variable that marks cids that need to be pickled.
self
.
_pickled_cids
=
set
()
def
__getstate__
(
self
):
return
{
**
vars
(
self
),
# In general, callbacks may not be pickled, so we just drop them,
# unless directed otherwise by self._pickled_cids.
"callbacks"
: {
s
: {
cid
:
proxy
()
for
cid
,
proxy
in
d
.
items
()
if
cid
in
self
.
_pickled_cids
}
for
s
,
d
in
self
.
callbacks
.
items
()},
# It is simpler to reconstruct this from callbacks in __setstate__.
"_func_cid_map"
:
None
,
"_cid_gen"
:
next
(
self
.
_cid_gen
)
}
def
__setstate__
(
self
,
state
):
cid_count
=
state
.
pop
(
'_cid_gen'
)
vars
(
self
).
update
(
state
)
self
.
callbacks
=
{
s
: {
cid
:
_weak_or_strong_ref
(
func
,
functools
.
partial
(
self
.
_remove_proxy
,
s
))
for
cid
,
func
in
d
.
items
()}
for
s
,
d
in
self
.
callbacks
.
items
()}
self
.
_func_cid_map
=
_UnhashDict
(
((
s
,
proxy
),
cid
)
for
s
,
d
in
self
.
callbacks
.
items
()
for
cid
,
proxy
in
d
.
items
())
self
.
_cid_gen
=
itertools
.
count
(
cid_count
)
def
connect
(
self
,
signal
,
func
):
"""Register *func* to be called when signal *signal* is generated."""
if
self
.
_signals
is
not
None
:
_api
.
check_in_list
(
self
.
_signals
,
signal
=
signal
)
proxy
=
_weak_or_strong_ref
(
func
,
functools
.
partial
(
self
.
_remove_proxy
,
signal
))
try
:
return
self
.
_func_cid_map
[
signal
,
proxy
]
except
KeyError
:
cid
=
self
.
_func_cid_map
[
signal
,
proxy
]
=
next
(
self
.
_cid_gen
)
self
.
callbacks
.
setdefault
(
signal
, {})[
cid
]
=
proxy
return
cid
def
_connect_picklable
(
self
,
signal
,
func
):
"""
Like `.connect`, but the callback is kept when pickling/unpickling.
Currently internal-use only.
"""
cid
=
self
.
connect
(
signal
,
func
)
self
.
_pickled_cids
.
add
(
cid
)
return
cid
# Keep a reference to sys.is_finalizing, as sys may have been cleared out
# at that point.
def
_remove_proxy
(
self
,
signal
,
proxy
,
*
,
_is_finalizing
=
sys
.
is_finalizing
):
if
_is_finalizing
():
# Weakrefs can't be properly torn down at that point anymore.
return
cid
=
self
.
_func_cid_map
.
pop
((
signal
,
proxy
),
None
)
if
cid
is
not
None
:
del
self
.
callbacks
[
signal
][
cid
]
self
.
_pickled_cids
.
discard
(
cid
)
else
:
# Not found
return
if
len
(
self
.
callbacks
[
signal
])
==
0
:
# Clean up empty dicts
del
self
.
callbacks
[
signal
]
@
_api
.
rename_parameter
(
"3.11"
,
"cid"
,
"cid_or_func"
)
def
disconnect
(
self
,
cid_or_func
,
*
,
signal
=
None
):
"""
Disconnect a callback.
Parameters
----------
cid_or_func : int or callable
If an int, disconnect the callback with that connection id.
If a callable, disconnect that function from signals.
signal : optional
Only used when *cid_or_func* is a callable. If given, disconnect
the function only from that specific signal. If not given,
disconnect from all signals the function is connected to.
Notes
-----
No error is raised if such a callback does not exist.
"""
if
isinstance
(
cid_or_func
,
int
):
if
signal
is
not
None
:
raise
ValueError
(
"signal cannot be specified when disconnecting by cid"
)
for
sig
,
proxy
in
self
.
_func_cid_map
:
if
self
.
_func_cid_map
[
sig
,
proxy
]
==
cid_or_func
:
break
else
:
# Not found
return
self
.
_remove_proxy
(
sig
,
proxy
)
elif
signal
is
not
None
:
# Disconnect from a specific signal
proxy
=
_weak_or_strong_ref
(
cid_or_func
,
None
)
self
.
_remove_proxy
(
signal
,
proxy
)
else
:
# Disconnect from all signals
proxy
=
_weak_or_strong_ref
(
cid_or_func
,
None
)
for
sig
,
prx
in
list
(
self
.
_func_cid_map
):
if
prx
==
proxy
:
self
.
_remove_proxy
(
sig
,
proxy
)
def
process
(
self
,
s
,
*
args
,
**
kwargs
):
"""
Process signal *s*.
All of the functions registered to receive callbacks on *s* will be
called with ``*args`` and ``**kwargs``.
"""
if
self
.
_signals
is
not
None
:
_api
.
check_in_list
(
self
.
_signals
,
signal
=
s
)
for
ref
in
list
(
self
.
callbacks
.
get
(
s
, {}).
values
()):
func
=
ref
()
if
func
is
not
None
:
try
:
func
(
*
args
,
**
kwargs
)
# this does not capture KeyboardInterrupt, SystemExit,
# and GeneratorExit
except
Exception
as
exc
:
if
self
.
exception_handler
is
not
None
:
self
.
exception_handler
(
exc
)
else
:
raise
@
contextlib
.
contextmanager
def
blocked
(
self
,
*
,
signal
=
None
):
"""
Block callback signals from being processed.
A context manager to temporarily block/disable callback signals
from being processed by the registered listeners.
Parameters
----------
signal : str, optional
The callback signal to block. The default is to block all signals.
"""
orig
=
self
.
callbacks
try
:
if
signal
is
None
:
# Empty out the callbacks
self
.
callbacks
=
{}
else
:
# Only remove the specific signal
self
.
callbacks
=
{
k
:
orig
[
k
]
for
k
in
orig
if
k
!=
signal
}
yield
finally
:
self
.
callbacks
=
orig
class
silent_list
(
list
):
"""
A list with a short ``repr()``.
This is meant to be used for a homogeneous list of artists, so that they
don't cause long, meaningless output.
Instead of ::
[<matplotlib.lines.Line2D object at 0x7f5749fed3c8>,
<matplotlib.lines.Line2D object at 0x7f5749fed4e0>,
<matplotlib.lines.Line2D object at 0x7f5758016550>]
one will get ::
<a list of 3 Line2D objects>
If ``self.type`` is None, the type name is obtained from the first item in
the list (if any).
"""
def
__init__
(
self
,
type
,
seq
=
None
):
self
.
type
=
type
if
seq
is
not
None
:
self
.
extend
(
seq
)
def
__repr__
(
self
):
if
self
.
type
is
not
None
or
len
(
self
)
!=
0
:
tp
=
self
.
type
if
self
.
type
is
not
None
else
type
(
self
[
0
]).
__name__
return
f"<a list of
{
len
(
self
)
}
{
tp
}
objects>"
else
:
return
"<an empty list>"
def
_local_over_kwdict
(
local_var
,
kwargs
,
*
keys
,
warning_cls
=
_api
.
MatplotlibDeprecationWarning
):
out
=
local_var
for
key
in
keys
:
kwarg_val
=
kwargs
.
pop
(
key
,
None
)
if
kwarg_val
is
not
None
:
if
out
is
None
:
out
=
kwarg_val
else
:
_api
.
warn_external
(
f'"
{
key
}
" keyword argument will be ignored'
,
warning_cls
)
return
out
def
strip_math
(
s
):
"""
Remove latex formatting from mathtext.
Only handles fully math and fully non-math strings.
"""
if
len
(
s
)
>=
2
and
s
[
0
]
==
s
[
-
1
]
==
"$"
:
s
=
s
[
1
:
-
1
]
for
tex
,
plain
in
[
(
r"\times"
,
"x"
),
# Specifically for Formatter support.
(
r"\mathdefault"
,
""
),
(
r"\rm"
,
""
),
(
r"\cal"
,
""
),
(
r"\tt"
,
""
),
(
r"\it"
,
""
),
(
"
\\
"
,
""
),
(
"{"
,
""
),
(
"}"
,
""
),
]:
s
=
s
.
replace
(
tex
,
plain
)
return
s
def
_strip_comment
(
s
):
"""Strip everything from the first unquoted #."""
pos
=
0
while
True
:
quote_pos
=
s
.
find
(
'"'
,
pos
)
hash_pos
=
s
.
find
(
'#'
,
pos
)
if
quote_pos
<
0
:
without_comment
=
s
if
hash_pos
<
0
else
s
[:
hash_pos
]
return
without_comment
.
strip
()
elif
0
<=
hash_pos
<
quote_pos
:
return
s
[:
hash_pos
].
strip
()
else
:
closing_quote_pos
=
s
.
find
(
'"'
,
quote_pos
+
1
)
if
closing_quote_pos
<
0
:
raise
ValueError
(
f"Missing closing quote in:
{
s
!r
}
. If you need a double-"
'quote inside a string, use escaping: e.g. "the
\"
char"'
)
pos
=
closing_quote_pos
+
1
# behind closing quote
def
is_writable_file_like
(
obj
):
"""Return whether *obj* looks like a file object with a *write* method."""
return
callable
(
getattr
(
obj
,
'write'
,
None
))
def
file_requires_unicode
(
x
):
"""
Return whether the given writable file-like object requires Unicode to be
written to it.
"""
try
:
x
.
write
(
b''
)
except
TypeError
:
return
True
else
:
return
False
def
to_filehandle
(
fname
,
flag
=
'r'
,
return_opened
=
False
,
encoding
=
None
):
"""
Convert a path to an open file handle or pass-through a file-like object.
Consider using `open_file_cm` instead, as it allows one to properly close
newly created file objects more easily.
Parameters
----------
fname : str or path-like or file-like
If `str` or `os.PathLike`, the file is opened using the flags specified
by *flag* and *encoding*. If a file-like object, it is passed through.
flag : str, default: 'r'
Passed as the *mode* argument to `open` when *fname* is `str` or
`os.PathLike`; ignored if *fname* is file-like.
return_opened : bool, default: False
If True, return both the file object and a boolean indicating whether
this was a new file (that the caller needs to close). If False, return
only the new file.
encoding : str or None, default: None
Passed as the *mode* argument to `open` when *fname* is `str` or
`os.PathLike`; ignored if *fname* is file-like.
Returns
-------
fh : file-like
opened : bool
*opened* is only returned if *return_opened* is True.
"""
if
isinstance
(
fname
,
os
.
PathLike
):
fname
=
os
.
fspath
(
fname
)
if
isinstance
(
fname
,
str
):
if
fname
.
endswith
(
'.gz'
):
fh
=
gzip
.
open
(
fname
,
flag
)
elif
fname
.
endswith
(
'.bz2'
):
# python may not be compiled with bz2 support,
# bury import until we need it
import
bz2
fh
=
bz2
.
BZ2File
(
fname
,
flag
)
else
:
fh
=
open
(
fname
,
flag
,
encoding
=
encoding
)
opened
=
True
elif
hasattr
(
fname
,
'seek'
):
fh
=
fname
opened
=
False
else
:
raise
ValueError
(
'fname must be a PathLike or file handle'
)
if
return_opened
:
return
fh
,
opened
return
fh
def
open_file_cm
(
path_or_file
,
mode
=
"r"
,
encoding
=
None
):
r"""Pass through file objects and context-manage path-likes."""
fh
,
opened
=
to_filehandle
(
path_or_file
,
mode
,
True
,
encoding
)
return
fh
if
opened
else
contextlib
.
nullcontext
(
fh
)
def
is_scalar_or_string
(
val
):
"""Return whether the given object is a scalar or string like."""
return
isinstance
(
val
,
str
)
or
not
np
.
iterable
(
val
)
def
get_sample_data
(
fname
,
asfileobj
=
True
):
"""
Return a sample data file. *fname* is a path relative to the
:file:`mpl-data/sample_data` directory. If *asfileobj* is `True`
return a file object, otherwise just a file path.
Sample data files are stored in the 'mpl-data/sample_data' directory within
the Matplotlib package.
If the filename ends in .gz, the file is implicitly ungzipped. If the
filename ends with .npy or .npz, and *asfileobj* is `True`, the file is
loaded with `numpy.load`.
"""
path
=
_get_data_path
(
'sample_data'
,
fname
)
if
asfileobj
:
suffix
=
path
.
suffix
.
lower
()
if
suffix
==
'.gz'
:
return
gzip
.
open
(
path
)
elif
suffix
in
[
'.npy'
,
'.npz'
]:
return
np
.
load
(
path
)
elif
suffix
in
[
'.csv'
,
'.xrc'
,
'.txt'
]:
return
path
.
open
(
'r'
)
else
:
return
path
.
open
(
'rb'
)
else
:
return
str
(
path
)
def
_get_data_path
(
*
args
):
"""
Return the `pathlib.Path` to a resource file provided by Matplotlib.
``*args`` specify a path relative to the base data path.
"""
return
Path
(
matplotlib
.
get_data_path
(),
*
args
)
def
flatten
(
seq
,
scalarp
=
is_scalar_or_string
):
"""
Return a generator of flattened nested containers.
For example:
>>> from matplotlib.cbook import flatten
>>> l = (('John', ['Hunter']), (1, 23), [[([42, (5, 23)], )]])
>>> print(list(flatten(l)))
['John', 'Hunter', 1, 23, 42, 5, 23]
By: Composite of Holger Krekel and Luther Blissett
From: https://code.activestate.com/recipes/121294-simple-generator-for-flattening-nested-containers/
and Recipe 1.12 in cookbook
"""
# noqa: E501
for
item
in
seq
:
if
scalarp
(
item
)
or
item
is
None
:
yield
item
else
:
yield
from
flatten
(
item
,
scalarp
)
class
_Stack
:
"""
Stack of elements with a movable cursor.
Mimics home/back/forward in a web browser.
"""
def
__init__
(
self
):
self
.
_pos
=
-
1
self
.
_elements
=
[]
def
clear
(
self
):
"""Empty the stack."""
self
.
_pos
=
-
1
self
.
_elements
=
[]
def
__call__
(
self
):
"""Return the current element, or None."""
return
self
.
_elements
[
self
.
_pos
]
if
self
.
_elements
else
None
def
__len__
(
self
):
return
len
(
self
.
_elements
)
def
__getitem__
(
self
,
ind
):
return
self
.
_elements
[
ind
]
def
forward
(
self
):
"""Move the position forward and return the current element."""
self
.
_pos
=
min
(
self
.
_pos
+
1
,
len
(
self
.
_elements
)
-
1
)
return
self
()
def
back
(
self
):
"""Move the position back and return the current element."""
self
.
_pos
=
max
(
self
.
_pos
-
1
,
0
)
return
self
()
def
push
(
self
,
o
):
"""
Push *o* to the stack after the current position, and return *o*.
Discard all later elements.
"""
self
.
_elements
[
self
.
_pos
+
1
:]
=
[
o
]
self
.
_pos
=
len
(
self
.
_elements
)
-
1
return
o
def
home
(
self
):
"""
Push the first element onto the top of the stack.
The first element is returned.
"""
return
self
.
push
(
self
.
_elements
[
0
])
if
self
.
_elements
else
None
def
safe_masked_invalid
(
x
,
copy
=
False
):
x
=
np
.
array
(
x
,
subok
=
True
,
copy
=
copy
)
if
not
x
.
dtype
.
isnative
:
# If we have already made a copy, do the byteswap in place, else make a
# copy with the byte order swapped.
# Swap to native order.
x
=
x
.
byteswap
(
inplace
=
copy
).
view
(
x
.
dtype
.
newbyteorder
(
'N'
))
try
:
xm
=
np
.
ma
.
masked_where
(
~
(
np
.
isfinite
(
x
)),
x
,
copy
=
False
)
except
TypeError
:
if
len
(
x
.
dtype
.
descr
)
==
1
:
# Arrays with dtype 'object' get returned here.
# For example the 'c' kwarg of scatter, which supports multiple types.
# `plt.scatter([3, 4], [2, 5], c=[(1, 0, 0), 'y'])`
return
x
else
:
# In case of a dtype with multiple fields
# for example image data using a MultiNorm
try
:
mask
=
np
.
empty
(
x
.
shape
,
dtype
=
np
.
dtype
(
'bool, '
*
len
(
x
.
dtype
.
descr
)))
for
dd
,
dm
in
zip
(
x
.
dtype
.
descr
,
mask
.
dtype
.
descr
):
mask
[
dm
[
0
]]
=
~
np
.
isfinite
(
x
[
dd
[
0
]])
xm
=
np
.
ma
.
array
(
x
,
mask
=
mask
,
copy
=
False
)
except
TypeError
:
return
x
return
xm
def
print_cycles
(
objects
,
outstream
=
sys
.
stdout
,
show_progress
=
False
):
"""
Print loops of cyclic references in the given *objects*.
It is often useful to pass in ``gc.garbage`` to find the cycles that are
preventing some objects from being garbage collected.
Parameters
----------
objects
A list of objects to find cycles in.
outstream
The stream for output.
show_progress : bool
If True, print the number of objects reached as they are found.
"""
import
gc
def
print_path
(
path
):
for
i
,
step
in
enumerate
(
path
):
# next "wraps around"
next
=
path
[(
i
+
1
)
%
len
(
path
)]
outstream
.
write
(
" %s -- "
%
type
(
step
))
if
isinstance
(
step
,
dict
):
for
key
,
val
in
step
.
items
():
if
val
is
next
:
outstream
.
write
(
f"[
{
key
!r
}
]"
)
break
if
key
is
next
:
outstream
.
write
(
f"[key] =
{
val
!r
}
"
)
break
elif
isinstance
(
step
,
list
):
outstream
.
write
(
"[%d]"
%
step
.
index
(
next
))
elif
isinstance
(
step
,
tuple
):
outstream
.
write
(
"( tuple )"
)
else
:
outstream
.
write
(
repr
(
step
))
outstream
.
write
(
" ->
\n
"
)
outstream
.
write
(
"
\n
"
)
def
recurse
(
obj
,
start
,
all
,
current_path
):
if
show_progress
:
outstream
.
write
(
"%d
\r
"
%
len
(
all
))
all
[
id
(
obj
)]
=
None
referents
=
gc
.
get_referents
(
obj
)
for
referent
in
referents
:
# If we've found our way back to the start, this is
# a cycle, so print it out
if
referent
is
start
:
print_path
(
current_path
)
# Don't go back through the original list of objects, or
# through temporary references to the object, since those
# are just an artifact of the cycle detector itself.
elif
referent
is
objects
or
isinstance
(
referent
,
types
.
FrameType
):
continue
# We haven't seen this object before, so recurse
elif
id
(
referent
)
not
in
all
:
recurse
(
referent
,
start
,
all
,
current_path
+
[
obj
])
for
obj
in
objects
:
outstream
.
write
(
f"Examining:
{
obj
!r
}
\n
"
)
recurse
(
obj
,
obj
, {}, [])
class
Grouper
:
"""
A disjoint-set data structure.
Objects can be joined using :meth:`join`, tested for connectedness
using :meth:`joined`, and all disjoint sets can be retrieved by
using the object as an iterator.
The objects being joined must be hashable and weak-referenceable.
Examples
--------
>>> from matplotlib.cbook import Grouper
>>> class Foo:
... def __init__(self, s):
... self.s = s
... def __repr__(self):
... return self.s
...
>>> a, b, c, d, e, f = [Foo(x) for x in 'abcdef']
>>> grp = Grouper()
>>> grp.join(a, b)
>>> grp.join(b, c)
>>> grp.join(d, e)
>>> list(grp)
[[a, b, c], [d, e]]
>>> grp.joined(a, b)
True
>>> grp.joined(a, c)
True
>>> grp.joined(a, d)
False
"""
def
__init__
(
self
,
init
=
()):
self
.
_mapping
=
weakref
.
WeakKeyDictionary
(
{
x
:
weakref
.
WeakSet
([
x
])
for
x
in
init
})
self
.
_ordering
=
weakref
.
WeakKeyDictionary
()
for
x
in
init
:
if
x
not
in
self
.
_ordering
:
self
.
_ordering
[
x
]
=
len
(
self
.
_ordering
)
self
.
_next_order
=
len
(
self
.
_ordering
)
# Plain int to simplify pickling.
def
__getstate__
(
self
):
return
{
**
vars
(
self
),
# Convert weak refs to strong ones.
"_mapping"
: {
k
:
set
(
v
)
for
k
,
v
in
self
.
_mapping
.
items
()},
"_ordering"
: {
**
self
.
_ordering
},
}
def
__setstate__
(
self
,
state
):
vars
(
self
).
update
(
state
)
# Convert strong refs to weak ones.
self
.
_mapping
=
weakref
.
WeakKeyDictionary
(
{
k
:
weakref
.
WeakSet
(
v
)
for
k
,
v
in
self
.
_mapping
.
items
()})
self
.
_ordering
=
weakref
.
WeakKeyDictionary
(
self
.
_ordering
)
def
__contains__
(
self
,
item
):
return
item
in
self
.
_mapping
def
join
(
self
,
a
,
*
args
):
"""
Join given arguments into the same set. Accepts one or more arguments.
"""
mapping
=
self
.
_mapping
try
:
set_a
=
mapping
[
a
]
except
KeyError
:
set_a
=
mapping
[
a
]
=
weakref
.
WeakSet
([
a
])
self
.
_ordering
[
a
]
=
self
.
_next_order
self
.
_next_order
+=
1
for
arg
in
args
:
try
:
set_b
=
mapping
[
arg
]
except
KeyError
:
set_b
=
mapping
[
arg
]
=
weakref
.
WeakSet
([
arg
])
self
.
_ordering
[
arg
]
=
self
.
_next_order
self
.
_next_order
+=
1
if
set_b
is
not
set_a
:
if
len
(
set_b
)
>
len
(
set_a
):
set_a
,
set_b
=
set_b
,
set_a
set_a
.
update
(
set_b
)
for
elem
in
set_b
:
mapping
[
elem
]
=
set_a
def
joined
(
self
,
a
,
b
):
"""Return whether *a* and *b* are members of the same set."""
return
(
self
.
_mapping
.
get
(
a
,
object
())
is
self
.
_mapping
.
get
(
b
))
def
remove
(
self
,
a
):
"""Remove *a* from the grouper, doing nothing if it is not there."""
self
.
_mapping
.
pop
(
a
, {
a
}).
remove
(
a
)
self
.
_ordering
.
pop
(
a
,
None
)
def
__iter__
(
self
):
"""
Iterate over each of the disjoint sets as a list.
The iterator is invalid if interleaved with calls to join().
"""
unique_groups
=
{
id
(
group
):
group
for
group
in
self
.
_mapping
.
values
()}
for
group
in
unique_groups
.
values
():
yield
sorted
(
group
,
key
=
self
.
_ordering
.
__getitem__
)
def
get_siblings
(
self
,
a
,
*
,
include_self
=
True
):
"""
Return all the items joined with *a*.
*a* is included in the list if *include_self* is True.
"""
siblings
=
self
.
_mapping
.
get
(
a
, [
a
])
result
=
sorted
(
siblings
,
key
=
self
.
_ordering
.
get
)
if
not
include_self
:
result
.
remove
(
a
)
return
result
class
GrouperView
:
"""Immutable view over a `.Grouper`."""
def
__init__
(
self
,
grouper
):
self
.
_grouper
=
grouper
def
__contains__
(
self
,
item
):
return
item
in
self
.
_grouper
def
__iter__
(
self
):
return
iter
(
self
.
_grouper
)
def
joined
(
self
,
a
,
b
):
"""
Return whether *a* and *b* are members of the same set.
"""
return
self
.
_grouper
.
joined
(
a
,
b
)
def
get_siblings
(
self
,
a
,
*
,
include_self
=
True
):
"""
Return all the items joined with *a*.
*a* is included in the list if *include_self* is True.
"""
return
self
.
_grouper
.
get_siblings
(
a
,
include_self
=
include_self
)
def
simple_linear_interpolation
(
a
,
steps
):
"""
Resample an array with ``steps - 1`` points between original point pairs.
Along each column of *a*, ``(steps - 1)`` points are introduced between
each original values; the values are linearly interpolated.
Parameters
----------
a : array, shape (n, ...)
steps : int
Returns
-------
array
shape ``((n - 1) * steps + 1, ...)``
"""
fps
=
a
.
reshape
((
len
(
a
),
-
1
))
xp
=
np
.
arange
(
len
(
a
))
*
steps
x
=
np
.
arange
((
len
(
a
)
-
1
)
*
steps
+
1
)
return
(
np
.
column_stack
([
np
.
interp
(
x
,
xp
,
fp
)
for
fp
in
fps
.
T
])
.
reshape
((
len
(
x
),)
+
a
.
shape
[
1
:]))
def
delete_masked_points
(
*
args
):
"""
Find all masked and/or non-finite points in a set of arguments,
and return the arguments with only the unmasked points remaining.
Arguments can be in any of 5 categories:
1) 1-D masked arrays
2) 1-D ndarrays
3) ndarrays with more than one dimension
4) other non-string iterables
5) anything else
The first argument must be in one of the first four categories;
any argument with a length differing from that of the first
argument (and hence anything in category 5) then will be
passed through unchanged.
Masks are obtained from all arguments of the correct length
in categories 1, 2, and 4; a point is bad if masked in a masked
array or if it is a nan or inf. No attempt is made to
extract a mask from categories 2, 3, and 4 if `numpy.isfinite`
does not yield a Boolean array.
All input arguments that are not passed unchanged are returned
as ndarrays after removing the points or rows corresponding to
masks in any of the arguments.
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL