Skip to content

Instantly share code, notes, and snippets.

@slarson
Created March 6, 2013 20:21
Show Gist options
  • Save slarson/5102708 to your computer and use it in GitHub Desktop.
Save slarson/5102708 to your computer and use it in GitHub Desktop.
Sample revision of d3-converter.py
#OpenWorm CSV to JSON
#Author: Gaston Gentile
import gspread, getpass, os
os.system('clear')
#Primer paso, los datos son tomados / First Step, data is taken.
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.
print 'Taking values...'
#Toma los valores de las celdas / Take value of celds.
colb = worksheet.col_values(2)
cold = worksheet.col_values(4)
colf = worksheet.col_values(6)
#Segundo paso, datos convertidos / Second Step, converted data.
print 'Converting values to JSON...'
celd = 2
celegans = open('celegans.json','w')
head = {'name': 'NeuroNetwork'}
chld = {}
for i in range(301):
chld = {'name': cold[celd] }
listOfReceptors = colf[celd]
pythonListOfReceptors = listOfReceptors.split()
jsonList = [{'name': cold[celd], 'size': 3000}]
for j in pythonListOfReceptors:
jsonList.append({'name': j, 'size': 3000})
chld['children'] = jsonList
celd += 1
head['children'] = chld
celegans.write(json.dumps(head, sort_keys=False, indent=4))
celegans.close()
@GasGen
Copy link

GasGen commented Apr 5, 2013

I could run the script, but the structure still is not the correct, here are my modifications:

#OpenWorm CSV to JSON
#Author: Gaston Gentile

import gspread, getpass, json, os

os.system('clear')

#Primer paso, los datos son tomados / First Step, data is taken.
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("https://docs.google.com/spreadsheet/ccc?key=0Avt3mQaA-HaMdGxUeGljZjktYUJoZkU0V0IwMDhIZ0E#gid=3")#apertura del documento / Open Doc.
    worksheet = sh.worksheet("Single Neurons")#Seleccion de la hoja / Sheet selection.

    print 'Taking values...'
    #Toma los valores de las celdas / Take value of celds.
    colb = worksheet.col_values(2)
    cold = worksheet.col_values(4)
    colf = worksheet.col_values(6)

    #Segundo paso, datos convertidos / Second Step, converted data.
    print 'Converting values to JSON...'
    celd = 2

    celegans = open('celegans.json','w')
    head = {'name': 'NeuroNetwork'}

    chld = {}

    for i in range(300):
        listOfNeurons = colb
        listOfNeurotransmitters = cold
        listOfReceptors = colf

        chld = {'neuron': listOfNeurons[celd]}

        #pythonListOfNeurotransmitters = cold[celd]

        jsonListN = [{'Neurotransmitter': cold[celd], 'size': 3000}]
        jsonListR = [{'Receptor': colf[celd], 'size': 3000}]

        for j in listOfNeurotransmitters:
            jsonListN.append({'Neurotransmitter': j, 'size': 3000})
        for r in listOfReceptors:
            jsonListR.append({'Receptor': r, 'size': 3000})

        chld['children'] = jsonListN, jsonListR
        celd += 1

    head['children'] = chld

    celegans.write(json.dumps(head, sort_keys=False, indent=4))
    celegans.close()

start()

I could run the script, but the structure still is not the correct. Here are my modifications:

Start with the modifications:
First, for not copy and paste the link of the sheet, I automate this step.

Second, I change the listOfReceptors to listOfNeurotransmitters, and I remove the index.

Third, I modify the jsonList variable to jsonListN (N for Neurotransmitters), and also I add jsonListR (R for Receptors).

Forth, in this loop I have a problem, the Neurotransmitters and Receptors are write in the json file, but not interspersed (namely: neurotransmitter, receptor, neurotransmitter, receptor, etc.), is written in blocks, namely first all the neurotransmitters and then all the receptors.

Other problem, is the name of each neuron, only appear in the first time, then do not appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment