Testing Nsaba Functionality

Current Methods
  • get_aba_ge()
  • ge_ratio()
  • get_ns_act()
  • make_ge_ns_mat()
  • coords_to_ge()

In [1]:
%matplotlib inline
from nsaba.nsaba import Nsaba
from nsaba.nsaba.visualizer import NsabaVisualizer
import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd
import itertools

In [2]:
%load_ext line_profiler

In [3]:
# Simon Path IO

data_dir = '../../data_dir'
os.chdir(data_dir)
Nsaba.aba_load()
Nsaba.ns_load()


This may take a minute or two ...
SampleAnnot.csv loaded.
MicroarrayExpression.csv loaded.
Probes.csv loaded.
Nsaba.aba['mni_coords'] initialized.

This may take a minute or two ...
database.txt loaded.
features.txt loaded.
Nsaba.ns['mni_coords'] initialized.


In [ ]:
#Torben Path IO

ns_path = "/Users/Torben/Documents/ABI analysis/current_data_new/"
aba_path = '/Users/Torben/Documents/ABI analysis/normalized_microarray_donor9861/'

Nsaba.aba_load(aba_path)
Nsaba.ns_load(ns_path)

In [20]:
# Loading gene expression for all ABA registered Entrez IDs.
A = Nsaba()
A.load_ge_pickle('Nsaba_ABA_ge.pkl')


This may take a minute or two ...
'ge' dictionary successfully loaded

In [22]:
%time A.get_ns_act('attention', thresh=-1)
A.get_ns_act('reward', thresh=-1)


This may take a few minutes...
CPU times: user 31.2 s, sys: 526 ms, total: 31.7 s
Wall time: 31.8 s
This may take a few minutes...

In [5]:
# Testing ge_ratio()
A = Nsaba()
A.ge_ratio((1813,1816))


