How to plot such figure in python usng NETCDF data - python-3.x

I am trying to produce a figure (shown below) using netcdf data and overlay stippling a 95% using ttest. I want to plot this graph in Python. Any help will be appreciated.
Figure, original source 1:
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
input = Dataset("v2p0chirps_25_1981_2016.nc.nc")
xlat = input.variables["lat"][:]
xlon = input.variables["lon"][:]
xtime = input.variables["time"][:]
precip = input.variables["pr"][:]
latmin = np.min(xlat)
latmax = np.max(xlat)
lonmin = np.min(xlon)
lonmax = np.max(xlon)
m = Basemap(projection='merc',llcrnrlat = latmin, urcrnrlat = latmax,llcrnrlon = lonmin, urcrnrlon = lonmax, resolution='i')
m.drawcoastlines()
m.drawcountries()
parallels = np.arange(latmin, latmax, 10.)
m.drawparallels(parallels,labels=[True, False, True, False],fontsize=12)
meridians = np.arange(lonmin, lonmax, 10.)
m.drawmeridians(meridians, labels=[True,False,False,True],fontsize=12)
lons, lats = np.meshgrid(xlon, xlat)
x, y = m(lons, lats)
cmap_r = mp.cm.get_cmap('autumn_r')
cs = m.contourf(x,y, np.mean(precip[0:6574,:,:], 0) -
np.mean(precip[6574:13145,:,:], 0), cmap="rainbow", extend='both')
plt.savefig("contour_map.eps")

Related

How to translate hexagon matplotlib plot to an interactive bokeh plot?

I have been working with the excellent minisom package and want to plot interactively the hexagonal map that reflects the results of the self-organising maps training process. There's already a code example that does this statically using matplotlib but to do so interactively, I would like to use bokeh. This is where I am struggling.
This is the code to generate a simplified matplotlib example of what's already on the package page:
from minisom import MiniSom
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib import cm
from bokeh.plotting import figure
from bokeh.io import save, show, output_file, output_notebook
output_notebook()
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/00236/seeds_dataset.txt',
names=['area', 'perimeter', 'compactness', 'length_kernel', 'width_kernel',
'asymmetry_coefficient', 'length_kernel_groove', 'target'], sep='\t+')
t = data['target'].values
data = data[data.columns[:-1]]
# data normalisation
data = (data - np.mean(data, axis=0)) / np.std(data, axis=0)
data = data.values
# initialisation and training
som = MiniSom(15, 15, data.shape[1], sigma=1.5, learning_rate=.7, activation_distance='euclidean',
topology='hexagonal', neighborhood_function='gaussian', random_seed=10)
som.train(data, 1000, verbose=True)
# plot hexagonal topology
f = plt.figure(figsize=(10,10))
ax = f.add_subplot(111)
ax.set_aspect('equal')
xx, yy = som.get_euclidean_coordinates()
umatrix = som.distance_map()
weights = som.get_weights()
for i in range(weights.shape[0]):
for j in range(weights.shape[1]):
wy = yy[(i, j)]*2/np.sqrt(3)*3/4
hex = RegularPolygon((xx[(i, j)], wy), numVertices=6, radius=.95/np.sqrt(3),
facecolor=cm.Blues(umatrix[i, j]), alpha=.4, edgecolor='gray')
ax.add_patch(hex)
for x in data:
w = som.winner(x)
# place a marker on the winning position for the sample xx
wx, wy = som.convert_map_to_euclidean(w)
wy = wy * 2 / np.sqrt(3) * 3 / 4
plt.plot(wx, wy, markerfacecolor='None',
markeredgecolor='black', markersize=12, markeredgewidth=2)
plt.show()
matplotlib hexagonal topology plot
I've tried to translate the code into bokeh but the resulting hex plot (to me, primitively) looks like it needs to be flipped vertically onto the points and for the skew to be straightened out.
tile_centres_column = []
tile_centres_row = []
colours = []
for i in range(weights.shape[0]):
for j in range(weights.shape[1]):
wy = yy[(i, j)] * 2 / np.sqrt(3) * 3 / 4
tile_centres_column.append(xx[(i, j)])
tile_centres_row.append(wy)
colours.append(cm.Blues(umatrix[i, j]))
weight_x = []
weight_y = []
for x in data:
w = som.winner(x)
wx, wy = som.convert_map_to_euclidean(xy=w)
wy = wy * 2 / np.sqrt(3) * 3/4
weight_x.append(wx)
weight_y.append(wy)
# plot hexagonal topology
plot = figure(plot_width=800, plot_height=800,
match_aspect=True)
plot.hex_tile(q=tile_centres_column, r=tile_centres_row,
size=.95 / np.sqrt(3),
color=colours,
fill_alpha=.4,
line_color='black')
plot.dot(x=weight_x, y=weight_y,
fill_color='black',
size=12)
show(plot)
bokeh hexagonal topology plot
How can I translate this into a bokeh plot?
Found out how to do it after reaching out to the minisom package author for help. Complete code available here.
from bokeh.colors import RGB
from bokeh.io import curdoc, show, output_notebook
from bokeh.transform import factor_mark, factor_cmap
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure, output_file
hex_centre_col, hex_centre_row = [], []
hex_colour = []
label = []
# define labels
SPECIES = ['Kama', 'Rosa', 'Canadian']
for i in range(weights.shape[0]):
for j in range(weights.shape[1]):
wy = yy[(i, j)] * 2 / np.sqrt(3) * 3 / 4
hex_centre_col.append(xx[(i, j)])
hex_centre_row.append(wy)
hex_colour.append(cm.Blues(umatrix[i, j]))
weight_x, weight_y = [], []
for cnt, i in enumerate(data):
w = som.winner(i)
wx, wy = som.convert_map_to_euclidean(xy=w)
wy = wy * 2 / np.sqrt(3) * 3 / 4
weight_x.append(wx)
weight_y.append(wy)
label.append(SPECIES[t[cnt]-1])
# convert matplotlib colour palette to bokeh colour palette
hex_plt = [(255 * np.array(i)).astype(int) for i in hex_colour]
hex_bokeh = [RGB(*tuple(rgb)).to_hex() for rgb in hex_plt]
output_file("resulting_images/som_seed_hex.html")
# initialise figure/plot
fig = figure(title="SOM: Hexagonal Topology",
plot_height=800, plot_width=800,
match_aspect=True,
tools="wheel_zoom,save,reset")
# create data stream for plotting
source_hex = ColumnDataSource(
data = dict(
x=hex_centre_col,
y=hex_centre_row,
c=hex_bokeh
)
)
source_pages = ColumnDataSource(
data=dict(
wx=weight_x,
wy=weight_y,
species=label
)
)
# define markers
MARKERS = ['diamond', 'cross', 'x']
# add shapes to plot
fig.hex(x='y', y='x', source=source_hex,
size=100 * (.95 / np.sqrt(3)),
alpha=.4,
line_color='gray',
fill_color='c')
fig.scatter(x='wy', y='wx', source=source_pages,
legend_field='species',
size=20,
marker=factor_mark(field_name='species', markers=MARKERS, factors=SPECIES),
color=factor_cmap(field_name='species', palette='Category10_3', factors=SPECIES))
# add hover-over tooltip
fig.add_tools(HoverTool(
tooltips=[
("label", '#species'),
("(x,y)", '($x, $y)')],
mode="mouse",
point_policy="follow_mouse"
))
show(fig)

