File: plot_visualizers.py

package info (click to toggle)
python-sidpy 0.12.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,988 kB
  • sloc: python: 11,456; makefile: 17
file content (190 lines) | stat: -rw-r--r-- 7,165 bytes parent folder | download
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
"""
================================================================================
Plotting Datasets
================================================================================

**Gerd Duscher**

08/25/2020

**Please download this example and run it as a notebook by scrolling to the
bottom of this page**
"""
# Ensure python 3 compatibility:
from __future__ import division, print_function, absolute_import, unicode_literals
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.append('../../')
import sidpy

print(sidpy.__version__)

###############################################################################
# Plotting an Image
# -----------------
# First, we make a sidpy dataset from a numpy array
x = np.random.normal(loc=3, scale=2.5, size=(128, 128))
dset = sidpy.Dataset.from_array(x)

###############################################################################
# Next, we add some information about this dataset

dset.data_type = 'image'
dset.units = 'counts'
dset.quantity = 'intensity'

###############################################################################
# For plotting it is important to set the dimensions correctly.

dset.set_dimension(0, sidpy.Dimension('x', np.arange(dset.shape[0])*.02))
dset.x.dimension_type = 'spatial'
dset.x.units = 'nm'
dset.x.quantity = 'distance'
dset.set_dimension(1, sidpy.Dimension('y', np.arange(dset.shape[1])*.02))
dset.y.dimension_type = 'spatial'
dset.yunits = 'nm'
dset.y.quantity = 'distance'

###############################################################################
# Now we plot the dataset:
dset.plot()

###############################################################################
# Creating an Image-Stack DataSet
# -------------------------------
# In the following we will make a numpy which resembles a stack of images
# 
# In the ``sidpy Dataset`` will set the ``data_type`` to ``image_stack`` for the plotting routine to know how to plot this dataset.
# 
# The dimensions have to contain at least two ``spatial`` dimensions and one that is identifiable as a stack dimension ('stack, 'frame', 'time').
# First we make a stack of images
x = np.random.normal(loc=3, scale=2.5, size=(25, 128, 128))

dset = sidpy.Dataset.from_array(x)
dset.data_type = 'image_stack'
dset.units = 'counts'
dset.quantity = 'intensity'

dset.set_dimension(0, sidpy.Dimension('frame', np.arange(dset.shape[0])))
dset.frame.dimension_type = 'time'
dset.set_dimension(1, sidpy.Dimension('x', np.arange(dset.shape[1])*.02))
dset.x.dimension_type = 'spatial'
dset.x.units = 'nm'
dset.x.quantity = 'distance'
dset.set_dimension(2, sidpy.Dimension('y', np.arange(dset.shape[2])*.02))
dset.y.dimension_type = 'spatial'
dset.yunits = 'nm'
dset.y.quantity = 'distance'


###############################################################################
# Plotting the Dataset
# --------------------
# Please note that the scroll wheel will move you through the stack.
# 
# Zoom to an area and let it play!
# 
# Click on the ``Average`` button and then click on it again.

dset.plot()

###############################################################################
# The kwargs dictionary is used to plot the image stack in TEM style with scale bar

kwargs = {'scale_bar': True, 'cmap': 'hot'}  # or maybe 'cmap': 'gray'
 
dset.plot(verbose=True, **kwargs)

###############################################################################
# Plot Dataset as Spectral Image
# ------------------------------
# We need to change the data_type of the dataset to ``spectrum_image`` and the dimension_type of one dimension to ``spectral``.
# 
# Now the plot function plots it as a spectrum image.
# 
# Select the spectrum with the mouse (left click).

dset.data_type = 'spectrum_image'
dset.set_dimension(0, sidpy.Dimension('spectrum',np.arange(dset.shape[0])))
dset.spectrum.dimension_type = 'spectral'

dset.plot()

###############################################################################
# We make the selection more visible by setting the binning of the spectra selection.
# 
# The binning averages over the binning box.
# Run the code-cell below and look in the plot above.
# While you can make the modifications in a jupyter noteboook in a code-cell after the
# dset.plot() command is executed, that does not work in a script.
# Here we use the explicit visualization command followed by a plt.show() command.

dset.view.set_bin([20, 20])
plt.show()

###############################################################################
# The axes (and figure) instances of matplotlib can be accessed through the ``view``
# attribute of  the sidpy dataset. For example ``dset.view``.
# Again that does not work in a prgram and we use the explicit command.
# Note that you always have to keep a reference for an interactive plot (here view)

"""
<<<<<<< HEAD:examples/viz/dataset/plot_visualizers.py
view = sidpy.viz.dataset_viz.SpectralImageVisualizer(dset)
view.set_bin([40,40])
x, y = np.mgrid[0:501:100, 0:501:100] + 5
view.axes[0].scatter(x, y, color='red');
=======
###############################################################################
#
kwargs = {'scale_bar': True, 'cmap': 'hot'}
    
view = sid.viz.dataset_viz.ImageStackVisualizer(dset, **kwargs)
<<<<<<< Updated upstream:examples/viz_dataset/plot_visualizers.py
=======
>>>>>>> 608507c4c878dbbaaf7968979bd27d058695deed:examples/viz_dataset/plot_visualizers.py
>>>>>>> Stashed changes:examples/viz/dataset/plot_visualizers.py
plt.show()

###############################################################################

<<<<<<< HEAD:examples/viz/dataset/plot_visualizers.py
=======
print(dset.shape)
kwargs = {'scale_bar': True, 'cmap': 'hot'}
view = sid.dataset_viz.ImageVisualizer(dset, image_number=5, **kwargs)
<<<<<<< Updated upstream:examples/viz_dataset/plot_visualizers.py
=======
>>>>>>> 608507c4c878dbbaaf7968979bd27d058695deed:examples/viz_dataset/plot_visualizers.py
>>>>>>> Stashed changes:examples/viz/dataset/plot_visualizers.py

###############################################################################
# The generic plot command of a dispy dataset looks for the ``data_type`` to
# decide how to plot the data.
# We cn force any plot with the expliit plot command, but we need to provide the
# ``dimension_type`` as information what axis to be used for the plot.

<<<<<<< HEAD:examples/viz/dataset/plot_visualizers.py
print(dset.shape)
kwargs = {'scale_bar': True, 'cmap': 'hot'}
view = sidpy.viz.dataset_viz.ImageVisualizer(dset, image_number = 5, **kwargs)
plt.show()

###############################################################################
=======
dset.data_type = 'spectrum_image'
dset.set_dimension(0, sidpy.Dimension('spectrum',np.arange(dset.shape[0])))
dset.spectrum.dimension_type = 'spectral'
view = sidpy.viz.dataset_viz.SpectralImageVisualizer(dset)
view.set_bin([30, 40])
plt.show()

###############################################################################
#
dset.data_type = 'spectrum_image'
dset.set_dimension(0, sidpy.Dimension('spectrum',np.arange(dset.shape[0])))
dset.spectrum.dimension_type = 'spectral'
# view = SpectralImageVisualizer(dset)
# dset.plot()
"""