octave slow, optimisation advice needed - octave

The loop takes a long time to execute. Is there a faster way of processing the loaded file?
a = fileread ("C:/Backup/Thermo Backups/2018-05-17.txt");
d = strsplit (a, "\n");
for i = 1 : length(d)
e (i , :) = strsplit ( d{i} );
endfor
f = cellfun(#str2num, e)
Data sample:
2 36.5333 43 43 43 43 68 0 0
2.5 36.5333 43 43 43 43 68 0 0
3 36.6667 43 43 43 43 68 0 0
3.5 36.6667 43 43 43 43 68 0 0
4 36.6667 43 43 43 43 68 0 0

Related

An sdf file that I produce doesn't get accepted by a converter

I made a program that converts csv files into sdf files. Those files were supposed to go in another converter that turns them into something called a "Nist MS Library". The problem is that my file doesn't get accepted by the converter for "No spectra have been converted" and I don't understand why.
The files seem identical to me and I think I'm missing something about the specific file extension.
I'm really sorry if this doesn't belong here, I will delete the post if this is the case, but I really do not know where to ask.
I tried to make the "mass spectral peaks" integer, floats, delete them and put some values that I knew for sure that were accepted by the Nist converter, but nothing seems to work.
I will put 2 molecules, the first one is mine, the one that doesn't get accepted, the other one is the one that is fine for the program.
Coumarin
No Structure
0 0 0 0 0 0 0 0 0 0 0
> <NAME>
Coumarin
> <INCHIKEY>
> <FORMULA>
> <MW>
> <CASNO>
91645
> <ID>
2
> <COMMENT>
SAFC Cat. n. W526509\nColumn: SLB-5ms part#28471-U; Supelcowax-10 part#24079; Equity-1 part#28046-U;\nwww.sigmaaldrich.com |RI:1438|
> <SYNONYMS>
Coumarin
> <NUM PEAKS>
140
> <MASS SPECTRAL PEAKS>
39 1
39 233
40 38
40 0
40 0
41 0
41 1
41 5
42 2
42 2
43 35
43 0
43 12
44 26
45 20
45 67
46 7
46 4
46 2
46 1
47 0
48 4
49 32
49 3
50 5
50 166
51 183
52 20
53 35
54 7
55 1
56 0
58 0
59 2
59 23
60 6
60 11
61 89
62 213
63 503
64 164
64 13
65 15
65 7
66 9
68 4
71 0
71 1
72 2
73 1
73 19
74 0
74 32
75 39
76 14
77 11
77 1
78 0
78 0
79 0
79 9
79 0
80 2
80 1
81 0
81 0
81 1
82 0
82 0
83 0
83 0
84 6
84 0
85 17
86 2
86 2
87 28
88 7
89 523
90 581
91 25
91 48
92 40
92 36
93 4
93 5
94 1
94 0
97 2
98 6
98 0
99 2
99 0
100 0
100 1
101 5
102 3
103 0
103 0
103 0
104 0
105 0
105 0
106 0
106 0
106 0
107 0
108 0
109 0
110 0
110 0
111 0
111 0
112 0
112 0
113 0
116 0
117 6
117 0
118 1000
119 94
120 34
120 10
121 4
121 0
122 0
122 0
131 0
135 0
145 1
146 0
146 547
147 58
148 5
183 0
246 0
334 0
351 0
359 0
382 0
> <RI value>
1430.1
$$$$
ETHYL HYDROSULFIDE
(C) 2015 John Wiley & Sons, Inc.
CAS rn = 75081, Library ID = 1
3 2 0 0 0 0 0 0 0 0999 V2000
0.0000 0.2061 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7146 -0.2061 0.0000 S 0 0 0 0 0 0 0 0 0 0 0 0
-0.7146 -0.2061 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 3 1 0 0 0 0
M END
> <NAME>
Ethyl hydrosulfide
> <SYNONYMS>
Ethanethiol
$:28DNJIEGIFACGWOD-UHFFFAOYSA-N
$:29n=703/0/1 p=620/0/1
> <FORMULA>
C2H6S
> <MW>
62
> <CASNO>
75081
> <ID>
1
> <COMMENT>
WileyID="LM_FFNSC3_1" RI1="703 (SLB-5MS (Hydro))" RI2="392 (SLB-5MS (FAMEs))" RI3="620 (Supelcowax-10 (FAMEs)" RI4="568 (Supelcowax-10 (FAEEs)" Contributor="Prof. L. Mondello (Chromaleont s.r.l./Univ. Messina, Italy)"
> <NUM PEAKS>
21
> <MASS SPECTRAL PEAKS>
44 20
45 235
46 147
47 727
48 20
49 32
50 2
51 2
52 2
53 2
54 2
55 2
56 16
57 84
58 115
59 84
60 16
61 155
62 999
63 36
64 44
$$$$

Add values in a lists which is a string column in hive

I have a set of data where a columns consists of lists which is of string data type.
Column_A|Column_B
AAA |1 23 56 89 74 52
BBB |63 99 44 2 80 87 58 63
CCC |96 45 23 84 62 74
Here, In the above data I need to add the values in column B as below:
Column_A|Column_B |Column_C
AAA |1 23 56 89 74 52 |295
BBB |63 99 44 2 80 87 58 63|496
CCC |96 45 23 84 62 74 |384
I have used cast function and converted the data type from string to int using the below query.
select Column_A,cast (Column_B as INT) as Column_B from Xyz
But summing the values is a great challenge.
Can someone help me out?
I'm learning RegEx too.. Is there any possibility to use RegEx?
Explode your column using split by space and aggregate.
This is demo in Hive:
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B, sum(cast(b.val_b as int)) as Column_C
from your_data a
lateral view outer explode(split(Column_B,' ')) b as val_b
group by Column_A,Column_B;
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 53.228 seconds, Fetched: 3 row(s)
Alternatively, if the maximum number of elements in the list is fixed, you can do the same without explode, it will work much faster:
create temporary macro cast_value(s string) nvl(cast(s as int),0);
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B,
cast_value(col_B_array[0])+
cast_value(col_B_array[1])+
cast_value(col_B_array[2])+
cast_value(col_B_array[3])+
cast_value(col_B_array[4])+
cast_value(col_B_array[5])+
cast_value(col_B_array[6])+
cast_value(col_B_array[7])+
cast_value(col_B_array[8])+
cast_value(col_B_array[9]) as Column_C
from(
select Column_A,Column_B, split(Column_B,' ') col_B_array
from your_data a
)s
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 0.82 seconds, Fetched: 3 row(s)

Resulted array to URL friendly form

In a coding contest I have a programming task to find minimum or maximum from different arrays, and the final result to put it at the end of the URL (www.example.com/result).
I can't figure out what format are they expecting...
They gave this hint:
look at the resulting values and see if they could represent something familiar), at the end of the root of the URL of the page you're on (of course add a slash if needed)
Update
For the result of the problem, you need to figure out a way to convert those numbers to a "URL friendly form" value.
For example, a URL friendly value means a string, so you need a 'standard' way to convert each number to a character and use the resulting string to go to the next problem.
I tried the following formats 1:2:3:4 , 1_2_3_4 no chance.
The numbers are 100,126,114,85,82,121,54 .
So I need them formatted in an url friendly form whatever that would mean.
This is the coding challenge :
You are given an input file containing:
On the first line a positive integer N
On the following N lines there will be arrays of integers, of variable length, each integer value separated by a white space from the next value
The start of each array will only contain one of the following possible numbers: 1 or -1
To get to the next step you will have to calculate the minimum (if start value is -1) or maximum (if start value is 1) of the arrays in the input file and then put the result, in a more human and URL friendly form (hint: look at the resulting values and see if they could represent somehting familiar), at the end of the root of the URL of the page you're on (of course add a slash if needed). Also keep the code used to solve this problem!
Here is your input data:
7
-1 156 198 171 134 197 120 149 177 130 100 103 126 169 115 199 188 119 168 151 161 167 141 111
-1 126 128 150 190 193 198 168 128 194 138 196 153 134 163 152 136 158 132 178 141 174 143 195 126 183 132 174 173 171 130 155 164 158
-1 178 115 166 161 164 134 130 164 147 114
1 73 78 81 66 77 0 26 42 48 42 12 24 33 4 54 31 50 34 78 13 21 37 29 85 56 68 12 79 81 82 25 7 16 44 32 82
1 62 35 54 82 30 7 28 78 74 34 12 80 40 16 5 39 29
-1 157 183 175 140 158 164 138 166 176 121 145 139 186 188 158 121 183 146 132 124 123 198 162 135 161 132 187 184 121 148 157 146 123 199 142 134 196 179
-1 70 74 81 105 121 129 148 91 160 76 146 94 64 154 54 102 142 68 62 88 63 144 143 138 118 117 148 166 146 159 162 130 183 184 156 172
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
function m(str) {
var parts = str.split(" ");
var what = parts.shift();
if (what=="-1") return Array.min(parts);
else return Array.max(parts);
}
var num=[];
num.push(7); // not sure about this one. Use the encode if yes
num.push(m("-1 156 198 171 134 197 120 149 177 130 100 103 126 169 115 199 188 119 168 151 161 167 141 111"))
num.push(m("-1 126 128 150 190 193 198 168 128 194 138 196 153 134 163 152 136 158 132 178 141 174 143 195 126 183 132 174 173 171 130 155 164 158"))
num.push(m("-1 178 115 166 161 164 134 130 164 147 114"))
num.push(m("1 1 73 78 81 66 77 0 26 42 48 42 12 24 33 4 54 31 50 34 78 13 21 37 29 85 56 68 12 79 81 82 25 7 16 44 32 82"))
num.push(m("1 62 35 54 82 30 7 28 78 74 34 12 80 40 16 5 39 29"))
num.push(m("-1 157 183 175 140 158 164 138 166 176 121 145 139 186 188 158 121 183 146 132 124 123 198 162 135 161 132 187 184 121 148 157 146 123 199 142 134 196 179"))
num.push(m("-1 70 74 81 105 121 129 148 91 160 76 146 94 64 154 54 102 142 68 62 88 63 144 143 138 118 117 148 166 146 159 162 130 183 184 156 172"))
var str = String.fromCharCode.apply(null, num);
console.log(num,str);
console.log(encodeURIComponent(str)); // if 7 is part of the numbers
// location = "http://www.example.com/"+str;