Reading in multiple files in Python and saving them one by one in a different directory

import glob
import pandas as pd
import seaborn as sns
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
files = glob.glob("Angular_position_*_*.csv")
output = pd.DataFrame()
for f in files:
df = pd.read_csv(f)
time = df.iloc[:,0]
time = time.to_numpy()
ynew = df.iloc[:,1:]
ynew = ynew.to_numpy()
lowPassCutoffFreq = 6.0 # Cut off frequency
Sample_freq = 150; #Target sample frequency
N = 2 # Order of the filter; In this case 2nd order
Wn = lowPassCutoffFreq/(Sample_freq/2) #Normalize frequency
b, a = signal.butter(5, Wn, btype='low',analog=False,output='ba')
#scipy.signal.butter(N, Wn, btype='low', analog=False, output='ba', fs=None)
output = signal.filtfilt(b, a, ynew, axis=0)
np.savetxt("enter directory path/Filtered_files/Filtered_Angular_position_*_*", output, delimiter = ', ', newline = "\n")
I am trying to read in all files in a directory, they are then low pass filtered. After that the results are saved one after the other but not in one file. The result gives each files with 3 columns and ideally I would like them to named with headers e.g. col1, col2, col3.
Without using glob, I can filter all my files individually but I have more than 100 such files.
Any help would be appreciated.
best wishes,
I have partially solved the issue apart from the header names:
import glob
import pandas as pd
from tnorma import tnorma
import seaborn as sns
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
path = r'location_of_dir'
all_files = glob.glob(path + '/*.csv')
# yn = np.zeros(shape = (101,1))
# tn = np.zeros(shape = (101,1))
#ynew = []
yn = np.zeros(shape = (101,1))
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
print(filename)
foo = filename.split("/")[-1]
#df = pd.read_csv(f)
time = df.iloc[:,0]
time = time.to_numpy()
ynew = df.iloc[:,1:]
ynew = ynew.to_numpy()
#print(ynew)
lowPassCutoffFreq = 6.0 # Cut off frequency
Sample_freq = 150; #Target sample frequency
N = 2 # Order of the filter; In this case 2nd order
Wn = lowPassCutoffFreq/(Sample_freq/2) #Normalize frequency
b, a = signal.butter(5, Wn, btype='low',analog=False,output='ba')
#scipy.signal.butter(N, Wn, btype='low', analog=False, output='ba', fs=None)
output = signal.filtfilt(b, a, ynew, axis=0)
#print (output)
tn = np.linspace(0, 100, 101) # new time vector for the new time-normalized data
yn, tn, indie = tnorma(output, k=3, smooth =1, mask = None, show = False)
np.savetxt("path_name/foldername/file"+ foo, yn, delimiter = ', ', newline = "\n")
However, I am having difficulty in putting header names on the 3 columns per file.

