Gnuplot: frequency per min - bash

I have a sample log file with 1000 lines, that looks like this,
TIME,STATUS
09:00,OK
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:01,OK
09:01,OK
09:01,OK
09:01,PERM
09:01,TEMP
09:01,TEMP
09:02,OK
09:02,TEMP
09:02,TEMP
09:03,OK
09:03,PERM
09:03,PERM
09:03,TEMP
09:03,TEMP
09:04,OK
09:04,PERM
09:04,PERM
09:04,TEMP
09:04,TEMP
09:04,TEMP
09:05,OK
09:05,OK
09:05,OK
09:05,PERM
09:05,TEMP
09:05,TEMP
09:05,TEMP
09:05,TEMP
09:06,OK
09:06,OK
09:06,PERM
09:06,PERM
09:06,PERM
09:06,PERM
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:07,OK
09:07,OK
09:07,TEMP
09:07,TEMP
09:07,TEMP
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,TEMP
09:08,TEMP
09:08,TEMP
09:08,TEMP
09:09,OK
09:09,OK
09:09,OK
09:09,PERM
09:10,OK
09:10,PERM
09:10,PERM
09:10,TEMP
09:11,OK
09:11,OK
09:11,OK
09:11,OK
09:11,PERM
09:11,PERM
09:11,PERM
09:11,PERM
09:11,TEMP
09:11,TEMP
09:11,TEMP
09:12,PERM
09:12,TEMP
09:12,TEMP
09:13,OK
09:13,OK
09:13,OK
09:13,OK
09:13,OK
09:13,PERM
09:13,PERM
09:13,PERM
09:13,TEMP
09:13,TEMP
09:14,OK
09:14,OK
09:14,OK
09:14,PERM
09:14,PERM
09:14,PERM
09:14,PERM
09:14,TEMP
09:16,OK
09:16,OK
09:16,OK
09:16,PERM
09:16,PERM
09:16,TEMP
09:16,TEMP
09:17,OK
09:17,OK
09:17,PERM
09:17,PERM
09:18,OK
09:18,OK
09:18,OK
09:18,OK
09:18,OK
09:18,PERM
09:18,PERM
09:18,TEMP
09:18,TEMP
09:18,TEMP
09:19,OK
09:19,OK
09:19,OK
09:19,OK
09:19,OK
09:19,PERM
09:20,OK
09:20,OK
09:20,PERM
09:20,PERM
09:20,TEMP
09:20,TEMP
09:21,OK
09:21,OK
09:21,OK
09:21,PERM
09:21,TEMP
09:22,OK
09:22,OK
09:22,PERM
09:22,PERM
09:22,TEMP
09:22,TEMP
09:23,OK
09:23,PERM
09:23,PERM
09:23,PERM
09:23,TEMP
09:23,TEMP
09:23,TEMP
09:24,PERM
09:24,PERM
09:24,PERM
09:25,OK
09:25,OK
09:25,PERM
09:25,TEMP
09:26,OK
09:26,OK
09:26,OK
09:26,OK
09:26,OK
09:26,PERM
09:26,TEMP
09:27,OK
09:27,OK
09:27,OK
09:27,PERM
09:27,PERM
09:27,TEMP
09:27,TEMP
09:27,TEMP
09:28,PERM
09:28,PERM
09:28,PERM
09:28,PERM
09:29,OK
...
while the final file will have 10K lines in the same time frame.
I need to create a graph to show number of statuses per minute for TEMP, PERM and OK. So I would like to use a line for the status (TEMP, PERM and OK), plot time on the X axis, and frequency of occurrence on the Y axis.
I installed Gnuplot only 2 days ago on my Ubuntu 20.04.4 LTS from the standard repo:
bi#green:bin$ apt list gnuplot* 2>/dev/null | grep installed
gnuplot-data/focal,focal,now 5.2.8+dfsg1-2 all [installed,automatic]
gnuplot-qt/focal,now 5.2.8+dfsg1-2 amd64 [installed,automatic]
gnuplot/focal,focal,now 5.2.8+dfsg1-2 all [installed]
and so far I haven't managed more than this,
#!/bin/bash
x=logoutcol
cat $x
gnuplot -p <<-EOF
#set ytics scale 0
#set yzeroaxis
reset
set format x "%H:%M" time
set xdata time
set yrange [0:*]
set ylabel "Occurences"
set ytics 2
#set margin at screen 0.95
binwidth=60
bin(val) = binwidth * floor(val/binwidth)
set boxwidth binwidth
set datafile separator ","
set term png
set output "$x.png"
plot "$x" using (bin(timecolumn(1,"%H%M"))):(2) smooth freq with boxes
EOF
shotwell $x.png
rm $x.png
which produces this:
Any help will be much appreciated.

