PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities/ImportONELinks.m

https://github.com/alirezamonfared/MobilityReport
Objective C | 229 lines | 209 code | 20 blank | 0 comment | 45 complexity | 8aa82d042ba4bf03bf637c3e66adbdcc MD5 | raw file
  1. function [CGs TimeSequence] = ImportONELinks( InputFile,Options )
  2. %IMPORTONELINKS Import a contact trace.
  3. % [CGs TimeSequence] = IMPORTONELINKS( InputFile,Options ) Opens the ONE
  4. % Link format: <Time> <CONN> <SrcID> <DstID> <UP/DOWN>,
  5. % and returns CGs of dimensions N x N x tm where N is the number of the nodes
  6. % and tm is the maximum time. Value of each entry of CGs is 1 is the link
  7. % is 'UP' and 0 is the link is 'DOWN'
  8. % Inputs: InputFile: name of the ONE file containing the link trace
  9. % Options: options used in the import utility
  10. % *NodeIDsStartFromOne: In a standard ONE format file, node IDs
  11. % start from 1, if the input file has node IDs starting from
  12. % 1, set this to true
  13. % *TraceMode: A 'Continuous' trace reader reads the timestamps in
  14. % the file and reports their corresponding connectivities. It
  15. % uses Options.DeltaT to aggregate all contacts every DeltaT
  16. % seconds. A 'Discrete' trace reader, reports connectivity graphs
  17. % for T=0...Tm-1 Assuming that data is updated every second. In
  18. % this mode, timsatmaps in the ONE file should be integers.
  19. % 'ReadAll' is a special mode that reads all teh time instants in
  20. % the original file and creates one snapshot for each. Note that
  21. % 'ReadAll' is different from 'Discrete', in the sense that it
  22. % does not assume that we need to report for all time instants in
  23. % 0...T-1
  24. % *DiscreteTimeStartFromOne: In a standard ONE format file with
  25. % discrete timestamps, timestamps start from 0.0, if the input
  26. % file has discrete timestamps starting from 1.0, set this to
  27. % true. Note that this is only needed if timestamps are discrete
  28. % *Symmetric: If set to true, ensures a symmetric connectivity
  29. % graph. i.e. if i-->j then j-->i.
  30. % *T: the desired number of timesteps for a Continuous trace to
  31. % be read. The corresponding DeltaT is set as DeltaT =
  32. % TraceLength / T.
  33. % *DeltaT: the timediffenrece between cinsecutive connectivity
  34. % snapshpts. DeltaT is 1 for discrete mode, it can be explicity
  35. % set for Continuous mode, or can be implicitly set by giving the
  36. % number of desired timesteps. For ReadAll mode it needs not to
  37. % be set.
  38. % Warning: This utility needs the node IDs to go from 1...N or 0..N-1
  39. % (based on the value of Options.NodeIDsStartFromOne), if
  40. % node IDs are custom numbers in another range, you need to
  41. % pre-process your trace to make node IDs go from 0..N-1 before
  42. % feeding it to the utility, and remeber the mapping for future
  43. % purposes.
  44. % Output: CGs: a NxNxT connectivity graph corresponding to the
  45. % snapshots of the connectivity in the given contact trace.
  46. % TimeSequence: A 1xT vector recording the original
  47. % timestamps in the InputFile.
  48. if nargin < 2
  49. Options = [];
  50. end
  51. if (~isfield(Options,'NodeIDsStartFromOne'))
  52. NodeOffset = 1;
  53. else
  54. NodeOffset = ~Options.NodeIDsStartFromOne;
  55. end
  56. if (~isfield(Options,'Symmetric'))
  57. Symmetric = false;
  58. else
  59. Symmetric = Options.Symmetric;
  60. end
  61. % Continuous / Dicrete / ReadAll
  62. if (~isfield(Options,'TraceMode'))
  63. Options.TraceMode = 'Continuous';
  64. end
  65. if (~isfield(Options,'DiscreteTimeStartFromOne') && ...
  66. strcmp(Options.TraceMode , 'Discrete'))
  67. TimeOffset = 1; % Assume discrete time starts from 0
  68. elseif (isfield(Options,'DiscreteTimeStartFromOne'))
  69. TimeOffset = ~Options.DiscreteTimeStartFromOne;
  70. else
  71. TimeOffset = 0;
  72. end
  73. %% Get the last line
  74. % T is the number of desired snapshots
  75. % DeltaT is the time difference between consecutive snapshots
  76. if (~isfield(Options,'DeltaT') && isfield(Options,'T'))
  77. % First Line
  78. Command = sprintf('sed q %s', InputFile);
  79. [status,result] = system(Command);
  80. result = regexp(result,'\s','split');
  81. T1 = str2double(result(1));
  82. % Last Line
  83. Command = sprintf('sed ''$!d'' %s', InputFile);
  84. [status,result] = system(Command);
  85. result = regexp(result,'\s','split');
  86. T2 = str2double(result(1));
  87. Options.DeltaT = floor((T2-T1) / Options.T);
  88. else
  89. Options.DeltaT = 1;
  90. end
  91. %%
  92. %Number of nodes
  93. if (~isfield(Options,'N'))
  94. Options.N = 1;
  95. end
  96. fid = fopen(InputFile);
  97. function Splitted = SplitString(Str)
  98. Parts = regexp(Str,'\s','split');
  99. Time = str2double(Parts{1}) + TimeOffset;
  100. NodeID1 = str2double(Parts{3}) + NodeOffset;
  101. NodeID2 = str2double(Parts{4}) + NodeOffset;
  102. Status = Parts{5};
  103. if (strcmp(Status,'UP') == 1 || strcmp(Status,'up') == 1)
  104. Status = 1;
  105. elseif (strcmp(Status,'DOWN') == 1 || strcmp(Status,'down') == 1)
  106. Status = 0;
  107. else
  108. Status = -1;
  109. end
  110. Splitted = [Time NodeID1 NodeID2 Status];
  111. end
  112. CGs = zeros(Options.N, Options.N, 1);
  113. TimeSequence = zeros(1);
  114. % Continuous Mode
  115. % In this mode, we report aggregate connectivity graphs every DeltaT
  116. % seconds. Timestamps correspond to these moments.
  117. if (strcmp(Options.TraceMode,'Continuous'))
  118. PreviousTime = -1;
  119. FirstLine = true;
  120. TimeStep = 1; % Current Timestep
  121. CurrentCG = zeros(Options.N,Options.N);
  122. while 1
  123. tline = fgets(fid);
  124. if(~ischar(tline))
  125. break
  126. end
  127. S = SplitString(tline);
  128. if (FirstLine)
  129. PreviousTime = S(1);
  130. TimeSequence(1) = PreviousTime;
  131. FirstLine = false;
  132. else
  133. CurrentTime = S(1);
  134. if (CurrentTime - PreviousTime >= Options.DeltaT)
  135. CGs(:,:,TimeStep) = CurrentCG;
  136. TimeStep = TimeStep + 1;
  137. TimeSequence(TimeStep) = CurrentTime;
  138. PreviousTime = CurrentTime;
  139. %CGs(:,:,TimeStep) = CGs(:,:,TimeStep-1); % Use the previous connectivity as base
  140. end
  141. end
  142. CurrentCG(S(2),S(3)) = S(4);
  143. if Symmetric
  144. CurrentCG(S(3),S(2)) = S(4);
  145. end
  146. end
  147. TimeSequence(TimeStep) = CurrentTime;
  148. CGs(:,:,TimeStep) = CurrentCG;
  149. % Discrete Mode
  150. % It this mode we assume that time goes from 1:T and we generate a
  151. % Connectivity graph for every value of timestamp in this range
  152. elseif (strcmp(Options.TraceMode,'Discrete'))
  153. TimeStep = 1;
  154. while 1
  155. tline = fgets(fid);
  156. if(~ischar(tline))
  157. break
  158. end
  159. S = SplitString(tline);
  160. TimeRead = S(1);
  161. while TimeRead > TimeStep
  162. TimeStep = TimeStep + 1;
  163. CGs(:,:,TimeStep) = CGs(:,:,TimeStep-1);
  164. end
  165. %TimeStep == TimeRead;
  166. CGs(S(2),S(3),TimeRead) = S(4);
  167. if Symmetric
  168. CGs(S(3),S(2),TimeRead) = S(4);
  169. end
  170. end
  171. TimeSequence = 0:S(1)-1;
  172. % ReadAll Mode
  173. % In this mode, we assume a continuous trace but read all the moments in
  174. % the trace. It is equivalent to setting Options.DeltaT to time difference
  175. % of every two consecutive mmoments in the trace
  176. % It is useful for discrete traces that have moments with custom timestamps
  177. % instead of 1:T style seen in the discrete mode
  178. elseif (strcmp(Options.TraceMode,'ReadAll'))
  179. TimeStep = 1;
  180. FirstLine = true;
  181. while 1
  182. tline = fgets(fid);
  183. if(~ischar(tline))
  184. break
  185. end
  186. S = SplitString(tline);
  187. if (FirstLine)
  188. PreviousTimeRead = S(1);
  189. FirstLine = false;
  190. end
  191. TimeRead = S(1);
  192. if TimeRead > PreviousTimeRead
  193. TimeSequence(TimeStep) = PreviousTimeRead;
  194. PreviousTimeRead = TimeRead;
  195. TimeStep = TimeStep + 1;
  196. CGs(:,:,TimeStep) = CGs(:,:,TimeStep-1);
  197. end
  198. CGs(S(2),S(3),TimeStep) = S(4);
  199. if Symmetric
  200. CGs(S(3),S(2),TimeStep) = S(4);
  201. end
  202. end
  203. TimeSequence(TimeStep) = TimeRead;
  204. else
  205. error ('Set Options.TraceMode to either Continuous or Discrete')
  206. end
  207. fclose(fid);
  208. %Options;
  209. end