Tuesday 26 July 2016

@Gartner_inc Mentions Network - now mapping @Gartner_Events , @Barnes_Hank and @jakesorofman #python + #rstats







from tweepy import OAuthHandler
from pymongo import MongoClient
import json

#
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
#
#

def on_data(self, data):
    client = MongoClient()
    db = client.db_INC_25JULY
    #

for All_Tweets in tweepy.Cursor(api.user_timeline,screen_name,include_entities=True).items(3000):

    tweets_all = dict()
    tweets_all['id']= All_Tweets.id
    tweets_all['created_at'] = All_Tweets.created_at # OK _ JSON 
    tweets_all['text'] = All_Tweets.text # OK _ JSON 
    tweets_all['retweet_count'] = All_Tweets.retweet_count
    tweets_all['favorite_count'] = All_Tweets.favorite_count
    tweets_all['geo'] = All_Tweets.geo # OK _ JSON 
    tweets_all['coordinates'] = All_Tweets.coordinates  # OK _ JSON 
    tweets_all['iso_language_code']= All_Tweets.lang # OK _ JSON 
    tweets_all['source']= All_Tweets.source # OK _ JSON

# Related R Code AS BELOW ---------------


library("sand", lib.loc="~/R/win-library/3.1")
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## 
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## 
## The following object is masked from 'package:base':
## 
##     union
## 
## Loading required package: igraphdata
## 
## Statistical Analysis of Network Data with R
## Type in C2 (+ENTER) to start with Chapter 2.
library("rgl", lib.loc="~/R/win-library/3.1")
library("igraph", lib.loc="~/R/win-library/3.1")