I am pretty sure that there was an almost identical question here on SO, however, it seems I can't find it maybe due to my incapability of finding the right keywords for SO's search function.
The key point is the boolean expression (strcol(2) eq word(myKeys,i)) together with smooth frequency. If the value of the second column is identical to your keyword the expression results in 1, and 0 otherwise.
You don't need bins like in creating other histograms because you want a bin of 1 minute (and your time resolution is already 1 minute).
Check the following example as starting point for further optimization.
Script:
### count occurrences of keywords
reset session
$Data <<EOD
# TIME,STATUS
09:00,OK
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:00,TEMP
09:01,OK
09:01,OK
09:01,OK
09:01,PERM
09:01,TEMP
09:01,TEMP
09:02,OK
09:02,TEMP
09:02,TEMP
09:03,OK
09:03,PERM
09:03,PERM
09:03,TEMP
09:03,TEMP
09:04,OK
09:04,PERM
09:04,PERM
09:04,TEMP
09:04,TEMP
09:04,TEMP
09:05,OK
09:05,OK
09:05,OK
09:05,PERM
09:05,TEMP
09:05,TEMP
09:05,TEMP
09:05,TEMP
09:06,OK
09:06,OK
09:06,PERM
09:06,PERM
09:06,PERM
09:06,PERM
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:06,TEMP
09:07,OK
09:07,OK
09:07,TEMP
09:07,TEMP
09:07,TEMP
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,OK
09:08,TEMP
09:08,TEMP
09:08,TEMP
09:08,TEMP
09:09,OK
09:09,OK
09:09,OK
09:09,PERM
09:10,OK
09:10,PERM
09:10,PERM
09:10,TEMP
09:11,OK
09:11,OK
09:11,OK
09:11,OK
09:11,PERM
09:11,PERM
09:11,PERM
09:11,PERM
09:11,TEMP
09:11,TEMP
09:11,TEMP
09:12,PERM
09:12,TEMP
09:12,TEMP
09:13,OK
09:13,OK
09:13,OK
09:13,OK
09:13,OK
09:13,PERM
09:13,PERM
09:13,PERM
09:13,TEMP
09:13,TEMP
09:14,OK
09:14,OK
09:14,OK
09:14,PERM
09:14,PERM
09:14,PERM
09:14,PERM
09:14,TEMP
09:16,OK
09:16,OK
09:16,OK
09:16,PERM
09:16,PERM
09:16,TEMP
09:16,TEMP
09:17,OK
09:17,OK
09:17,PERM
09:17,PERM
09:18,OK
09:18,OK
09:18,OK
09:18,OK
09:18,OK
09:18,PERM
09:18,PERM
09:18,TEMP
09:18,TEMP
09:18,TEMP
09:19,OK
09:19,OK
09:19,OK
09:19,OK
09:19,OK
09:19,PERM
09:20,OK
09:20,OK
09:20,PERM
09:20,PERM
09:20,TEMP
09:20,TEMP
09:21,OK
09:21,OK
09:21,OK
09:21,PERM
09:21,TEMP
09:22,OK
09:22,OK
09:22,PERM
09:22,PERM
09:22,TEMP
09:22,TEMP
09:23,OK
09:23,PERM
09:23,PERM
09:23,PERM
09:23,TEMP
09:23,TEMP
09:23,TEMP
09:24,PERM
09:24,PERM
09:24,PERM
09:25,OK
09:25,OK
09:25,PERM
09:25,TEMP
09:26,OK
09:26,OK
09:26,OK
09:26,OK
09:26,OK
09:26,PERM
09:26,TEMP
09:27,OK
09:27,OK
09:27,OK
09:27,PERM
09:27,PERM
09:27,TEMP
09:27,TEMP
09:27,TEMP
09:28,PERM
09:28,PERM
09:28,PERM
09:28,PERM
09:29,OK
EOD
set datafile separator comma
myKeys = "OK TEMP PERM"
myKey(i) = word(myKeys,i)
myTimeFmt = "%H:%M"
set format x myTimeFmt timedate
plot for [i=1:words(myKeys)] $Data u (timecolumn(1,myTimeFmt)):(strcol(2) eq word(myKeys,i)) smooth freq w lp pt 7 ti word(myKeys,i)
### end of script
Result:

