• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timcera / pyslice / 12340932278

15 Dec 2024 05:42PM CUT coverage: 45.447%. Remained the same
12340932278

Pull #4

github

web-flow
Merge d90cfa984 into 2ffee6f33
Pull Request #4: build(deps): bump actions/setup-python from 4 to 5

529 of 1164 relevant lines covered (45.45%)

2.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.84
/src/pyslice/pyslice_lib/PySPG/PyGrace.py
1
import math
5✔
2
import sys
5✔
3

4
#
5
# IMPLEMENTS A GRACE DATASET         ###########
6
#
7

8

9
class GraceDataSet:
5✔
10
    basdefault = {
5✔
11
        "hidden": "false",
12
        "type": "xy",
13
        "baseline type": 0,
14
        "comment": '"Cols 1:2"',
15
        "legend": '" "',
16
    }
17

18
    bassymbol = {
5✔
19
        " ": 1,
20
        "size": 0.65,
21
        "color": 1,
22
        "pattern": 1,
23
        "fill color": 1,
24
        "fill pattern": 0,
25
        "linewidth": 1.0,
26
        "linestyle": 1,
27
        "char": 65,
28
        "char font": 0,
29
        "skip": 0,
30
    }
31

32
    basline = {"type": 1, "linestyle": 1, "linewidth": 2.0, "color": 1, "pattern": 1}
5✔
33

34
    basfill = {"type": 0, "rule": 0, "color": 1, "pattern": 1}
5✔
35

36
    def __init__(self, n=0, d=None, gT="xy"):
5✔
37
        if d is None:
×
38
            d = []
×
39
        self.data = d
×
40
        self.name = n
×
41

42
        # copia todos los diccionarios
43
        self.default = self.basdefault.copy()
×
44
        self.symbol = self.bassymbol.copy()
×
45
        self.line = self.basline.copy()
×
46
        self.fill = self.basfill.copy()
×
47
        self.default["type"] = gT
×
48

49
    def __str__(self):
5✔
50
        return (
×
51
            (
52
                (
53
                    "\n".join(
54
                        [
55
                            f"@    s{str(self.name)} {a} {str(b)}"
56
                            for a, b in list(self.default.items())
57
                        ]
58
                    )
59
                    + "\n"
60
                )
61
                + "\n".join(
62
                    [
63
                        f"@    s{str(self.name)} symbol {a} {str(b)}"
64
                        for a, b in list(self.symbol.items())
65
                    ]
66
                )
67
                + "\n"
68
            )
69
            + "\n".join(
70
                [
71
                    f"@    s{str(self.name)} line {a} {str(b)}"
72
                    for a, b in list(self.line.items())
73
                ]
74
            )
75
            + "\n"
76
        ) + "\n".join(
77
            [
78
                f"@    s{str(self.name)} fill {a} {str(b)}"
79
                for a, b in list(self.fill.items())
80
            ]
81
        )
82

83

84
#
85
# IMPLEMENTS A GRACE GRAPH           #########
86
#
87
class GraceGraph:
5✔
88
    bdefault = {
5✔
89
        " ": "on",
90
        "hidden": "false",
91
        "type": "XY",
92
        "stacked": "false",
93
        "bar hgap": "0.000000",
94
    }
95

96
    bworld = {"xmin": 0.0, "xmax": 1.0, "ymin": 0.0, "ymax": 1.0}
5✔
97

98
    bview = {"xmin": 0.18, "xmax": 1.21, "ymin": 0.13, "ymax": 0.88}
5✔
99

100
    btitle = {" ": '" "', "font": 0, "size": 1.5, "color": 1}
5✔
101

102
    bsubtitle = {" ": '" "', "font": 0, "size": 1.0, "color": 1}
5✔
103

104
    baxes = {"scale": "Normal", "invert": "off"}
5✔
105

