openworm / OpenWorm

Repository for the main Dockerfile with the OpenWorm software stack and project-wide issues
http://openworm.org
MIT License
2.61k stars 205 forks source link

First DataViz Experiment using D3JS #6

Closed slarson closed 11 years ago

slarson commented 11 years ago

We'd like to create a map of the completeness of the model and it involves a few different steps. We've found the following viz example that we'd like to try to build off of:

http://mbostock.github.com/d3/talk/20111116/pack-hierarchy.html

this uses the following library (http://d3js.org/)

The pack-hierarchy example is driven by the following JSON:

http://mbostock.github.com/d3/talk/20111116/flare.json

We figure if we can write JSON out in this manner that comes from the data sets that we have then it will be easy to create this visualization without having to do much d3js programming. So the problem is basically just converting a spreadsheet into this JSON.

For iteration 0, we'd like to have a Python script that can take this sheet that lists the c. elegans neurons and creates JSON like flare.json that can drive a visualization.

The simplest way to do this is to download the sheet as CSV and parse as text. A better way would be to use the GSpread library to access the spreadsheet programmatically on Google Drive.

The top level should be the neuron names in Column B, then as children from that top level should be the neurotransmitter in column D and the Receptor information in Column F. From here there are a lot of bells and whistles to add but this is a good place to start. We are interested in having a Python script rather than a program in another language because we'd like to put it under version control and evolve the visualization as we have additional data to put into it (we already do but merging the other data sets is yet another project).

The neurons send chemical signals using different molecules -- those are the neurotransmitters. The receptors are the "locks" that the "key" of the neurotransmitters fit into. The receptors then have their own dynamics that influences how the neurons work.

slarson commented 11 years ago

Great start! Your general structure looks good. A few comments below.

#OpenWorm CSV to JSON
#author: Gaston Gentile

import gspread
import csv
import json
import getpass

'''Para inicio de sesion automatico borrar variables user & pwd
y escibrilos entre comillas en la variable gc.
To automatic sign in, remove user & pwd variables, and write
them between quotes in gc variable respectively'''

#FIRST STEP
def start():
    user = raw_input("Gmail user: ")
    pwd = getpass.getpass("Gmail Password: ")
    gc = gspread.login(user,pwd)
    docurl = raw_input("Spreadsheet url: ")
    sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
    worksheet = sh.sheet1#Seleccion de la hoja/Sheet selection.

Here it looks like you may not be selecting the correct sheet yet but I think you know this.

    allsheet = worksheet.get_all_values()#Toma todos los valores de la hoja/Take all the values of the sheet.
    parse = str(allsheet)#Convierte los datos a string/Parse data to string.
    #Guarda el CSV en Celegans.csv/Save the CSV in Celegans.csv
    csv = open('Celegans.csv', 'w')
    escribir = csv.write(parse)
    csv.close() 

#SECOND STEP
#it's permited add more fieldnames, but i dont know which are the positions (neurotransmitter, neuron names, etc.)
def convert():
    f = open('Celegans.csv','r')
    reader = csv.DictReader(f,fieldnames=('fieldname1','fieldname2','fieldname3'),delimiter=';')
    lineas = [l for l in reader]
    salida = json.dumps(lineas)

For this part, I'm not sure this will format the JSON to have the required structure as we see in flare.json. Not all json is created alike. But you have correctly found the right libraries to use so bravo for that. For a next step, I would try using Celegans.json in place of flare.json in the pack-hierarchy example in your browser and see if it looks at all correct.

    open('Celegans.json','w').write(salida)

start()
convert()
GasGen commented 11 years ago

Right, the structure of Celegans.json is not like flare.json, I have doubts about if it works, see if this works correctly, also I have to work more in the script...but the basis of the script is here

Thanks Stephen!

slarson commented 11 years ago

Yeah -- looking at flare.json, you are going to at least need to add in the fields "name", "children" and "size" as they have.

GasGen commented 11 years ago

Hi! To select the correct sheet, can modify this part of code: worksheet = sh.sheet1

to this: worksheet = sh.worksheet('name of the sheet')

or by index: worksheet = sh.get_worksheet(indexnumber)

GasGen commented 11 years ago

Sorry I closed the issue accidentally. Now is open again.

slarson commented 11 years ago

No problem. Great progress!

GasGen commented 11 years ago

Well, I reforming the code for import only the necessary columns, namely: B (names), D (Neurotransmitters) and F (Receptor), instead of importing the entire sheet (If you notice, the lines of code that imported entire sheet, are commented). I called the three columns, and then converted to string separately. Now I have only the columns I need.

The changes... Extraction of columns:

colb = worksheet.col_values(2)
cold = worksheet.col_values(4)
colf = worksheet.col_values(6)

Parse columns, individually:

parseb = str(colb)
parsed = str(cold)
parsef = str(colf)

Write columns in same file:

csv = open('Celegans.csv', 'w')
escribirb = csv.write(parseb)
escribird = csv.write(parsed)
escribirf = csv.write(parsef)
csv.close()

All the code:

#OpenWorm CSV to JSON
#author: Gaston Gentile

import gspread, csv, json, getpass, os

'''Para inicio de sesion automatico borrar variables user & pwd
y escibrilos entre comillas en la variable gc.
To automatic sign in, remove user & pwd variables, and write
them between quotes in gc variable respectively'''

os.system('clear')

#FIRST STEP
def start():
    print 'Convert CSV to JSON'
    user = raw_input("Gmail user: ")
    pwd = getpass.getpass("Gmail Password: ")
    gc = gspread.login(user,pwd)
    docurl = raw_input("Spreadsheet url: ")
    sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
    worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.

    #allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

    #colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
    colb = worksheet.col_values(2)
    cold = worksheet.col_values(4)
    colf = worksheet.col_values(6)

    #parse = str(allsheet)Convierte los datos a string/Parse data to string.

    #Convierte valores de columnas en strings/Parse columns values in to string.
    parseb = str(colb)
    parsed = str(cold)
    parsef = str(colf)

    #Guarda el CSV en Celegans.csv/Save the CSV in Celegans.csv
    csv = open('Celegans.csv', 'w')
    escribirb = csv.write(parseb)
    escribird = csv.write(parsed)
    escribirf = csv.write(parsef)
    csv.close()

#SECOND STEP - EXPERIMENTAL
def convert():
    f = open('Celegans.csv','r')
    reader = csv.DictReader(f,fieldnames=(''),delimiter=',')
    lineas = [row for row in reader]
    salida = json.dumps(lineas, sort_keys=True, indent=1)
    open('Celegans.json','w').write(salida)

start()
convert()

If you notice, in the second step I say "Experimental", that's because still I'm fixing the code to be functional the convertion to json. But now, I think, the csv part are complete. Only I have to focus json (second part).

slarson commented 11 years ago

Cool! To make the next step, you may want to try writing by hand a simple version of the JSON you think is needed by the visualizer. You could do this by taking flare.json and copying it into a new document. You could delete most of it, and then replace what is left with the first two rows from the spreadsheet. All of this done just by typing. Then, try to load up the visualizer with your sample JSON and make sure it works. All of this is done before writing any more python code. Once you are satisfied that you have the right format of JSON, then try to write the python code that creates it. Once your python code writes the sample JSON, expand the python code to read in the data from the spreadsheet and produce the full JSON needed.

On Mon, Jan 21, 2013 at 12:43 PM, GasGen notifications@github.com wrote:

Well, I reforming the code for import only the necessary columns, namely: B (names), D (Neurotransmitters) and F (Receptor), instead of importing the entire sheet (If you notice, the lines of code that imported entire sheet, are commented). I called the three columns, and then converted to string separately. Now I have only the columns I need.

The changes... Extraction of columns:

colb = worksheet.col_values(2)cold = worksheet.col_values(4)colf = worksheet.col_values(6)

Parse columns, individually:

parseb = str(colb)parsed = str(cold)parsef = str(colf)

Write columns in same file:

csv = open('Celegans.csv', 'w')escribirb = csv.write(parseb)escribird = csv.write(parsed)escribirf = csv.write(parsef)csv.close()

All the code:

OpenWorm CSV to JSON#author: Gaston Gentile

import gspread, csv, json, getpass, os '''Para inicio de sesion automatico borrar variables user & pwdy escibrilos entre comillas en la variable gc.To automatic sign in, remove user & pwd variables, and writethem between quotes in gc variable respectively''' os.system('clear')

FIRST STEPdef start():

print 'Convert CSV to JSON'
user = raw_input("Gmail user: ")
pwd = getpass.getpass("Gmail Password: ")
gc = gspread.login(user,pwd)
docurl = raw_input("Spreadsheet url: ")
sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.
#allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

#colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
colb = worksheet.col_values(2)
cold = worksheet.col_values(4)
colf = worksheet.col_values(6)

#parse = str(allsheet)Convierte los datos a string/Parse data to string.

#Convierte valores de columnas en strings/Parse columns values in to string.
parseb = str(colb)
parsed = str(cold)
parsef = str(colf)

#Guarda el CSV en Celegans.csv/Save the CSV in Celegans.csv
csv = open('Celegans.csv', 'w')
escribirb = csv.write(parseb)
escribird = csv.write(parsed)
escribirf = csv.write(parsef)
csv.close()

SECOND STEP - EXPERIMENTALdef convert():

f = open('Celegans.csv','r')
reader = csv.DictReader(f,fieldnames=(''),delimiter=',')
lineas = [row for row in reader]
salida = json.dumps(lineas, sort_keys=True, indent=1)
open('Celegans.json','w').write(salida)

start()convert()

If you notice, in the second step I say "Experimental", that's because still I'm fixing the code to be functional the convertion to json. But now, I think, the csv part are complete. Only I have to focus json (second part).

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-12516502.

GasGen commented 11 years ago

Great idea, I will try first make the json manually, and then pass in the Python Script.

GasGen commented 11 years ago

Hi!, I used your advice and wrote by hand as would be the display adapted to the network of neurons. Is a example, I used only three neurons of the sheet. I used to json validator, and is ok, but would have to try it for graphically see how is the rendering.

Well, here is the test, first the "class" NeuroNetwork, this is the top level, then have childrens, the childrens are single neurons with your name, neurotransmitter and receptor. The size parameter, is only for the size of the circles.

{
    "name": "NeuroNetwork",
    "children": [
        {
            "name": "ADAL",
            "Neurotransmitter": "Glumate",
            "Receptor": "empty",
            "size": "1400",
    "children": [
        {
            "name": "ADAR",
            "Neurotransmitter": "Glumate",
            "Receptor": "Empty",
            "size": "1700",
    "children": [
        {
            "name": "ADEL",
            "Neurotransmitter": "Dopamine",
            "Receptor": "DOP-2, EXP-1"
                        }
                    ]
                }
            ]
        }
    ]
}
slarson commented 11 years ago

OK! Seems reasonable, but you are right, we'd need to plug it in to see graphically if it works. Can you try and send a screenshot?

On Friday, January 25, 2013, GasGen wrote:

Hi!, I used your advice and wrote by hand as would be the display adapted to the network of neurons. Is a example, I used only three neurons of the sheet. I used to json validator, and is ok, but would have to try it for graphically see how is the rendering.

Well, here is the test, first the "class" NeuroNetwork, this is the top level, then have childrens, the childrens are single neurons with your name, neurotransmitter and receptor. The size parameter, is only for the size of the circles.

{ "name": "NeuroNetwork", "children": [ { "name": "ADAL", "Neurotransmitter": "Glumate", "Receptor": "empty", "size": "1400", "children": [ { "name": "ADAR", "Neurotransmitter": "Glumate", "Receptor": "Empty", "size": "1700", "children": [ { "name": "ADEL", "Neurotransmitter": "Dopamine", "Receptor": "DOP-2, EXP-1" } ] } ] } ]}

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-12730482.

GasGen commented 11 years ago

I try to create the graph to see if everything is correct, and then post the screenshot

GasGen commented 11 years ago

Hi!, Still I don't create the graph because for represent I need some JavaScript and I don't know more about him. But I continued working with the Python Script and I have an idea to create the json file, is very simple (still not respects the identation), consists take the data of the cells (Columns: B,D,F) and then paste them in a variable for represent them in the json (after I use a loop to repeat the lines of code for all the childrens classes)

First the head:

head = "{\n""\"name\":\"NeuroNetwork\"\n"

and now the children classes (Columns not in quotes):

chld = "\"children\": [\n{\n\"name\":\",""COLUMNB"",\n\"Neurotransmitter\":\"""COLUMND,""\n\"Receptor\":\"""COLUMNF""\",\n"

The variable chld (children) be repeated for loop until complete all the neuro cells.

slarson commented 11 years ago

HI Gaston,

Before you go there I think you are closer than you think to having something render on the screen. I was having a look at the pack-heirarchy example and created a local version of it on my machine. I haven't gotten it to render, but I'm getting close, so I thought I'd hand it over to you and see if it makes sense. I'll email you what I have with a further description and see how it goes, but you should read this:

http://www.dashingd3js.com/creating-svg-elements-based-on-data

Which is a piece of a great tutorial that begins here:

http://www.dashingd3js.com/d3js-first-steps

Best, Stephen

On Mon, Jan 28, 2013 at 3:04 PM, GasGen notifications@github.com wrote:

Hi!, Still I don't create the graph because for represent I need some JavaScript and I don't know more about him. But I continued working with the Python Script and I have an idea to create the json file, is very simple (still not respects the identation), consists take the data of the cells (Columns: B,D,F) and then paste them in a variable for represent them in the json (after I use a loop to repeat the lines of code for all the childrens classes)

First the head:

head = "{\n""\"name\":\"NeuroNetwork\"\n"

and now the children classes (Columns not in quotes):

chld = "\"children\": [\n{\n\"name\":\",""COLUMNB"",\n\"Neurotransmitter\":\"""COLUMND,""\n\"Receptor\":\"""COLUMNF""\",\n"

The variable chld (children) be repeated for loop until complete all the neuro cells.

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-12810703.

GasGen commented 11 years ago

Other advances in the source, now the columns are independent files (colb.csv, cold.csv and colf.csv), Why?, because thus is most easy take the values. Also, the script delete the commas and spaces generated by the csv convertion.

#OpenWorm CSV to JSON
#author: Gaston Gentile

import gspread, csv, json, getpass, os

'''Para inicio de sesion automatico borrar variables user & pwd
y escibrilos entre comillas en la variable gc.
To automatic sign in, remove user & pwd variables, and write
them between quotes in gc variable respectively'''

os.system('clear')

#FIRST STEP
def start():
    print 'Convert CSV to JSON'
    user = raw_input("Gmail user: ")
    pwd = getpass.getpass("Gmail Password: ")
    gc = gspread.login(user,pwd)
    docurl = raw_input("Spreadsheet url: ")
    sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
    worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.

    #allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

    #colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
    colb = worksheet.col_values(2)
    cold = worksheet.col_values(4)
    colf = worksheet.col_values(6)

    #parse = str(allsheet)Convierte los datos a string/Parse data to string.

    #Convierte valores de columnas en strings/Parse columns values in to string.
    parseb = str(colb)
    parsed = str(cold)
    parsef = str(colf)

    #Guarda el CSV separado por columnas/Save the CSV separate by columns
    colbwrite = open('colb.csv', 'w')
    coldwrite = open('cold.csv', 'w')
    colfwrite = open('colf.csv', 'w')
    escribirb = colbwrite.write(parseb)
    colbwrite.close()
    escribird = coldwrite.write(parsed)
    coldwrite.close()
    escribirf = colfwrite.write(parsef)
    colfwrite.close()

#SECOND STEP - EXPERIMENTAL
def convert():
    #Abre columna b, elimina comas y espacios./Open column b and delet commas and spaces.
    comma = ","

    fb = open('colb.csv','r')
    leer = fb.read()
    deleted = leer.replace(comma, "")
    espacios = deleted.replace(' ','')
    fb.close()
    fb = open('colb.csv','w')
    fb.write(espacios)
    fb.close()

    #Abre columna d, elimina comas y espacios./Open column b and delet commas and spaces.
    fd = open('cold.csv','r')
    leer = fd.read()
    deleted = leer.replace(comma, "")
    espacios = deleted.replace(' ','')
    fd.close()
    fd = open('cold.csv','w')
    fd.write(espacios)
    fd.close()

    #Abre columna f, elimina comas y espacios./Open column b and delet commas and spaces.   
    ff = open('colf.csv','r')
    leer = ff.read()
    deleted = leer.replace(comma, "")
    espacios = deleted.replace(' ','')
    ff.close()
    ff = open('colf.csv','w')
    ff.write(espacios)
    ff.close()

    #head = "{\n""\"name\":\"NeuroNetwork\"\n"
    #chld = "\"children\": [\n{\n\"name\":,\""+cellb+"\",\n\"Neurotransmitter\":\""+celld+"\",\n\"Receptor\":\""+cellf+"\",\n"

start()
convert()

Now what is missing is create loop to take the first value of column b, d and f and paste them in the json file, and then take the second, the third, etc. If you look the comments, there are two new variables, first "head" and second "chld". head variable is the header of the json file and chld is the body of json, to be attached to a loop.

{
    "name": "NeuroNetwork",
    "children": [
        {
            "name": "FIRST VALUE COLUMN B",
            "Neurotransmitter": "FIRST VALUE COLUMN D",
            "Receptor": "FIRST VALUE COLUMN F",
            "size": "1400",
    "children": [
        {
            "name": "SECOND VALUE COLUMN B",
            "Neurotransmitter": "SECOND VALUE COLUMN D",
            "Receptor": "SECOND VALUE COLUMN F",
            "size": "1700",
    "children": [
        {
            "name": "THIRD VALUE COLUMN B",
            "Neurotransmitter": "THIRD VALUE COLUMN D",
            "Receptor": "THIRD VALUE COLUMN F"
                        }
                    ]
                }
            ]
        }
    ]
}

@slarson, about the errors on the visualization, I make a quick view of them, I start Chrome without web security (--disble-web-secury) to make a test, and the errors are disappear, but the problem continues, not shown. Evenly, was a quick test then I'll try to correct them :)!

slarson commented 11 years ago

Cool! Looks like you are on the right track. That's strange about the visualization errors. If you can't figure it out by the middle of the week, send me a note and I'll either look into it some more myself or get some other help.

On Saturday, February 2, 2013, GasGen wrote:

Other advances in the source, now the columns are independent files (colb.csv, cold.csv and colf.csv), Why?, because thus is most easy take the values. Also, the script delete the commas and spaces generated by the csv convertion.

OpenWorm CSV to JSON#author: Gaston Gentile

import gspread, csv, json, getpass, os '''Para inicio de sesion automatico borrar variables user & pwdy escibrilos entre comillas en la variable gc.To automatic sign in, remove user & pwd variables, and writethem between quotes in gc variable respectively''' os.system('clear')

FIRST STEPdef start():

print 'Convert CSV to JSON'
user = raw_input("Gmail user: ")
pwd = getpass.getpass("Gmail Password: ")
gc = gspread.login(user,pwd)
docurl = raw_input("Spreadsheet url: ")
sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.
#allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

#colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
colb = worksheet.col_values(2)
cold = worksheet.col_values(4)
colf = worksheet.col_values(6)

#parse = str(allsheet)Convierte los datos a string/Parse data to string.

#Convierte valores de columnas en strings/Parse columns values in to string.
parseb = str(colb)
parsed = str(cold)
parsef = str(colf)

#Guarda el CSV separado por columnas/Save the CSV separate by columns
colbwrite = open('colb.csv', 'w')
coldwrite = open('cold.csv', 'w')
colfwrite = open('colf.csv', 'w')
escribirb = colbwrite.write(parseb)
colbwrite.close()
escribird = coldwrite.write(parsed)
coldwrite.close()
escribirf = colfwrite.write(parsef)
colfwrite.close()

SECOND STEP - EXPERIMENTALdef convert():

#Abre columna b, elimina comas y espacios./Open column b and delet commas and spaces.
comma = ","
fb = open('colb.csv','r')
leer = fb.read()
deleted = leer.replace(comma, "")
espacios = deleted.replace(' ','')
fb.close()
fb = open('colb.csv','w')
fb.write(espacios)
fb.close()

#Abre columna d, elimina comas y espacios./Open column b and delet commas and spaces.
fd = open('cold.csv','r')
leer = fd.read()
deleted = leer.replace(comma, "")
espacios = deleted.replace(' ','')
fd.close()
fd = open('cold.csv','w')
fd.write(espacios)
fd.close()

#Abre columna f, elimina comas y espacios./Open column b and delet commas and spaces.
ff = open('colf.csv','r')
leer = ff.read()
deleted = leer.replace(comma, "")
espacios = deleted.replace(' ','')
ff.close()
ff = open('colf.csv','w')
ff.write(espacios)
ff.close()

#head = "{\n""\"name\":\"NeuroNetwork\"\n"
#chld = "\"children\": [\n{\n\"name\":,\""+cellb+"\",\n\"Neurotransmitter\":\""+celld+"\",\n\"Receptor\":\""+cellf+"\",\n"

start()convert()

Now what is missing is create loop to take the first value of column b, d and f and paste them in the json file, and then take the second, the third, etc. If you look the comments, there are two new variables, first "head" and second "chld". head variable is the header of the json file and chld is the body of json, to be attached to a loop.

{ "name": "NeuroNetwork", "children": [ { "name": "FIRST VALUE COLUMN B", "Neurotransmitter": "FIRST VALUE COLUMN D", "Receptor": "FIRST VALUE COLUMN F", "size": "1400", "children": [ { "name": "SECOND VALUE COLUMN B", "Neurotransmitter": "SECOND VALUE COLUMN D", "Receptor": "SECOND VALUE COLUMN F", "size": "1700", "children": [ { "name": "THIRD VALUE COLUMN B", "Neurotransmitter": "THIRD VALUE COLUMN D", "Receptor": "THIRD VALUE COLUMN F" } ] } ] } ]}

@slarson https://github.com/slarson, about the errors on the visualization, I make a quick view of them, I start Chrome without web security (--disble-web-secury), and the errors are disappear, but the problem continues. Evenly, was a quick test then I'll try to correct them :)!

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-13036679.

GasGen commented 11 years ago

I complete the code, but has a bug (I try to fix this tomorrow). The data is take ok, but when stored values, all are the same!, I have the error in front of my eyes, but i can't see!, I know the error is for the for loop.

#OpenWorm CSV to JSON
#author: Gaston Gentile

import gspread, csv, json, getpass, os

'''Para inicio de sesion automatico borrar variables user & pwd
y escibrilos entre comillas en la variable gc.
To automatic sign in, remove user & pwd variables, and write
them between quotes in gc variable respectively'''

os.system('clear')

#FIRST STEP
def start():
    print 'Convert CSV to JSON'
    user = raw_input("Gmail user: ")
    pwd = getpass.getpass("Gmail Password: ")
    gc = gspread.login(user,pwd)
    docurl = raw_input("Spreadsheet url: ")
    sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
    worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.

    #allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

    #colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
    colb = worksheet.col_values(2)
    cold = worksheet.col_values(4)
    colf = worksheet.col_values(6)

    #parse = str(allsheet)Convierte los datos a string/Parse data to string.

    #Convierte valores de columnas en strings/Parse columns values in to string.
    parseb = str(colb)
    parsed = str(cold)
    parsef = str(colf)

    #Guarda el CSV separado por columnas/Save the CSV separate by columns
    colbwrite = open('colb.csv', 'w')
    coldwrite = open('cold.csv', 'w')
    colfwrite = open('colf.csv', 'w')
    escribirb = colbwrite.write(parseb)
    colbwrite.close()
    escribird = coldwrite.write(parsed)
    coldwrite.close()
    escribirf = colfwrite.write(parsef)
    colfwrite.close()

#SECOND STEP - EXPERIMENTAL
def convert():
    #Abre columna b, elimina comas y espacios./Open column b and delet commas and spaces.
    coma = ","
    fb = open('colb.csv','r')
    leer = fb.read()
    eliminado = leer.replace(coma, "")
    espacios = eliminado.replace(' ','')
    fb.close()
    fb = open('colb.csv','w')
    fb.write(espacios)
    fb.close()

    #Abre columna d, elimina comas y espacios./Open column b and delet commas and spaces.
    coma = ","
    fd = open('cold.csv','r')
    leer = fd.read()
    eliminado = leer.replace(coma, "")
    espacios = eliminado.replace(' ','')
    fd.close()
    fd = open('cold.csv','w')
    fd.write(espacios)
    fd.close()

    #Abre columna f, elimina comas y espacios./Open column b and delet commas and spaces.   
    coma = ","
    ff = open('colf.csv','r')
    leer = ff.read()
    eliminado = leer.replace(coma, "")
    espacios = eliminado.replace(' ','')
    ff.close()
    ff = open('colf.csv','w')
    ff.write(espacios)
    ff.close()

    #Index para tomar valores de las celdas / Index for take values of cells
    loop = 1
    celd = 3 #El valor es tres, porque es el primer dato que aparece/The value is third because is the first data.

    head = "{\n""\"name\":\"NeuroNetwork\",\n"
    foot = "\n}\n]"*302 #302 porque es al cantidad de childrens / 302 because are the numbers of childrens.

    fb = open('colb.csv','r')
    fd = open('cold.csv', 'r')
    ff = open('colf.csv', 'r')
    celegans = open('celegans.json','w')

    celegans.write(head)

    #Las vueltas del bucle es igual a la cantidad de celulas / The laps of loop are equal to numbers of cells.
    while loop <= 302:

        #Los datos se encuentran en posiciones impares, con esta porcion de codigo, utilizamos solo esas posiciones. / The data is only in odd positions, with this portion of code, use only them.
        if celd%2 == 0:
            celd = celd + 1

        #Columna b / Column b
        for line in fb.readlines():
            if "\'" in line:
                cellb = line.split('\'')[celd]
        #Columna d / Column d   
        for line in fd.readlines():
            if "\'" in line:
                celld= line.split('\'')[celd]
        #Columna f / Column f
        for line in ff.readlines():
            if "\'" in line:
                cellf = line.split('\'')[celd]

        chld = "\"children\": [\n{\n\"name\":\""+cellb+"\",\n\"Neurotransmitter\":\""+celld+"\",\n\"Receptor\":\""+cellf+"\",\n\"size\":\"1400\",\n"

        #Escribe el children / Write the children
        celegans.write(chld)
        #Cuenta las vueltas del bucle / Loop count.
        loop = loop + 1
        #Celda a utilizar en la proxima vuelta / Celd to use in the next lap.
        celd = celd + 1 

    #Escribe el pie del archivo json/ Write the foot of the json file
    celegans.write(foot)
    #Cierra el archivo json / Close the json file.
    celegans.close()

    fb.close()

    print "\nWORKS!, celegans.json was generated!\n"

start()
convert()

If you try the script, you see that the json file is generated, but...the values in "Name", "Neurotransmitter", and "Receptor", are repeated.

I will try to fix the bug of the visualization, if I can't fix it, I send you a message.

slarson commented 11 years ago

You've made great progress. I've gone ahead and committed your script to a new data-viz repo. You've been added as an owner of the organization. Please clone the data-viz repo and make your future changes there, and commit them back:

https://github.com/openworm/data-viz/commit/f9d62209957a5b221c2777bc5246b8fc37195814

I'll make a note to do a more formal code review in the next day or two.

On Sun, Feb 3, 2013 at 8:57 PM, GasGen notifications@github.com wrote:

I complete the code, but has a bug (I try to fix this tomorrow). The data is take ok, but when stored values, all are the same!, I have the error in front of my eyes, but i can't see!, I know the error is for the for loop.

OpenWorm CSV to JSON#author: Gaston Gentile

import gspread, csv, json, getpass, os '''Para inicio de sesion automatico borrar variables user & pwdy escibrilos entre comillas en la variable gc.To automatic sign in, remove user & pwd variables, and writethem between quotes in gc variable respectively''' os.system('clear')

FIRST STEPdef start():

print 'Convert CSV to JSON'
user = raw_input("Gmail user: ")
pwd = getpass.getpass("Gmail Password: ")
gc = gspread.login(user,pwd)
docurl = raw_input("Spreadsheet url: ")
sh = gc.open_by_url(docurl)#apertura del documento/Open Doc.
worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja/Sheet selection.
#allsheet = worksheet.get_all_values()Toma todos los valores de la hoja/Take all the values of the sheet.

#colb, cold, colf toma los valores de las columnas (b,d,f)/ Take values of columns (b,d,f)
colb = worksheet.col_values(2)
cold = worksheet.col_values(4)
colf = worksheet.col_values(6)

#parse = str(allsheet)Convierte los datos a string/Parse data to string.

#Convierte valores de columnas en strings/Parse columns values in to string.
parseb = str(colb)
parsed = str(cold)
parsef = str(colf)

#Guarda el CSV separado por columnas/Save the CSV separate by columns
colbwrite = open('colb.csv', 'w')
coldwrite = open('cold.csv', 'w')
colfwrite = open('colf.csv', 'w')
escribirb = colbwrite.write(parseb)
colbwrite.close()
escribird = coldwrite.write(parsed)
coldwrite.close()
escribirf = colfwrite.write(parsef)
colfwrite.close()

SECOND STEP - EXPERIMENTALdef convert():

#Abre columna b, elimina comas y espacios./Open column b and delet commas and spaces.
coma = ","

fb = open('colb.csv','r')
leer = fb.read()

eliminado = leer.replace(coma, "")
espacios = eliminado.replace(' ','')

fb.close()
fb = open('colb.csv','w')
fb.write(espacios)
fb.close()

#Abre columna d, elimina comas y espacios./Open column b and delet commas and spaces.

coma = ","

fd = open('cold.csv','r')
leer = fd.read()

eliminado = leer.replace(coma, "")
espacios = eliminado.replace(' ','')

fd.close()
fd = open('cold.csv','w')
fd.write(espacios)
fd.close()

#Abre columna f, elimina comas y espacios./Open column b and delet commas and spaces.

coma = ","

ff = open('colf.csv','r')
leer = ff.read()

eliminado = leer.replace(coma, "")
espacios = eliminado.replace(' ','')

ff.close()
ff = open('colf.csv','w')
ff.write(espacios)
ff.close()

#Index para tomar valores de las celdas / Index for take values of cells
loop = 1
celd = 3 #El valor es tres, porque es el primer dato que aparece/The value is third because is the first data.

head = "{\n""\"name\":\"NeuroNetwork\",\n"
foot = "\n}\n]"*302 #302 porque es al cantidad de childrens / 302 because are the numbers of childrens.

fb = open('colb.csv','r')

fd = open('cold.csv', 'r')

ff = open('colf.csv', 'r')

celegans = open('celegans.json','w')

celegans.write(head)

#Las vueltas del bucle es igual a la cantidad de celulas / The laps of loop are equal to numbers of cells.
while loop <= 302:

    #Los datos se encuentran en posiciones impares, con esta porcion de codigo, utilizamos solo esas posiciones. / The data is only in odd positions, with this portion of code, use only them.
    if celd%2 == 0:
        celd = celd + 1

    #Columna b / Column b
    for line in fb.readlines():
        if "\'" in line:
            cellb = line.split('\'')[celd]
    #Columna d / Column d
    for line in fd.readlines():
        if "\'" in line:
            celld= line.split('\'')[celd]
    #Columna f / Column f
    for line in ff.readlines():
        if "\'" in line:
            cellf = line.split('\'')[celd]

    chld = "\"children\": [\n{\n\"name\":\""+cellb+"\",\n\"Neurotransmitter\":\""+celld+"\",\n\"Receptor\":\""+cellf+"\",\n\"size\":\"1400\",\n"

    #Escribe el children / Write the children
    celegans.write(chld)
    #Cuenta las vueltas del bucle / Loop count.
    loop = loop + 1
    #Celda a utilizar en la proxima vuelta / Celd to use in the next lap.
    celd = celd + 1

#Escribe el pie del archivo json/ Write the foot of the json file
celegans.write(foot)
#Cierra el archivo json / Close the json file.
celegans.close()

fb.close()

print "\nWORKS!, celegans.json was generated!\n"

start()convert()

If you try the script, you see that the json file is generated, but...the values in "Name", "Neurotransmitter", and "Receptor", are repeated.

I will try to fix the bug of the visualization, if I can't fix it, I send you a message.

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-13060728.

GasGen commented 11 years ago

Thanks!, I clone the data-viz repo!.

slarson commented 11 years ago

Had a look at this code. I think it is important that you take advantage of the json.dumps(x) function that is talked about in the tutorial here:

http://www.doughellmann.com/PyMOTW/json/

Currently you are doing way too much string manipulation in this code, specifically here:

https://github.com/openworm/data-viz/blob/master/GasGen/d3js-converter.py#L134

There should be no need to do string concatenation like that.

Also, you should not bother writing out 3 csv files as you do on lines 38-46. You then have to read them back in. Instead, just use colb, cold, colf directly (or possibly parseb, parsed, parsef). You can loop over those structures directly by doing colb[x] or parseb[x]

The bug you are running into is because the for loops you have on 122, 126 and 130 are running through the whole CSV file every time the while loop on 115 runs. That means that cellb, celld and cellf gets the last value in each csv file you have. It doesn't get anything but the last value in those csv files because you only write chld on line 134 outside of the loops on 122, 126, and 130.

Please make your edits in GitHub for your next version.

https://github.com/openworm/data-viz/blob/master/GasGen/d3js-converter.py

Good luck!

On Mon, Feb 4, 2013 at 9:04 PM, GasGen notifications@github.com wrote:

Thanks!, I clone the data-viz repo!.

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-13114855.

GasGen commented 11 years ago

Thanks for the notes Stephen, now I see how to optimize the code!, I start to work in this!

Thanks again!

Edit - (02/03/2013): Hi, Well, I try to modify my base code, but think that the best form to optimize it, is write again from zero. Why?, because, as tell Stephen, the code have a lot of string manipulation, that cause first: the performance is very low, and second if somebody or I try to modify it, is very confusing. Hence, as tell Stephen, now I use the json module in Python.

The Monday or Tuesday I refresh the DataViz repo with a new code.

Edit - (03/03/2013): Hi, The code was updated. https://github.com/openworm/data-viz/blob/master/GasGen/d3js-converter.py

slarson commented 11 years ago

Hi! Good to hear it is updated. How close is it to generating some output we can look at?

GasGen commented 11 years ago

Hi Stephen!, (sorry for the delay) I never use JavaScript, but I can try make a tests to show an output :D!

Edit I'm very glad to tell that I could generate the first DataViz!!!, I reform again the script (for correct some littles bugs in json) and then in basis of the original html and css (of the first post), can rendering the DataViz!. The design is very simple, but the basis is here!

I share some pictures:

General Vision (The 302 neurons): dataviz-general-vision

Neuron Vision (With Neurotransmitter and Receptor) dataviz-neuron-vision

Now, in the repo I will update the code, and also I will put the html, css and js necessary for the execution.

slarson commented 11 years ago

Great job! I just got this working on my local system & made some adjustments. Let's discuss more about this on Wednesday.

On Mon, Mar 4, 2013 at 6:05 PM, GasGen notifications@github.com wrote:

Hi Stephen!, (sorry for the delay)

I never use JavaScript, but I can try make a tests to show an output :D!

Edit I'm very glad to tell that I could generate the first DataViz!!!, I reform again the script (for correct some littles bugs in json) and then in basis of the original html and css (of the first post), can rendering the DataViz!. The design is very simple, but the basis is here!

I share some pictures:

General Vision (The 302 neurons): [image: dataviz-general-vision]https://f.cloud.github.com/assets/3247512/219598/91754a80-8538-11e2-91f2-b1dff2db879c.png

Neuron Vision (With Neurotransmitter and Receptor) [image: dataviz-neuron-vision]https://f.cloud.github.com/assets/3247512/219599/a965a00e-8538-11e2-8d6c-5efda692a904.png

Now, in the repo I will update the code, and also I will put the html, css and js necessary for the execution.

— Reply to this email directly or view it on GitHubhttps://github.com/openworm/OpenWorm/issues/6#issuecomment-14419426 .

GasGen commented 11 years ago

Thanks!, ok, on Wednesday we will discuss about this.

slarson commented 11 years ago

Hi Gaston, any update on this?

GasGen commented 11 years ago

Hi Stephen!, sorry for the delay. Tomorrow I try to bring advances.

slarson commented 11 years ago

The first iteration on this has been completed. Let's move on to the next in the next release.