Related

Why are all the values same in ARIMA model predictions?

The data set had 1511 observations. I used the first 1400 values to fit ARIMA model of order (1,1,9), keeping the rest for predictions. But when I look at the predictions, apart from the first 16 values all the remaining values are the same. Here's what I tried:
model2=ARIMA(tstrain,order=(1,1,9))
fitted_model2=model2.fit()
And for prediction:
start=len(tstrain)
end=len(tstrain)+len(tstest)-1
predictions=fitted_model2.predict(start,end,typ='levels')
Here tstrain and tstest are the train and test sets.
predictions.head(30)
1400 214.097742
1401 214.689674
1402 214.820804
1403 215.621131
1404 215.244980
1405 215.349230
1406 215.392444
1407 215.022312
1408 215.020736
1409 215.021384
1410 215.021118
1411 215.021227
1412 215.021182
1413 215.021201
1414 215.021193
1415 215.021196
1416 215.021195
1417 215.021195
1418 215.021195
1419 215.021195
1420 215.021195
1421 215.021195
1422 215.021195
1423 215.021195
1424 215.021195
1425 215.021195
1426 215.021195
1427 215.021195
1428 215.021195
1429 215.021195
Please help me out here. What am I missing?

Peak removal/interpolation

I have some signals that look like following:
I would like to remove the two peaks by doing linear interpolation, so I can get something like this:
where the orange line segment should replace the two peaks after the interpolation.
I understand this very difficult because even for human being you can do it differently like this:
or even this:
So it is really a challenging problem, and might not be a definite answer, but I just think something that looks comfortable, natural, and capturing details as much as possible.
I tried using mask, but edge is pretty noisy, and often times the width of the mask is far from the actual width of the spike. I also tried smoothing, and then applying finite difference to detect the starting and end position of the edges, but again it really does not as accurate as it should be.
I am wondering anyone has experience dealing with this problem? What algorithm I should use? Any literature describing the processing?
For this articular data set, the points are here:
-0.0568
-0.0536
-0.0528
-0.0500
-0.0379
-0.0169
-0.0005
0.0127
0.0075
0.0133
0.0123
0.0130
0.0084
0.0126
0.0144
0.0030
0.0093
0.0168
0.0101
0.0096
0.0078
0.0117
0.0106
0.0138
0.0128
0.0059
0.0075
0.0062
0.0056
0.0017
0.0037
0.0173
0.0114
0.0143
0.0113
0.0117
0.0040
0.0118
0.0085
0.0079
0.0063
0.0152
0.0064
0.0024
0.0058
0.0041
0.0101
0.0086
0.0086
0.0154
0.0018
0.0130
0.0094
0.0094
0.0096
0.0103
0.0170
0.0081
0.0035
0.0138
0.0123
0.0031
0.0120
0.0039
0.0043
0.0063
0.0191
0.0023
0.0165
0.0174
0.0129
0.0135
0.0153
0.0100
0.0066
0.0135
0.0109
0.0038
0.0129
0.0084
0.0095
0.0109
0.0121
0.0077
0.0116
0.0128
0.0101
0.0158
0.0134
0.0042
0.0054
0.0063
0.0059
0.0136
0.0029
0.0139
0.0104
0.0215
0.0180
0.0153
0.0187
0.0138
0.0236
0.0190
0.0267
0.0209
0.0112
0.0108
0.0238
0.0280
0.0266
0.0300
0.0256
0.0278
0.0260
0.0263
0.0257
0.0334
0.0309
0.0301
0.0325
0.0280
0.0300
0.0286
0.0359
0.0317
0.0381
0.0348
0.0422
0.0389
0.0491
0.1754
0.4760
0.8146
1.0172
1.0757
0.9471
0.8509
0.7955
0.7526
0.7314
0.7092
0.7073
0.6906
0.6787
0.6654
0.6646
0.6553
0.6420
0.6385
0.6390
0.6373
0.6305
0.6216
0.6218
0.6212
0.6108
0.6161
0.6054
0.6106
0.6006
0.6032
0.6100
0.6006
0.5975
0.6042
0.6027
0.6044
0.6138
0.6106
0.6051
0.6084
0.6065
0.6212
0.6207
0.6306
0.6270
0.6484
0.6605
0.6742
0.6828
0.6972
0.7076
0.7062
0.6918
0.6905
0.6759
0.6459
0.6134
0.5989
0.5790
0.5663
0.5595
0.5609
0.5467
0.5442
0.5400
0.5317
0.5267
0.5182
0.5187
0.5101
0.4975
0.4951
0.4907
0.4855
0.4745
0.4505
0.4604
0.5814
0.7370
0.8355
0.9012
0.9498
0.9783
1.0188
1.0496
1.0727
1.1201
1.1639
1.2085
1.2465
1.2691
1.3170
1.3553
1.4211
1.4715
1.5169
1.5694
1.5963
1.6341
1.6722
1.7125
1.7388
1.7725
1.8040
1.8505
1.8817
1.9064
1.9337
1.9837
1.9992
2.0385
2.0719
2.1062
2.1415
2.1767
2.2151
2.2385
2.2427
2.2591
2.2856
2.3185
2.3572
2.3638
2.3905
2.4077
2.4429
2.4662
2.4841
2.4977
2.5204
2.5549
2.5709
2.5810
2.6063
2.6301
2.6245
2.6519
2.6594
2.6707
2.6836
2.7045
2.7642
2.8208
2.8278
2.8821
2.8950
2.9526
3.0908
3.1539
3.1935
3.1544
3.1317
3.1717
3.1677
3.1526
3.1489
3.1292
3.1129
3.1293
3.1561
3.1556
3.1857
3.1856
3.1327
3.1160
3.0868
3.1122
3.1407
3.1970
3.2136
3.2211
3.2376
3.2222
3.2521
3.3035
3.4006
3.5001
3.5602
3.5756
3.6020
3.6014
3.5830
3.5640
3.5016
3.4363
3.3618
3.3640
3.4059
3.4812
3.4943
3.5307
3.5735
3.5193
3.5079
3.5052
3.4986
3.4955
3.4303
3.3649
3.3260
3.2755
3.1902
3.0984
3.0574
3.0174
2.9852
2.9648
2.9462
2.9398
2.9393
2.9490
2.9268
2.9042
2.9143
2.9065
2.9340
3.0154
3.0141
3.0202
3.0782
3.1301
3.1803
3.2108
3.2176
3.2588
3.2822
3.3173
3.3732
3.3976
3.4492
3.4675
3.5090
3.5702
3.5230
3.4513
3.3371
3.2674
3.2867
3.3829
3.4563
3.5314
3.5805
3.6043
3.6157
3.6267
3.6450
3.6317
3.5860
3.4163
3.3502
3.3793
3.3572
3.5124
3.8337
4.2717
4.6394
4.8060
4.7245
4.5504
4.3687
4.3737
4.6887
5.4021
6.0749
6.5674
6.7279
6.8391
6.8456
6.8219
6.8410
6.7609
6.5246
5.7718
4.4415
3.5784
3.4720
3.3728
3.4125
3.5051
3.4689
3.2906
3.2217
3.1706
3.1218
3.3428
3.7802
4.5759
5.3222
5.6758
6.0151
6.1276
6.1647
6.0552
5.9937
5.9784
5.7171
5.0609
4.8232
4.2979
3.7390
3.3099
2.9529
2.6971
2.6021
2.5640
2.6019
2.6515
2.6531
2.6558
2.7166
2.7408
2.8190
2.8535
2.8639
2.8700
2.7703
2.6353
2.5842
2.5137
2.4497
2.3751
2.3382
2.1323
1.8490
1.6700
1.5507
1.4733
1.4242
1.3643
1.2997
1.2203
1.1462
1.0776
0.9962
0.8265
0.4876
0.1304
0.0341
0.0296
0.0263
0.0261
0.0247
0.0232
0.0256
0.0214
0.0232
0.0208
0.0205
0.0182
0.0186
0.0169
0.0236
0.0198
0.0157
0.0143
0.0179
0.0118
0.0136
0.0139
0.0115
0.0093
0.0096
0.0107
0.0132
0.0090
0.0074
0.0103
0.0071
0.0086
0.0069
0.0052
0.0069
0.0062
0.0115
0.0068
0.0179
0.0121
0.0092
0.0098
0.0138
0.0081
0.0055
0.0077
0.0048
0.0059
0.0052
0.0095
0.0087
0.0114
0.0036
0.0080
0.0110
0.0049
0.0079
0.0065
0.0080
0.0110
0.0059
0.0158
0.0146
0.0095
0.0045
0.0081
0.0116
0.0091
0.0080
0.0095
0.0105
0.0077
0.0098
0.0138
0.0069
0.0118
0.0087
0.0046
0.0056
0.0072
0.0136
0.0110
0.0054
0.0090
0.0147
0.0102
0.0066
0.0102
0.0092
0.0045
0.0089
0.0134
0.0222
0.0336
0.0362
0.0464
0.0354
0.0420
0.0445
0.0400
0.0338
0.0369
0.0441
0.0397
0.0383
0.0353
0.0319
0.0342
0.0366
0.0414
0.0401
0.0452
0.0507
0.0444
0.0358
0.0432
0.0394
0.0406
0.0441
0.0386
0.0410
0.0409
0.0330
0.0282
0.0186
0.0137
0.0103
0.0033
0.0101
0.0080
0.0141
0.0097
0.0102
0.0092
0.0094
0.0055
0.0119
0.0140
0.0116
0.0077
0.0148
0.0063
0.0021
0.0048
0.0033
0.0123
0.0109
0.0108
0.0168
0.0112
0.0046
0.0085
0.0068
0.0091
0.0096
0.0061
0.0063
0.0082
0.0084
0.0094
0.0070
0.0087
0.0042
0.0077
0.0060
0.0123
0.0127
0.0107
0.0019
0.0082
0.0051
0.0068
0.0064
0.0061
0.0057
0.0094
0.0162
0.0141
0.0165
0.0065
0.0121
0.0047
0.0120
0.0076
0.0050
0.0080
0.0139
0.0023
0.0139
0.0123
0.0087
0.0151
0.0060
0.0103
0.0039
0.0042
0.0043
-0.0011
0.0080
0.0028
0.0074
0.0042
0.0018
0.0087
0.0049
0.0076
0.0156
0.0076
0.0091
0.0056
0.0091
0.0075
0.0012
0.0056
0.0123
0.0137
0.0087
0.0025
0.0084
0.0104
0.0086
-0.0008
0.0072
0.0110
0.0096
0.0081
0.0126
0.0020
0.0098
0.0070
0.0041
0.0027
0.0075
0.0040
0.0069
0.0098
0.0180
0.0143
0.0182
0.0120
0.0003
-0.0011
0.0063
0.0104
0.0043
0.0128
0.0075
0.0051
0.0065
0.0063
0.0005
0.0097
0.0099
0.0084
0.0105
0.0017
0.0080
0.0140
0.0054
0.0048
The easiest is to remove the points above a given threshold, without replacing them.
You could instead try to remove the points that exhibit a difference superior to a certain value (it seems that you should only consider the positive difference in the special case of your problem), and not replace them; this may require several passes to erase the peaks.
There are more complicated approached if this doesn't work.

