-
Notifications
You must be signed in to change notification settings - Fork 1
/
calma_tempo.R
148 lines (124 loc) · 4.71 KB
/
calma_tempo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
library("RCurl")
library("SPARQL")
library("rrdf")
library("ggplot2")
setwd(tempdir())
#following line: see http://www.bramschoenmakers.nl/en/node/726
options( java.parameters = "-Xmx3g" )
endpoint = "http://etree.linkedmusic.org/sparql"
etreeTrackQuery <- "
PREFIX etree:<http://etree.linkedmusic.org/vocab/>
PREFIX mo:<http://purl.org/ontology/mo/>
PREFIX event:<http://purl.org/NET/c4dm/event.owl#>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX timeline:<http://purl.org/NET/c4dm/timeline.owl#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX calma:<http://calma.linkedmusic.org/vocab/>
SELECT ?track ?calma
{
BIND(<http://etree.linkedmusic.org/artist/422feb50-4aac-012f-19e9-00254bd44c28> as ?artist) .
?artist mo:performed ?performance .
?performance event:hasSubEvent ?track .
?track calma:data ?calma ;
skos:prefLabel \"Happier\" .
}
ORDER BY ?calma
LIMIT 50
"
whatFeaturesQuery <- "
prefix prov: <http://www.w3.org/ns/prov#>
select distinct ?feature where {
?file prov:wasAssociatedWith ?feature
}
ORDER BY ?feature"
calmaFeatureQuery <- "
PREFIX af: <http://purl.org/ontology/af/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX event: <http://purl.org/NET/c4dm/event.owl#>
PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>
select ?event ?feature where {
?file a mo:AudioFile .
?event a af:Tempo ;
af:feature ?feature .
}
ORDER BY ?file"
graphify <- function(files, remote=TRUE) {
if(remote) {
files <- gsub('<|>', '', files) # get rid of uri decoration if any
data <- gsub('\\"', '"', getURL(files)) # un-escape quote marks
files = gsub("http://calma.linkedmusic.org/data/", "", files)
files = gsub("/", "--", files)
}
else { # got sent a local directory
files <- paste0(files, "/", list.files(files))
}
g = new.rdf()
for (f in 1:length(files)) {
if(remote){ # need to write to our local tmp folder
track <- files[f]
track <- regmatches(track, gregexpr(pattern = "track_[^/]*", track))[[1]]
cat(x=data[f], file=files[f], append=FALSE)
}
thisg = load.rdf(filename = files[f], format="TURTLE")
g = combine.rdf(g, load.rdf(filename = files[f], format="TURTLE"))
}
return(g)
}
untarBlobs <- function(blobURIs) {
tmp = tempfile(tmpdir = tempdir())
blobFiles <- basename(blobURIs)
for (f in 1:length(blobFiles)) {
download.file(blobURIs[f], blobFiles[f])
untar(blobFiles[f], compressed = 'gzip', exdir = tmp)
file = gsub(".tar.gz", "", blobFiles[f])
file = gsub(".tar.bz2", "", file)
file <- paste0(tmp, "/", file)
}
files <- paste0(tmp, "/", list.files(tmp))
print("---------------------")
for (f in 1:length(files)) {
contextualized <- gsub("<#>", paste0("<", files[f], "#>"), readLines(files[f]))
cat(x = contextualized, file=files[f], append = FALSE)
}
return(tmp)
}
queryFeatureFiles <- function(g, feature) {
calmaFileQuery <- paste0("
prefix prov: <http://www.w3.org/ns/prov#>
select distinct ?file where {
?file prov:wasAssociatedWith <", feature, ">
}
ORDER BY ?file")
files <- sparql.rdf(g, calmaFileQuery)
return (files)
}
result <- SPARQL(endpoint, etreeTrackQuery)$results
# chop off the <'s and >'s
calma <- substr(result$calma, 2, nchar(result$calma))
calma <- substr(calma, 1, nchar(calma)-1)
# add trailing /
calma <- paste0(calma, "/")
files <- paste0(calma, "analyses.ttl")
etreeCalma <- cbind(result, files)
g <- graphify(files)
# What features are available for these files?
features <- sparql.rdf(g, whatFeaturesQuery)
featurefiles <- queryFeatureFiles(g, features[18])
g <- graphify(featurefiles)
etreeCalma <- cbind(etreeCalma, featurefiles)
names(etreeCalma) <- c("etree", "calma", "analysis.ttl", "feature")
etreeCalma$etree <- factor(etreeCalma$etree)
etreeCalma$calma<- factor(etreeCalma$calma)
query = "select distinct ?blob where { ?s <http://calma.linkedmusic.org/vocab/feature_blob> ?blob }"
blobFileURIs <- sparql.rdf(g, query)
blobFileDir <- untarBlobs(blobFileURIs)
featureGraph <- graphify(blobFileDir, remote=FALSE)
summarize.rdf(featureGraph)
featureData <- as.data.frame(sparql.rdf(featureGraph, calmaFeatureQuery))
featureData$track <- factor((gsub(".*/([^/#]+)#.*", "\\1", as.character(featureData$event),fixed=FALSE, perl=TRUE)))
featureData$eventNum <- as.numeric(gsub(".*event_(\\d+)", "\\1", featureData$event,fixed=FALSE, perl=TRUE))
featureData$feature <- as.numeric(as.character(featureData$feature))
featureData <- unique(featureData[order(featureData$track, featureData$eventNum),])
ggplot(featureData, aes(track, feature)) + geom_boxplot() + theme_bw() +
theme(axis.text.x = element_text(angle=90))
#ggplot(featureData, aes(eventNum, feature)) + geom_line(aes(color=track)) + facet_wrap(~track) + theme_bw()