File: plot_spec_test.py

package info (click to toggle)
python-pymzml 2.5.2%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,792 kB
  • sloc: python: 6,495; pascal: 341; makefile: 233; sh: 30
file content (211 lines) | stat: -rwxr-xr-x 7,757 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3.4
# encoding: utf-8
"""
Plot a test spectrum for different plotting styles
"""
import pymzml
import os
import sys
import test_file_paths
from pymzml.plot import Factory
import unittest


class PlotTest(unittest.TestCase):
    """
    """

    def setUp(self):
        self.paths = test_file_paths.paths
        self.path = self.paths[2]
        self.Run = pymzml.run.Reader(self.path)
        self.spec = self.Run[6]

        self.layout = {
            "font": {"family": "Arial", "size": 20, "color": "#000000"},
            "autosize": False,
            "width": 1700,
            "height": 700,
            "margin": {"l": 100, "r": 80, "t": 100, "b": 60},
            # 'separatethousands' : False,
            # 'autoexpand':True,
            "xaxis1": {
                "type": "linear",
                "color": "#000000",
                "title": "<i>m/z</i>",
                "titlefont": {"family": "Arial", "size": 24, "color": "#000000"},
                "tickmode": "auto",
                "nticks": 0,
                "tickprefix": "",
                "ticksuffix": "",
                "showticklabels": True,
                "tickfont": {"family": "Arial", "size": 20, "color": "rgb(0, 0, 0)"},
                # "tickangle": "auto",
                "tickformat": "",
                "showexponent": "all",
                "exponentformat": "B",
                "ticklen": 5,
                "tickwidth": 1,
                "tickcolor": "#000000",
                "ticks": "outside",
                "showline": True,
                "mirror": False,
                "showgrid": False,
                "zerolinecolor": "rgb(0, 0, 0)",
                "zerolinewidth": 1,
                "zeroline": False,
                "anchor": "y",
                "side": "bottom",
                "tick0": 0,
                "dtick": 100,
            },
            "yaxis1": {
                "type": "linear",
                "color": "#000000",
                "title": "Intensity",
                "titlefont": {"family": "Arial", "size": 24, "color": "#000000"},
                "nticks": 0,
                "tickprefix": "",
                "ticksuffix": "",
                "showticklabels": True,
                "tickfont": {"family": "Arial", "size": 20, "color": "rgb(0, 0, 0)"},
                # "tickangle": "auto",
                "tickformat": "",
                "showexponent": "all",
                "exponentformat": "B",
                # 'separatethousands' : True,
                "ticklen": 5,
                "tickwidth": 1,
                "tickcolor": "#000000",
                "ticks": "outside",
                "showline": True,
                "mirror": False,
                "showgrid": False,
                "zerolinecolor": "rgb(0, 0, 0)",
                "zerolinewidth": 1,
                "zeroline": False,
                "anchor": "x",
                "side": "left",
                "tick0": 0,
                "dtick": 20000000,
            },
            "legend": {
                "bgcolor": "#fff",
                "bordercolor": "#444",
                "borderwidth": 0,
                "font": {"family": "Arial", "size": 20, "color": "#000000"},
                "orientation": "v",
                "traceorder": "normal",
                "x": 1.02,
                "xanchor": "left",
                "y": 1,
                "yanchor": "auto",
            },
        }
        self.test_styles = [
            ("lines", (200, 200, 200), "solid", 0.8, None, None),
            ("sticks", (200, 0, 0), "solid", 0.8, None, (200, 300)),
            ("points", (0, 0, 200), "solid", 0.8, None, None),
            ("label.sticks", (0, 0, 200), "dot", 0.8, None, None),
            ("triangle.big", (200, 0, 200), "solid", 0.8, None, None),
            ("triangle.small", (200, 0, 0), "solid", 0.8, None, None),
            ("label.triangle.MS_precision", (0, 200, 0), "dash", 0.8, None, None),
            ("label.spline", (0, 200, 0), "dash", 0.3, None, None),
            ("label.triangle.small", (0, 200, 0), "solid", 0.8, None, None),
            ("label.linear.bottom", (0, 200, 100), "solid", 0.8, self.layout, None),
        ]
        self.pf = Factory()
        self.file_name = os.path.join(os.path.dirname(self.path), "test_plot.html")

    def test_new_plot(self):
        self.pf.new_plot(MS_precision="100e-6", title="test")
        self.assertEqual(self.pf.MS_precisions[0], "100e-6")
        self.assertEqual(self.pf.titles[0], "test")
        self.pf.new_plot()
        self.assertEqual(len(self.pf.plots), 2)
        self.assertEqual(self.pf.MS_precisions[1], "5e-6")
        self.assertEqual(self.pf.titles[1], None)
        self.pf = Factory()

    def test_add_plots(self):
        for plotnumber, plot in enumerate(self.test_styles):
            self.pf.new_plot()
            style, color, dash, opacity, layout, mz_range = plot
            split_style = style.split(".")
            if split_style[0] == "label":
                self.pf.add(
                    self.spec.highest_peaks(100),
                    color=(200, 200, 200),
                    style="lines",
                    name="datapoints",
                    title="test spec plot",
                    plot_num=plotnumber,
                    mz_range=mz_range,
                )
                label_list = []
                for peak in self.spec.highest_peaks(100):
                    label_list.append((peak[0], 100, "spline_top", "label"))
                self.pf.add(
                    label_list,
                    color=color,
                    style=style,
                    name="Label",
                    opacity=opacity,
                    dash=dash,
                    title="test spec plot",
                    plot_num=plotnumber,
                    mz_range=mz_range,
                )
                self.assertEqual(len(self.pf.plots[plotnumber]), 2)
            else:
                peak_list = self.spec.highest_peaks(100)
                self.pf.add(
                    peak_list,
                    color=color,
                    style=style,
                    name="Label",
                    opacity=opacity,
                    dash=dash,
                    title="test spec plot",
                    plot_num=plotnumber,
                    mz_range=mz_range,
                )

            self.assertEqual(
                self.pf.plots[plotnumber][-1]["line"]["color"],
                "rgba({0},{1},{2},{3})".format(
                    color[0], color[1], color[2], opacity
                ),
            )
            self.assertEqual(self.pf.plots[plotnumber][-1]["line"]["dash"], dash)
            if "label" in style:
                if "spline" in style:
                    mode = "lines+markers+text"
                else:
                    mode = "text+lines"
            elif style == "points":
                mode = "markers"
            else:
                mode = "lines"
            self.assertEqual(self.pf.plots[plotnumber][-1]["mode"], mode)
            if mz_range is not None:
                self.assertLessEqual(self.pf.x_max[plotnumber], mz_range[1])
        self.assertEqual(len(self.pf.plots), len(self.test_styles))

    def test_save_plot(self):
        self.pf.add(
            self.spec.highest_peaks(100),
            color=(200, 200, 200),
            style="sticks",
            name="datapoints",
        )
        self.pf.save(filename=self.file_name, layout=self.layout)
        self.assertTrue(os.path.exists(self.file_name))

    def tearDown(self):
        if os.path.exists(self.file_name):
            os.remove(self.file_name)


if __name__ == "__main__":
    unittest.main(verbosity=3)