Cluster faces into groups

The data:
2328568501515627770 2328529760910617771 100.0
2328529760910617771 2328568501515627770 100.0
2328529760910617771 2328530052968393930 99.976524
2328529760910617771 2328514835899260501 99.69356
2328529760910617771 2328515153726841781 98.76936
2328529760910617771 2328515132252005201 98.741165
2328529760910617771 2328515149431874457 98.6116
2328529760910617771 2328515158021809084 98.47021
2328529760910617771 2328515145136907144 98.156456
2328529760910617771 2328515089302332012 97.53229
2328529760910617771 2328515153726841775 97.449005
2328568501515627770 2328530052968393930 99.976524
2328530052968393930 2328529760910617771 99.976524
2328530052968393930 2328568501515627770 99.976524
2328530052968393930 2328514835899260501 99.68713
2328530052968393930 2328515132252005201 98.70858
2328530052968393930 2328515158021809084 98.612816
2328530052968393930 2328515153726841781 98.59485
2328530052968393930 2328515149431874457 98.43197
2328530052968393930 2328515145136907144 98.12278
2328530052968393930 2328515089302332012 97.5466
2328530052968393930 2328515153726841775 97.299934
2328515153726841775 2328568501515627770 97.44901
2328515153726841775 2328530052968393930 97.299934
2328515153726841775 2328514835899260501 97.28116
2328515153726841775 2328515149431874457 96.93521
First column is face id
Second column is matched face id
Third column is similarity score
EDIT: How can I apply hierarchical clustering on the above dataset.
All similarities are way larger than 80%.
So everything is one cluster.