Elasticsearch return raw JSON with Golang Beats

I'm working on a beat that interrogate our elastic search, the goal is to get elements in elastics to modify them to avoid duplication.
I've written a search and it works:
client,err :=elasticsearch.NewClient(elasticsearch.ClientSettings{URL:host,Index:indexSel,Username: username,Password: password,Timeout: 60 * time.Second,}, nil)
if err != nil {
log.Fatal(err)
}
params := map[string]string{
"q": "_id:"+maref,
}
_, resp, err := client.SearchURI(index, "", params)
if err != nil {
log.Fatal(err)
return
} else {
fmt.Println(string(resp))
}
And here is the function from the elastic api.go:
func (es *Connection) SearchURI(index string, docType string, params map[string]string) (int, *SearchResults, error) {
status, resp, err := es.apiCall("GET", index, docType, "_search", "", params, nil)
if err != nil {
return status, nil, err
}
result, err := readSearchResult(resp)
return status, result, err
}
func readSearchResult(obj []byte) (*SearchResults, error) {
var result SearchResults
if obj == nil {
return nil, nil
}
err := json.Unmarshal(obj, &result)
if err != nil {
return nil, err
}
return &result, err
}
With the following types:
type SearchResults struct {
Took int `json:"took"`
Shards json.RawMessage `json:"_shards"`
Hits Hits `json:"hits"`
Aggs map[string]json.RawMessage `json:"aggregations"`
}
type Hits struct {
Total int
Hits []json.RawMessage `json:"hits"`
}
For now the answer is a raw JSON:
&{11 [123 34 116 111 116 97 108 34 58 53 44 34 115 117 99 99 101 115 115 102 117 108 34 58 53 44 34 102 97 105 108 101 100 34 58 48 125] {1 [[123 34 95 105 110 100 101 120 34 58 34 99 111 112 105 108 98 101 97 116 45 50 48 49 54 46 49 48 46 50 53 34 44 34 95 116 121 112 101 34 58 34 73 110 99 105 100 101 110 116 34 44 34 95 105 100 34 58 34 65 86 102 54 55 69 103 109 74 66 45 119 85 116 103 82 99 90 113 97 34 44 34 95 115 99 111 114 101 34 58 49 46 48 44 34 95 115 111 117 114 99 101 34 58 123 34 64 116 105 109 101 115 116 97 109 112 34 58 34 50 48 49 54 45 49 48 45 50 53 84 48 56 58 49 54 58 53 55 46 53 53 57 90 34 44 34 97 103 101 110 116 95 105 100 34 58 49 53 44 34 98 101 97 116 34 58 123 34 104 111 115 116 110 97 109 101 34 58 34 53 55 99 53 99 49 57 49 101 48 100 57 34 44 34 110 97 109 101 34 58 34 53 55 99 53 99 49 57 49 101 48 100 57 34 125 44 34 100 101 115 99 114 105 112 116 105 111 110 34 58 34 101 108 97 115 116 105 99 34 44 34 102 105 110 97 108 99 108 97 115 115 34 58 34 34 44 34 105 100 34 58 34 73 45 48 48 53 56 50 54 34 44 34 111 114 103 95 105 100 34 58 49 55 44 34 111 114 103 95 110 97 109 101 34 58 34 83 104 97 109 34 44 34 112 114 105 111 114 105 116 121 34 58 52 44 34 114 101 102 101 114 101 110 99 101 34 58 34 73 45 48 48 53 56 50 54 34 44 34 115 116 97 114 116 95 100 97 116 101 34 58 34 50 48 49 54 45 49 48 45 50 53 84 49 48 58 49 54 58 53 54 46 56 56 49 55 55 52 49 43 48 50 58 48 48 34 44 34 115 116 97 116 117 115 34 58 34 97 115 115 105 103 110 101 100 34 44 34 116 116 111 34 58 48 44 34 116 116 111 95 49 48 48 95 116 114 105 103 103 101 114 101 100 34 58 48 44 34 116 116 114 34 58 48 44 34 116 116 114 95 49 48 48 95 116 114 105 103 103 101 114 101 100 34 58 48 44 34 116 121 112 101 34 58 34 73 110 99 105 100 101 110 116 34 125 125]]} map[]}
So my question is:
How can I convert the result to ascii?
I've read it's probably due to the json.RawMessage type that doesn't convert JSON but I can't find how to convert it.
Thanks by advance,
Plosxh.
EDIT : Solution:
Thx to icza, here's my solution I change my "fmt.Println(resp)" into:
for _, v := range resp.Hits.Hits {
fmt.Println(string(v))
}
It works like a charm!
Edit 2 :
I had to re-create a whole type en json.Unmarshal "resp.Hits.Hits" in order to use it like a regular json.
json.RawMessage is a byte slice:
type RawMessage []byte
You can convert a byte slice to a string with a simple type conversion:
var result SearchResults
// ...
for _, v := range result.Hits {
fmt.Println("Total:", v.Total, "Hits:", string(v.Hits))
}
Or:
fmt.Printf("Total: %d, Hits: %s\n", v.Total, v.Hits)
See this example:
var raw json.RawMessage
raw = json.RawMessage(`something raw`)
fmt.Println(raw)
fmt.Println(string(raw))
fmt.Printf("%s\n", raw)
Output (try it on the Go Playground):
[115 111 109 101 116 104 105 110 103 32 114 97 119]
something raw
something raw

