-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThesis.py
63 lines (50 loc) · 1.54 KB
/
Thesis.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
#! /usr/bin/env python3
import contextlib
import os
import matplotlib.pyplot as plt
# If data_path or save_path are not provided, the default values are used
# try:
# assert data_path
# except NameError:
# data_path = 'data/'
# try:
# assert save_path
# except NameError:
# save_path = 'figures/'
# Temporary default overwrite
# PLOT_PARAMS = {
# 'figure.figsize': plt.rcParams["figure.figsize"], # inches
# 'figure.dpi': 900, # dots per inch
# 'figure.subplot.left': 0, # %
# 'figure.subplot.bottom': 0, # %
# 'figure.subplot.right': 1, # %
# 'figure.subplot.top': 1, # %
# 'figure.subplot.wspace': 0, # %
# 'figure.subplot.hspace': 0, # %
# 'image.cmap': 'gray', # str or Colormap
# }
# Custom functions
@contextlib.contextmanager
def new_cd(directory: str | os.PathLike):
"""
Create a context manager to temporarily change the current working directory.
Parameters
----------
directory : str | os.PathLike
Path to the directory to change to.
See Also
--------
https://stackoverflow.com/a/75049063
"""
cur_dir = os.getcwd()
# This could raise an exception, but it's probably
# best to let it propagate and let the caller
# deal with it, since they requested `directory`
os.chdir(directory)
try:
yield
finally:
# This could also raise an exception, but you *really*
# aren't equipped to figure out what went wrong if the
# old working directory can't be restored.
os.chdir(cur_dir)