Find the pixel_maxima in the image and also find its coordinates as well as the peak intensity

I have this input image and I wan to find pixel maxima and it should look like image2.
I tried contouring but its not working.I also tried scipy pixel_maxima it doesn't gives me result as image2.
here is the code, I have refered this answer (Get coordinates of local maxima in 2D array above certain value)
import numpy as np
import scipy.misc
import scipy.ndimage as ndimage
import scipy.ndimage.filters as filters
import matplotlib.pyplot as plt
import imageio
from skimage.color import rgb2gray
fname = 'slice-0001-trim.jpg'
neighborhood_size = 10
threshold = 12
data = imageio.imread(fname,as_gray=True)
data_max = filters.maximum_filter(data, neighborhood_size)
maxima = (data == data_max)
data_min = filters.minimum_filter(data, neighborhood_size)
diff = ((data_max - data_min) > threshold)
maxima[diff == 0] = 0
labeled, num_objects = ndimage.label(maxima)
slices = ndimage.find_objects(labeled)
x, y = [], []
for dy,dx in slices:
x_center = (dx.start + dx.stop - 1)/2
x.append(x_center)
y_center = (dy.start + dy.stop - 1)/2
y.append(y_center)
plt.imshow(data)
plt.savefig('sample.jpg', bbox_inches = 'tight')
plt.autoscale(False)
plt.plot(x,y, 'ro')
plt.savefig('result.png', bbox_inches = 'tight')
I need this image as result
But, I get this image as output

Stack area chart with quantitative x-axis with altair

Area charts in altair are automatically stacked when the x-axis is time. But when x belongs to a quantitative data type, areas are not stacked.
import pandas as pd
import numpy as np
import string
import altair as alt
np.random.seed(394378)
n_series = 3
series_names = list(string.ascii_lowercase)[:n_series]
x_range = range(0, 21)
df = pd.DataFrame({"Series": np.tile(series_names, len(x_range)),
"X": np.repeat(x_range, n_series),
"Y": np.random.poisson(lam = 10, size = len(x_range) * n_series)})
alt.Chart(df).\
mark_area().\
encode(
x = "X:Q",
y = "Y:Q",
color = "Series:N"
)
How can I stack areas?
You can do this by passing stack=True to the y encoding. For example:
alt.Chart(df).\
mark_area().\
encode(
x = "X:Q",
y = alt.Y("Y:Q", stack=True),
color = "Series:N"
)

is it possible to return the edge/node info to python after rendering in bokeh?

I have used this code to draw network diagram.
so far, I have seen ways to change graph attributes when click or hover.
but now I want to record object info when clicked
for example: if I click on an edge, I want to return to python the edge details to do some processing.
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
G=nx.karate_club_graph()
plot = Plot(plot_width=400, plot_height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot.title.text = "Graph Interaction Demonstration"
plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
plot.renderers.append(graph_renderer)
output_file("interactive_graphs.html")
show(plot)
This code uses TapTool callback to display selected indices and start/end nodes for selected edges (tap on a node to make a selection first). Code works for Bokeh v1.1.0
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool, CustomJS, Column, TextInput
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
from bokeh.models.widgets.inputs import TextInput
G = nx.karate_club_graph()
plot = Plot(plot_width = 400, plot_height = 400,
x_range = Range1d(-1.1, 1.1), y_range = Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"
plot.add_tools(HoverTool(tooltips = [('index', '$index'), ('start', '#start'), ('end', '#end')]), TapTool(), BoxSelectTool())
graph_renderer = from_networkx(G, nx.circular_layout, scale = 1, center = (0, 0))
graph_renderer.node_renderer.glyph = Circle(size = 15, fill_color = Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size = 15, fill_color = Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size = 15, fill_color = Spectral4[1])
graph_renderer.edge_renderer.glyph = MultiLine(line_color = "#CCCCCC", line_alpha = 0.8, line_width = 5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color = Spectral4[2], line_width = 5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color = Spectral4[1], line_width = 5)
graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
plot.renderers.append(graph_renderer)
info_indices = TextInput(title = 'Selected indices:', value = '')
info_start_end = TextInput(title = 'Start => End Values:', value = '')
esource = graph_renderer.edge_renderer.data_source
code = """ info_indices.value = String(esource.selected.indices);
start_end_values = []
for(idx in esource.selected.indices)
{
index = esource.selected.indices[idx]
start_end_values.push(esource.data['start'][index] + '=>' + esource.data['end'][index] )
}
info_start_end.value = String(start_end_values); """
callback = CustomJS(args = dict(esource = esource,
info_indices = info_indices,
info_start_end = info_start_end),
code = code)
plot.select_one(TapTool).callback = callback
show(Column(plot, info_start_end))
Result:

Resources