forked from poplarShift/python-data-science-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandard_imports.py
More file actions
137 lines (110 loc) · 3.33 KB
/
standard_imports.py
File metadata and controls
137 lines (110 loc) · 3.33 KB
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
__doc__ = """USAGE:
from utils.standard_imports import *
"""
# https://github.com/ajalt/fuckitpy
from . import fuckit # cause why not??
# silence everything
@fuckit
def imports(name_space):
#BASIC
from collections import OrderedDict
import os
import itertools
from functools import reduce
import datetime as dt
import warnings
# MISC
from pysolar.solar import get_altitude
import gsw
from scipy.io import loadmat
# DATA
import pandas as pd
import geopandas as gpd
import numpy as np
import xarray as xr
# monkey patch xarray until https://github.com/pydata/xarray/pull/2917 is released
def load_dataset(fname, **kwargs):
with xr.open_dataset(fname, **kwargs) as ds:
return ds.load()
xr.load_dataset = load_dataset
# GEO
from fiona.crs import from_epsg
from scipy.spatial import cKDTree
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xesmf
import shapely
from shapely.ops import nearest_points
from shapely.geometry import Point, MultiPoint, box, LinearRing, LineString, MultiLineString, Polygon
## VIZ
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
from bokeh.models import WMTSTileSource
import holoviews as hv
try:
hv.extension('bokeh', 'matplotlib')
except:
pass
from holoviews import dim, opts, Options, Dimension, Options, Cycle
from holoviews.core.io import Pickler
from holoviews.operation.datashader import datashade, rasterize, regrid, aggregate
import datashader as dsh
import hvplot.pandas
import hvplot.xarray
try:
# extents=(-90, 65, -45, 78)
bathy = gpd.read_file('~/database/IBCAO/IBCAO_1min.shp').set_index('contour').geometry
# e.g. bathy200 = bathy.loc[200]
# bathy = bathy.intersection(shapely.geometry.box(*extents))
# bathy.crs = from_epsg(4326)
except:
pass
# BOKEH STYLING
from bokeh.themes import Theme
theme = Theme(json={
'attrs' : {
'Figure' : {
# 'background_fill_color': '#2F2F2F',
# 'border_fill_color': '#2F2F2F',
'outline_line_color': 'black',
'outline_line_alpha': 1,
},
'Axis' : {
'major_tick_in': 0,
'major_tick_out': 8,
'minor_tick_out': 4,
# 'axis_label_standoff': 0,
# 'axis_label_text_color': 'black',
# 'axis_label_text_font_size': '15pt',
'major_label_text_font_size': '13pt',
'axis_label_text_font_style': 'normal',
},
'Legend': {
'label_text_font_size': '12pt'
},
'Grid': {
'grid_line_dash': [6, 4],
'grid_line_alpha': .9,
},
'Title': {
# 'text_color': 'red',
'text_font_size': '15pt',
},
'ColorBar': {
'major_label_text_font_size': '14pt',
}
}})
try:
hv.renderer('bokeh').theme = theme
except:
pass
for name, func in locals().items():
if name != 'name_space':
name_space[name] = func
imports(globals())
# the boring way with way more boilerplate:
# from contextlib import suppress
# with suppress(ImportError):
# etc