You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveRandomNodes3.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. function [ data, attData, missing_nodes_mapping ] = RemoveRandomNodes3( data, attData, totalAttNum, num_missing_nodes, missing_nodes_mapping, non_neighbors_distance, missingNodesInput )
  2. %RemoveRandomNodes Remove num_missing_nodes from data. If some nodes are
  3. %removed already, provide missing_nodes_mapping
  4. % Detailed explanation goes here
  5. if nargin >= 7 % i.e. getting missingNodesInput
  6. missing_nodes = missingNodesInput;
  7. else
  8. %%data_orig = data;
  9. numAttPerPH = 0;
  10. % if the mapping is larger than the number of nodes we want to remove, empty
  11. % it and start a new mapping. This can happen if we finished looping over
  12. % the number of missing nodes and started a new iteration of an outer loop.
  13. if size(missing_nodes_mapping,2) > num_missing_nodes
  14. missing_nodes_mapping = [];
  15. num_nodes_to_remove = num_missing_nodes;
  16. else
  17. num_nodes_to_remove = num_missing_nodes - size(missing_nodes_mapping,2);
  18. end
  19. % randomly choose missing nodes
  20. %missing_nodes = ChooseMissingNodes_old(num_nodes_to_remove, data, missing_nodes_mapping, non_neighbors_distance);
  21. missing_nodes = ChooseMissingNodes(num_nodes_to_remove, data, attData, totalAttNum, numAttPerPH, missing_nodes_mapping);
  22. %sort the list and create a list of the new nodes that each missing node is mapped to - each link
  23. %to a missing node is replaced by a link to a new, "UNK" node
  24. missing_nodes = sort( unique(missing_nodes) , 'descend');
  25. missing_nodes_mapping = missing_nodes;
  26. %replace each link to a missing node with a link to a new node
  27. %find all missing node neighbors
  28. missing_nodes_all_neighbors = zeros(1, size(data,2));
  29. for curr_nissing_node = missing_nodes
  30. missing_nodes_all_neighbors = missing_nodes_all_neighbors | data(curr_nissing_node,:);
  31. end
  32. missing_nodes_all_neighbors = find(missing_nodes_all_neighbors);
  33. %for each node in missing_nodes_all_neighbors add edges to placeholder
  34. for i = missing_nodes_all_neighbors
  35. neighbors = find(data(i,:));
  36. missing_neighbors = intersect(neighbors, missing_nodes);
  37. missing_neighbors = sort(missing_neighbors, 'descend');
  38. for curr_missing_neighbor = missing_neighbors
  39. if data(i,curr_missing_neighbor) == 1
  40. % append col & row for the placeholder
  41. data = ExpandDataByOne(data, i, non_neighbors_distance);
  42. attData = ExpandAttByOne(attData, curr_missing_neighbor, non_neighbors_distance, totalAttNum, numAttPerPH);
  43. %add the new UNK node to the missing nodes mapping j is the index of the missing node
  44. %look for the first zero in column j of the missing nodes mapping and put the new node
  45. %index there
  46. added_node = 0;
  47. %add it in the first position which equals zero
  48. j = find(missing_nodes == curr_missing_neighbor, 1);
  49. for k = 1 : size(missing_nodes_mapping,1)
  50. if missing_nodes_mapping(k, j) == 0
  51. %if we start with 1000 nodes, and we have 5 missing nodes, after
  52. %adding one node at this point, the size of the graph is 1001. 5 nodes
  53. %will be removed so the correct index of the new node will be 1001 - 5 = 996.
  54. %The next one is 997 and so on.
  55. missing_nodes_mapping(k, j) = size(data,1) - num_missing_nodes;
  56. added_node = 1;
  57. break;
  58. end
  59. end
  60. %if all the column is non-zero, add a new row and put it there
  61. if added_node == 0
  62. missing_nodes_mapping = [missing_nodes_mapping; zeros(1, size(missing_nodes_mapping,2))];
  63. missing_nodes_mapping(size(missing_nodes_mapping,1), j) = size(data,1) - num_missing_nodes;
  64. end
  65. end %if friend
  66. end %missing_neighbors
  67. end %missing_nodes_all_neighbors
  68. end % if getting missingNodesInput
  69. %remove the missing nodes from the matrix (missing nodes MUST be sorted in descending order!!
  70. %so that removing one does not affect the index of the others)
  71. for j = 1:size(missing_nodes,2)
  72. missing_node_idx = missing_nodes(j);
  73. %remove column
  74. data(:, missing_node_idx) = [];
  75. %remove row
  76. data(missing_node_idx, :) = [];
  77. attData(missing_node_idx, :) = [];
  78. end
  79. end %function RemoveRandomNodes3
  80. %sigal - move old implementation to function
  81. function [missing_nodes] = ChooseMissingNodes(num_nodes_to_remove, data, attData, totalAttNum, numAttPerPH, missing_nodes_mapping)
  82. missing_nodes_all_neighbors = zeros(1, size(data,2));
  83. %randomize a list of nodes to remove and sort it
  84. if size(missing_nodes_mapping,1)> 0
  85. missing_nodes = sort(missing_nodes_mapping(1,:) , 'descend');
  86. %find all missing node neighbors
  87. for curr_missing_node = missing_nodes
  88. missing_nodes_all_neighbors = missing_nodes_all_neighbors | data(curr_missing_node,:);
  89. missing_nodes_all_neighbors(1,curr_missing_node)=1;
  90. end
  91. else
  92. missing_nodes = [];
  93. end
  94. % outlier1 - nodes with only one edge
  95. numEdges = sum(data,1);
  96. invalidNodes1a = (numEdges==1); %%numEdges<3); %%(numEdges==1);
  97. missing_nodes_all_neighbors(1,invalidNodes1a) = 1;
  98. %invalidNodes1b = (numEdges>7); %% 6.13 (mem issues) use 7
  99. %invalidNodes1b = (numEdges>15); %%25); %%(numEdges==1); %% sigal - 6.2.13 max=15 (sarit)
  100. invalidNodes1b = (numEdges>8); %%15); %% sigal/sarit - 9.12.13 max=8
  101. missing_nodes_all_neighbors(1,invalidNodes1b) = 1;
  102. % outlier2 - nodes with less than numAttPerPH attributes
  103. numAttr = sum(attData,2)';
  104. invalidNodes2 = (numAttr<numAttPerPH);
  105. missing_nodes_all_neighbors(1,invalidNodes2) = 1;
  106. % outlier statistics
  107. count = nnz(invalidNodes1a|invalidNodes1b|invalidNodes2);
  108. if count*1.5 > size(data,2)
  109. fprintf('RemoveRandomNodes2: too many outliers nodes %d.\n',count);
  110. end
  111. for i=1:num_nodes_to_remove
  112. valid_nodes = find(missing_nodes_all_neighbors~=1);
  113. inx = ceil(rand(1)*size(valid_nodes,2));
  114. node = valid_nodes(inx);
  115. % add selected node to missing_nodes list and update the all neighbors list
  116. missing_nodes = [missing_nodes node];
  117. missing_nodes_all_neighbors(1,node)=1;
  118. missing_nodes_all_neighbors = missing_nodes_all_neighbors | data(node,:);
  119. end
  120. end %ChooseMissingNodes
  121. % sigal - append col & row for the placeholder
  122. function [data] = ExpandDataByOne(data, friend, non_neighbors_distance)
  123. new_col = ones(size(data, 1), 1) * non_neighbors_distance;
  124. new_col(friend) = 1;
  125. data = [data new_col];
  126. new_row = ones(1,size(data, 2)) * non_neighbors_distance;
  127. new_row(friend) = 1;
  128. data = [data; new_row];
  129. data(size(data, 1), size(data,2)) = 0;
  130. end %ExpandDataByOne
  131. % sigal - append row for the placeholder
  132. function [attData] = ExpandAttByOne(attData, orgNode, non_neighbors_distance, totalAttNum, numAttPerPH)
  133. if totalAttNum>0 && numAttPerPH>0
  134. attIndices = find(attData(orgNode, :)==1);
  135. while size(attIndices,2) > numAttPerPH
  136. inx = ceil(rand(1)*size(attIndices,2));
  137. attIndices(:,inx) = [];
  138. end
  139. else
  140. attIndices=[];
  141. end
  142. new_row = ones(1,size(attData, 2)) * non_neighbors_distance;
  143. for i=1:size(attIndices,2)
  144. new_row(i)=1;
  145. end
  146. attData = [attData; new_row];
  147. end %ExpandAttByOne