<commit_before><?php

// writes skeleton to SWC

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

$tnid = isset( $_REQUEST[ 'tnid' ] ) ? intval( $_REQUEST[ 'tnid' ] ) : 0;

if ( $pid )
{
  if ( $uid )
  {
    $ele_id = $db->getRelationId( $pid, 'element_of' );
    if(!$ele_id) { echo makeJSON( array( '"error"' => 'Can not find "element_of" relation for this project' ) ); return; }
    
    // retrieve skeleton for treenode
    $res = $db->getClassInstanceForTreenode( $pid, $tnid, "element_of");
    
    if(!empty($res)) { $skelid = $res[0]['class_instance_id']; } 
    else {
      echo makeJSON( array( '"error"' => 'There seems not to exist a skeleton for treenode id '));
      return;
    }
    // SWC columns are
    // unique identity value for trace point, structure type, x coordinate, y coordinate,
    // z coordinate, radius, identity value for parent
    
    $res = $db->getResult(
    'SELECT "treenode"."id", ("treenode"."location")."x", ("treenode"."location")."y", 
    ("treenode"."location")."z",
    "treenode"."confidence", "treenode"."parent_id" FROM "treenode_class_instance" AS "tci", "treenode"
    WHERE "tci"."project_id" = '.$pid.' AND
    "tci"."relation_id" = '.$ele_id.' AND
    "tci"."class_instance_id" = '.$skelid.' AND
    "treenode"."id" = "tci"."treenode_id"
    ORDER BY "treenode"."parent_id" DESC');
    
	foreach($res as $key => $ele) {
		$out = "";
		$out .= $ele['id']." ";
		$out .= $ele['x']." ";
		$out .= $ele['y']." ";
		$out .= $ele['z']." ";
		$out .= $ele['confidence']." ";
		if($ele['parent_id']=="")
			$out .= "-1\n";
		else
			$out .= $ele['parent_id']."\n";
		
		echo $out;
	}
    //header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    //header('Content-Type: application/csv');
    //header('Content-Disposition: attachment; filename=test.csv');
    //header("Content-Length: " . strlen($out));
    //echo(var_dump($res));
    //echo date("l");
    //echo "\r\n";
    // XXX retrieve treenodes with parent and location information
    //exit;
    //echo json_encode($res);
    
  }
  else
    echo makeJSON( array( 'error' => 'You are not logged in currently.  Please log in to be able to retrieve treenodes.' ) );
}
else
  echo makeJSON( array( 'error' => 'Project closed. Can not retrieve synapses.' ) );

?><commit_msg>Fix the SWC export code<commit_after><?php

// writes skeleton to SWC

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

$tnid = isset( $_REQUEST[ 'tnid' ] ) ? intval( $_REQUEST[ 'tnid' ] ) : 0;

if ( $pid )
{
  if ( $uid )
  {
    $ele_id = $db->getRelationId( $pid, 'element_of' );
    if(!$ele_id) { echo makeJSON( array( '"error"' => 'Can not find "element_of" relation for this project' ) ); return; }
    
    // retrieve skeleton for treenode
    $res = $db->getClassInstanceForTreenode( $pid, $tnid, "element_of");
    
    if(!empty($res)) { $skelid = $res[0]['class_instance_id']; } 
    else {
      echo makeJSON( array( '"error"' => 'There seems not to exist a skeleton for treenode id '));
      return;
    }
    // SWC columns are
    // unique identity value for trace point, structure type, x coordinate, y coordinate,
    // z coordinate, radius, identity value for parent
    
    $res = $db->getResult(
    'SELECT "treenode"."id", ("treenode"."location")."x", ("treenode"."location")."y", 
    ("treenode"."location")."z",
    "treenode"."confidence", "treenode"."parent_id" FROM "treenode_class_instance" AS "tci", "treenode"
    WHERE "tci"."project_id" = '.$pid.' AND
    "tci"."relation_id" = '.$ele_id.' AND
    "tci"."class_instance_id" = '.$skelid.' AND
    "treenode"."id" = "tci"."treenode_id"
    ORDER BY "treenode"."parent_id" DESC');
    
	foreach($res as $key => $ele) {
		$out = "";
		$out .= $ele['id']." ";
		$out .= '0 '; // SWC type
		$out .= $ele['x']." ";
		$out .= $ele['y']." ";
		$out .= $ele['z']." ";
		$out .= "0 "; // radius
		if($ele['parent_id']=="")
			$out .= "-1\n";
		else
			$out .= $ele['parent_id']."\n";
		
		echo $out;
	}
    //header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    //header('Content-Type: application/csv');
    //header('Content-Disposition: attachment; filename=test.csv');
    //header("Content-Length: " . strlen($out));
    //echo(var_dump($res));
    //echo date("l");
    //echo "\r\n";
    // XXX retrieve treenodes with parent and location information
    //exit;
    //echo json_encode($res);
    
  }
  else
    echo makeJSON( array( 'error' => 'You are not logged in currently.  Please log in to be able to retrieve treenodes.' ) );
}
else
  echo makeJSON( array( 'error' => 'Project closed. Can not retrieve synapses.' ) );

?>
{'fraction_non_alphanumeric': 0.21181887950882577, 'fraction_numerical': 0.0030698388334612432, 'mean_word_length': 3.204032258064516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6795685', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1876, 'n_words': 453}