SQL query select unique images from multiple tag_id

I have a sql table similar to the one below. And I want to bring back unique results where tag_id matches multiple items.
So if I want to find each image_id that has the tag_id = 106 and 73 I would like it to bring back image_id 12345714 and 12345712.
I've tried doing
SELECT *
FROM tag_relationship
WHERE tag_id
IN ( 106, 73 )
But this brings back
id image_id tag_id
61 12345706 73
70 12345712 73
72 12345712 106
76 12345714 73
77 12345714 106
I've also experimented with GROUP BY, but that doesn't seem to be quite right.
Sample of full table:
image_id can have the same number in different rows, but will have a different tag_id.
id image_id tag_id
1 12345679 63
2 12345679 83
3 12345680 74
4 12345680 108
5 12345680 75
6 12345683 103
7 12345682 87
8 12345682 105
9 12345682 74
10 12345682 81
11 12345683 79
12 12345683 109
13 12345689 111
14 12345689 69
15 12345690 104
16 12345687 110
17 12345687 69
18 12345687 91
19 12345687 93
20 12345687 63
21 12345692 69
22 12345692 104
23 12345692 80
24 12345692 76
25 12345693 74
26 12345693 99
27 12345693 96
28 12345693 94
29 12345691 63
30 12345691 69
31 12345697 92
32 12345697 76
33 12345698 74
34 12345699 97
35 12345698 94
36 12345699 98
37 12345698 81
38 12345699 105
39 12345697 91
40 12345694 100
41 12345694 101
42 12345694 94
43 12345694 74
44 12345696 78
45 12345696 95
46 12345696 112
47 12345701 113
48 12345701 114
49 12345700 94
50 12345700 91
51 12345700 90
52 12345702 87
53 12345702 115
54 12345702 80
55 12345702 74
56 12345704 78
57 12345705 83
58 12345705 84
59 12345704 63
60 12345705 104
61 12345706 73
62 12345706 64
63 12345706 86
64 12345706 88
65 12345706 89
66 12345713 80
67 12345713 115
68 12345713 81
69 12345717 63
70 12345712 73
71 12345717 64
72 12345712 106
73 12345717 79
74 12345712 74
75 12345709 76
76 12345714 73
77 12345714 106
78 12345715 68
79 12345716 116
80 12345715 69
81 12345716 71
SELECT image_id
FROM tag_relationship
WHERE tag_id IN ( 106, 73 )
GROUP BY image_id
HAVING COUNT(*) = 2
SELECT DISTINCT image_id
FROM tag_relationship
WHERE tag_id
IN ( 106, 73 )
It might be really stupid/simple, but I think this will work:
SELECT image_id
FROM tag_relationship
WHERE
tag_id = 106 AND tag_id = 73
Try this
SELECT DISTINCT t1.image_id
FROM tag_relationship t1
JOIN tag_relationship t2 ON t1.image_id=t2.image_id
WHERE t1.tag_id=106
and t2.tag_id=73