g <- graph.formula(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 4-6,4-7, 5-6, 6-7)
g
## IGRAPH UN-- 7 10 -- 
## + attr: name (v/c)
## + edges (vertex names):
##  [1] 1--2 1--3 2--3 2--4 3--5 4--5 4--6 4--7 5--6 6--7
#
V(g) # # Vertex sequence: this differs from Github code for Book
## + 7/7 vertices, named:
## [1] 1 2 3 4 5 6 7
E(g) ## Edge sequence: this differs from Github code for Book
## + 10/10 edges (vertex names):
##  [1] 1--2 1--3 2--3 2--4 3--5 4--5 4--6 4--7 5--6 6--7
str(g)
## IGRAPH UN-- 7 10 -- 
## + attr: name (v/c)
## + edges (vertex names):
## 1 -- 2, 3
## 2 -- 1, 3, 4
## 3 -- 1, 2, 5
## 4 -- 2, 5, 6, 7
## 5 -- 3, 4, 6
## 6 -- 4, 5, 7
## 7 -- 4, 6
#
# -- Own Code Snippet Starts - Dhankar 
g1 <- graph.formula(1-5, 1-7, 2-9, 2-4, 3-5, 4-5, 4-6,4-7, 5-6, 6-7,7-8,8-9)
g1
## IGRAPH UN-- 9 12 -- 
## + attr: name (v/c)
## + edges (vertex names):
##  [1] 1--5 1--7 5--4 5--3 5--6 7--4 7--6 7--8 2--9 2--4 9--8 4--6
#
V(g1) # # Vertex sequence: - Own Code Snippet - Dhankar 
## + 9/9 vertices, named:
## [1] 1 5 7 2 9 4 3 6 8
E(g1) ## Edge sequence: Own Code Snippet - Dhankar 
## + 12/12 edges (vertex names):
##  [1] 1--5 1--7 5--4 5--3 5--6 7--4 7--6 7--8 2--9 2--4 9--8 4--6
str(g1)
## IGRAPH UN-- 9 12 -- 
## + attr: name (v/c)
## + edges (vertex names):
##  [1] 1--5 1--7 5--4 5--3 5--6 7--4 7--6 7--8 2--9 2--4 9--8 4--6
plot(g1)
# ?igraph
rglplot(g1)
## Not run: 
g2 <- make_lattice( c(5,5,5) )
coords <- layout_with_fr(g2, dim=3)
rglplot(g2, layout=coords)
str(g2)
## IGRAPH U--- 125 300 -- Lattice graph
## + attr: name (g/c), dimvector (g/n), nei (g/n), mutual (g/l),
## | circular (g/l)
## + edges:
##   1 --   2   6  26                 2 --   1   3   7  27        
##   3 --   2   4   8  28             4 --   3   5   9  29        
##   5 --   4  10  30                 6 --   1   7  11  31        
##   7 --   2   6   8  12  32         8 --   3   7   9  13  33    
##   9 --   4   8  10  14  34        10 --   5   9  15  35        
##  11 --   6  12  16  36            12 --   7  11  13  17  37    
##  13 --   8  12  14  18  38        14 --   9  13  15  19  39    
##  15 --  10  14  20  40            16 --  11  17  21  41        
##  17 --  12  16  18  22  42        18 --  13  17  19  23  43    
##  19 --  14  18  20  24  44        20 --  15  19  25  45        
##  21 --  16  22  46                22 --  17  21  23  47        
##  23 --  18  22  24  48            24 --  19  23  25  49        
##  25 --  20  24  50                26 --   1  27  31  51        
##  27 --   2  26  28  32  52        28 --   3  27  29  33  53    
##  29 --   4  28  30  34  54        30 --   5  29  35  55        
##  31 --   6  26  32  36  56        32 --   7  27  31  33  37  57
##  33 --   8  28  32  34  38  58    34 --   9  29  33  35  39  59
##  35 --  10  30  34  40  60        36 --  11  31  37  41  61    
##  37 --  12  32  36  38  42  62    38 --  13  33  37  39  43  63
##  39 --  14  34  38  40  44  64    40 --  15  35  39  45  65    
##  41 --  16  36  42  46  66        42 --  17  37  41  43  47  67
##  43 --  18  38  42  44  48  68    44 --  19  39  43  45  49  69
##  45 --  20  40  44  50  70        46 --  21  41  47  71        
##  47 --  22  42  46  48  72        48 --  23  43  47  49  73    
##  49 --  24  44  48  50  74        50 --  25  45  49  75        
##  51 --  26  52  56  76            52 --  27  51  53  57  77    
##  53 --  28  52  54  58  78        54 --  29  53  55  59  79    
##  55 --  30  54  60  80            56 --  31  51  57  61  81    
##  57 --  32  52  56  58  62  82    58 --  33  53  57  59  63  83
##  59 --  34  54  58  60  64  84    60 --  35  55  59  65  85    
##  61 --  36  56  62  66  86        62 --  37  57  61  63  67  87
##  63 --  38  58  62  64  68  88    64 --  39  59  63  65  69  89
##  65 --  40  60  64  70  90        66 --  41  61  67  71  91    
##  67 --  42  62  66  68  72  92    68 --  43  63  67  69  73  93
##  69 --  44  64  68  70  74  94    70 --  45  65  69  75  95    
##  71 --  46  66  72  96            72 --  47  67  71  73  97    
##  73 --  48  68  72  74  98        74 --  49  69  73  75  99    
##  75 --  50  70  74 100            76 --  51  77  81 101        
##  77 --  52  76  78  82 102        78 --  53  77  79  83 103    
##  79 --  54  78  80  84 104        80 --  55  79  85 105        
##  81 --  56  76  82  86 106        82 --  57  77  81  83  87 107
##  83 --  58  78  82  84  88 108    84 --  59  79  83  85  89 109
##  85 --  60  80  84  90 110        86 --  61  81  87  91 111    
##  87 --  62  82  86  88  92 112    88 --  63  83  87  89  93 113
##  89 --  64  84  88  90  94 114    90 --  65  85  89  95 115    
##  91 --  66  86  92  96 116        92 --  67  87  91  93  97 117
##  93 --  68  88  92  94  98 118    94 --  69  89  93  95  99 119
##  95 --  70  90  94 100 120        96 --  71  91  97 121        
##  97 --  72  92  96  98 122        98 --  73  93  97  99 123    
##  99 --  74  94  98 100 124       100 --  75  95  99 125        
## 101 --  76 102 106               102 --  77 101 103 107        
## 103 --  78 102 104 108           104 --  79 103 105 109        
## 105 --  80 104 110               106 --  81 101 107 111        
## 107 --  82 102 106 108 112       108 --  83 103 107 109 113    
## 109 --  84 104 108 110 114       110 --  85 105 109 115        
## 111 --  86 106 112 116           112 --  87 107 111 113 117    
## 113 --  88 108 112 114 118       114 --  89 109 113 115 119    
## 115 --  90 110 114 120           116 --  91 111 117 121        
## 117 --  92 112 116 118 122       118 --  93 113 117 119 123    
## 119 --  94 114 118 120 124       120 --  95 115 119 125        
## 121 --  96 116 122               122 --  97 117 121 123        
## 123 --  98 118 122 124           124 --  99 119 123 125        
## 125 -- 100 120 124
V(g2)$name <- c("Sam", "Mary", "Tom","3","4","5") # TBD this gives an error 
## Warning in vattrs[[name]][index] <- value: number of items to replace is
## not a multiple of replacement length
##
## Source - http://sna.stanford.edu/ - forked as is on 04 SEP 15 
##########################################################################
# You may cite these labs as follows: McFarland, Daniel, Solomon Messing,
# Mike Nowak, and Sean Westwood. 2010. "Social Network Analysis          
# Labs in R." Stanford University.                                       
##########################################################################