106
    baxis = {
5✔
107
        " ": "on",
108
        "type zero": "false",
109
        "offset": "0.0 , 0.0",
110
        "bar": "on",
111
        "bar color": 1,
112
        "bar linestyle": 1,
113
        "bar linewidth": 1.0,
114
        "label": '"x"',
115
        "label layout": "para",
116
        "label char size": 1.9,
117
        "label font": 1,
118
        "label color": 1,
119
        "label place": "normal",
120
        "tick major": 0.5,
121
        "tick minor ticks": 1,
122
        "tick default": 6,
123
        "tick place rounded": "true",
124
        "tick": "in",
125
        "tick major size": 1.0,
126
        "tick major color": 1,
127
        "tick major linewidth": 1.0,
128
        "tick major linestyle": 1,
129
        "tick major grid": "off",
130
        "tick minor color": 1,
131
        "tick minor linewidth": 1.0,
132
        "tick minor linestyle": 1,
133
        "tick minor grid": "off",
134
        "tick minor size": 0.5,
135
        "ticklabel": "on",
136
        "ticklabel format": "general",
137
        "ticklabel prec": 5,
138
        "ticklabel formula": '""',
139
        "ticklabel append": '""',
140
        "ticklabel prepend": '""',
141
        "ticklabel angle": 0,
142
        "ticklabel skip": 0,
143
        "ticklabel stagger": 0,
144
        "ticklabel place": "normal",
145
        "ticklabel offset": "0.00 , 0.01",
146
        "ticklabel start type": "auto",
147
        "ticklabel start": 0.0,
148
        "ticklabel stop type": "auto",
149
        "ticklabel stop": 0.0,
150
        "ticklabel char size": 1.36,
151
        "ticklabel font": 0,
152
        "ticklabel color": 1,
153
        "tick place": "both",
154
        "tick spec type": "none",
155
    }
156

157
    blegend = {
5✔
158
        "": "on",
159
        "loctype": "view",
160
        " ": "0.85, 0.8",
161
        "box color": 1,
162
        "box pattern": 1,
163
        "box linewidth": 1.0,
164
        "box linestyle": 1,
165
        "box fill color": 0,
166
        "box fill pattern": 1,
167
        "font": 0,
168
        "char size": 1.0,
169
        "color": 1,
170
        "length": 4,
171
        "vgap": 1,
172
        "hgap": 1,
173
        "invert": "false",
174
    }
175

176
    bframe = {
5✔
177
        "type": 0,
178
        "linestyle": 1,
179
        "linewidth": 1.0,
180
        "color": 1,
181
        "pattern": 1,
182
        "background color": 0,
183
        "background pattern": 0,
184
    }
185

186
    def __init__(self, n=0):
5✔
187
        self.default = self.bdefault.copy()
×
188
        self.world = self.bworld.copy()
×
189
        self.view = self.bview.copy()
×
190
        self.title = self.btitle.copy()
×
191
        self.subtitle = self.bsubtitle.copy()
×
192
        self.xaxes = self.baxes.copy()
×
193
        self.yaxes = self.baxes.copy()
×
194

195
        self.xaxis = self.baxis.copy()
×
196
        self.yaxis = self.baxis.copy()
×
197
        self.legend = self.blegend.copy()
×
198
        self.frame = self.bframe.copy()
×
199

200
        self.name = n
×
201

202
    def __str__(self):