Convert string in Image and blit in Pyglet

I want to convert a string that contains the data of an PNG image in image again and blit in Pyglet. But I didn't. Show this error:
Error:
File "a14.py", line 52, in __init__
self.sprite = pyglet.sprite.Sprite(img, 0, 0)
File "/usr/local/lib/python3.4/dist-packages/pyglet-1.2.4-py3.4.egg/pyglet/sprite.py", line 234, in __init__
AttributeError: 'bytes' object has no attribute 'get_texture'
Code: -----------------------------------------------------------------
imageData:
8950 4e47 0d0a 1a0a 0000 000d 4948 4452
0000 0010 0000 001c 0806 0000 0068 313f
1a00 0000 0473 4249 5408 0808 087c 0864
8800 0002 0249 4441 5438 8d95 552d 8cdb
3018 7dad 0a02 47a6 2bec 58d8 32b4 c2b0
8d4c 0a8c 74c4 5649 d819 e6c0 a181 05e6
5848 1593 d319 562a c958 60c7 3c66 5898
69a4 d0ac 0349 9c38 3fd5 ee49 561c e77b
ef7b 9fe3 2f59 ecf3 fc8a 191c 291d ad7d
cb73 eb7e 758b 287c 562f 6400 a27a 1a52
6a89 8c04 8e94 1a62 515d e0c6 0a1b 6c81
2241 e145 a364 8b7e 092d b9a8 2e00 8040
f1b9 eab0 6f5c 2ca7 c881 e237 c900 b06b
ca5c b664 e212 437e 0b96 730f 348b a0b5
ae47 6f3e 2c61 0500 8245 0009 e178 be21
b770 1c07 5a96 f555 6b43 340e ee7f ff42
5a1d c616 4838 e9ec fed3 07fc fdf9 3851
0217 5dd6 34ab 27ea 54bb e102 5a96 803a
8d04 572f 1f3f 83f5 ea76 d2ac 0e06 e078
3e92 700d b60e ac04 169a 7370 0570 d52c
32a3 5dbb 35f6 797e 5d36 22b6 7500 5a96
c6c9 4c62 0083 93b8 a3b4 b33c 83b4 3ae0
fd97 1fdd 1e0c 558f 94a2 70eb f7cd 9b43
b50d d726 a64f 1e09 f41b 292c 5310 9740
7905 183f 770e c8c6 12b1 7a81 b8c4 3c10
3e1b 9101 80f1 f3cc 3968 d076 e2ff c208
482f 3135 b722 aefc 8a94 6c2c c2b0 046b
0fa4 9780 cbd8 7426 571c 5040 8a4e 84f1
335e 5e5f a705 36c1 3b48 d422 2d3c 7542
db83 a590 c8b6 299e 1eee a605 5a91 4b90
c10f bd9b b5b7 307b f0f4 7087 f3a1 dbc0
5248 33e7 6189 5248 9c0f 172b fba4 833e
fa22 7358 0cff 0bdf 9fff cc06 0fb3 4f3a
88dc 7414 148b 0a49 b8c6 8e56 e32f d254
f010 5a31 c4a2 8256 cc7c 8d67 1d4c 89c4
a276 e54c b8fb 0775 650d 8c04 f5e9 f200
0000 0049 454e 44ae 4260 82
the code that I tried:
import pyglet
import binascii
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
imgData = #ABOVE /\, IN MY CODE IMAGEDATA IS HERE
img = "".join(imgData.split())
img = binascii.unhexlify(img)
self.sprite = pyglet.sprite.Sprite(img, 0, 0)
def on_draw(self):
self.clear()
self.sprite.draw()
def main():
window = Window(width=640, height=480, caption='Pyglet')
pyglet.app.run()
if __name__ == '__main__':
main()
There is a way to do it, if yes, how? Can someone help me?
Sprite expects image object not bytes array. You can use io.BytesIO to use bytes as file object and then load it using image.load().
Full working example
import pyglet
import binascii
import io
imgData = '''8950 4e47 0d0a 1a0a 0000 000d 4948 4452
0000 0010 0000 001c 0806 0000 0068 313f
1a00 0000 0473 4249 5408 0808 087c 0864
8800 0002 0249 4441 5438 8d95 552d 8cdb
3018 7dad 0a02 47a6 2bec 58d8 32b4 c2b0
8d4c 0a8c 74c4 5649 d819 e6c0 a181 05e6
5848 1593 d319 562a c958 60c7 3c66 5898
69a4 d0ac 0349 9c38 3fd5 ee49 561c e77b
ef7b 9fe3 2f59 ecf3 fc8a 191c 291d ad7d
cb73 eb7e 758b 287c 562f 6400 a27a 1a52
6a89 8c04 8e94 1a62 515d e0c6 0a1b 6c81
2241 e145 a364 8b7e 092d b9a8 2e00 8040
f1b9 eab0 6f5c 2ca7 c881 e237 c900 b06b
ca5c b664 e212 437e 0b96 730f 348b a0b5
ae47 6f3e 2c61 0500 8245 0009 e178 be21
b770 1c07 5a96 f555 6b43 340e ee7f ff42
5a1d c616 4838 e9ec fed3 07fc fdf9 3851
0217 5dd6 34ab 27ea 54bb e102 5a96 803a
8d04 572f 1f3f 83f5 ea76 d2ac 0e06 e078
3e92 700d b60e ac04 169a 7370 0570 d52c
32a3 5dbb 35f6 797e 5d36 22b6 7500 5a96
c6c9 4c62 0083 93b8 a3b4 b33c 83b4 3ae0
fd97 1fdd 1e0c 558f 94a2 70eb f7cd 9b43
b50d d726 a64f 1e09 f41b 292c 5310 9740
7905 183f 770e c8c6 12b1 7a81 b8c4 3c10
3e1b 9101 80f1 f3cc 3968 d076 e2ff c208
482f 3135 b722 aefc 8a94 6c2c c2b0 046b
0fa4 9780 cbd8 7426 571c 5040 8a4e 84f1
335e 5e5f a705 36c1 3b48 d422 2d3c 7542
db83 a590 c8b6 299e 1eee a605 5a91 4b90
c10f bd9b b5b7 307b f0f4 7087 f3a1 dbc0
5248 33e7 6189 5248 9c0f 172b fba4 833e
fa22 7358 0cff 0bdf 9fff cc06 0fb3 4f3a
88dc 7414 148b 0a49 b8c6 8e56 e32f d254
f010 5a31 c4a2 8256 cc7c 8d67 1d4c 89c4
a276 e54c b8fb 0775 650d 8c04 f5e9 f200
0000 0049 454e 44ae 4260 82'''
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
img = "".join(imgData.split())
img = binascii.unhexlify(img)
file_object = io.BytesIO(img)
img = pyglet.image.load("noname.png", file=file_object)
self.sprite = pyglet.sprite.Sprite(img, 0, 0)
def on_draw(self):
self.clear()
self.sprite.draw()
def main():
window = Window(width=640, height=480, caption='Pyglet')
pyglet.app.run()
if __name__ == '__main__':
main()