Out[5]:
array([ 0.57446139,  0.99023239,  1.11575466,  1.01100606,  0.90041432,
        0.51675821,  0.60023131,  0.79145099,  0.49431709,  0.48482013,
        0.69057069,  0.81420569,  0.47455972,  0.84989612,  0.62712447,
        0.63073593,  0.53755516,  0.44417201,  0.58291914,  0.442529  ,
        0.57296542,  0.66765314,  0.78020594,  0.60869062,  1.17611206,
        1.2758823 ,  0.6127578 ,  0.57277835,  1.10183741,  0.5111504 ,
        0.54529738,  0.50251657,  0.7318342 ,  0.94982714,  0.82175693,
        0.74350377,  1.06186932,  1.32446539,  0.45594681,  0.43065786,
        0.5671627 ,  0.59765326,  0.47546101,  0.46028177,  0.5367075 ,
        0.46101267,  0.42851074,  0.54803596,  0.56806979,  0.46818031,
        0.47677465,  0.41901151,  0.57470563,  0.48748218,  0.43258916,
        0.4557625 ,  0.48683465,  0.55563527,  0.63151196,  0.56402208,
        0.46587026,  0.59480653,  0.60864499,  0.50248616,  0.57100629,
        0.51971739,  0.50266858,  0.49048015,  0.5850243 ,  0.59838787,
        0.47702738,  0.59090007,  0.60104165,  0.58182095,  0.37653838,
        0.48518633,  0.43795997,  0.58950278,  0.49533073,  1.37820916,
        0.48045498,  0.59848414,  0.42094496,  0.51167704,  0.61382094,
        0.60477084,  0.56507526,  0.44055382,  0.48925664,  0.56694732,
        0.54641357,  0.502431  ,  0.46886492,  0.54479983,  0.58980693,
        0.53124606,  0.63540946,  0.53236562,  0.57955355,  0.43894693,
        1.39396035,  1.43277442,  0.4953226 ,  0.43696396,  0.42994692,
        1.43877062,  0.55010966,  0.50184539,  0.53271097,  0.49717748,
        0.55878968,  0.50261043,  0.55342758,  0.57706499,  0.71059221,
        0.51565715,  0.51249374,  0.70967863,  0.71101219,  0.53245578,
        0.65182318,  0.58873535,  0.60540382,  0.63675189,  0.64871194,
        1.17653603,  0.94808957,  0.90847539,  1.24458946,  1.33770475,
        1.06329307,  0.70651549,  0.64750649,  0.6907428 ,  0.58007908,
        0.65690029,  0.6434247 ,  0.59769837,  0.60148205,  0.643085  ,
        0.62359892,  0.70759297,  0.6396921 ,  0.68942534,  0.75630289,
        0.57527313,  0.57187016,  0.67733897,  0.61347643,  0.65336543,
        0.63416574,  0.57675493,  0.65114102,  0.61415467,  0.61071167,
        0.65053359,  0.70259764,  0.5731787 ,  0.69116079,  0.53555912,
        0.6884125 ,  0.68045385,  0.57818539,  0.6483705 ,  0.70876025,
        0.72344649,  0.56266611,  0.56138331,  0.64233029,  0.75796705,
        0.60547183,  0.57847054,  0.56456586,  0.63364713,  0.55550077,
        0.53226535,  0.70953331,  0.60535958,  0.54906669,  0.57456914,
        0.6710054 ,  0.68666173,  0.67441986,  0.58883574,  0.62754924,
        0.65548517,  0.75152638,  0.62355545,  0.5379467 ,  1.36796249,
        0.5996335 ,  0.52831734,  0.70755093,  0.5831287 ,  1.36196162,
        1.14438369,  1.09017965,  1.24606447,  1.19246525,  1.08939184,
        1.42373515,  0.50968596,  1.05685245,  1.32365796,  1.22049484,
        1.11777755,  1.37027099,  1.5471981 ,  1.19918404,  1.44053991,
        1.06305225,  1.11788925,  1.13857016,  0.78677393,  1.31909965,
        1.17211507,  1.31709133,  1.48246312,  1.38837764,  1.40269552,
        1.13777443,  1.45874076,  1.27680501,  1.11593103,  0.90896172,
        1.06843278,  1.12190502,  1.26272323,  1.03182006,  1.16722679,
        1.24230832,  1.11437386,  1.23740238,  1.05659626,  1.17840835,
        1.23527385,  1.25092369,  1.2187971 ,  1.17423453,  1.2037413 ,
        1.1720205 ,  1.30352434,  1.31529436,  1.6572168 ,  1.30753524,
        1.55449765,  1.37004914,  1.80486912,  1.62991897,  1.21333516,
        1.4096478 ,  1.45584425,  1.25556913,  1.32046023,  1.1563044 ,
        1.17375154,  1.4631757 ,  1.21180383,  0.98276905,  1.62804817,
        1.08495496,  1.20778754,  1.87268742,  1.25898534,  0.82397674,
        0.51752705,  0.57880174,  0.56845861,  0.65825955,  0.60088884,
        0.62083003,  0.59862165,  0.63705517,  0.69264633,  0.59165402,
        0.56005789,  0.60702408,  0.52950393,  0.56747137,  0.61948834,
        0.51654759,  0.58209119,  0.61570189,  0.59348125,  0.56314645,
        0.66540577,  0.65059983,  0.57120976,  0.55764354,  0.57143383,
        0.59759027,  0.62553765,  0.5613298 ,  0.65370127,  0.61024317,
        0.57217279,  1.32595584,  1.403048  ,  0.47610703,  1.23444002,
        0.62249374,  1.27533233,  0.6062356 ,  0.57808062,  1.31468874,
        1.20106898,  1.19319262,  1.23849676,  0.58304617,  0.64046317,
        0.63169584,  1.27136323,  1.36582862,  1.10239446,  0.86201587,
        0.71827644,  1.27978542,  0.62239591,  0.52614976,  0.52451168,
        0.58885776,  0.58935346,  0.57881691,  0.55874042,  0.51941105,
        0.68756675,  0.60221957,  0.60527692,  0.56860901,  0.50648885,
        0.53192984,  0.49634884,  0.54646424,  0.50234452,  0.57803057,
        0.51480263,  0.58829121,  0.51376152,  0.55191654,  0.60697633,
        0.52120334,  0.59912484,  0.56546997,  0.55092642,  0.55603219,
        0.60321334,  0.6857977 ,  0.54748702,  0.62974419,  0.61658869,
        0.63788073,  0.59939194,  0.56868984,  0.67857455,  0.61298005,
        0.58318256,  0.51451851,  0.57741996,  0.51550278,  0.59249272,
        0.51733884,  0.59134432,  0.60863186,  1.31245462,  1.23217378,
        0.63214044,  0.66293815,  0.52302202,  0.50955424,  0.54236758,
        0.75633759,  1.00680181,  1.07145625,  1.12401347,  0.4975224 ,
        0.47253723,  0.52153153,  0.5641989 ,  0.62066872,  0.61123672,
        0.7550087 ,  0.55508521,  0.52429058,  1.24173926,  0.6897293 ,
        0.60068332,  0.65667225,  0.66123935,  0.66548474,  0.56020502,
        0.52905277,  0.50702359,  0.40722692,  0.54402864,  0.5025992 ,
        0.47356166,  0.56768908,  0.55705583,  0.46195153,  0.45530148,
        0.60744693,  0.52316687,  0.55003556,  0.58932575,  0.70708727,
        0.7647043 ,  0.70904022,  0.70057101,  0.65885814,  0.63210778,
        0.68489943,  0.43703457,  0.50659747,  0.66585395,  0.82730246,
        0.75477311,  0.51468325,  0.66434266,  0.60531118,  0.76837738,
        0.76282032,  0.82618141,  0.65084354,  0.5365462 ,  0.74479694,
        0.62170627,  0.54588945,  0.59659081,  0.60953117,  0.81654161,
        0.67281272,  0.63690386,  0.6753435 ,  0.47236843,  0.53233239,
        0.67194446,  0.68564123,  0.71502843,  0.73080232,  0.64867958,
        0.65814154,  0.74853021,  0.65095239,  0.71174942,  0.69789513,
        0.58539424,  0.59253427,  0.51541751,  0.51879891,  0.81059605,
        0.80130527,  0.61364865,  0.55602895,  0.71438695,  0.63901161,
        0.60453672,  0.804082  ,  1.59202393,  1.59538349,  0.92018735,
        1.14200453,  0.65024902,  1.42975509,  0.39682242,  0.4531745 ,
        0.60880992,  0.64317765,  0.65570332,  0.75759681,  0.63490186,
        0.6438243 ,  0.7011007 ,  0.77925168,  0.59545459,  0.59511576,
        1.03675623,  0.82922895,  0.72805882,  0.76011522,  0.5529902 ,
        0.70889701,  0.58740333,  0.68913927,  1.12966047,  1.33982557,
        0.98883835,  1.65876964,  0.88016924,  0.89617421,  0.68975687,
        0.5961179 ,  0.46286274,  0.60757452,  0.72056975,  1.04813604,
        1.11571874,  1.24565047,  0.87120335,  1.7418675 ,  0.83378288,
        0.89030617,  0.97035535,  0.77885258,  0.62658343,  0.55334172,
        0.65137043,  0.46718595,  0.5090942 ,  0.52608703,  0.9681349 ,
        0.98878223,  0.83559178,  0.8985212 ,  0.75140451,  1.37224669,
        1.35114382,  0.74866017,  0.86538327,  1.41843684,  1.10757657,
        0.88590199,  0.81800556,  1.26678965,  1.02204877,  0.98185715,
        1.14268181,  0.75921855,  0.67076029,  0.66342245,  0.75078723,
        0.82721709,  1.11364808,  1.14257542,  1.21392985,  1.37947598,
        1.2877877 ,  0.60961799,  1.29961169,  0.79894894,  1.14825936,
        1.451838  ,  0.85026024,  1.26445954,  1.33588057,  0.93229065,
        1.22017179,  1.18926553,  1.1539034 ,  0.93503027,  0.7698934 ,
        1.06202385,  1.05289273,  0.87927575,  0.9035668 ,  0.5449617 ,
        0.7855242 ,  0.7145351 ,  1.19595577,  0.69496677,  0.77146192,
        1.32619836,  1.07928485,  1.12123351,  1.1957222 ,  0.8125032 ,
        1.03078237,  0.7514356 ,  0.51360628,  1.37765163,  0.52071614,
        0.44439729,  0.44926742,  0.54562832,  0.55462017,  1.31667949,
        0.44473908,  0.52887893,  0.4935384 ,  0.60164305,  0.50153877,
        0.57198432,  0.49765478,  0.59866373,  0.62811011,  0.52301028,
        0.66236825,  0.62566905,  0.54926513,  0.53862497,  0.60375575,
        0.55433877,  0.5333104 ,  0.59663452,  0.55911132,  0.65369035,
        0.57316849,  1.23898908,  0.60902597,  0.94860789,  0.52520479,
        0.53050748,  0.48223301,  0.50051961,  0.55707835,  0.56312753,
        0.49999732,  0.50550167,  0.49835656,  0.48861331,  0.57138931,
        0.58843594,  0.58188343,  0.47456234,  0.52011409,  0.46953503,
        0.55045332,  0.58378504,  0.53396618,  0.55685064,  0.74924179,
        0.51838818,  0.62567684,  0.53985026,  0.56567994,  0.58203582,
        0.5304339 ,  0.50496257,  0.45845153,  0.61829781,  0.59151995,
        0.64568364,  0.51315464,  0.57870715,  0.70104277,  0.49062354,
        0.54497513,  0.62606181,  0.62170931,  0.62596483,  0.49174621,
        0.55050802,  0.67005001,  0.68907247,  0.61741111,  0.82235098,
        0.55543725,  0.86771326,  0.8826589 ,  0.64495513,  0.86007621,
        0.82992595,  0.79706978,  0.77478125,  0.82767627,  0.88285768,
        1.07053878,  0.83824402,  1.12881957,  0.98432613,  0.57685415,
        0.54819011,  0.67945254,  0.64067614,  0.62090325,  0.62915394,
        0.65360051,  0.47372872,  0.70169237,  0.60631793,  0.6571401 ,
        0.60777947,  0.62055016,  0.60253806,  0.57535433,  0.58044938,
        0.61401404,  0.51303989,  0.53917842,  0.57673773,  0.58828537,
        0.66998901,  0.59595175,  0.47201045,  0.49435845,  0.51901652,
        0.64979033,  0.57636026,  0.45441478,  0.63831021,  0.59986428,
        0.55471681,  0.53187362,  0.57442244,  0.55676441,  0.50019451,
        0.5406876 ,  0.49544702,  0.66289887,  0.52338694,  0.56847346,
        0.63566399,  0.57966903,  0.68662384,  0.51273448,  0.55432217,
        0.59347925,  0.61673358,  0.59801775,  0.54349234,  0.47466528,
        0.78941431,  0.51524419,  0.61593043,  0.64365618,  0.52328694,
        0.51403783,  0.73210131,  0.6581762 ,  0.64398609,  0.65818564,
        0.6349168 ,  0.63813786,  0.66456267,  0.74823027,  0.6304077 ,
        0.51253505,  0.58105667,  0.53738808,  0.66116242,  0.75914031,
        0.94024105,  1.1025868 ,  1.22888756,  0.89572443,  0.98158117,
        1.14089644,  1.31850877,  0.8367748 ,  1.01323815,  0.80615838,
        0.67208378,  0.68279672,  0.67392533,  0.79997801,  0.74181371,
        0.74035251,  0.84971153,  0.67831439,  0.74590473,  1.10286677,
        1.41026825,  0.62520939,  0.61227471,  0.75060896,  0.82961185,
        0.57783706,  0.69869672,  1.14143681,  0.9951852 ,  0.96422477,
        0.7739442 ,  0.96004386,  0.71880725,  1.04579847,  1.17103934,
        1.10035671,  1.06223303,  0.81004846,  0.78294589,  0.80964811,
        0.64275545,  1.39694298,  0.67534382,  0.64263177,  0.61405834,
        0.7387326 ,  0.67705468,  0.77763183,  0.80669825,  0.67522979,
        0.75780169,  0.9142449 ,  1.09444595,  1.11781504,  1.22216935,
        1.01565475,  1.18254257,  0.69376461,  0.84600482,  1.01879743,
        0.55941731,  0.63876471,  0.6203956 ,  0.51454003,  1.15186796,
        1.36549496,  0.96956048,  0.46177306,  0.4882787 ,  0.4653367 ,
        0.48010857,  0.54554067,  0.4322615 ,  0.46791391,  0.45659595,
        0.50548615,  0.60073376,  0.51272593,  0.51378986,  0.52200452,
        0.49707917,  0.47183192,  0.56434031,  0.55460589,  0.54420789,
        0.43919784,  0.49228881,  0.55845358,  0.52084658,  0.53287045,
        0.51617528,  0.50610745,  0.47374555,  0.48005545,  0.54755477,
        0.5974667 ,  0.56131304,  0.47045873,  0.5424871 ,  0.41184534,
        1.19845513,  0.59963467,  0.44422653,  0.49481696,  0.40262953,
        0.43945702,  0.45175387,  0.50123642,  1.29624099,  1.2408311 ,
        0.81292087,  0.64353999,  0.63003577,  0.59438215,  0.69084728,
        0.66970059,  0.68635984,  1.0864691 ,  1.43707891,  0.97630706,
        1.12166048,  1.18125359,  1.46350438,  0.63420522,  0.68393878,
        0.52235967,  0.60418795,  1.22044167,  1.58374068,  0.73544324,
        0.97834901,  0.78518105,  0.93496047,  0.87345514,  0.67906195,
        0.73550297,  0.73336989,  1.28386362,  0.72790268,  0.67672424,
        1.05688212,  0.82976774,  0.83801192,  0.73701068,  0.74612178,
        0.90444974,  0.64256486,  0.58314164,  0.84868324,  0.8634234 ,
        0.75828155,  0.88359673,  0.75477142,  0.75373627,  0.68161559,
        0.80412929,  1.36726021,  0.60282219,  0.64784386,  0.73612734,
        0.82530609,  0.79216419,  0.68052941])

In [ ]:

Coordinates to gene expression:

Provide a list of coordinates and entrez_ids and the function will return matrix of coordinates by gene expression


In [6]:
rand = lambda null: np.random.uniform(-10,10,3).tolist()
coord_num = 20
coords = [rand(0) for i in range(coord_num)]

A.coords_to_ge(coords, entrez_ids=[1813,1816], search_radii=8)


Out[6]:
array([[ 4.46098545,  5.56290643],
       [ 6.10550948,  5.81598194],
       [ 5.49340693,  5.85582506],
       [ 6.16328081,  5.75113376],
       [ 4.59995825,  6.51853235],
       [ 5.54722688,  5.83684735],
       [ 5.95456259,  5.76134138],
       [ 3.75308604,  4.77731621],
       [ 5.62745438,  5.7942564 ],
       [ 3.88491976,  6.30668048],
       [ 4.30003925,  5.21318464],
       [ 4.65774472,  5.51680598],
       [ 5.83086712,  5.76739085],
       [ 4.64030036,  6.68383691],
       [ 5.64121372,  6.11691668],
       [ 4.50806372,  6.07487379],
       [ 4.84681105,  5.87540839],
       [ 3.54018647,  6.50701842],
       [ 3.86313015,  4.78370111],
       [ 5.73868657,  6.04158638]])

