Program_UserInput_DifferentDB.cs
17.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using log4net;
using MongoDB.Driver;
using System.IO;
using Newtonsoft.Json.Linq;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
namespace AIAHighlight.DBSave
{
[BsonIgnoreExtraElements]
public class BodyRegionImage
{
public ObjectId _id { get; set; }
public string imageName;
public int bodyViewIndex;
public int layerNumber;
public int bodyRegionId;
public int zoom;
public List<Term> terms;
public BodyRegionImage()
{
this.terms = new List<Term>();
}
}
public class Term
{
public int TermNumber;
public List<int> SystemNumbers;
public List<int> PixelLocations;
public int ActualTermNumber;
public Term(int termNumber, int systemNumber, int pixelLocation)
{
this.SystemNumbers = new List<int>();
this.PixelLocations = new List<int>();
this.TermNumber = termNumber;
this.SystemNumbers.Add(systemNumber);
this.PixelLocations.Add(pixelLocation);
}
}
public class Program
{
//public JObject data { get; set; }
// public Dictionary<int, Term> termList;
internal void saveImageData(JObject data, int bodyviewId)
{
// DateTime datetime = new DateTime();
Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+
data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]);
try
{
BodyRegionImage bodyRegionImage = new BodyRegionImage();
// var client = new MongoClient("mongodb://192.168.90.43:27017");
var client = new MongoClient();
var db = client.GetDatabase("AIADB_bodyviewId"+bodyviewId.ToString());
var col = db.GetCollection<BodyRegionImage>("DAImages");
// var list = col.Find(Builders<BodyRegionImage>.Filter.Eq("imageName", image["imageName"].ToString())).ToList();
JObject imageData = data;
//var projection = Builders<BodyRegionImage>.Projection.Include("_id");
//FilterDefinition<BodyRegionImage>[] filterCondition = { Builders<BodyRegionImage>.Filter.Eq("imageName", data["imageName"].ToString()),
// Builders<BodyRegionImage>.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])),
// Builders<BodyRegionImage>.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])),
// Builders<BodyRegionImage>.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])),
// Builders<BodyRegionImage>.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))};
//var list = col.Find(Builders<BodyRegionImage>.Filter.And(filterCondition)).Project<BodyRegionImage>(projection).ToList();
//if (list.Count > 0)
// return;
//Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..record not found.");
string bodyViewIndex = data["bodyViewIndex"].ToString();
//string termFilePath = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json";
string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\dist\assets\data\json\da\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json";
//string path = @"\\ADAMSRVR\dist\assets\data\json\da\body-views\" + bodyViewIndex+"\\";
//string fileName = "da_dat_body_system_term_" + bodyViewIndex + ".json";
string systemTermMapping;
using (StreamReader reader = new StreamReader(termFilePath))
// using (StreamReader reader = new StreamReader(path+fileName))
{
systemTermMapping = reader.ReadToEnd();
}
JObject systemTermMappingObject = JObject.Parse(systemTermMapping);
//Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject created.");
dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"];
dynamic imageDataArray = data["data"];
//int[] imageDataIntArray = (int[])imageDataArray;
//int numToRemove = 0;
//dynamic imageDataWithoutZero = imageDataIntArray.Where(val => val != numToRemove).ToArray();
Dictionary<int,Term> termList = new Dictionary<int, Term>();
Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " +
data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]);
for (int i = 0; i < imageDataArray.Count; i += 4)
{
var RED = Convert.ToInt32(imageDataArray[i]) - 9;
var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9;
var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9;
if (RED == -9 && GREEN == -9 && BLUE == -9)
continue;
var Red = "";
var Green = "";
var Blue = "";
string zero = "0";
if ((RED).ToString().Length != 2)
{
Red = zero + RED.ToString();
}
else
{
Red = RED.ToString();
}
if ((GREEN).ToString().Length != 2)
{
Green = zero + GREEN.ToString();
}
else
{
Green = GREEN.ToString();
}
if ((BLUE).ToString().Length != 2)
{
Blue = zero + BLUE.ToString();
}
else
{
Blue = BLUE.ToString();
}
string Icolor = (Red + Green + Blue);
// alert('Icolor: ' + Icolor)
if(Icolor=="000000")
foreach (dynamic systemTerm in systemTerms)
{
int iColor = Convert.ToInt32(Icolor);
int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString());
int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString());
if (systemTerm._TermNumber.ToString() == Icolor)
{
this.AddToTermList(termNumber, systemNumber, i, termList);
this.AddToTermList(termNumber, systemNumber, i, termList);
this.AddToTermList(termNumber, systemNumber, i + 1, termList);
this.AddToTermList(termNumber, systemNumber, i + 2, termList);
break;
}
}
}
Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " +
data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]);
bodyRegionImage.imageName = data["imageName"].ToString();
bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]);
bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]);
bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]);
bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]);
foreach (int key in termList.Keys)
{
bodyRegionImage.terms.Add(termList[key]);
}
Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " +
data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]);
col.InsertOne(bodyRegionImage);
Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " +
data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]);
var logCol = db.GetCollection<BodyRegionImage>("DAImageLogs");
BodyRegionImage bodyRegionImageTolog = new BodyRegionImage();
bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]);
bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]);
bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]);
bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]);
logCol.InsertOne(bodyRegionImageTolog);
bodyRegionImageTolog = null;
data = null;
termList = null;
systemTermMappingObject = null;
systemTerms = null;
}
catch(Exception ex)
{
Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " +
data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace);
}
}
public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary<int, Term> termList)
{
if (!termList.ContainsKey(termNumber))
{
termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation));
}
else
{
bool isSystemNumberExisting = false;
foreach (int sn in termList[termNumber].SystemNumbers)
{
if (systemNumber == sn)
{
isSystemNumberExisting = true;
break;
}
}
if (!isSystemNumberExisting)
termList[termNumber].SystemNumbers.Add(systemNumber);
bool isPixelLocationExisting = false;
foreach (int pl in termList[termNumber].PixelLocations)
{
if (pixelLocation == pl)
{
isPixelLocationExisting = true;
break;
}
}
if (!isPixelLocationExisting)
termList[termNumber].PixelLocations.Add(pixelLocation);
}
}
//static void Main(string[] args)
//{
// Console.WriteLine("Enter BodyViewIndex");
// string bodyViewId = Console.ReadLine();
// string searchPattern = "BodyView" + bodyViewId+"_layer*";
// string[] files = Directory.GetFiles(@"\\ADAMSRVR\data", searchPattern);
// Program program;
// foreach (string filePath in files)
// {
// program = new Program();
// program.getDataFromFileAndSaveInDB(filePath);
// program = null;
// }
//}
static void Main(string[] args)
{
//string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data", "*.json");
//string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data", "*.json");
Console.WriteLine("Enter BodyViewId");
int BodyViewIndex = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter LayerNumber");
string layerNumber = Console.ReadLine();
int totalLayers = 0;
switch (BodyViewIndex)
{
case 1:
totalLayers = 330;
break;
case 2:
totalLayers = 288;
break;
case 3:
totalLayers = 127;
break;
case 4:
totalLayers = 186;
break;
case 5:
totalLayers = 229;
break;
case 6:
totalLayers = 289;
break;
case 7:
totalLayers = 130;
break;
case 8:
totalLayers = 186;
break;
case 9:
totalLayers = 64;
break;
case 10:
totalLayers = 72;
break;
case 11:
totalLayers = 64;
break;
case 12:
totalLayers = 72;
break;
}
for (int i = Convert.ToInt32(layerNumber); i <= totalLayers; i++)
{
string searchPattern = "BodyView" + BodyViewIndex + "_layer" + i + "*";
// string searchPattern = "BodyView" + bodyViewId + "_layer*";
string[] files = Directory.GetFiles(@"D:\100-PROJECTS\008-AIAHighlight.FileSave\data", searchPattern);
// string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\", searchPattern);
Program program = new Program();
program.processFiles(files, BodyViewIndex);
program = null;
}
}
internal void processFiles(string[] files,int bodyviewId)
{
//with multithreading
// prepareArrayOfFiles(files, startIndex, endIndex);
//without multithreading
foreach(string filename in files)
{
getDataFromFileAndSaveInDB(filename, bodyviewId);
}
}
//internal void prepareArrayOfFiles(string[] files,int startIndex, int endIndex)
//{
// string[] filesToProcess = new string[20];
// int processedFiles = 1;
// int j = 0;
// for (int i = startIndex; i <= endIndex; i++)
// {
// filesToProcess[j] = files[i];
// j++;
// }
// Parallel.ForEach(filesToProcess, (currentFile) =>
// {
// if (currentFile != null)
// {
// using (StreamReader reader = new StreamReader(currentFile))
// {
// getDataFromFileAndSaveInDB(currentFile);
// processedFiles++;
// }
// if (processedFiles == 20)
// {
// startIndex = endIndex + 1;
// endIndex = startIndex + 19;
// filesToProcess = null;
// //in case after adding 20, the endIndex reaches more than length of files array(total files)
// if (endIndex > files.Length - 1)
// {
// // int remainingFiles = endIndex - files.Length;
// endIndex = files.Length - 1;
// }
// if (endIndex < files.Length)
// {
// processFiles(files, startIndex, endIndex);
// }
// //}
// }
// }
// }
// );
//}
internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId)
{
if (File.Exists(filePath))
{
string fileData;
using (StreamReader reader = new StreamReader(filePath))
{
fileData = reader.ReadToEnd();
JObject data = JObject.Parse(fileData);
this.saveImageData(data, bodyViewId);
}
}
else
{
Console.WriteLine("FILE.NOT.FOUND AT" +
": " + filePath);
}
}
}
}