5✔
203
        return (
×
204
            (
205
                (
206
                    (
207
                        (
208
                            (
209
                                (
210
                                    (
211
                                        (
212
                                            (
213
                                                (
214
                                                    (
215
                                                        "\n".join(
216
                                                            [
217
                                                                f"@g{str(self.name)} {a} {str(b)}"
218
                                                                for a, b in list(
219
                                                                    self.default.items()
220
                                                                )
221
                                                            ]
222
                                                        )
223
                                                        + "\n@with g"
224
                                                    )
225
                                                    + str(self.name)
226
                                                    + "\n"
227
                                                )
228
                                                + "\n".join(
229
                                                    [
230
                                                        f"@    world {a} {str(b)}"
231
                                                        for a, b in list(
232
                                                            self.world.items()
233
                                                        )
234
                                                    ]
235
                                                )
236
                                                + "\n"
237
                                            )
238
                                            + "\n".join(
239
                                                [
240
                                                    f"@    view {a} {str(b)}"
241
                                                    for a, b in list(self.view.items())
242
                                                ]
243
                                            )
244
                                            + "\n"
245
                                        )
246
                                        + "\n".join(
247
                                            [
248
                                                f"@    title {a} {str(b)}"
249
                                                for a, b in list(self.title.items())
250
                                            ]
251
                                        )
252
                                        + "\n"
253
                                    )
254
                                    + "\n".join(
255
                                        [
256
                                            f"@    subtitle {a} {str(b)}"
257
                                            for a, b in list(self.subtitle.items())
258
                                        ]
259
                                    )
260
                                    + "\n"
261
                                )
262
                                + "\n".join(
263
                                    [
264
                                        f"@    xaxes {a} {str(b)}"
265
                                        for a, b in list(self.xaxes.items())
266
                                    ]
267
                                )
268
                                + "\n"
269
                            )
270
                            + "\n".join(
271
                                [
272
                                    f"@    yaxes {a} {str(b)}"
273
                                    for a, b in list(self.yaxes.items())
274
                                ]
275
                            )
276
                            + "\n"
277
                        )
278
                        + "\n".join(
279
                            [
280
                                f"@    xaxis {a} {str(b)}"
281
                                for a, b in list(self.xaxis.items())
282
                            ]
283
                        )
284
                        + "\n"
285
                    )
286
                    + "\n".join(
287
                        [
288
                            f"@    yaxis {a} {str(b)}"
289
                            for a, b in list(self.yaxis.items())
290
                        ]
291
                    )
292
                    + "\n"
293
                )
294
                + "\n".join(
295
                    [f"@    legend {a} {str(b)}" for a, b in list(self.legend.items())]
296
                )
297
                + "\n"
298
            )
299
            + "\n".join(
300
                [f"@    frame {a} {str(b)}" for a, b in list(self.frame.items())]
301
            )
302
            + "\n"
303
        )
304

305

306
#
307
# IMPLEMENTS A GRACE DOC        ################
308
#
309
class GraceDocument:
5✔
310
    #
311
    #:::~ Constants
312
    basdefault = {"version": 50100}
5✔
313

314
    baspage = {"size": "792,612", "scroll": "5%", "inout": "5%"}
5✔
315

316
    basmap_font = {
5✔
317
        0: ('"Times-Roman"', '"Times-Roman"'),
318
        1: ('"Times-Italic"', '"Times-Italic"'),
319
        2: ('"Times-Bold"', '"Times-Bold"'),
320
        3: ('"Times-BoldItalic"', '"Times-BoldItalic"'),
321
        4: ('"Helvetica"', '"Helvetica"'),
322
        5: ('"Helvetica-Oblique"', '"Helvetica-Oblique"'),
323
        6: ('"Helvetica-Bold"', '"Helvetica-Bold"'),
324
        7: ('"Helvetica-BoldOblique"', '"Helvetica-BoldOblique"'),
325
        8: ('"Courier"', '"Courier"'),
326
        9: ('"Courier-Oblique"', '"Courier-Oblique"'),
327
        10: ('"Courier-Bold"', '"Courier-Bold"'),
328
        11: ('"Courier-BoldOblique"', '"Courier-BoldOblique"'),
329
        12: ('"Symbol"', '"Symbol"'),
330
        13: ('"ZapfDingbats"', '"ZapfDingbats"'),
331
    }
332