Pandas performance issue of dataframe column "rename" and "drop"

Below is the line_profiler record of a function :
Wrote profile results to FM_CORE.py.lprof
Timer unit: 2.79365e-07 s
File: F:\FM_CORE.py
Function: _rpt_join at line 1068
Total time: 1.87766 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1068 #profile
1069 def _rpt_join(dfa, dfb, join_type='inner'):
1070 ''' join two dataframe together by ('STK_ID','RPT_Date') multilevel index.
1071 'join_type' can be 'inner' or 'outer'
1072 '''
1073
1074 2 56 28.0 0.0 try: # ('STK_ID','RPT_Date') are normal column
1075 2 2936668 1468334.0 43.7 rst = pd.merge(dfa, dfb, how=join_type, on=['STK_ID','RPT_Date'], left_index=True, right_index=True)
1076 except: # ('STK_ID','RPT_Date') are index
1077 rst = pd.merge(dfa, dfb, how=join_type, left_index=True, right_index=True)
1078
1079
1080 2 81 40.5 0.0 try: # handle 'STK_Name
1081 2 426472 213236.0 6.3 name_combine = pd.concat([dfa.STK_Name, dfb.STK_Name])
1082
1083
1084 2 900584 450292.0 13.4 nameseries = name_combine[-Series(name_combine.index.values, name_combine.index).duplicated()]
1085
1086 2 1138140 569070.0 16.9 rst.STK_Name_x = nameseries
1087 2 596768 298384.0 8.9 rst = rst.rename(columns={'STK_Name_x': 'STK_Name'})
1088 2 722293 361146.5 10.7 rst = rst.drop(['STK_Name_y'], axis=1)
1089 except:
1090 pass
1091
1092 2 94 47.0 0.0 return rst
What surprise me is these two lines:
1087 2 596768 298384.0 8.9 rst = rst.rename(columns={'STK_Name_x': 'STK_Name'})
1088 2 722293 361146.5 10.7 rst = rst.drop(['STK_Name_y'], axis=1)
Why a simple dataframe column "rename" and "drop" operation costs that much percentage of time (8.9% + 10.7%)? Anyway, the "merge" operation only costs 43.7% , and "rename"/"drop" looks not like a calculation-intensive operation. How to improve it ?

Resources