magact96 = read.delim("http://sna.stanford.edu/sna_R_labs/data/mag_act96.txt", na.strings = "na", check.names = FALSE)
magact97 = read.delim("http://sna.stanford.edu/sna_R_labs/data/mag_act97.txt", na.strings = "na", check.names = FALSE)
magact98 = read.delim("http://sna.stanford.edu/sna_R_labs/data/mag_act98.txt", na.strings = "na", check.names = FALSE)
# 
# Write CSV files of these data.frames for later refrence
#?write.table()
# write.table(magact96, file = "magact96.csv", sep = ",")
# write.table(magact97, file = "magact97.csv", sep = ",")
# write.table(magact98, file = "magact98.csv", sep = ",")

#
# (2) Create the attribute data. Attributes appear the same
# from 1996-1998
magattrib = magact96[,1:4]

# Drop columns to have a square incidence matrix for each year
g96 <- as.matrix(magact96[,-(1:4)]); row.names(g96) = magact96[,1]
g97 <- as.matrix(magact97[,-(1:4)]); row.names(g97) = magact97[,1]
g98 <- as.matrix(magact98[,-(1:4)]); row.names(g98) = magact98[,1]

# For future reference, you can access these via
# data(studentnets.magact96.97.98, package = "NetData")

# Now load in these two-mode graphs into igraph.
ig96 <- graph.incidence(g96, mode=c("all") )
ig97 <- graph.incidence(g97, mode=c("all") )
ig98 <- graph.incidence(g98, mode=c("all") )

# A "shorthand" to access edge or vertex (node) attributes works
# similarly to R's handeling of lists and dataframes (e.g.,
# 'dataframe$variablename' or'list$sublist$object').

# to be noted - Edge attributes and Vertex attributes can be both - discrete or continuous data types. 

V(ig96)$color[1:1294] = rgb(red = 1, green = 0, blue = 0, alpha = .5)
## Warning in vattrs[[name]][index] <- value: number of items to replace is
## not a multiple of replacement length
V(ig96)$color[1]
## [1] "#FF000080"
V(ig96)$color[2]
## [1] "#FF000080"
V(ig96)$color[120]
## [1] "#FF000080"
V(ig96)$color[1295:1385] = rgb(red = 0, green = 1, blue = 0, alpha = .5)
V(ig96)$color[1296]
## [1] "#00FF0080"
V(ig96)$color[1297]
## [1] "#00FF0080"
# as seen we have assigned - 2 different colors 
# "#00FF0080" - RGB value == #FF00 is RED as seen here - http://cloford.com/resources/colours/500col.htm

# Now we'll set some other graph attributes:
V(ig96)$label = V(ig96)$name
V(ig96)$label.color = rgb(0,0,.2,.5)
V(ig96)$label.cex = .4
V(ig96)$size = 6
V(ig96)$frame.color = V(ig96)$color

# You can also set edge attributes. Here we'll make the edges
# nearly transparent and slightly yellow because there will be so
# many edges in this graph:
E(ig96)$color = rgb(.5,.5,0,.2)

# Now, we'll open a pdf "device" on which to plot. This is just a
# connection to a pdf file. Note that the code below will take a
# minute or two to execute (or longer if you have a pre- Intel
# dual-core processor), because the graph is so large.
  
pdf("lab5-ig96.pdf")
plot(ig96, layout=layout.fruchterman.reingold)
dev.off()
## png 
##   2
pdf("lab5-ig96_kamada.pdf")
plot(ig96, layout=layout.kamada.kawai)
dev.off()
## png 
##   2
jpeg("lab5-ig96_kamada.jpg")
plot(ig96, layout=layout.kamada.kawai)
dev.off()
## png 
##   2
#
# # Directional Graph Code - TBD -- 
# dg <- graph.formula(Sam-+Mary, Sam-+Tom, Mary++Tom)
# str(dg)