In [7]:
A.get_aba_ge([733,33,88])

In [8]:
A.get_ns_act("attention", thresh=-1, method='knn')

# You can use the sphere method too, if you want to weight by bucket. 
# e.g:
# A.get_ns_act("attention", thresh=.3, method='sphere')

A.make_ge_ns_mat('attention', [733, 33, 88])


This may take a few minutes...
Out[8]:
array([[  1.77019499e+00,   2.71416887e+00,   7.37862011e+00,
          1.69450157e-02],
       [  2.61296890e+00,   2.59225281e+00,   9.75253298e+00,
          4.37812120e-03],
       [  3.68978274e+00,   2.46659906e+00,   1.04777183e+01,
          8.46056433e-03],
       ..., 
       [  4.31693813e+00,   2.73499764e+00,   8.83434632e+00,
          0.00000000e+00],
       [  3.85346407e+00,   2.75270006e+00,   8.90296164e+00,
          2.18180238e-03],
       [  3.77839228e+00,   2.63289585e+00,   7.94519592e+00,
          0.00000000e+00]])

In [9]:
A.make_ge_ns_mat('attention', [733, 33, 88])


Out[9]:
array([[  1.77019499e+00,   2.71416887e+00,   7.37862011e+00,
          1.69450157e-02],
       [  2.61296890e+00,   2.59225281e+00,   9.75253298e+00,
          4.37812120e-03],
       [  3.68978274e+00,   2.46659906e+00,   1.04777183e+01,
          8.46056433e-03],
       ..., 
       [  4.31693813e+00,   2.73499764e+00,   8.83434632e+00,
          0.00000000e+00],
       [  3.85346407e+00,   2.75270006e+00,   8.90296164e+00,
          2.18180238e-03],
       [  3.77839228e+00,   2.63289585e+00,   7.94519592e+00,
          0.00000000e+00]])

In [ ]:

Visualization Methods (testing)


In [10]:
NV = NsabaVisualizer(A)

In [11]:
NV.visualize_ge([1813])



In [12]:
NV.visualize_ns('attention', alpha=.3)


This may take a minute or two ...

In [13]:
NV.lstsq_ns_ge('attention', [1813])


Out[13]:
(array([[ 1.       , -0.0882573],
        [-0.0882573,  1.       ]]),
 [-0.0013288032880511449, 0.015664542190155135])

In [17]:
NV.lstsq_ge_ge(1813, 1816);



In [25]:
NV.lstsq_ns_ns('attention', 'reward')


Term reward has not been initialized. Use self.no.get_ns_act(reward,thresh = -1)

In [ ]: