25/05/2011

pro-AD website: MRI analysis for prodromal Alzheimer’s assessment

Recently I participated in the pro-AD’s website development.


pro-AD is a website created at DIFI, the University of Genoa’s Department of Physics, to help medics automagically analyse MRI images for the prodromal Alzheimer's disease assessment.

Registered medics can easily submit DICOM or NII files through the website to their private local folder via the java applet JUploader or the PHP uploader which is shown automatically when no java plugin is detected on the user's browser. They can then modify the information associated with those images: age, gender and an optional unique ID - no personal data about the patients is stored anywhere anytime during the process.


Easy access for the medic's personal profile is provided through a dedicated page; if a valid e-mail address is provided, it is possible to enable e-mail notifications about processing results.


When one or more files are selected for processing, the server enqueues them in a multithreaded pipeline and starts the analysis. At the end, a single number which describes the patient's likelihood of being affected in the near future by the disease is returned for each file sent to processing. If the file processed was of really bad quality or was not a valid file, the process fails and an error is shown instead of the result.


All results are stored for easy future access and can be viewed and exported in XLS format from a dedicated page. If a file is sent to processing more times, only the latest result is stored.

[PHP] Create XLS document

If you need to create XLS documents via PHP, you may need these functions:

//XLS Begin of file
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
//XLS End of file
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
//Writes a number in a cell
function xlsWriteNumber($Row, $Col, $Value) {
if($Value == null){
xlsWriteLabel($Row, $Col, $Value);
}
else{
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
}
return;
}
//Writes a string in a cell
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

//Creates headers to download the file
function xlsWriteHeader(){
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=EXPORTNAME.xls ");
header("Content-Transfer-Encoding: binary ");
}

[PHP] Send mail with attachment using PEAR

If you use PEAR and want to send mails with attachments via PHP, you may find this helpful.

First, ensure you have installed PEAR’s Mail and Mail_mime packages. Then, you just need a function like this:

 

function send_mail($mail, $subject, $bodytxt, $bodyhtml, $attachment){
    require_once('Mail.php');
    require_once('Mail/mime.php');
    //mail parameters, this is the basic one
    $params = array("host"=>"YOUR_HOST");
    //creates smtp mail object
    $mail_sender = Mail::factory("smtp", $params);
    //creates mail headers
    $headers = array("From"=>"YOUR_ADDRESS", "To"=>$mail, "Subject"=>$subject);
    //creates attachment and body fields
    $crlf = "\n";
    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($bodytxt);
    $mime->setHTMLBody($bodyhtml);
    $mime->addAttachment($attachment, "ATTACHMENT_TYPE");
    //never change these lines order
    $body = $mime->get();
    $headers = $mime->headers($headers);
    $error=$mail_sender->send($mail, $headers, $body);
    return $error;
}

 

where:

  • YOUR_HOST is your mail host, something like smtp.gmail.com
  • $mail is the recipient’s mail address
  • $bodytxt is the mail’s body in TXT format, do not use HTML markup here!
  • ATTACHMENT_TYPE is the attachment’s MIME type as per IANA’s specifications, something like image/jpeg
  • if you change the order of the last lines, the attach operation will not work

13/05/2011

[PHP, XML] Twitter friends graph

Brief example on how to create and draw some Twitter user’s friends graph.

Piccolo esempio di come creare e disegnare il grafo dei friends di un dato utente Twitter.

 

English:

With friends Twitter means “people whom the user follows”. Starting from a chosen user, we grab all his friends until a depth level of 3 and we create a XML file with a proper structure to be opened by Gephi in order to graphically visualize the result. The number of users we get is far lower than the real one due to Twitter API’s limitations.

To create the gexf file for Gephi, just run the HowDoYouGraph script after editing the $username variable with the desired username. After a little coffee break, in the same folder as the script you should find a file named grafo.gexf.

 

Show an example graph for user VivoMikiX and download the gexf source.

 

Stats about the undirected example graph:

  • Total nodes: 5013
  • Total edges: 5539
  • Medium degree = 2.21
  • Diameter = 6
  • Density = 0
  • Modularity = 0.876
    • Number of communities = 72
  • Weakly connected components = 1
  • Medium clustering coefficient = 0.014
    • Total triangles = 202
  • Eigenvector centrality with 300 iterations = 0.0325
  • Medium path length = 4.496
    • Number of shortest paths = 25125156
    • Radius = 3

 

Italiano:

Con friends si intendono tutte le persone che l'utente segue. Partendo da un dato utente, recuperiamo i suoi friends fino al livello 3 e generiamo un file XML con una struttura gradita a Gephi per visualizzare graficamente il risultato. Il numero di utenti recuperati e' inferiore al numero reale a causa di limitazioni imposte dalle API di Twitter.

Per creare il file gexf per Gephi, basta far girare lo script HowDoYouGraph dopo aver modificato la variabile $username con lo username desiderato. Dopo aver preso un caffe', nella stessa cartella dello script apparira' un file chiamato grafo.gexf che e' quello che ci interessa.

 

Visiona un grafo di esempio per l’utente VivoMikiX e scarica il file gexf sorgente.

 

Statistiche sul grafo d’esempio, considerandolo indiretto:

  • Totale nodi: 5013
  • Totale archi: 5539
  • Grado Medio = 2.21
  • Diametro = 6
  • Densità = 0
  • Modularità = 0.876
    • Numero di Comunità = 72
  • Componenti connesse debolmente = 1
  • Coefficiente di Clustering Medio = 0.014
    • Triangoli totali = 202
  • Centralità di autovettori con 300 iterazioni = 0.0325
  • Lunghezza cammino medio = 4.496
    • Numero di percorsi piu' corti = 25125156
    • Raggio = 3