333
    basmap_color = {
5✔
334
        0: ((255, 255, 255), '"white"'),
335
        1: ((0, 0, 0), '"black"'),
336
        2: ((255, 0, 0), '"red"'),
337
        3: ((0, 255, 0), '"green"'),
338
        4: ((0, 0, 255), '"blue"'),
339
        5: ((255, 255, 0), '"yellow"'),
340
        6: ((188, 143, 143), '"brown"'),
341
        7: ((220, 220, 220), '"grey"'),
342
        8: ((148, 0, 211), '"violet"'),
343
        9: ((0, 255, 255), '"cyan"'),
344
        10: ((255, 0, 255), '"magenta"'),
345
        11: ((255, 165, 0), '"orange"'),
346
        12: ((114, 33, 188), '"indigo"'),
347
        13: ((103, 7, 72), '"maroon"'),
348
        14: ((64, 224, 208), '"turquoise"'),
349
        15: ((0, 139, 0), '"green4"'),
350
    }
351

352
    def __init__(self):
5✔
353
        # copies all the dicts
354
        self.datasets = {}
×
355
        self.graph = GraceGraph()
×
356

357
        self.default = self.basdefault.copy()
×
358
        self.page = self.baspage.copy()
×
359
        self.map_font = self.basmap_font.copy()
×
360
        self.map_color = self.basmap_color.copy()
×
361

362
    def __str__(self):
5✔
363
        return (
×
364
            (
365
                (
366
                    "\n".join([f"@{a} {str(b)}" for a, b in list(self.default.items())])
367
                    + "\n"
368
                )
369
                + "\n".join([f"@page {a} {str(b)}" for a, b in list(self.page.items())])
370
                + "\n"
371
            )
372
            + "\n".join(
373
                [
374
                    f"@map font {str(a)} to {b}, {c}"
375
                    for a, (b, c) in list(self.map_font.items())
376
                ]
377
            )
378
            + "\n"
379
        ) + "\n".join(
380
            [
381
                "@map color "
382
                + str(a)
383
                + " to ("
384
                + str(b)
385
                + ","
386
                + str(c)
387
                + ","
388
                + str(d)
389
                + "), "
390
                + str(e)
391
                for a, ((b, c, d), e) in list(self.map_color.items())
392
            ]
393
        )
394

395
    #
396
    #
397
    #
398
    #
399
    #
400
    #
401

402
    def set_data(self, ls, legend="", graphType="xy"):
5✔
403
        newds = GraceDataSet(len(self.datasets), ls, graphType)
×
404
        newds.symbol[" "] = len(self.datasets) + 1
×
405
        newds.line["color"] = len(self.datasets) + 1
×
406
        newds.symbol["color"] = len(self.datasets) + 1
×
407
        if legend == "":
×
408
            legend = f"y{len(self.datasets)}"
×
409
        legend = f'"{legend}"'
×
410
        newds.default["legend"] = legend
×
411
        self.datasets[len(self.datasets)] = newds
×
412

413
    def set_world(self, minx, maxx, miny, maxy, tickx=None, ticky=None):
5✔
414
        if minx == maxx:
×
415
            minx -= 0.5
×
416
            maxx += 0.5
×
417

418
        if miny == maxy:
×
419
            miny -= 0.5
×
420
            maxy += 0.5
×
421
        if not tickx:
×
422
            tickx = (maxx - minx) // 4.0
×
423
        if not ticky:
×
424
            ticky = (maxy - miny) // 4.0
×
425

426
        self.graph.world["xmin"] = minx
×
427
        self.graph.world["xmax"] = maxx
×
428
        self.graph.world["ymin"] = miny
×
429
        self.graph.world["ymax"] = maxy
×
430

431
        self.graph.xaxis["tick major"] = tickx
×
432
        self.graph.yaxis["tick major"] = ticky
×
433

434
    def getRoundedValues(self, scale, a2, a1):
5✔
435
        sign1 = sign2 = 1
×
436
        if a1 < 0:
×
437
            sign1 = -1
×
438
        if a2 < 0:
×
439
            sign2 = -1
×
440
        aa1 = abs(a1)
×
441
        aa2 = abs(a2)
×
442
        scale1 = math.floor(math.log10(max(1e-10, aa1)))
