-
Notifications
You must be signed in to change notification settings - Fork 1
/
genusdata.cc
249 lines (231 loc) · 6.6 KB
/
genusdata.cc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "genusdata.h"
#include <stdexcept>
#include <sstream>
void Genusdata::init(const string aFilename, uint aR, uint aKind, uint aSpecies, vector<double> aY)
{
// category実装前
ifstream tFile(aFilename.c_str());
if (!tFile)
{
cerr << "Error: cannot open genus file" << endl;
exit(1);
}
string tLine;
while (getline(tFile, tLine))
{
mGenus.push_back(tLine);
}
mSpecies = aSpecies;
vector<string> allergen_list, nonallergen_list;
if(mGenus.size() != aY.size()){
std::stringstream errmsg;
errmsg << "Number of instances in the data file is " << aY.size() << ", but the genus file \"" << aFilename << "\" contains " << mGenus.size() << " instances";
std::cerr << errmsg.str() << std::endl;
throw new std::runtime_error(errmsg.str());
}
if (mSpecies)
{
cout << "mSpecies: " << mSpecies << endl;
// 202009 categoryがspeciesの場合
// 一回全部読んでallergen, nonallergenの全カテゴリを記録, mFoodxを作成
for (int i = 0; i < mGenus.size(); i++)
{
string genus = mGenus[i];
double index = aY[i];
if (index == 1)
{
auto it_find = find(allergen_list.begin(), allergen_list.end(), genus);
if (it_find == allergen_list.end())
{
allergen_list.push_back(genus);
}
}
if (index == -1)
{
auto it_find = find(nonallergen_list.begin(), nonallergen_list.end(), genus);
if (it_find == nonallergen_list.end())
{
nonallergen_list.push_back(genus);
}
}
}
for (int i = 0; i < mGenus.size(); i++)
{
string genus = mGenus[i];
auto it_al_find = find(allergen_list.begin(), allergen_list.end(), genus);
auto it_nonal_find = find(nonallergen_list.begin(), nonallergen_list.end(), genus);
if (it_al_find != allergen_list.end() && it_nonal_find != nonallergen_list.end())
{
mFoodx.push_back(i);
mPairedList.push_back(genus);
}
else
{
mNonFoodx.push_back(i);
}
}
}
else
{
//mFoodx
uint i = 0;
for (auto it : mGenus)
{
if (it == "Food")
mFoodx.push_back(i);
i++;
}
//mNonFoodx
i = 0;
for (auto it : mGenus)
{
if (it != "Food")
mNonFoodx.push_back(i);
i++;
}
}
//debug: allergen_list, の中身を出力
cout << "allergen_list:" << endl;
for (auto it : allergen_list)
{
cout << it << ", ";
}
cout << endl;
cout << "nonallergen_list:" << endl;
for (auto it : nonallergen_list)
{
cout << it << ", ";
}
cout << endl;
cout << "mPairedList:" << endl;
for (auto it : mPairedList)
{
cout << it << ", ";
}
cout << endl;
//debug: mFoodxの中身を出力
cout << "mFoodx:" << endl;
for (auto it : mFoodx)
{
cout << it << ", ";
}
cout << endl
<< "mNonFoodx:" << endl;
for (auto it : mNonFoodx)
{
cout << it << ", ";
}
cout << endl;
//mKind
mKind = aKind;
//mTempIndexの初期化
for (uint i = 0; i < aR; i++)
{
vector<uint> tTempIndex;
for (uint j = 0; j < mGenus.size(); j++)
{
tTempIndex.push_back(j);
}
mTempIndex.push_back(tTempIndex);
}
}
void Genusdata::set_tempindex(const vector<vector<uint>> aTempIndex)
{
mTempIndex = aTempIndex;
}
void Genusdata::make_foodx(uint aR)
{
for (uint i = 0; i < aR; i++)
{
vector<uint> tTempFoodx;
vector<uint> tTempNonFoodx;
for (uint j = 0; j < mGenus.size(); j++)
{
//mTempIndex[i]にi回目シャッフルに関してシャッフル後のインデックスが入っているので, これをjで上からなぞればmGenus[mTempIndex[i][j]]がシャッフル後j番目の属
//TempIndex廃止して大人しくmTempGenusをvector<vector<string>>で作れば簡潔にはなる
if (mSpecies == 1)
{
// 202009 categoryがspeciesの場合
string genus = mGenus[mTempIndex[i][j]];
auto it_find = find(mPairedList.begin(), mPairedList.end(), genus);
if (it_find != mPairedList.end())
{
tTempFoodx.push_back(j);
}
else
{
tTempNonFoodx.push_back(j);
}
}
else
{
// 元の実装
// if (i == 0)
// cout << "mGenus[mTempIndex[" << i << "][" << j << "]]" << mGenus[mTempIndex[i][j]] << endl;
if (mGenus[mTempIndex[i][j]] == "Food")
{
tTempFoodx.push_back(j);
}
else
{
tTempNonFoodx.push_back(j);
}
}
}
mTempFoodx.push_back(tTempFoodx);
mTempNonFoodx.push_back(tTempNonFoodx);
}
// デバッグ mTempFoodx, mTempNonFoodxを表示
// for (int i = 0; i < aR; i++)
// {
// cout << "mTempFoodx[" << i << "]: " << mTempFoodx[i].size() << endl;
// for (int j = 0; j < mTempFoodx[i].size(); j++)
// {
// cout << mTempFoodx[i][j] << ", ";
// }
// cout << endl;
// cout << "mTempNonFoodx[" << i << "]: " << mTempNonFoodx[i].size() << endl;
// for (int j = 0; j < mTempNonFoodx[i].size(); j++)
// {
// cout << mTempNonFoodx[i][j] << ", ";
// }
// cout << endl;
// }
}
//ゲッター群
vector<string> Genusdata::get_genus() const
{
return mGenus;
}
vector<uint> Genusdata::get_foodx() const
{
return mFoodx;
}
vector<uint> Genusdata::get_nonfoodx() const
{
return mNonFoodx;
}
vector<vector<uint>> Genusdata::get_tempindex() const
{
return mTempIndex;
}
vector<vector<uint>> Genusdata::get_tempfoodx() const
{
return mTempFoodx;
}
vector<uint> Genusdata::get_tempfoodx(uint i) const
{
return mTempFoodx[i];
}
vector<vector<uint>> Genusdata::get_tempnonfoodx() const
{
return mTempNonFoodx;
}
vector<uint> Genusdata::get_tempnonfoodx(uint i) const
{
return mTempNonFoodx[i];
}
uint Genusdata::get_mkind() const
{
return mKind;
}