0001 function [ network, inputList, outputList ] = setIoByModule( network, nInput, nOutput )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 moduleId = modularity_dir(full(network.matrix))';
0015 network.moduleId = moduleId;
0016 uniModuleId = unique(moduleId);
0017 nModule = numel(uniModuleId);
0018 for iModule = 1:nModule
0019 tmpModuleId = uniModuleId(iModule);
0020 tmpIx = find(moduleId == tmpModuleId);
0021 neuronIdEachModule{tmpModuleId} = network.label(tmpIx);
0022 end
0023
0024 isValid = false;
0025 while isValid == false
0026 nModuleInput = randi(nModule - 1);
0027 nModuleOutput = nModule - nModuleInput;
0028
0029 uniModuleId = uniModuleId(randperm(numel(uniModuleId)));
0030 inputModuleId = uniModuleId(1:nModuleInput);
0031 outputModuleId = uniModuleId((nModuleInput + 1):end);
0032
0033 inputModuleNeuronId = [neuronIdEachModule{inputModuleId}];
0034 outputModuleNeuronId = [neuronIdEachModule{outputModuleId}];
0035 if (numel(inputModuleNeuronId) < nInput) || ...
0036 (numel(outputModuleNeuronId) < nOutput)
0037 continue;
0038 end
0039
0040 inputModuleNeuronId = ...
0041 inputModuleNeuronId(randperm(numel(inputModuleNeuronId)));
0042 outputModuleNeuronId = ...
0043 outputModuleNeuronId(randperm(numel(outputModuleNeuronId)));
0044 inputList = inputModuleNeuronId(1:nInput);
0045 outputList = outputModuleNeuronId(1:nOutput);
0046 isValid = true;
0047 end
0048 network = sortByIO(network, [inputList, outputList]);
0049 network.inputList = inputList;
0050 network.outputList = outputList;
0051 network.inputNumber = nInput;
0052 network.outputNumber = nOutput;
0053
0054 function [ network ] = sortByIO( network, ioList )
0055 for ix = 1:length(ioList)
0056 wantNode = ioList(ix);
0057 if isnumeric(ioList)
0058 wantNodeIndex = find(network.label == wantNode, 1);
0059 elseif ischar(ioList)
0060 error('Still cannot access string label');
0061 end
0062 network.matrix = swapCol(network.matrix, ix, wantNodeIndex);
0063 network.matrix = swapRow(network.matrix, ix, wantNodeIndex);
0064 network.label = swapCol(network.label, ix, wantNodeIndex);
0065 network.moduleId = swapCol(network.moduleId, ix, wantNodeIndex);
0066 end
0067
0068 function [ matrix ] = swapCol( matrix, col1, col2 )
0069 matrix(:, [col1, col2]) = matrix(:, [col2, col1]);
0070
0071 function [ matrix ] = swapRow( matrix, col1, col2 )
0072 matrix([col1, col2], :) = matrix([col2, col1], :);