cmap 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/cmap.gemspec +30 -0
- data/lib/cmap/graph_to_sql.rb +48 -0
- data/lib/cmap/propositions_to_graph.rb +28 -0
- data/lib/cmap/sql_runner.rb +21 -0
- data/lib/cmap/version.rb +3 -0
- data/lib/cmap.rb +14 -0
- data/pictures/.DS_Store +0 -0
- data/pictures/cmap_example.png +0 -0
- data/pictures/db_ending.png +0 -0
- data/pictures/db_starting.png +0 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e708efa17e0c09e3dc56930c3a9e954af117ab1
|
4
|
+
data.tar.gz: 1e34a84f38dfdbc420c22600ccaaaea0893ba475
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 711553c586c488f6d58434cec963fcd55d0d2d5e37af4a08448ca7b69966feb377c46719b016d23fbe2d442cf7a5d5d7ea55bb5aa393edd924237f0cb4b0a56f
|
7
|
+
data.tar.gz: 723e351eb971b4f0ca369e218e71c5d53fe7f42dae8509c82461f8ebfabb5a7b9ca0231277abff16b9bfe59af7474b7128bc31e29da5d0d0dc78a8875d1b2eeb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 MrPowers
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Cmap
|
2
|
+
|
3
|
+
The cmap gem converts the 'Propositions to Text' export file into a directed graph data structure. The graph can then be converted into a series of postgres SQL queries to generate profiles.
|
4
|
+
|
5
|
+
Suppose we start with the following directed graph:
|
6
|
+
|
7
|
+
![](https://github.com/MrPowers/cmap/blob/master/pictures/cmap_example.png)
|
8
|
+
|
9
|
+
And the following starting database state:
|
10
|
+
|
11
|
+
![](https://github.com/MrPowers/cmap/blob/master/pictures/db_starting.png)
|
12
|
+
|
13
|
+
cmaps can be exported as a tab delimited text file of the graph edges with the "Propositions to Text" option. The `Cmap::PropositionsToGraph` is meant to be instantiated with the path to one of these "Propositions to Text" exports.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
propositions_to_graph = Cmap::PropositionsToGraph.new("/path/to/propositions_to_text_file")
|
17
|
+
propositions_to_graph.graph # a DirectedGraph::Graph object
|
18
|
+
```
|
19
|
+
|
20
|
+
The `GraphToSql` class is instantiated with the `DirectedGraph::Graph` object and converts the graph to an array of SQL queries:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
propositions_to_graph = Cmap::PropositionsToGraph.new("/path/to/propositions_to_text_file")
|
24
|
+
today = "'2015-01-01'"
|
25
|
+
gsubs = [["90D", "created_at > DATE(#{today}) - interval '90' day"]]
|
26
|
+
to_sql = GraphToSql.new("human_lab_data", propositions_to_graph.graph, gsubs)
|
27
|
+
|
28
|
+
to_sql.run_queries # returns the following array
|
29
|
+
|
30
|
+
[
|
31
|
+
"alter table human_lab_data add column seniors int2; update human_lab_data set seniors = 1 where (age > 65);",
|
32
|
+
"alter table human_lab_data add column american int2; update human_lab_data set american = 1 where (nationality = 'american');",
|
33
|
+
"alter table human_lab_data add column senior_americans int2; update human_lab_data set senior_americans = 1 where (seniors = 1 and american = 1);",
|
34
|
+
"alter table human_lab_data add column recently_created_american int2; update human_lab_data set recently_created_american = 1 where (nationality = 'american' and created_at > DATE('2015-01-01') - interval '90' day);",
|
35
|
+
"alter table human_lab_data add column kids int2; update human_lab_data set kids = 1 where (age < 12);",
|
36
|
+
"alter table human_lab_data add column kid_recently_created_american int2; update human_lab_data set kid_recently_created_american = 1 where (kids=1 AND recently_created_american =1);"
|
37
|
+
]
|
38
|
+
```
|
39
|
+
|
40
|
+
The `SqlRunner` class can be used to actually run the queries and generate the profiles in the database.
|
41
|
+
|
42
|
+
![](https://github.com/MrPowers/cmap/blob/master/pictures/db_ending.png)
|
43
|
+
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
Add this line to your application's Gemfile:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
gem 'cmap'
|
50
|
+
```
|
51
|
+
|
52
|
+
And then require the gem in your project:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
require 'cmap'
|
56
|
+
```
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MrPowers/cmap.
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
66
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cmap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/cmap.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cmap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cmap"
|
8
|
+
spec.version = Cmap::VERSION
|
9
|
+
spec.authors = ["MrPowers"]
|
10
|
+
spec.email = ["matthewkevinpowers@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Converts cmap exports to SQL}
|
13
|
+
spec.description = %q{Converts cmap exports that follow strict conventions into postgreSQL for data analysis}
|
14
|
+
spec.homepage = "https://github.com/MrPowers/cmap"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
|
26
|
+
spec.add_dependency "pg", "0.18.3"
|
27
|
+
spec.add_dependency "pry"
|
28
|
+
spec.add_dependency "simple-graph"
|
29
|
+
spec.add_dependency "directed_graph"
|
30
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Cmap; class GraphToSql
|
2
|
+
|
3
|
+
attr_reader :table_name, :graph, :query_gsubs, :column_gsubs
|
4
|
+
|
5
|
+
def initialize(table_name, graph, query_gsubs = [], column_gsubs = [])
|
6
|
+
@table_name = table_name
|
7
|
+
@graph = graph
|
8
|
+
@query_gsubs = query_gsubs
|
9
|
+
@column_gsubs = column_gsubs
|
10
|
+
end
|
11
|
+
|
12
|
+
def queries
|
13
|
+
edges.inject([]) do |memo, edge|
|
14
|
+
memo.push(edge_to_query(edge))
|
15
|
+
memo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def edge_to_query(edge)
|
22
|
+
c = column(edge)
|
23
|
+
q = query(edge)
|
24
|
+
"alter table #{table_name} add column #{c} int2; update #{table_name} set #{c} = 1 where (#{q});"
|
25
|
+
end
|
26
|
+
|
27
|
+
def column(edge)
|
28
|
+
c = edge.destination_vertex
|
29
|
+
column_gsubs.each do |gsub|
|
30
|
+
c = c.gsub(*gsub)
|
31
|
+
end
|
32
|
+
c
|
33
|
+
end
|
34
|
+
|
35
|
+
def query(edge)
|
36
|
+
q = edge.value
|
37
|
+
query_gsubs.each do |gsub|
|
38
|
+
q = q.gsub(*gsub)
|
39
|
+
end
|
40
|
+
q
|
41
|
+
end
|
42
|
+
|
43
|
+
def edges
|
44
|
+
graph.ordered_edges.uniq {|e| [e.destination_vertex, e.value]}
|
45
|
+
end
|
46
|
+
|
47
|
+
end; end
|
48
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Cmap; class PropositionsToGraph
|
2
|
+
|
3
|
+
attr_reader :propositions_path
|
4
|
+
|
5
|
+
def initialize(propositions_path)
|
6
|
+
@propositions_path = propositions_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def graph
|
10
|
+
DirectedGraph::Graph.new(edges)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def propositions
|
16
|
+
csv_path = File.expand_path(propositions_path, File.dirname(__FILE__))
|
17
|
+
CSV.read(csv_path, { :col_sep => "\t", :quote_char => '"' })
|
18
|
+
end
|
19
|
+
|
20
|
+
def edges
|
21
|
+
@edges ||= propositions.inject([]) do |memo, e|
|
22
|
+
origin_vertex, value, destination_vertex = e
|
23
|
+
memo << DirectedGraph::Edge.new(origin_vertex, destination_vertex, value)
|
24
|
+
memo
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end; end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Cmap; class SqlRunner
|
2
|
+
|
3
|
+
attr_reader :db_config
|
4
|
+
|
5
|
+
def initialize(db_config)
|
6
|
+
@db_config = db_config
|
7
|
+
end
|
8
|
+
|
9
|
+
def run_queries(queries)
|
10
|
+
queries.each do |q|
|
11
|
+
connection.exec(q)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def connection
|
16
|
+
@connection ||= PGconn.connect(db_config)
|
17
|
+
end
|
18
|
+
|
19
|
+
end; end
|
20
|
+
|
21
|
+
|
data/lib/cmap/version.rb
ADDED
data/lib/cmap.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "cmap/version"
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'pg'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
require 'directed_graph'
|
8
|
+
|
9
|
+
require_relative "./cmap/propositions_to_graph.rb"
|
10
|
+
require_relative "./cmap/graph_to_sql.rb"
|
11
|
+
require_relative "./cmap/sql_runner.rb"
|
12
|
+
|
13
|
+
module Cmap
|
14
|
+
end
|
data/pictures/.DS_Store
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MrPowers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.18.3
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.18.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simple-graph
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: directed_graph
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Converts cmap exports that follow strict conventions into postgreSQL
|
112
|
+
for data analysis
|
113
|
+
email:
|
114
|
+
- matthewkevinpowers@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- cmap.gemspec
|
129
|
+
- lib/cmap.rb
|
130
|
+
- lib/cmap/graph_to_sql.rb
|
131
|
+
- lib/cmap/propositions_to_graph.rb
|
132
|
+
- lib/cmap/sql_runner.rb
|
133
|
+
- lib/cmap/version.rb
|
134
|
+
- pictures/.DS_Store
|
135
|
+
- pictures/cmap_example.png
|
136
|
+
- pictures/db_ending.png
|
137
|
+
- pictures/db_starting.png
|
138
|
+
homepage: https://github.com/MrPowers/cmap
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.4.5
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Converts cmap exports to SQL
|
162
|
+
test_files: []
|