×
443
        scale2 = math.floor(math.log10(max(1e-10, aa2)))
×
444

445
        int1 = eval(f"{aa1:e}"[0]) + 1
×
446
        int2 = eval(f"{aa2:e}"[0]) - 1
×
447

448
        if scale == "Normal":
×
449
            propose1 = sign1 * int1 * 10**scale1
×
450
            propose2 = sign2 * int2 * 10**scale2
×
451
            abs((int1 * 10**scale1 - int2 * 10**scale2) // 4)
×
452
            return propose2, propose1, propose1 // 4
×
453
        propose1 = 10 ** (scale1 + 1)
×
454
        propose2 = 10 ** (scale2 - 1)
×
455

456
        return propose2, propose1, 10 ** int((scale1 - scale2) + 1)
×
457

458
    def autoscale(self, autoscaleaxis="xy"):
5✔
459
        minx = 1e10
×
460
        maxx = -1e10
×
461
        miny = 1e10
×
462
        maxy = -1e10
×
463

464
        for it in list(self.datasets.values()):
×
465
            colx = [a[0] for a in it.data]
×
466
            coly = [a[1] for a in it.data]
×
467
            if colx:
×
468
                minx = min(minx, min(colx))
×
469
                maxx = max(maxx, max(colx))
×
470
            if coly:
×
471
                miny = min(miny, min(coly))
×
472
                maxy = max(maxy, max(coly))
×
473

474
        minx, maxx, tickx = self.getRoundedValues(self.graph.xaxes["scale"], minx, maxx)
×
475
        miny, maxy, ticky = self.getRoundedValues(self.graph.yaxes["scale"], miny, maxy)
×
476
        if autoscaleaxis.find("x") >= 0:
×
477
            self.graph.world["xmin"] = minx
×
478
            self.graph.world["xmax"] = maxx
×
479
            self.graph.xaxis["tick major"] = tickx
×
480

481
        if autoscaleaxis.find("y") >= 0:
×
482
            self.graph.world["ymin"] = miny
×
483
            self.graph.world["ymax"] = maxy
×
484

485
            self.graph.yaxis["tick major"] = ticky
×
486

487
    def set_labels(self, stx, sty):
5✔
488
        self.graph.xaxis["label"] = f'"{stx}"'
×
489
        self.graph.yaxis["label"] = f'"{sty}"'
×
490

491
    def set_title(self, st):
5✔
492
        self.graph.subtitle[" "] = f'"{st}"'
×
493

494
    def dump(self, outStream=None):
5✔
495
        old_stdout = sys.stdout
×
496
        if outStream:
×
497
            sys.stdout = outStream
×
498

499
        print(self)
×
500
        print(self.graph)
×
501
        for i in list(self.datasets.keys()):
×
502
            print(self.datasets[i])
×
503
        for i in list(self.datasets.keys()):
×
504
            print(f"@target G{str(self.graph.name)}.S{str(i)}")
×
505
            print(f"@type {str(self.datasets[i].default['type'])}")
×
506
            print("\n".join(["\t".join(map(str, a)) for a in self.datasets[i].data]))
×
507
            print("\n&")
×
508

509
        sys.stdout = old_stdout
×
510

511
    def set_scale(self, x="Normal", y="Normal"):
5✔
512
        self.graph.xaxes["scale"] = x
×
513
        self.graph.yaxes["scale"] = y
×
514

515

516
#  Example of use
517
#
518
#  g1=GraceDocument()
519
#
520
#  g1.set_data([[0.12,0.001],[0.1,0.5],[0.2,0.6],[0.3,0.8]],"foo")
521
#  g1.set_data([[.1,.1],[0.1,0.4],[0.2,0.5],[0.3,0.9]])
522

523
#  g1.autoscale()
524
#  g1.set_labels("x","y")
525
#  g1.set_scale(x="Logarithmic",y="Logarithmic")
526
# g1.set_world(-1,1,-1,1)
527
#  g1.set_title("Foo")
528
#  g1.dump()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc