diff options
| author | ziejd2 | 2019-06-27 13:58:58 -0500 |
|---|---|---|
| committer | ziejd2 | 2019-06-27 13:58:58 -0500 |
| commit | 5310fa747a6a46a0e96dc649cbca863e2c44aeb4 (patch) | |
| tree | 3d4ba8a8a8a6f50b4877d1153e0fb390bbf86564 | |
| parent | 2be4664d4ef668feee3d1e8972c7fd0813aea7e8 (diff) | |
| download | BNW-5310fa747a6a46a0e96dc649cbca863e2c44aeb4.tar.gz | |
Version 1.22
Adding new visualization options
37 files changed, 4630 insertions, 2 deletions
diff --git a/home.php b/home.php index 1858e092..6df6c6dc 100644 --- a/home.php +++ b/home.php @@ -13,7 +13,7 @@ if($str_arrmat[1]>70.0) ?> <script> -window.open("http://bnw.genenetwork.org/BNW_1.21/sourcecodes/home.php",'_self',false); +window.open("http://bnw.genenetwork.org/BNW_1.22/sourcecodes/home.php",'_self',false); </script> <?php } @@ -21,7 +21,7 @@ else { ?> <script> -window.open("http://compbio.uthsc.edu/BNW_1.21/sourcecodes/home.php",'_self',false); +window.open("http://compbio.uthsc.edu/BNW_1.22/sourcecodes/home.php",'_self',false); </script> <?php diff --git a/info_files/plotly_notes.txt b/info_files/plotly_notes.txt new file mode 100644 index 00000000..10130e85 --- /dev/null +++ b/info_files/plotly_notes.txt @@ -0,0 +1,11 @@ +I modified files.py to use the absolute paths for the PLOTLY_DIR and TEST_DIR +This file is in: +/home/jziebart/python/lib/python2.7/site-packages/plotly + +Commands to run as apache: +Run from /sourcecodes/data: +sudo su -s /bin/bash apache -c "/home/jziebart/python/Python-2.7.15/python ../cv_plotly.py LvQ" +sudo su -s /bin/bash apache -c "sh ../plotly_loo.sh" + +Run from /sourcecodes: +sudo su -s /bin/bash apache -c "./run_loo LvQ Weight" diff --git a/sourcecodes/graph-creator.css b/sourcecodes/graph-creator.css new file mode 100644 index 00000000..c5fa7ce2 --- /dev/null +++ b/sourcecodes/graph-creator.css @@ -0,0 +1,98 @@ +body{ + margin: 0; + padding: 0; + overflow:hidden; +} + +p{ + text-align: center; + overflow: overlay; + position: relative; +} + +body{ + -webkit-touch-callout: none; + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + background-color: rgb(248, 248, 248) +} + +#toolbox{ + position: absolute; + bottom: 0; + left: 0; + margin-bottom: 0.5em; + margin-left: 1em; + border: 2px solid #EEEEEE; + border-radius: 5px; + padding: 1em; + z-index: 5; +} + +#toolbox input{ + width: 30px; + opacity: 0.4; +} +#toolbox input:hover{ + opacity: 1; + cursor: pointer; +} + +#hidden-file-upload{ + display: none; +} + +#download-input{ + margin: 0 0.5em; +} + +.conceptG text{ + pointer-events: none; +} + +marker{ + fill: #333; +} + +g.conceptG circle{ + fill: #F6FBFF; + stroke: #333; + stroke-width: 2px; +} + +g.conceptG:hover circle{ + fill: rgb(200, 238, 241); +} + +g.selected circle{ + fill: rgb(250, 232, 255); +} +g.selected:hover circle{ + fill: rgb(250, 232, 255); +} + +path.link { + fill: none; + stroke: #333; + stroke-width: 6px; + cursor: default; +} + +path.link:hover{ + stroke: rgb(94, 196, 204); +} + +g.connect-node circle{ + fill: #BEFFFF; +} + +path.link.hidden{ + stroke-width: 0; +} + +path.link.selected { + stroke: rgb(229, 172, 247); +} diff --git a/sourcecodes/graph-creator.js b/sourcecodes/graph-creator.js new file mode 100644 index 00000000..d9764d46 --- /dev/null +++ b/sourcecodes/graph-creator.js @@ -0,0 +1,275 @@ +var colors = d3.scaleOrdinal(d3.schemeCategory10); + +var svg = d3.select("svg"), + width = +svg.attr("width"), + height = +svg.attr("height"), + node, + link; + +svg.append('defs').append('marker') + .attrs({'id':'arrowhead', + 'viewBox':'-0 -5 10 10', + 'refX':13, + 'refY':0, + 'orient':'auto', + 'markerWidth':13, + 'markerHeight':13, + 'xoverflow':'visible', + 'stroke-width':'4px', + 'stroke-opacity':0.8}) + .append('svg:path') + .attr('d', 'M 0,-5 L 10 ,0 L 0,5') + .attr('fill', '#999') + .style('stroke','none'); + +var simulation = d3.forceSimulation() + .force("link", d3.forceLink().id(function (d) {return d.id;}).distance(400).strength(0)) + // .force("charge", d3.forceManyBody()) + //.force("center", d3.forceCenter(width / 2, height / 2)); + +d3.json("graph.json", function (error, graph) { + if (error) throw error; + update(graph.links, graph.nodes); + }) + + function update(links, nodes) { + link = svg.selectAll(".link") + .data(links) + .enter() + .append("line") + .attr("class", "link") + .attr('marker-end','url(#arrowhead)') + .attr('stroke',"#999") + .attr('stroke-opacity',.8) + .attr('stroke-width',"1px"); + + link.append("title") + .text(function (d) {return d.type;}); + + edgepaths = svg.selectAll(".edgepath") + .data(links) + .enter() + .append('path') + .attrs({ + 'class': 'edgepath', + 'fill-opacity': 0, + 'stroke-opacity': 0, + 'id': function (d, i) {return 'edgepath' + i} + }) + .style("pointer-events", "none"); + + edgelabels = svg.selectAll(".edgelabel") + .data(links) + .enter() + .append('text') + .style("pointer-events", "none") + .attrs({ + 'class': 'edgelabel', + 'id': function (d, i) {return 'edgelabel' + i}, + 'font-size': 17, + 'fill': '#aaa', + 'dx': 0, + 'dy': -4 + }); + + edgelabels.append('textPath') + .attr('xlink:href', function (d, i) {return '#edgepath' + i}) + .style("text-anchor", "middle") + .style("pointer-events", "none") + .attr("startOffset", "50%") + .attr("startOffset", "50%") + .text(function (d) {return d.type}); + + node = svg.selectAll(".node") + .data(nodes) + .enter() + .append("g") + .attr("class", "node") + .call(d3.drag() + .on("start", dragstarted) + .on("drag", dragged) + //.on("end", dragended) + ); + + node.append("circle") + .attr("r", 5) + .style("fill","#999"); + // .attr("r", 5) + // .style("fill", function (d, i) {return colors(i);}) + + node.append("title") + .text(function (d) {return d.name;}); + + node.append("text") + .text(function (d) {return d.name;}) + .attr("x",10) + .attr("dy", ".35em") + .attr("font","20px") + .attr("font-size",20) + //.attr("dy", -3) + // .text(function (d) {return d.name+":"+d.label;}); + + simulation + .nodes(nodes) + .on("tick", ticked); + + simulation.force("link") + .links(links); +} + +function ticked() { + link + .attr("x1", function (d) {return d.source.x;}) + .attr("y1", function (d) {return d.source.y;}) + .attr("x2", function (d) {return d.target.x;}) + .attr("y2", function (d) {return d.target.y;}); + + node + .attr("transform", function (d) {return "translate(" + d.x + ", " + d.y + ")";}); + + edgepaths.attr('d', function (d) { + return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y; + }); + + edgelabels.attr('transform', function (d) { + if (d.target.x < d.source.x) { + var bbox = this.getBBox(); + + rx = bbox.x + bbox.width / 2; + ry = bbox.y + bbox.height / 2; + return 'rotate(180 ' + rx + ' ' + ry + ')'; + } + else { + return 'rotate(0)'; + } + }); +} + +function dragstarted(d) { + if (!d3.event.active) simulation.alphaTarget(0.3).restart() + d.fx = d.x; + d.fy = d.y; +} + +function dragged(d) { + d.fx = d3.event.x; + d.fy = d3.event.y; +} + +// function dragended(d) { +// if (!d3.event.active) simulation.alphaTarget(0); +// d.fx = undefined; +// d.fy = undefined; +// } + + + +d3.select('#saveButton').on('click', function(){ + var svgString = getSVGString(svg.node()); + svgString2Image( svgString, 2*width, 2*height, 'png', save ); // passes Blob and filesize String to the callback + + function save( dataBlob, filesize ){ + saveAs( dataBlob, 'D3 vis exported to PNG.png' ); // FileSaver.js function + } + }); + +// Below are the functions that handle actual exporting: +// getSVGString ( svgNode ) and svgString2Image( svgString, width, height, format, callback ) +function getSVGString( svgNode ) { + svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink'); + var cssStyleText = getCSSStyles( svgNode ); + appendCSS( cssStyleText, svgNode ); + + var serializer = new XMLSerializer(); + var svgString = serializer.serializeToString(svgNode); + svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink='); // Fix root xlink without namespace + svgString = svgString.replace(/NS\d+:href/g, 'xlink:href'); // Safari NS namespace fix + + return svgString; + + function getCSSStyles( parentElement ) { + var selectorTextArr = []; + + // Add Parent element Id and Classes to the list + selectorTextArr.push( '#'+parentElement.id ); + for (var c = 0; c < parentElement.classList.length; c++) + if ( !contains('.'+parentElement.classList[c], selectorTextArr) ) + selectorTextArr.push( '.'+parentElement.classList[c] ); + + // Add Children element Ids and Classes to the list + var nodes = parentElement.getElementsByTagName("*"); + for (var i = 0; i < nodes.length; i++) { + var id = nodes[i].id; + if ( !contains('#'+id, selectorTextArr) ) + selectorTextArr.push( '#'+id ); + + var classes = nodes[i].classList; + for (var c = 0; c < classes.length; c++) + if ( !contains('.'+classes[c], selectorTextArr) ) + selectorTextArr.push( '.'+classes[c] ); + } + + // Extract CSS Rules + var extractedCSSText = ""; + for (var i = 0; i < document.styleSheets.length; i++) { + var s = document.styleSheets[i]; + + try { + if(!s.cssRules) continue; + } catch( e ) { + if(e.name !== 'SecurityError') throw e; // for Firefox + continue; + } + + var cssRules = s.cssRules; + for (var r = 0; r < cssRules.length; r++) { + if ( contains( cssRules[r].selectorText, selectorTextArr ) ) + extractedCSSText += cssRules[r].cssText; + } + } + + + return extractedCSSText; + + function contains(str,arr) { + return arr.indexOf( str ) === -1 ? false : true; + } + + } + + function appendCSS( cssText, element ) { + var styleElement = document.createElement("style"); + styleElement.setAttribute("type","text/css"); + styleElement.innerHTML = cssText; + var refNode = element.hasChildNodes() ? element.children[0] : null; + element.insertBefore( styleElement, refNode ); + } +} + + +function svgString2Image( svgString, width, height, format, callback ) { + var format = format ? format : 'png'; + + var imgsrc = 'data:image/svg+xml;base64,'+ btoa( unescape( encodeURIComponent( svgString ) ) ); // Convert SVG string to data URL + + var canvas = document.createElement("canvas"); + var context = canvas.getContext("2d"); + + canvas.width = width; + canvas.height = height; + + var image = new Image(); + image.onload = function() { + context.clearRect ( 0, 0, width, height ); + context.drawImage(image, 0, 0, width, height); + + canvas.toBlob( function(blob) { + var filesize = Math.round( blob.length/1024 ) + ' KB'; + if ( callback ) callback( blob, filesize ); + }); + + + }; + + image.src = imgsrc; +} diff --git a/sourcecodes/layout_cyto.php b/sourcecodes/layout_cyto.php new file mode 100644 index 00000000..100ba044 --- /dev/null +++ b/sourcecodes/layout_cyto.php @@ -0,0 +1,371 @@ +<!DOCTYPE html> +<head> + +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cytoscape-panzoom/2.5.3/cytoscape.js-panzoom.css"> +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css"> + + +<?php + +include("header_new.inc"); +include("input_validate.php"); +$keyval=valid_keyval($_GET["My_key"]); + +$dir="./data/"; + +$input_json=$dir.$keyval."network.json"; +$output_png=$keyval."modified_network.png"; + + +?> + + +<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> +<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.7.1/cytoscape.min.js"></script> +<script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script> +<script src="http://spades.bioinf.spbau.ru/~alla/graph_viewer/js/cytoscape-dagre.js"></script> +<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape-panzoom/2.5.3/cytoscape-panzoom.js"></script> +<!--- +<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/2.7.29/cytoscape.min.js"></script> +<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.7.1/cytoscape.min.js"></script> +--> +<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script> + + +<style type = "text/css"> + #cy { + width: 80%; + height: 80%; +position: absolute; +/*float: top;*/ +top: 5em; +left: 10em; +overflow: auto; +border: 2px solid; +border-radius: 0.5em; +} + + +#loading { +position: absolute; +display: block; +top: 10%; +width: 45%; +color: #000; +font-size: 8em; +text-align: center; +} + +#loading.loaded { +display: none; +} + +#filterOut { + color: white; +} + +</style> + +<script> + + // var default_layout = { name: 'breadthfirst', + // directed: true, + // maximal: true, + // grid: true, + // spacingFactor: 1, + // fit: true, //whether to fit the viewport to the graph + // padding: 10 // the padding on fit + //} + +var default_layout = { name: 'dagre', + fit: true, + padding: 30, + spacingFactor: 1.25 + } + + $.getJSON("<?php print($input_json);?>",function (data) { + //$.getJSON("./data/cwHnetwork.json",function (data) { + // console.log(data); + // document.addEventListener('DOMContentLoaded', function(){ + var cy = window.cy = cytoscape({ + container: document.getElementById('cy'), + elements: data, + layout: default_layout, + style: [ + { + selector: 'node', + style: { + 'label': 'data(label)', + 'font-size': 20, + 'shape': 'roundrectangle', + 'text-halign': 'center', + 'text-valign': 'center', + 'width': 'label', + 'height': 'label', + 'padding': '6px', + 'color': 'black', + 'background-color': 'white', + 'border-style': 'solid', + 'border-color': 'black', + 'border-width': '2px' + } + }, + { + selector: '.selected1', + style:{ + 'color': 'green', + 'shape': 'ellipse', + 'background-color': '#DCDCDC', + 'border-color': 'green', + 'border-width': '2px', + 'border-style': 'dashed' + } + }, + { + selector: '.selected2', + style:{ + 'color': 'red', + 'shape': 'octagon', + 'border-color': 'red', + 'border-width': '2px', + 'background-color': '#DCDCDC', + 'border-style': 'dashed' + } + }, + { + selector: 'edge', + style: { + 'line-color': 'black', + 'curve-style': 'bezier', + //'target-endpoint': 'outside-to-node-or-label', + 'width': '3px', + 'target-arrow-shape': 'triangle', + 'target-arrow-color': 'black', + 'control-point-step-size': '140px', + 'arrow-scale': '2' + } + }, + { + selector: '.selected3', + style:{ + 'line-color': 'red', + 'target-arrow-color': 'red', + 'line-style': 'dashed' + } + } + ] + }); + // }); + + + + //window.onload=function() { + + var eles = cy.filter(); //var containing all elements so they can be restored after being removed + + // the default values of each option are outlined below: + var defaults = { + zoomFactor: 0.05, // zoom factor per zoom tick + zoomDelay: 45, // how many ms between zoom ticks + minZoom: 0.1, // min zoom level + maxZoom: 10, // max zoom level + fitPadding: 30, // padding when fitting + panSpeed: 10, // how many ms in between pan ticks + panDistance: 10, // max pan distance per tick + panDragAreaSize: 75, // the length of the pan drag box in which the vector for panning is calculated (bigger = finer control of pan speed and direction) + panMinPercentSpeed: 0.25, // the slowest speed we can pan by (as a percent of panSpeed) + panInactiveArea: 8, // radius of inactive area in pan drag box + panIndicatorMinOpacity: 0.5, // min opacity of pan indicator (the draggable nib); scales from this to 1.0 + zoomOnly: false, // a minimal version of the ui only with zooming (useful on systems with bad mousewheel resolution) + fitSelector: undefined, // selector of elements to fit + animateOnFit: function(){ // whether to animate on fit + return false; + }, + fitAnimationDuration: 1000, // duration of animation on fit + + // icon class names + sliderHandleIcon: 'fa fa-minus', + zoomInIcon: 'fa fa-plus', + zoomOutIcon: 'fa fa-minus', + resetIcon: 'fa fa-expand', + }; + + + + cy.panzoom( defaults ); + + $("#loading").addClass("loaded"); + +//This is probably more complex than it needs to be; +// the goal is to ensure: +// 1) No more than two nodes can be selected at a time. +// 2) Only one parent node (selected1) and only one child node (selected2) +// can be selected +cy.on('tap', 'node', function(event) { + if(cy.filter("node.selected1").length == 0) { + event.target.addClass('selected1'); + event.target.removeClass('selected2'); + } else { + event.target.removeClass('selected1'); + if(cy.filter("node.selected1").length == 1) { + if(cy.filter("node.selected2").length == 0) { + event.target.addClass('selected2'); + } else { + event.cy.filter("node.selected2").removeClass("selected2"); + } + } else { + event.cy.filter("node.selected1").removeClass("selected1"); + event.cy.filter("node.selected2").removeClass("selected2"); + } + } + }); +//The code below works with older versions of Cytoscape.js (before v3) +//cy.on('tap', 'node', function(event) { +// if(cy.filter("node.selected1").length == 0) { +// event.cyTarget.addClass('selected1'); +// event.cyTarget.removeClass('selected2'); +// } else { +// event.cyTarget.removeClass('selected1'); +// if(cy.filter("node.selected1").length == 1) { +// if(cy.filter("node.selected2").length == 0) { +// event.cyTarget.addClass('selected2'); +// } else { +// event.cy.filter("node.selected2").removeClass("selected2"); +// } +// } else { +// event.cy.filter("node.selected1").removeClass("selected1"); +// event.cy.filter("node.selected2").removeClass("selected2"); +// } +// } +// }); + + +$("#addEdge").click(function (e) { + if(cy.filter("node.selected1").length !=1) + return; + if(cy.filter("node.selected1").length !=1) + return; + var edge = new Object(); + edge.group = 'edges'; + edge.data = {source: cy.filter("node.selected1")[0].data('id'), target: cy.$("node.selected2")[0].data('id'), weight: 1}; + cy.add(edge); + cy.filter("node.selected1").removeClass('selected1',false); + cy.filter("node.selected2").removeClass('selected2',false); + }); + + +//cy.on('tap', 'edge', function(event) { +// event.cyTarget.toggleClass('selected3'); +// }); + +cy.on('tap', 'edge', function(event) { + event.target.toggleClass('selected3'); + }); + +$("#deleteEdge").click(function (e) { + var tEdges = cy.filter("edge.selected3"); + for (var i = 0; i < tEdges.length; i++) + { + cy.remove(tEdges[i]); + } + }); + + +//$("#saveNetwork").click(function (e) { +// console.log(cy.elements().jsons()); +//}); + +$("#downloadNetwork").click(function (e) { + var png64 = cy.png(); + $(this).attr('href',png64); + $(this).attr('download',"<?php print($output_png);?>"); + }); + +$("#saveNetwork").click(function (e) { + //var jsonObject = JSON.stringify(cy.elements().jsons()); + + $("#loading").removeClass("loaded"); + var node_labs = cy.nodes().map(function (ele) { + return ele.data('label'); + }); + var sources = cy.edges().map(function (ele) { + return ele.data('source'); + }); + var targets = cy.edges().map(function (ele) { + return ele.data('target'); + }); + var weights = cy.edges().map(function (ele) { + return ele.data('weight'); + }); + + var old_key = "<?php print($keyval);?>"; + $.post("modified_network.php", { + old_key : old_key, + nnodes : node_labs.length, + node_labs : JSON.stringify(node_labs), + nedges : sources.length, + sources : JSON.stringify(sources), + targets : JSON.stringify(targets), + weights : JSON.stringify(weights) + }, + function( data ) { + window.location.href = data; + }); + // $.post("modified_network.php", { + // str : old_key, + //json : jsonObject + // }, +}); + + + + + +$('#pos_slide').change(function (e) { + eles.restore(); + pos_slide_val = $('#pos_slide').val(); + cy.$("edge[weight < " + pos_slide_val + "]").remove(); + }); + +// }; +}); + + + +</script> + + + + +</head> + + +<body> +<!-- Site navigation menu --> +<ul class="navbar2"> + <li><p>Network ID:<br><?php print($keyval);?></p></li> +</ul> + +<ul class="navbar"> + <li><a id="addEdge" href="#">Add edge between selected nodes</a></li> + <li><a id="deleteEdge" href="#">Remove selected edges</a></li> + <li><a id="weightFilter" href="#">Filter edges by weight:</a> + <form name="weightValue"> + <output name="filterOut" id="filterOut">0.5</output><br> + <input type="range" name="weightOutputName" id= "pos_slide" min="0.5" max="1" value ="0.5" step="0.01" list="weight" style="display: inline; width: 120px" oninput="filterOut.value = pos_slide.value"> + </form> + </li> + <li><a id="saveNetwork" href="#">Use modified network</a></li> + <li><a id="downloadNetwork" href="#">Save network as png</a></li> + <li><a href="help.php" target='_blank'>Help</a> + <li><a href="../home.php">Home</a> +</ul> + +<div id="cy"></div> +<div = id="loading"> + <span class="fa fa-refresh fa-spin"></spin> +</div> + +</body> + +</html> \ No newline at end of file diff --git a/sourcecodes/layout_svg_no.php b/sourcecodes/layout_svg_no.php new file mode 100644 index 00000000..673da217 --- /dev/null +++ b/sourcecodes/layout_svg_no.php @@ -0,0 +1,264 @@ +<!DOCTYPE html> +<html> +<head> +<style> +.button { + font-family: Times; + background-color: #33339F; + border: none; + color: white; + padding: 5px 5px; + text-align: center; + font-size: 16px; + } + +.button:hover { + background-color: blue; + } +</style> +</head> + +<?php + +include("header_new.inc"); +include("input_validate.php"); +$keyval=valid_keyval($_GET["My_key"]); + +$dir="./data/"; + +$svg_file=$dir.$keyval."network_no_edge.svg"; +$png_file=$keyval."network_no_edge.png"; + + +?> +<script language="JavaScript"> +<!-- +function calcHeight() +{ + //find the height of the internal page + var the_height= + document.getElementById('the_iframe').contentWindow. + document.body.scrollHeight; + + //change the height of the iframe + document.getElementById('the_iframe').height= + the_height; +} +//--> +</script> + +<script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script> +<script src="http://d3js.org/d3-selection-multi.v1.js"></script> +<script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script> +<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script> + + +<!-- Site navigation menu --> +<ul class="navbar2"> + <li><p>Network ID:<br><?php print($keyval);?></p></li> +</ul> + +<ul class="navbar"> +<li><a href="layout.php?My_key=<?php print($keyval);?>">Use network to make predictions</a> +<li><a href="layout_cyto.php?My_key=<?php print($keyval);?>" target='_blank'>Modify network structure</a> +<li><a href="modify_structure_learning.php?My_key=<?php print($keyval);?>" target='_blank';>Modify structure learning settings</a> + <li><a href="javascript:void(0);" +NAME="Model Averaging Matrix" title="Model Averaging Matrix" +onClick=window.open("matrix.php?My_key=<?php print($keyval);?>","Ratting","width=950,height=270,0,status=0,");>Display structure matrix</a> + <li><a href="help.php" target='_blank'>Help</a> + <li><a href="../home.php">Home</a> +</ul> + +<div id="svg_div"> + +<a href="layout_svg_wt.php?My_key=<?php print($keyval);?>"><button type="submit" class="button">Show edge weights</button></a> + + + +<button id='saveButton' class="button">Save network as PNG</button> + + + +<a href="<?php print($svg_file);?>" download><button type="submit" class="button">Save network as SVG</button></a> +<br> +<br> + + +<script> + //onload = "makeDraggable(evt)"; + +d3.xml("<?php print($svg_file);?>", function(error, documentFragment){ + if (error) {console.log(error); return;} + + var svgNode = documentFragment + .getElementsByTagName("svg")[0]; + + d3.select("#svg_div").node().appendChild(svgNode); + // nodes = d3.selectAll('.node'); + // links = d3.selectAll('.edge'); + + width = svgNode.getBBox().width*5; + height = svgNode.getBBox().height*5; + + // nodes + // .call(d3.drag() + // .on("start",dragstarted) + // .on("drag",dragged) + // .on("end",dragended)); + + // links + // .call(d3.drag() + // .on("start",dragstarted) + // .on("drag",dragged) + // .on("end",dragended)); + + + //function dragstarted(d) { + // d3.select(this).raise().classed("active", true); + //} + + //function dragged(d) { + //this.x = this.x || 0; + //this.y = this.y || 0; + //this.x += d3.event.dx; + //this.y += d3.event.dy; + //d3.select(this) + // .attr("transform","translate(" + this.x + "," + this.y + ")"); + //} + + //function dragended(d) { + //d3.select(this).classed("active", false); + //} + + //console.log(svgNode); + //console.log(nodes); + //console.log(links); + + }); + +</script> + + + +<script> + + d3.select('#saveButton').on('click', function(){ + var svgString = getSVGString(d3.select("#svg_div").select('svg').node()); + svgString2Image( svgString, width, height, 'png', save ); +// passes Blob and filesize String to the callback + + function save( dataBlob, filesize ){ + saveAs( dataBlob, "<?php echo $png_file; ?>" ); // FileSaver.js function + } + }); +// Below are the functions that handle actual exporting: +// getSVGString ( svgNode ) and svgString2Image( svgString, width, height, format, callback ) +function getSVGString( svgNode ) { + // svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink'); + var cssStyleText = getCSSStyles( svgNode ); + appendCSS( cssStyleText, svgNode ); + + var serializer = new XMLSerializer(); + var svgString = serializer.serializeToString(svgNode); + svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink='); // Fix root xlink without namespace + svgString = svgString.replace(/NS\d+:href/g, 'xlink:href'); // Safari NS namespace fix + + return svgString; + +function getCSSStyles( parentElement ) { + var selectorTextArr = []; + + // Add Parent element Id and Classes to the list + selectorTextArr.push( '#'+parentElement.id ); + for (var c = 0; c < parentElement.classList.length; c++) + if ( !contains('.'+parentElement.classList[c], selectorTextArr) ) + selectorTextArr.push( '.'+parentElement.classList[c] ); + + // Add Children element Ids and Classes to the list + var nodes = parentElement.getElementsByTagName("*"); + for (var i = 0; i < nodes.length; i++) { + var id = nodes[i].id; + if ( !contains('#'+id, selectorTextArr) ) + selectorTextArr.push( '#'+id ); + + var classes = nodes[i].classList; + for (var c = 0; c < classes.length; c++) + if ( !contains('.'+classes[c], selectorTextArr) ) + selectorTextArr.push( '.'+classes[c] ); + } + + // Extract CSS Rules + var extractedCSSText = ""; + for (var i = 0; i < document.styleSheets.length; i++) { + var s = document.styleSheets[i]; + + try { + if(!s.cssRules) continue; + } catch( e ) { + if(e.name !== 'SecurityError') throw e; // for Firefox + continue; + } + + var cssRules = s.cssRules; + for (var r = 0; r < cssRules.length; r++) { + if ( contains( cssRules[r].selectorText, selectorTextArr ) ) + extractedCSSText += cssRules[r].cssText; + } + } + + + return extractedCSSText; + + function contains(str,arr) { + return arr.indexOf( str ) === -1 ? false : true; + } + +} + +function appendCSS( cssText, element ) { + var styleElement = document.createElement("style"); + styleElement.setAttribute("type","text/css"); + styleElement.innerHTML = cssText; + var refNode = element.hasChildNodes() ? element.children[0] : null; + element.insertBefore( styleElement, refNode ); +} +} + +function svgString2Image( svgString, width, height, format, callback ) { + var format = format ? format : 'png'; + + var imgsrc = 'data:image/svg+xml;base64,'+ btoa( unescape( encodeURIComponent( svgString ) ) ); // Convert SVG string to data URL + + var canvas = document.createElement("canvas"); + var context = canvas.getContext("2d"); + + var canvas = document.createElement("canvas"); + var context = canvas.getContext("2d"); + + canvas.width = width; + canvas.height = height; + + var image = new Image(); + image.onload = function() { + context.clearRect ( 0, 0, width, height ); + context.drawImage(image, 0, 0, width, height); + + canvas.toBlob( function(blob) { + var filesize = Math.round( blob.length/1024 ) + ' KB'; + if ( callback ) callback( blob, filesize ); + }); + + + }; + + image.src = imgsrc; +} + + + +</script> + + +</div> +</body> +</html> \ No newline at end of file diff --git a/sourcecodes/layout_svg_wt.php b/sourcecodes/layout_svg_wt.php new file mode 100644 index 00000000..f24bb338 --- /dev/null +++ b/sourcecodes/layout_svg_wt.php @@ -0,0 +1,265 @@ +<!DOCTYPE html> +<html> +<head> +<style> +.button { + font-family: Times; + background-color: #33339F; + border: none; + color: white; + padding: 5px 5px; + text-align: center; + font-size: 16px; + } + +.button:hover { + background-color: blue; + } +</style> +</head> + +<?php + +include("header_new.inc"); +include("input_validate.php"); +$keyval=valid_keyval($_GET["My_key"]); + +$dir="./data/"; + +$svg_file=$dir.$keyval."network.svg"; +$png_file=$keyval."network.png"; + + +?> +<script language="JavaScript"> +<!-- +function calcHeight() +{ + //find the height of the internal page + var the_height= + document.getElementById('the_iframe').contentWindow. + document.body.scrollHeight; + + //change the height of the iframe + document.getElementById('the_iframe').height= + the_height; +} +//--> +</script> + +<script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script> +<script src="http://d3js.org/d3-selection-multi.v1.js"></script> +<script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script> +<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script> + + +<!-- Site navigation menu --> +<ul class="navbar2"> + <li><p>Network ID:<br><?php print($keyval);?></p></li> +</ul> + +<ul class="navbar"> +<li><a href="layout.php?My_key=<?php print($keyval);?>">Use network to make predictions</a> +<li><a href="layout_cyto.php?My_key=<?php print($keyval);?>" target='_blank'>Modify network structure</a> +<li><a href="modify_structure_learning.php?My_key=<?php print($keyval);?>" target='_blank';>Modify structure learning settings</a> + <li><a href="javascript:void(0);" +NAME="Model Averaging Matrix" title="Model Averaging Matrix" +onClick=window.open("matrix.php?My_key=<?php print($keyval);?>","Ratting","width=950,height=270,0,status=0,");>Display structure matrix</a> + <li><a href="help.php" target='_blank'>Help</a> + <li><a href="../home.php">Home</a> +</ul> + +<div id="svg_div"> + +<a href="layout_svg_no.php?My_key=<?php print($keyval);?>"><button type="submit" class="button">Remove edge weights</button></a> + + + +<button id='saveButton' class="button">Save network as PNG</button> + + + +<a href="<?php print($svg_file);?>" download><button type="submit" class="button">Save network as SVG</button></a> +<br> +<br> + + +<script> + //onload = "makeDraggable(evt)"; + +d3.xml("<?php print($svg_file);?>", function(error, documentFragment){ + if (error) {console.log(error); return;} + + + var svgNode = documentFragment + .getElementsByTagName("svg")[0]; + + d3.select("#svg_div").node().appendChild(svgNode); + //nodes = d3.selectAll('.node'); + //links = d3.selectAll('.edge'); + + width = svgNode.getBBox().width*5; + height = svgNode.getBBox().height*5; + + // nodes + // .call(d3.drag() + // .on("start",dragstarted) + // .on("drag",dragged) + // .on("end",dragended)); + + // links + // .call(d3.drag() + // .on("start",dragstarted) + // .on("drag",dragged) + // .on("end",dragended)); + + + //function dragstarted(d) { + // d3.select(this).raise().classed("active", true); + //} + + //function dragged(d) { + // this.x = this.x || 0; + // this.y = this.y || 0; + // this.x += d3.event.dx; + // this.y += d3.event.dy; + // d3.select(this) + // .attr("transform","translate(" + this.x + "," + this.y + ")"); + //} + + //function dragended(d) { + // d3.select(this).classed("active", false); + //} + +//console.log(svgNode); +//console.log(nodes); +//console.log(links); + + }); + +</script> + + + +<script> + + d3.select('#saveButton').on('click', function(){ + var svgString = getSVGString(d3.select("#svg_div").select('svg').node()); + svgString2Image( svgString, width, height, 'png', save ); +// passes Blob and filesize String to the callback + + function save( dataBlob, filesize ){ + saveAs( dataBlob, "<?php echo $png_file; ?>" ); // FileSaver.js function + } + }); +// Below are the functions that handle actual exporting: +// getSVGString ( svgNode ) and svgString2Image( svgString, width, height, format, callback ) +function getSVGString( svgNode ) { + // svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink'); + var cssStyleText = getCSSStyles( svgNode ); + appendCSS( cssStyleText, svgNode ); + + var serializer = new XMLSerializer(); + var svgString = serializer.serializeToString(svgNode); + svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink='); // Fix root xlink without namespace + svgString = svgString.replace(/NS\d+:href/g, 'xlink:href'); // Safari NS namespace fix + + return svgString; + +function getCSSStyles( parentElement ) { + var selectorTextArr = []; + + // Add Parent element Id and Classes to the list + selectorTextArr.push( '#'+parentElement.id ); + for (var c = 0; c < parentElement.classList.length; c++) + if ( !contains('.'+parentElement.classList[c], selectorTextArr) ) + selectorTextArr.push( '.'+parentElement.classList[c] ); + + // Add Children element Ids and Classes to the list + var nodes = parentElement.getElementsByTagName("*"); + for (var i = 0; i < nodes.length; i++) { + var id = nodes[i].id; + if ( !contains('#'+id, selectorTextArr) ) + selectorTextArr.push( '#'+id ); + + var classes = nodes[i].classList; + for (var c = 0; c < classes.length; c++) + if ( !contains('.'+classes[c], selectorTextArr) ) + selectorTextArr.push( '.'+classes[c] ); + } + + // Extract CSS Rules + var extractedCSSText = ""; + for (var i = 0; i < document.styleSheets.length; i++) { + var s = document.styleSheets[i]; + + try { + if(!s.cssRules) continue; + } catch( e ) { + if(e.name !== 'SecurityError') throw e; // for Firefox + continue; + } + + var cssRules = s.cssRules; + for (var r = 0; r < cssRules.length; r++) { + if ( contains( cssRules[r].selectorText, selectorTextArr ) ) + extractedCSSText += cssRules[r].cssText; + } + } + + + return extractedCSSText; + + function contains(str,arr) { + return arr.indexOf( str ) === -1 ? false : true; + } + +} + +function appendCSS( cssText, element ) { + var styleElement = document.createElement("style"); + styleElement.setAttribute("type","text/css"); + styleElement.innerHTML = cssText; + var refNode = element.hasChildNodes() ? element.children[0] : null; + element.insertBefore( styleElement, refNode ); +} +} + +function svgString2Image( svgString, width, height, format, callback ) { + var format = format ? format : 'png'; + + var imgsrc = 'data:image/svg+xml;base64,'+ btoa( unescape( encodeURIComponent( svgString ) ) ); // Convert SVG string to data URL + + var canvas = document.createElement("canvas"); + var context = canvas.getContext("2d"); + + var canvas = document.createElement("canvas"); + var context = canvas.getContext("2d"); + + canvas.width = width; + canvas.height = height; + + var image = new Image(); + image.onload = function() { + context.clearRect ( 0, 0, width, height ); + context.drawImage(image, 0, 0, width, height); + + canvas.toBlob( function(blob) { + var filesize = Math.round( blob.length/1024 ) + ' KB'; + if ( callback ) callback( blob, filesize ); + }); + + + }; + + image.src = imgsrc; +} + + + +</script> + + +</div> +</body> +</html> \ No newline at end of file diff --git a/sourcecodes/modified_network.php b/sourcecodes/modified_network.php new file mode 100644 index 00000000..a8eabefa --- /dev/null +++ b/sourcecodes/modified_network.php @@ -0,0 +1,57 @@ +<?php +include("input_validate.php"); + +//Get data sent from cytoscape file +//$json = $_POST['json']; +$old_key = $_POST['old_key']; +$nnodes = $_POST['nnodes']; +$node_labs = $_POST['node_labs']; +$nedges = $_POST['nedges']; +$sources = $_POST['sources']; +$targets = $_POST['targets']; +$weights = $_POST['weights']; + +/////////////Generate a random key///////////////////// +$alphas=array(); +$alphas = array_merge(range('A', 'Z'), range('a', 'z')); + +$al1=rand(0,51); +$al2=rand(0,51); +$al3=rand(0,51); + +$alpha="$alphas[$al1]"."$alphas[$al2]"."$alphas[$al3]"; +$keyval=$alpha; + +if($_POST["My_key"]!="") + $keyval=$_POST["My_key"]; + $keyval=valid_keyval($keyval); + + +$filename = "./data/".$old_key."modify_edge.txt"; + +$file = fopen($filename,'w'); +//fwrite($file,$json); +fwrite($file,$nnodes); +fwrite($file,"\n"); +fwrite($file,$node_labs); +fwrite($file,"\n"); +fwrite($file,$nedges); +fwrite($file,"\n"); +fwrite($file,$sources); +fwrite($file,"\n"); +fwrite($file,$targets); +fwrite($file,"\n"); +fwrite($file,$weights); +fwrite($file,"\n"); +fclose($file); + +shell_exec('./run_scripts/run_mod_edges '.$old_key.' '.$keyval); + + +$new_page = "graphviz_structure.php?My_key=".$keyval; +echo $new_page; +//echo "<script type='text/javascript'>location.href=".$new_page."</script>;"; + + + +?> diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m new file mode 100644 index 00000000..9d107628 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m @@ -0,0 +1,72 @@ +function Predictmultiple(pre) +dfile=strcat(pre,'structure_input.txt'); +sfile=dfile; +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); + +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +[bnet]=parameterLearning(bnet,cases); + +fvarfile=strcat(pre,'var.txt'); +fvar = fopen(fvarfile,'r'); +select_var_new = fscanf(fvar,'%d'); + +fvardfile=strcat(pre,'vardata.txt'); +fvard = fopen(fvardfile,'r'); +select_var_data_new = fscanf(fvard,'%f'); + +means_orig = cell(1,nnodes); +stdevs_orig = cell(1,nnodes); +labels_orig = cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); + +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + + +filename=strcat(pre,'net_figure_new.txt'); + +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); + +writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); + +end diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m b/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m new file mode 100644 index 00000000..e9f741f2 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m @@ -0,0 +1,95 @@ +function Predictmultipleintrvention(pre) +dfile=strcat(pre,'structure_input.txt'); +sfile=dfile; +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); + +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +fvarnamefile=strcat(pre,'varname.txt'); + +varfile = fopen(fvarnamefile,'r'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +[bnet]=parameterLearning(bnet,cases); + +fvarfile=strcat(pre,'var.txt'); +fvar = fopen(fvarfile,'r'); +select_var_new = fscanf(fvar,'%d'); + +nm = numel(select_var_new); + +varlabels = cell(1,nm); +varbuffer = fgetl(varfile); %get header line as a string +for j=1:nm + [varnext,varbuffer] = strtok(varbuffer); + varlabels{j} = varnext; + for i=1:nnodes + if strcmp(varlabels{j},labels{i}) + select_var_new(j)=i; + end + end + +end + + + + +fvardfile=strcat(pre,'vardata.txt'); + +fvard = fopen(fvardfile,'r'); + +select_var_data_new = fscanf(fvard,'%f'); + +means_orig = cell(1,nnodes); +stdevs_orig = cell(1,nnodes); +labels_orig = cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); + +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + +filename=strcat(pre,'net_figure_new.txt'); + +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); + +writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); + +end diff --git a/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m b/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m new file mode 100644 index 00000000..c9d0692c --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m @@ -0,0 +1,37 @@ +function [ ] = checkDiscreteNodes( bnet, cases) + %checkDiscreteNodes Checks if states of discrete nodes are be integers from 1 to M + % where M is the number of states of the node. (M should be the same as + % node_sizes in the bnet). + % + %Input: + % bnet: BNT bnet + % cases: cell array of data + % +% +node_sizes = bnet.node_sizes; +dnodes = bnet.dnodes; +ndisc = size(dnodes,2); +ncases = size(cases,2); + +%check to see that all data for discrete nodes are integers +for i = 1:ndisc + inode = dnodes(i); + data = cases(inode,:); + isize = node_sizes(inode); + states = zeros(1,isize); + for j = 1:isize + states(j) = j; + end + for j = 1:ncases + k = int64(data{j}); + if ~any(k==states) + error(['Discrete nodes must be integers from 1 to the number of states']); + end + end +end + + +end + + + diff --git a/sourcecodes/parameter_learning/code_backup/checkStructure.m b/sourcecodes/parameter_learning/code_backup/checkStructure.m new file mode 100644 index 00000000..b4de9403 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/checkStructure.m @@ -0,0 +1,78 @@ +function [ labels, cases, dag, node_sizes, ord_flag ] = checkStructure(labels, cases, dag, node_sizes) + %checkStructure Check to see if nodes are sorted correctly. Nodes must be + % in topological order (i.e., parents before children) before parameter + % learning can take place. This function performs this sorting. + % + %Input and output have the same meaning. The output has just been + %topologically ordered. + % labels = cell array with the names of the nodes. + % cases = cell array with the data. + % dag = matrix with the strucutre of the network. + % node_sizes = vector with the size of each node. + +%make connections array +%count how big you need the connections array to be +nnodes = size(dag,1); +narcs = 0; +for i = 1:nnodes + for j = 1:nnodes + if dag(i,j) == 1 + narcs = narcs + 1; + end + end +end +%fill connections array with label names +connections = cell(narcs,2); +ncount = 0; +for i = 1:nnodes + for j = 1:nnodes + if dag(i,j) == 1 + ncount = ncount + 1; + connections{ncount,1} = labels{i}; + connections{ncount,2} = labels{j}; + end + end +end + +%get topologically sorted dag and labels +[new_dag, new_labels] = mk_adj_mat(connections, labels, 1); + +%check to see if order changed +ord_flag = 0; +for i = 1:nnodes + if ~strcmp(new_labels{i},labels{i}) + ord_flag = 1; + end +end + +if ord_flag + %get new ordering of nodes + order = cell(1,nnodes); + for i = 1:nnodes + for j = 1:nnodes + if strcmp(new_labels{j},labels{i}) + order{i} = j; + end + end + end + + %reorder cases and node_sizes + new_cases = cell(size(cases)); + for i = 1:nnodes + new_cases(order{i},:) = cases(i,:); + end + new_node_sizes = zeros(1,nnodes); + for i = 1:nnodes + new_node_sizes(order{i}) = node_sizes(i); + end + + + dag = new_dag; + cases = new_cases; + node_sizes = new_node_sizes; + labels = new_labels; +end + +end +%end checkStructure.m + diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m b/sourcecodes/parameter_learning/code_backup/drawFigure.m new file mode 100644 index 00000000..f84bffa3 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigure.m @@ -0,0 +1,390 @@ +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigure writes the parameters and data that are needed to draw the +%structure of a Bayesian network for BNW. +% This is the first function that + + + +if nargin < 8, + drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means); +else + drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata); +end; + +end + + + +function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line + +%Create an empty evidence cell array. + +%val=cases; +%for i = 1:nnodes +% val(i,1)=val(i,2); + +%end + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +evidence{selectvar}=selectdata; + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); + +%%%%Evidence node +fprintf(fileID,'%i\n',selectvar); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + xvals(j,1) = xvals(j,1)*stdevs{i}+means{i} + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1); + end + +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + + + + + +function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + +%Create an empty evidence cell array. +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); + +%Get canvas size + +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +%[x,y] = layout_dag(bnet.dag); + + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + %cases(i) + % MAX(cases(i)) + % MIN(cases(i)) + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m~ b/sourcecodes/parameter_learning/code_backup/drawFigure.m~ new file mode 100644 index 00000000..404a65f7 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigure.m~ @@ -0,0 +1,388 @@ +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigure writes the parameters and data that are needed to draw the +%structure of a Bayesian network. + + +if nargin < 8, + drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means); +else + drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata); +end; + +end + + + +function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line + +%Create an empty evidence cell array. + +%val=cases; +%for i = 1:nnodes +% val(i,1)=val(i,2); + +%end + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +evidence{selectvar}=selectdata; + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); + +%%%%Evidence node +fprintf(fileID,'%i\n',selectvar); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + xvals(j,1) = xvals(j,1)*stdevs{i}+means{i} + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1); + end + +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + + + + + +function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + +%Create an empty evidence cell array. +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); + +%Get canvas size + +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +%[x,y] = layout_dag(bnet.dag); + + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + %cases(i) + % MAX(cases(i)) + % MIN(cases(i)) + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigureM.m b/sourcecodes/parameter_learning/code_backup/drawFigureM.m new file mode 100644 index 00000000..91b8698f --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigureM.m @@ -0,0 +1,230 @@ +function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigureM writes the parameters and data that are needed to draw the +%structure of a Bayesian network after added evidence or intervention + +fileID = fopen(filename,'w'); + + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardized evidence for continuous nodes. + if bnet.node_sizes(di) == 1 + ev_dat(di) = (ev_dat(di) - means{di}) / stdevs{di}; + end + evidence{di}=ev_dat(di); + fprintf(fileID,'%i\t',di); +end + +fprintf(fileID,'\n'); + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +x = x - min(x); +y = 1 - y; +y = y - min(y); +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim); +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + if bnet.node_sizes(i) == 1, + fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i)*stdevs{i}+means{i},1); + else + fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i),1); + endif + end + +end + +fclose(fileID); +end + + + + + + + + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/getParams.m b/sourcecodes/parameter_learning/code_backup/getParams.m new file mode 100644 index 00000000..31f84ffb --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/getParams.m @@ -0,0 +1,22 @@ +function [ bnet ] = getParams( bnet, cases ) +%getParams Code to initialize CPT and do parameter learning. +%This will be very basic for now. I can add more options later. + +dnodes = bnet.dnodes; +cnodes = bnet.cnodes; +nnodes = size(dnodes,2)+size(cnodes,2); + +%make dnodes tabular_CPT +for i = 1:size(dnodes,2) + bnet.CPD{dnodes(i)} = tabular_CPD(bnet,dnodes(i)); +end + +for i = 1:size(cnodes,2) + bnet.CPD{cnodes(i)} = gaussian_CPD(bnet,cnodes(i)); +end + +bnet = learn_params(bnet,cases); + + +end + diff --git a/sourcecodes/parameter_learning/code_backup/looCrossValid.m b/sourcecodes/parameter_learning/code_backup/looCrossValid.m new file mode 100644 index 00000000..21d6560d --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/looCrossValid.m @@ -0,0 +1,241 @@ +function looCrossValid(pre,predict_label) +% This function will peform leave-one-out cross-validation. +% This requires the specification of the name of the variable +% that you want to predict. +% For now, I will assume that the variable is a discrete variable. + + + +sfile=strcat(pre,'structure_input.txt'); +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +for i=1:nnodes + if strcmp(labels(i),predict_label) + predict_node = i; + end +end + +predict_cases = bnet.node_sizes(predict_node); + +if predict_cases == 1 + looCV_continuous(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases); +else + looCV_discrete(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases); +endif + +end + +function looCV_continuous(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases) + +ncases = size(cases,2); + +%%Read in original means and standard deviations to report output as +%% untransformed values. +means_orig=cell(1,nnodes); +stdevs_orig=cell(1,nnodes); +labels_orig=cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + + +loopredictions=zeros(size(cases,2),2); + +%t=cputime; +%First get loo predictions +for i =1:ncases +% i + current_data = cases(:,i); + cases_new = cases; + cases_new(:,i) = []; + evidence = current_data; + evidence{predict_node} = {}; + [bnet]=parameterLearning(bnet,cases_new); + engine = jtree_inf_engine(bnet); + [engine,loglik] = enter_evidence(engine,evidence); + predict = marginal_nodes(engine,predict_node); + adj_mu = predict.mu*stdevs{predict_node}+means{predict_node}; + adj_sigma = stdevs{predict_node}*predict.Sigma; + loopredictions(i,1) = adj_mu; + loopredictions(i,2) = adj_sigma; +end +%e=cputime-t; + + +%Open output file. +filename = strcat(pre,'looCV.txt'); +fileID = fopen(filename,'w'); + +fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); + + +%%Print the predictions +fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); +fprintf(fileID,'Mean\tStDev\n'); +for i = 1:ncases + fprintf(fileID,'%i\t',i); + fprintf(fileID,'%6.4f\t%6.4f\n',loopredictions(i,:)); +end + +end + + +function looCV_discrete(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases) + +ncases = size(cases,2); + +%This next section just gets the original names of the levels. +% so they can be written to the output file. +%%Get the maximum_number of states so array will be big enough +%%Add 1 because the input includes the node name +max_states = max(bnet.node_sizes) + 1; +disc_nodes = size(bnet.dnodes,2); + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; + buffer = fgetl(flevels); + for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + +pred_levels = cell(1,predict_cases); +for i = 1:disc_nodes + if strcmp(levels{i,1},predict_label); + for j = 1:predict_cases + pred_levels{j} = levels{i,j+1}; + end + break + end +end + +loopredictions=zeros(size(cases,2),predict_cases); + +%t=cputime; +%First get loo predictions +for i =1:ncases +% i + current_data = cases(:,i); + cases_new = cases; + cases_new(:,i) = []; + evidence = current_data; + evidence{predict_node} = {}; + + [bnet]=parameterLearning(bnet,cases_new); + engine = jtree_inf_engine(bnet); + [engine,loglik] = enter_evidence(engine,evidence); + predict = marginal_nodes(engine,predict_node); + for j = 1:predict_cases + loopredictions(i,j) = predict.T(j); + end +end +%e=cputime-t; + +%Now compare with actual outcomes +actual_states = zeros(1,predict_cases); +for i=1:ncases + for j = 1:predict_cases + if cell2mat(cases(predict_node,i)) == j + actual_states(j) = actual_states(j) + 1; + end + end +end + +actual_states; +pred_states = zeros(1,ncases); + +for i=1:ncases + max_state = 1; + for j = 2:predict_cases + if loopredictions(i,j) > loopredictions(i,max_state) + max_state = j; + end + end + pred_states(i) = max_state; +end + +correct = 0; +for i=1:ncases + if pred_states(i) == cell2mat(cases(predict_node,i)) + correct = correct + 1; + end +end + +accuracy = correct/ncases; + +%Open output file. +filename = strcat(pre,'looCV.txt'); +fileID = fopen(filename,'w'); + +fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); + + +%%Print the accuracy +fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); + +%%Print the predictions +fprintf(fileID,'Predicted likelihood of each state for each case:\n'); +fprintf(fileID,'%s\t','Case'); +fprintf(fileID,'%s\t',pred_levels{1:end-1}); +fprintf(fileID,'%s\n',pred_levels{end}); +for i = 1:ncases + fprintf(fileID,'%i\t',i); + fprintf(fileID,'%6.4f\t',loopredictions(i,1:end-1)); + fprintf(fileID,'%6.4f\n',loopredictions(i,end)); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/parameterLearning.m b/sourcecodes/parameter_learning/code_backup/parameterLearning.m new file mode 100644 index 00000000..872e94b1 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/parameterLearning.m @@ -0,0 +1,17 @@ +function [ bnet ] = parameterLearning( bnet,cases,engine_name ) +%parameterLearning Do parameter learning and inference + +%engine is an optional argument +if nargin < 3 + engine_name = 'jtree_inf_engine'; +end + + +%First do parameter learning with all the data +[bnet] = getParams(bnet,cases); + + + + +end + diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m b/sourcecodes/parameter_learning/code_backup/prepareInput.m new file mode 100644 index 00000000..838dcd2c --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/prepareInput.m @@ -0,0 +1,294 @@ +function [ ] = prepareInput( pre ) + % + % This function takes files that are uploaded to BNW and creates output + % files that can be used for structure and parameter learning. + % It replaces php code that was previously in bn_file_load_gom.php. + % There are several improvements in performance and ease of use: + % 1) Loading files is significantly (~5x) faster for large input files. + % 2) The allowed values for discrete variables are more flexible. + % (e.g., A genotype variable be 'B' and 'D' instead of having + % to replace to make them '1' and '2'.) + % 3) Continuous variables may be identified as continuous in some cases + % even if there is not a period. + % 4) The states of discrete variables should be correctly ordered in + % almost all cases. + % 5) An additional output file is written that will let users check if + % the input file has been uploaded and parsed correctly. + % 6) Future updates to this code should be easier than updating the php. + % + % + % Input: ???continuous_input_orig.txt + % This is the input file that is uploaded to BNW. + % It is directly written out by the BNW php code with no modification. + % The file format is a header line containing the variable names + % followed by the data, with each case in a row. + % + % Output: There are many output files. + % 1) The main output file is ???continuous_input.txt that can be + % used by the structure learning code and parameter learning codes. + % The first line is variable names, the second line is the node type + % (continuous nodes should have 1, discrete nodes have the number + % of states), and the rest is the data. + % 2) A new output file is ???input_desc.txt, a file that describes the + % data so users can check that it has been parsed correctly. + % 3) ???nlevels.txt: The states of discrete variables. + % 4) ???name.txt: The names of the variables as uploaded. + % 5) ???type.txt: The number of states for each variables + % (1 indicates a continuous variable.) + % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases + % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and + % ???parent.txt: Files with default values for structure learning. + % + +% open file for input, include error handling +dfile=strcat(pre,'continuous_input_orig.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Get the number of cases (the number of rows in the file excluding the header) +ncases = fskipl(fin,Inf) - 1; + +frewind(fin); + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data +data = cell(ncases,nnodes); +for i = 1:ncases + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{i,j} = next; + end +end + +% Determine whether or not the nodes are continuous or discrete. +% First, treat them as all discrete and get the states and number of stats(levels). +levels = cell(1,nnodes); +states = []; +for j = 1:nnodes + states{end+1} = unique(data(:,j)); + levels{j} = size(states{j},1); +end + +reason = cell(1,nnodes); +%Now do some checks to see if nodes are discrete or continuous +for j = 1:nnodes + % If there are 3 or less unique values, I will assume that the node is discrete. + if levels{j} < 4; + reason{j} = "It was determined to be discrete because there are a small number (<4) of possible values."; + continue + % If there are as many unique values as a third of the number of cases, + % I will assume that the node is continuous. + elseif levels{j} > ncases/3; + levels{j} = 1; + reason{j} = "It was determined to be continuous because there are a large number of possible values compared to the number of cases."; + continue + % If there are more than twenty unique values, + % I will assume that the node is continuous. + elseif levels{j} > 20; + levels{j} = 1; + reason{j} = "It was determined to be continuous because there are many (>20) possible values."; + continue + % Otherwise, I will scan through the individual values. + % If any of the values contain a '.', I will assume it is continuous. + else + reason{j} = "It was determined to be discrete by default."; + period_test = 0; + column = data(:,j); + k = 1; + while period_test == 0 + period_test = sum(cell2mat(strfind(column(k),"."))); + if period_test != 0; + reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period(.)."; + levels{j} = 1; + end + k++; + if k > ncases + break + end + end + end +end + +%I need to check if any discrete nodes are listed after continuous nodes. +%If so, I need to rearrange the columns. +max_disc = 0; +min_cont = nnodes + 1; +for i = 1:nnodes + if levels{i} > 1 + max_disc = i; + elseif min_cont == nnodes+1 + min_cont = i; + end +end +%If max_disc > min_cont, you need to rearrange the nodes +% to put the discrete nodes first. +if max_disc > min_cont + levels_old = levels; + labels_old = labels; + data_old = data; + states_old = states; + reason_old = reason; + new_order = {}; + for i=1:nnodes + if levels_old{i} > 1 + new_order{end+1} = i; + end + end + for i=1:nnodes + if levels_old{i} == 1 + new_order{end+1} = i; + end + end + labels = {}; + levels = {}; + states = {}; + reason = {}; + for i =1:nnodes + labels{i} = labels_old{new_order{i}}; + levels{i} = levels_old{new_order{i}}; + states{i} = states_old{new_order{i}}; + reason{i} = reason_old{new_order{i}}; + for j=1:ncases + data{j,i} = data_old{j,new_order{i}}; + end + end + +endif + + +%Write other files that are used by BNW for this key. +%The first group of files establish default settings for structure learning. +outfile = strcat(pre,'white.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'ban.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'k.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'1\n'); +fclose(fout); + +outfile = strcat(pre,'parent.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'4\n'); +fclose(fout); + +outfile = strcat(pre,'thr.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'0.5\n'); +fclose(fout); + + +%The next group of files have information about the uploaded file. +outfile = strcat(pre,'name.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fclose(fout); + +outfile = strcat(pre,'nnode.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',nnodes); +fclose(fout); + +outfile = strcat(pre,'nrows.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',ncases); +fclose(fout); + +outfile = strcat(pre,'type.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +fclose(fout); + +%This output file contains the states for discrete nodes. +% The unique matlab function already sorts the states. +outfile = strcat(pre,'nlevels.txt'); +fout = fopen(outfile,'w'); +for i = 1:nnodes + if levels{i} > 1 + fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); + fprintf(fout,'%s\n',states{i}{end}); + end +end +fclose(fout); + + +%Print a file with a short description of the input. +descfile = strcat(pre,'input_desc.txt'); +dout = fopen(descfile,'w'); +fprintf(dout,['As loaded, the input file had the following properties:\n\n']); +dout = fopen(descfile,'a'); +fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); +fprintf(dout,'The variable names are:\n'); +fprintf(dout,'%s\t',labels{1:end-1}); +fprintf(dout,'%s\n\n',labels{end}); +for i=1:nnodes + if levels{i} == 1 + fprintf(dout,'%s is a continuous variable.\n',labels{i}); + fprintf(dout,'%s\n',reason{i}); + column = str2double(data(:,i)); + colmean = mean(column); + colstd = std(column); + fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) + else + fprintf(dout,'%s is a discrete variable with %i states.\n',labels{i},levels{i}); + fprintf(dout,'%s\n',reason{i}); + fprintf(dout,'The states are: '); + fprintf(dout,'%s ',states{i}{1:end-1}); + fprintf(dout,'%s\n\n',states{i}{end}); + end +end +fclose(fout); + +outfile = strcat(pre,'continuous_input.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +%Need to replace states in discrete variables with integers for BNT +for i = 1:nnodes + if levels{i} > 1 + for j = 1:ncases + for k=1:size(states{i},1) + if data{j,i} == states{i}{k} + data{j,i} = sprintf('%i',num2cell(k){1});; + break + end + end + end + end +end +for i = 1:ncases + fprintf(fout,'%s\t',data{i,1:end-1}); + fprintf(fout,'%s\n',data{i,end}); +end +fclose(fout); + + + + + +end +% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m~ b/sourcecodes/parameter_learning/code_backup/prepareInput.m~ new file mode 100644 index 00000000..9fc0f97f --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/prepareInput.m~ @@ -0,0 +1,294 @@ +function [ ] = prepareInput( pre ) + % + % This function takes files that are uploaded to BNW and creates output + % files that can be used for structure and parameter learning. + % It replaces php code that was previously in bn_file_load_gom.php. + % There are several improvements in performance and ease of use: + % 1) Loading files is significantly (~5x) faster for large input files. + % 2) The allowed values for discrete variables are more flexible. + % (e.g., A genotype variable be 'B' and 'D' instead of having + % to replace to make them '1' and '2'.) + % 3) Continuous variables may be identified as continuous in some cases + % even if there is not a period. + % 4) The states of discrete variables should be correctly ordered in + % almost all cases. + % 5) An additional output file is written that will let users check if + % the input file has been uploaded and parsed correctly. + % 6) Future updates to this code should be easier than updating the php. + % + % + % Input: ???continuous_input_orig.txt + % This is the input file that is uploaded to BNW. + % It is directly written out by the BNW php code with no modification. + % The file format is a header line containing the variable names + % followed by the data, with each case in a row. + % + % Output: There are many output files. + % 1) The main output file is ???continuous_input.txt that can be + % used by the structure learning code and parameter learning codes. + % The first line is variable names, the second line is the node type + % (continuous nodes should have 1, discrete nodes have the number + % of states), and the rest is the data. + % 2) A new output file is ???input_desc.txt, a file that describes the + % data so users can check that it has been parsed correctly. + % 3) ???nlevels.txt: The states of discrete variables. + % 4) ???name.txt: The names of the variables as uploaded. + % 5) ???type.txt: The number of states for each variables + % (1 indicates a continuous variable.) + % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases + % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and + % ???parent.txt: Files with default values for structure learning. + % + +% open file for input, include error handling +dfile=strcat(pre,'continuous_input_orig.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Get the number of cases (the number of rows in the file excluding the header) +ncases = fskipl(fin,Inf) - 1; + +frewind(fin); + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data +data = cell(ncases,nnodes); +for i = 1:ncases + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{i,j} = next; + end +end + +% Determine whether or not the nodes are continuous or discrete. +% First, treat them as all discrete and get the states and number of stats(levels). +levels = cell(1,nnodes); +states = []; +for j = 1:nnodes + states{end+1} = unique(data(:,j)); + levels{j} = size(states{j},1); +end + +reason = cell(1,nnodes); +%Now do some checks to see if nodes are discrete or continuous +for j = 1:nnodes + % If there are 3 or less unique values, I will assume that the node is discrete. + if levels{j} < 4; + reason{j} = "This was determined to be discrete because there are few (<4) different values."; + continue + % If there are as many unique values as a third of the number of cases, + % I will assume that the node is continuous. + elseif levels{j} > ncases/3; + levels{j} = 1; + reason{j} = "This was determined to be continuous because there are a large number of different values compared to the number of cases."; + continue + % If there are more than twenty unique values, + % I will assume that the node is continuous. + elseif levels{j} > 20; + levels{j} = 1; + reason{j} = "This was determined to be continuous because there are many (>20) possible values."; + continue + % Otherwise, I will scan through the individual values. + % If any of the values contain a '.', I will assume it is continuous. + else + reason{j} = "This variable was determined to be discrete."; + period_test = 0; + column = data(:,j); + k = 1; + while period_test == 0 + period_test = sum(cell2mat(strfind(column(k),"."))); + if period_test != 0; + reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period (".")."; + levels{j} = 1; + end + k++; + if k > ncases + break + end + end + end +end + +%I need to check if any discrete nodes are listed after continuous nodes. +%If so, I need to rearrange the columns. +max_disc = 0; +min_cont = nnodes + 1; +for i = 1:nnodes + if levels{i} > 1 + max_disc = i; + elseif min_cont == nnodes+1 + min_cont = i; + end +end +%If max_disc > min_cont, you need to rearrange the nodes +% to put the discrete nodes first. +if max_disc > min_cont + levels_old = levels; + labels_old = labels; + data_old = data; + states_old = states; + reason_old = reason; + new_order = {}; + for i=1:nnodes + if levels_old{i} > 1 + new_order{end+1} = i; + end + end + for i=1:nnodes + if levels_old{i} == 1 + new_order{end+1} = i; + end + end + labels = {}; + levels = {}; + states = {}; + reason = {}; + for i =1:nnodes + labels{i} = labels_old{new_order{i}}; + levels{i} = levels_old{new_order{i}}; + states{i} = states_old{new_order{i}}; + reason{i} = reason_old{new_order{i}}; + for j=1:ncases + data{j,i} = data_old{j,new_order{i}}; + end + end + +endif + + +%Write other files that are used by BNW for this key. +%The first group of files establish default settings for structure learning. +outfile = strcat(pre,'white.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'ban.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'k.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'1\n'); +fclose(fout); + +outfile = strcat(pre,'parent.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'4\n'); +fclose(fout); + +outfile = strcat(pre,'thr.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'0.5\n'); +fclose(fout); + + +%The next group of files have information about the uploaded file. +outfile = strcat(pre,'name.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fclose(fout); + +outfile = strcat(pre,'nnode.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',nnodes); +fclose(fout); + +outfile = strcat(pre,'nrows.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',ncases); +fclose(fout); + +outfile = strcat(pre,'type.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +fclose(fout); + +%This output file contains the states for discrete nodes. +% The unique matlab function already sorts the states. +outfile = strcat(pre,'nlevels.txt'); +fout = fopen(outfile,'w'); +for i = 1:nnodes + if levels{i} > 1 + fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); + fprintf(fout,'%s\n',states{i}{end}); + end +end +fclose(fout); + + +%Print a file with a short description of the input. +descfile = strcat(pre,'input_desc.txt'); +dout = fopen(descfile,'w'); +fprintf(dout,['As loaded, the input file had the following properties:\n\n']); +dout = fopen(descfile,'a'); +fprintf(dout,'There are %i variables and %i cases(rows)\n',size(labels,2),ncases); +fprintf(dout,'The variable names are:\n'); +fprintf(dout,'%s\t',labels{1:end-1}); +fprintf(dout,'%s\n\n',labels{end}); +for i=1:nnodes + if levels{i} == 1 + fprintf(dout,'%s is a continuous variable\n',labels{i}); + fprintf(dout,'%s\n',reason{i}); + column = str2double(data(:,i)); + colmean = mean(column); + colstd = std(column); + fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) + else + fprintf(dout,'%s is a discrete variable with %i states\n',labels{i},levels{i}); + fprintf(dout,'%s\n',reason{i}); + fprintf(dout,'The states are: '); + fprintf(dout,'%s ',states{i}{1:end-1}); + fprintf(dout,'%s\n\n',states{i}{end}); + end +end +fclose(fout); + +outfile = strcat(pre,'continuous_input.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +%Need to replace states in discrete variables with integers for BNT +for i = 1:nnodes + if levels{i} > 1 + for j = 1:ncases + for k=1:size(states{i},1) + if data{j,i} == states{i}{k} + data{j,i} = sprintf('%i',num2cell(k){1});; + break + end + end + end + end +end +for i = 1:ncases + fprintf(fout,'%s\t',data{i,1:end-1}); + fprintf(fout,'%s\n',data{i,end}); +end +fclose(fout); + + + + + +end +% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInput.m b/sourcecodes/parameter_learning/code_backup/readInput.m new file mode 100644 index 00000000..891d7f36 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInput.m @@ -0,0 +1,63 @@ +function [ labels, cases, bnet, node_sizes, data,labelsold] = readInput( dfile, sfile, nnodes, std_flag ) + %readInput is to be used when reading in a network with a known structure + % + %Input: + % dfile = name of the file containing the data (required) + % sfile = name of the file containing the structure (required) + % nnodes = number of nodes in the network (required) + % std_flag = flag for whether or not to standardize the data. + % (optional-- Default is FALSE) + % + % See readInputData.m and readInputStructure.m for description of the + % format of the dfile and sfile, respectively. + % + %Output: + % labels = cell array with the names of the nodes. + % cases = cell array with the data. + % bnet = BNT bayesian network with the input structure. + +if nargin < 4 + std_flag = false(1); +end + + +% read in the file with the data +[labelsold,node_sizes,cases, data] = readInputData(dfile,nnodes); + + +% read in the file with the structure +[dag] = readInputStructure(sfile,labelsold); + + +% check the ordering of the nodes and reorder if necessary +[labels,cases,dag,node_sizes,ord_flag] = checkStructure(labelsold,cases,dag,node_sizes); + +dcount = 0; +for i = 1:nnodes + if node_sizes(i) ~= 1 + dcount = dcount + 1; + end +end +discrete = zeros(1,dcount); +dcount = 0; +for i = 1:nnodes + if node_sizes(i) ~= 1 + dcount = dcount + 1; + discrete(dcount) = i; + end +end + +bnet = mk_bnet(dag,node_sizes,'discrete',discrete,'names',labels); + +%bnet.dag + +checkDiscreteNodes(bnet,cases); + +% standardize continuous data to have a mean = 0 and std = 1 +if (std_flag) + [cases] = standardizeData(labels,node_sizes,cases); +end + + +end +% end of readInput.m diff --git a/sourcecodes/parameter_learning/code_backup/readInputData.m b/sourcecodes/parameter_learning/code_backup/readInputData.m new file mode 100644 index 00000000..706e2751 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInputData.m @@ -0,0 +1,75 @@ +function [ labels , node_sizes, cases, data] = readInputData( dfile , nnodes ) + % readColData reads data from a file containing data in columns + % that have text titles, and possibly other header text + % + % Input: + % dfile = name of the file containing the data.(required) + % nnodes = number of columns in the data file. (required) + % + % Function assumes the following format for the input file: + % 1) First line has labels for each of the nodes. There cannot + % be spaces in any node label. + % 2) The next line is the "node_sizes" of the nodes. If the + % nodes are discrete, this number will be equal to the number + % of states. If the nodes are continuous, they should be + % equal to 1. The function assumes that any nodes with + % node_size = 1 is continuous. + % 3) The rest of the file is numeric data. The data in the input + % data has the number of columns equal to the number of + % nodes in the network and the number of rows equal to + % the number of samples. + % + % + % Output: + % labels = cell array with node (column) labels. + % node_sizes = vector with the size of each node + % cases = cell array with the data. The cases array is transposed + % in comparison with the input data to agree with the format of + % cell data used in BNT. + +% open file for input, include error handling +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Read in first line to get the node labels. +labels = cell(1,nnodes); +buffer = fgetl(fin); %get header line as a string +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data. Use the vetorized fscanf function to load all +% numerical values into one vector. Then reshape this vector into a +% matrix. + +data = fscanf(fin,'%f'); % Load the numerical values into one long vector + + + + +nd = length(data); % total number of data points +nr = nd/nnodes; % number of rows; check (next statement) to make sure +if nr ~= round(nd/nnodes) + fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); + fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); + error('data is not rectangular') +end + +data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array + + +node_sizes = zeros(1,nnodes); +for j = 1:nnodes + node_sizes(j) = data(1,j); +end + +nr = nr - 1; +data(1,:) = []; +cases = cell(nnodes,nr); +cases(:,:) = num2cell(data'); + +end +% end of readInputData.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInputStructure.m b/sourcecodes/parameter_learning/code_backup/readInputStructure.m new file mode 100644 index 00000000..6b3cbece --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInputStructure.m @@ -0,0 +1,72 @@ +function [ dag ] = readInputStructure( sfile, labels ) +%readInputStructure Read in file with structure information + % + %Input: + % sfile = name of the file containing the data (required) + % labels = cell array with node labels. (required) + % nnodes = number of columns in the data file. (required) + % + % Function assumes the following format for the structure input file: + % 1) The first line has node labels. These must be the same as + % in the input data file. They cannot contain spaces. + % 2) The remainder of the file contains the structure of the dag. + % The structure of a graph is a N-by-N matrix, where N is the + % number of nodes. There are 1's in the matrix representing + % parent-child relationships. For each 1, the row indicates + % the parent and the column indicates the child. For + % example, a 1 in the (2,3) position of the matrix indicates + % that there is an arc pointing from node 2 to node 3. + % + % + % Output: + % dag = matrix with the structure. +% +% Read in first line of the structure file +% open file for input, include error handling +fin = fopen(sfile,'r'); +if fin < 0 + error(['Could not open ',sfile,' for input']); +end + +nnodes = size(labels,2); +% Read in first line to get the node labels. +labels_test = cell(1,nnodes); +buffer = fgetl(fin); %get header line as a string +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels_test{j} = next; +end + +for j=1:nnodes + if labels_test{j} ~= labels{j} + fprintf(['Label of node ',j,' is not consistent in input and structure files']) + end +end + +data = fscanf(fin,'%f'); + +nd = length(data); % total number of data points +nr = nd/nnodes; % number of rows; check (next statement) to make sure +if nr ~= round(nd/nnodes) + fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); + fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); + error('Structure file does not have the correct dimensions (1)') +end +% check to make sure that structure is square +if nr ~= nnodes + error('Structure file does not have the correct dimensions (2)') +end + +data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array + + +dag = zeros(nnodes,nnodes); +for i = 1:size(data,1) + for j = 1:size(data,2) + dag(i,j) = data(i,j); + end +end + + +end +% end of readInputStructure.m diff --git a/sourcecodes/parameter_learning/code_backup/runBN_initial.m b/sourcecodes/parameter_learning/code_backup/runBN_initial.m new file mode 100644 index 00000000..0deff1b5 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/runBN_initial.m @@ -0,0 +1,57 @@ +function runBN_initial(pre) +sfile=strcat(pre,'structure_input.txt'); +dfile=strcat(pre,'continuous_input.txt'); + +nnodefile=strcat(pre,'nnode.txt'); +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + + +mapfilename=strcat(pre,'mapdata.txt'); +mapvalfilename=strcat(pre,'map.txt'); + +mapfile = fopen(mapfilename,'w'); + +mapval = fopen(mapvalfilename,'w'); + + +Std_flag=true; +[labels,cases,bnet,node_sizes,data,labelsold]=readInput(dfile,sfile,nnodes,Std_flag); +s=std(data,0,1); +m=mean(data); + +for i=1:nnodes + fprintf(mapval,'%s\t%f\t%f\n',labelsold{i},s(i),m(i)); +end + +fprintf(mapfile,'%s',labels{1}); +for i=2:nnodes + fprintf(mapfile,'\t%s',labels{i}); +end +fprintf(mapfile,'\n'); +fclose(mapval); +fclose(mapfile); + +%Need to rearrange the means and stdevs to match the new labeling. +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labels{i},labelsold{j}) + means{i} = m(j); + stdevs{i} = s(j); + break + end + end +end + + +[bnet]=parameterLearning(bnet,cases); + +filename=strcat(pre,'net_figure.txt'); + +drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means); + +writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m); + +end diff --git a/sourcecodes/parameter_learning/code_backup/standardizeData.m b/sourcecodes/parameter_learning/code_backup/standardizeData.m new file mode 100644 index 00000000..db5e04c7 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/standardizeData.m @@ -0,0 +1,25 @@ +function [ cases ] = standardizeData( labels, node_sizes, cases ) +%standardizeData standardizes continuous nodes so they have a mean = 0 +% and standard deviation = 1 + + +nnodes = size(labels,2); + +%fprintf(['Standardizing data for continuous nodes\n']) +for i = 1:nnodes + if node_sizes(i) == 1 + temp = cell2num(cases(i,:)); + [temp] = standardize(temp); + cases(i,:) = num2cell(temp); + end +end + +%write standardized data to file +%fprintf(['Standardized data is written to file standardized_data.txt\n']) +%fout = 'standardized_data.txt'; +%txt = sprintf([repmat('%s\t',1,size(labels,2))],labels{:}); +%dlmwrite(fout,txt,''); +%dlmwrite(fout,cell2num(cases'),'-append','delimiter','\t'); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters.m b/sourcecodes/parameter_learning/code_backup/writeParameters.m new file mode 100644 index 00000000..0790a8e2 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters.m @@ -0,0 +1,106 @@ +function [] = writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m) +%Writes a file that contains the parameters of the network with no evidence. + + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; + buffer = fgetl(flevels); + for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open output file. +filename = strcat(pre,'parameters.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + predict = marginal_nodes(engine,nodeid); + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous node\n'; + fprintf(fileID,line); + %%% 'i' in the line below is correct: m and s are had original node labeling + adj_mu = predict.mu*s(i)+m(i); + adj_sigma = s(i)*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Discrete node with %i states\n'; + fprintf(fileID,line,bnet.node_sizes(nodeid)); + %line = 'Probability of each state\n'; + %fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m b/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m new file mode 100644 index 00000000..fc24e2e5 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m @@ -0,0 +1,151 @@ +function [] = writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) +%Writes a file that contains the parameters of the network after entering evidence. + +%Read in original node labels to get node IDs. +infile = strcat(pre,'continuous_input.txt'); +fin = fopen(infile,'r'); +labelsold = cell(1,nnodes); +buffer = fgetl(fin); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + labelsold{j} = next; +end +fclose(fin); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; +buffer = fgetl(flevels); +for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardize evidence for continuous nodes. + if bnet.node_sizes(di) == 1, + ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; + end + evidence{di} = ev_dat(di); +end + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open output file. +filename = strcat(pre,'parameters_ev.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + predict = marginal_nodes(engine,nodeid); + if isempty(evidence{nodeid}) + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous parameters considering evidence:\n'; + fprintf(fileID,line); + %line = 'Mean and standard deviation of Gaussian distribution\n'; + %fprintf(fileID,line); + adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; + adj_sigma = stdevs{nodeid}*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Probability of states considering evidence:\n'; + fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + end + else + if bnet.node_sizes(nodeid) == 1; + line = 'Evidence was observed for this node. The observed value was:\n'; + fprintf(fileID,line); + adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; + fprintf(fileID,'%6.4f\n\n',adj_mu); + else + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + line = 'Evidence was observed for this node. The observed state was:\n'; + fprintf(fileID,line); + state_ev = uint16(ev_dat(nodeid)); +% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); + fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); + end + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_int.m b/sourcecodes/parameter_learning/code_backup/writeParameters_int.m new file mode 100644 index 00000000..ed92d593 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters_int.m @@ -0,0 +1,186 @@ +function [] = writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) +%Writes a file that contains the parameters of the network after intervention. + + +%First read input file to get node labels to get node IDs. +infile = strcat(pre,'continuous_input.txt'); +fin = fopen(infile,'r'); +labelsold = cell(1,nnodes); +buffer = fgetl(fin); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + labelsold{j} = next; +end + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; +buffer = fgetl(flevels); +for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardize evidence for continuous nodes. + if bnet.node_sizes(di) == 1, + ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; + end + evidence{di} = ev_dat(di); +end + +[engine,loglik]=enter_evidence(engine,evidence); + +%Get list of nodes that are children, grandchildren, etc. of intervened nodes +%int_nodes contains the list of these children nodes +int_nodes = zeros(1,nnodes); +%new_nodes is just a temporary array to know when to keep looking +new_nodes = zeros(1,nnodes); +for i = 1:nnodes + if !isempty(evidence{i}); + new_nodes(i) = 1; + int_nodes(i) = 1; + end +end +while sum(new_nodes) != 0 + new_nodes_old = new_nodes; + new_nodes = zeros(1,nnodes); + for i = 1:nnodes + if new_nodes_old(i) == 1 + for j = 1:nnodes + if int_nodes(j) == 0 + if bnet.dag(i,j) == 1, + new_nodes(j) = 1; + end + end + end + end + end + for i = 1:nnodes + if new_nodes(i) == 1; + int_nodes(i) = 1; + end + end +end + + +%Open output file. +filename = strcat(pre,'parameters_ev.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + %check to see if this is a node impacted by intervention + if int_nodes(nodeid) == 1 + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + predict = marginal_nodes(engine,nodeid); + if isempty(evidence{nodeid}) + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous parameters considering intervention:\n'; + fprintf(fileID,line); + %line = 'Mean and standard deviation of Gaussian distribution\n'; + %fprintf(fileID,line); + adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; + adj_sigma = stdevs{nodeid}*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Probability of states considering intervention:\n'; + fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + end + else + if bnet.node_sizes(nodeid) == 1; + line = 'Intervention on this node assigned the following value:\n'; + fprintf(fileID,line); + adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; + fprintf(fileID,'%6.4f\n\n',adj_mu); + else + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + line = 'Intervention on this node assigned the following state:\n'; + fprintf(fileID,line); + state_ev = uint16(ev_dat(nodeid)); +% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); + fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); + end + end + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/createJSON.m b/sourcecodes/parameter_learning/createJSON.m new file mode 100644 index 00000000..65758356 --- /dev/null +++ b/sourcecodes/parameter_learning/createJSON.m @@ -0,0 +1,130 @@ +function [ ] = createJSON( pre ) + % This function will create a JSON file for network visualization. + % + % Input: + % 1) prestructure_input.txt + % Structure file. + % 2) prestructure_input_temp.txt + % Structure file with model averaging scores. + % + % Output: + % prenetwork.json-- json file. + % + + +% open file for input, include error handling +dfile=strcat(pre,'structure_input.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Read in first line to get the number of nodes and the node labels. +buffer = strtrim(fgetl(fin)); %get header line as a string +nnodes = numel(strfind(buffer,"\t"))+1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the edges +edges = cell(nnodes,nnodes); +for i = 1:nnodes + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + edges{i,j} = next; + end +end + + +% open file to read in model averaging scores +dfile2=strcat(pre,'structure_input_temp.txt'); + +fin2 = fopen(dfile2,'r'); +if fin2 < 0 + error(['Could not open ',dfile2,' for input']); +end + +buffer = fgetl(fin2); %get header line as a string + +% Read in the scores +scores = cell(nnodes,nnodes); +for i = 1:nnodes + buffer = fgetl(fin2); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + scores{i,j} = next; + end +end + + + + +nedges = 0; +sources = []; +targets = []; +scores1 = []; +for i = 1:nnodes + for j = 1:nnodes + if edges{i,j} == "1" + nedges = nedges + 1; + sources = [sources; i]; + targets = [targets; j]; + scores1 = [scores1; str2num(scores{i,j})]; + end + end +end + + +outfile = strcat(pre,'network.json'); +fout = fopen(outfile,'w'); +fprintf(fout,"{\n"); +fprintf(fout," \"nodes\": [\n"); +for i = 1:(nnodes-1) + fprintf(fout," {\n"); + fprintf(fout," \"data\": {\n"); + fprintf(fout," \"id\": \"%i\",\n",i); + fprintf(fout," \"label\": \"%s\"\n",labels{i}); + fprintf(fout," }\n"); + fprintf(fout," },\n"); +end +fprintf(fout," {\n"); +fprintf(fout," \"data\": {\n"); +fprintf(fout," \"id\": \"%i\",\n",nnodes); +fprintf(fout," \"label\": \"%s\"\n",labels{nnodes}); +fprintf(fout," }\n"); +fprintf(fout," }\n"); +fprintf(fout," ],\n"); +fprintf(fout," \"edges\": [\n"); +for i = 1:(nedges-1) + fprintf(fout," {\n"); + fprintf(fout," \"data\": {\n"); + fprintf(fout," \"id\": \"%i%i\",\n",sources(i),targets(i)); + fprintf(fout," \"source\": \"%i\",\n",sources(i)); + fprintf(fout," \"target\": \"%i\",\n",targets(i)); + fprintf(fout," \"weight\": %3.2f\n",scores1(i)); + fprintf(fout," }\n"); + fprintf(fout," },\n"); +end +fprintf(fout," {\n"); +fprintf(fout," \"data\": {\n"); +fprintf(fout," \"id\": \"%i%i\",\n",sources(nedges),targets(nedges)); +fprintf(fout," \"source\": \"%i\",\n",sources(nedges)); +fprintf(fout," \"target\": \"%i\",\n",targets(nedges)); +fprintf(fout," \"weight\": %3.2f\n",scores1(nedges)); +fprintf(fout," }\n"); +fprintf(fout," }\n"); +fprintf(fout," ]\n"); +fprintf(fout,"}"); +%fprintf(fout,'%s\t',labels{1:end-1}); +%fprintf(fout,'%s\n',labels{end}); +%for i = 1:nnodes +% fprintf(fout,'%s\t',edges{i,1:end-1}); +% fprintf(fout,'%s\n',edges{i,end}); +%end +fclose(fout); + +end diff --git a/sourcecodes/parameter_learning/createSVG.m b/sourcecodes/parameter_learning/createSVG.m new file mode 100644 index 00000000..f7cc9b64 --- /dev/null +++ b/sourcecodes/parameter_learning/createSVG.m @@ -0,0 +1,137 @@ +function [ ] = createSVG( pre ) + % This function will create a JSON file for network visualization. + % + % Input: + % 1) prestructure_input.txt + % Structure file. + % 2) prestructure_input_temp.txt + % Structure file with model averaging scores. + % 3) pregrviz_name_file.txt + % Node names in graph viz file. + % 4) pregraphviz.txt + % Original graphviz input file. + % + % Output: + % pregraphviz_svg.txt-- Modified graphviz input file. + % + + +% open file for input, include error handling +dfile=strcat(pre,'structure_input.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + + +% Read in the edges +edges = cell(nnodes,nnodes); +for i = 1:nnodes + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + edges{i,j} = next; + end +end + + +% open file to read in model averaging scores +dfile2=strcat(pre,'structure_input_temp.txt'); + +if (exist(dfile2) == 0) + scores = cell(nnodes,nnodes); + for i = 1:nnodes + for j = 1:nnodes + scores{i,j} = edges{i,j}; + end + end +else + fin2 = fopen(dfile2,'r'); + if fin2 < 0 + error(['Could not open ',dfile2,' for input']); + end + + buffer = fgetl(fin2); %get header line as a string + +% Read in the scores + scores = cell(nnodes,nnodes); + for i = 1:nnodes + buffer = fgetl(fin2); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + scores{i,j} = next; + end + end +end + +% open file to get positions of variables +dfile3=strcat(pre,'grviz_name_file.txt'); + +fin3 = fopen(dfile3,'r'); +if fin3 < 0 + error(['Could not open ',dfile3,' for input']); +end + +grphviz_names = cell(nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + grphviz_names{j} = next; +end + + +nedges = 0; +sources = []; +targets = []; +scores1 = []; +for i = 1:nnodes + for j = 1:nnodes + if edges{i,j} == "1" + nedges = nedges + 1; + sources = [sources; i]; + targets = [targets; j]; + scores1 = [scores1; str2num(scores{i,j})]; + end + end +end + + +outfile = strcat(pre,'graphviz_svg.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,"digraph G {\n"); +%fprintf(fout,"size=\"10,10\"; ratio = fill;\n"); +fprintf(fout,"size=\"10,10\"; remincross = true;\n"); +fprintf(fout,"node [shape=rectangle, width=1.0, fontsize=22];\n"); +fprintf(fout,"edge [fontsize=16];\n"); +for i = 1:nedges + fprintf(fout,"\"%s\" -> \"%s\" [ label=\"%3.2f\", penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},scores1(i),2*scores1(i)); +end +fprintf(fout,"}/n"); +fclose(fout); + + +outfile2 = strcat(pre,'graphviz_svg_no_edge.txt'); +fout2 = fopen(outfile2,'w'); +fprintf(fout2,"digraph G {\n"); +%fprintf(fout,"size=\"10,10\"; ratio = fill;\n"); +fprintf(fout2,"size=\"10,10\"; remincross = true;\n"); +fprintf(fout2,"node [shape=rectangle, width=1.0, fontsize=22];\n"); +fprintf(fout2,"edge [fontsize=16];\n"); +for i = 1:nedges + fprintf(fout,"\"%s\" -> \"%s\" [ penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},2*scores1(i)); +end +fprintf(fout2,"}/n"); +fclose(fout2); + + + +end diff --git a/sourcecodes/run_scripts/#run_octave# b/sourcecodes/run_scripts/#run_octave# new file mode 100644 index 00000000..2beea5d8 --- /dev/null +++ b/sourcecodes/run_scripts/#run_octave# @@ -0,0 +1,15 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +runBN_initial(arg_list{1}); +createSVG(arg_list{1}); +createJSON(arg_list{1}); +output = strcat(arg_list{1},"network.svg"); +command = cstrcat("/usr/bin/dot -Tsvg -o",output," ",arg_list{1},"graphviz_svg.txt > ",output); +system(command); +output2 = strcat(arg_list{1},"network_no_edge.svg"); +command2 = cstrcat("/usr/bin/dot -Tsvg -o",output2," ",arg_list{1},"graphviz_svg_no_edge.txt > ",output2); +system(command2); diff --git a/sourcecodes/run_scripts/run_mod_edges.bk b/sourcecodes/run_scripts/run_mod_edges.bk new file mode 100644 index 00000000..30176e9c --- /dev/null +++ b/sourcecodes/run_scripts/run_mod_edges.bk @@ -0,0 +1,24 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +modifyEdges(arg_list{1},arg_list{2}); + +fname1=strcat(arg_list{1},"continuous_input.txt"); +fname2=strcat(arg_list{2},"continuous_input.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"continuous_input_orig.txt"); +fname2=strcat(arg_list{2},"continuous_input_orig.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"nnode.txt"); +fname2=strcat(arg_list{2},"nnode.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"name.txt"); +fname2=strcat(arg_list{2},"name.txt"); +copyfile(fname1,fname2); + diff --git a/sourcecodes/run_scripts/run_mod_edges~ b/sourcecodes/run_scripts/run_mod_edges~ new file mode 100644 index 00000000..1659c684 --- /dev/null +++ b/sourcecodes/run_scripts/run_mod_edges~ @@ -0,0 +1,45 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +modifyEdges(arg_list{1},arg_list{2}); + +fname1=strcat(arg_list{1},"continuous_input.txt"); +fname2=strcat(arg_list{2},"continuous_input.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"continuous_input_orig.txt"); +fname2=strcat(arg_list{2},"continuous_input_orig.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"nnode.txt"); +fname2=strcat(arg_list{2},"nnode.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"name.txt"); +fname2=strcat(arg_list{2},"name.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"type.txt"); +fname2=strcat(arg_list{2},"type.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"nlevels.txt"); +fname2=strcat(arg_list{2},"nlevels.txt"); +copyfile(fname1,fname2); + +fname1=strcat(arg_list{1},"grviz_name_file.txt"); +fname2=strcat(arg_list{2},"grviz_name_file.txt"); +copyfile(fname1,fname2); + +runBN_initial(arg_list{2}); +createSVG(arg_list{2}); +createJSON(arg_list{2}); +output = strcat(arg_list{2},"network.svg"); +command = cstrcat("/usr/bin/dot -Tsvg -o",output," ",arg_list{1},"graphviz_svg.txt > ",output); +system(command); +output2 = strcat(arg_list{2},"network_no_edge.svg"); +command2 = cstrcat("/usr/bin/dot -Tsvg -o",output2," ",arg_list{2},"graphviz_svg_no_edge.txt > ",output2); +system(command2); diff --git a/sourcecodes/run_scripts/run_octave_test~ b/sourcecodes/run_scripts/run_octave_test~ new file mode 100644 index 00000000..3879280e --- /dev/null +++ b/sourcecodes/run_scripts/run_octave_test~ @@ -0,0 +1,7 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +runBN_initial(arg_list{1}); diff --git a/sourcecodes/run_scripts/run_octave~ b/sourcecodes/run_scripts/run_octave~ new file mode 100644 index 00000000..5a83f277 --- /dev/null +++ b/sourcecodes/run_scripts/run_octave~ @@ -0,0 +1,14 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +runBN_initial(arg_list{1}); +createSVG(arg_list{1}); +output = strcat(arg_list{1},"network.svg"); +command = cstrcat("/usr/bin/dot -Tsvg -o",output," ",arg_list{1},"graphviz_svg.txt > ",output); +system(command); +output2 = strcat(arg_list{1},"network_no_edge.svg"); +command2 = cstrcat("/usr/bin/dot -Tsvg -o",output2," ",arg_list{1},"graphviz_svg_no_edge.txt > ",output2); +system(command2); diff --git a/sourcecodes/run_scripts/test b/sourcecodes/run_scripts/test new file mode 100644 index 00000000..fc47416b --- /dev/null +++ b/sourcecodes/run_scripts/test @@ -0,0 +1,7 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +createJSON(arg_list{1}); diff --git a/sourcecodes/run_scripts/test~ b/sourcecodes/run_scripts/test~ new file mode 100644 index 00000000..2beea5d8 --- /dev/null +++ b/sourcecodes/run_scripts/test~ @@ -0,0 +1,15 @@ +#!/usr/bin/octave -qf +cd ./data +arg_list = argv(); +addpath("../bnt-master"); +addpath(genpathKPM("../bnt-master")); +addpath("../parameter_learning"); +runBN_initial(arg_list{1}); +createSVG(arg_list{1}); +createJSON(arg_list{1}); +output = strcat(arg_list{1},"network.svg"); +command = cstrcat("/usr/bin/dot -Tsvg -o",output," ",arg_list{1},"graphviz_svg.txt > ",output); +system(command); +output2 = strcat(arg_list{1},"network_no_edge.svg"); +command2 = cstrcat("/usr/bin/dot -Tsvg -o",output2," ",arg_list{1},"graphviz_svg_no_edge.txt > ",output2); +system(command2); |
