Skip to content

Commit

Permalink
MOre work on Virtual Bird Tour Experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Apr 7, 2013
1 parent 68e0043 commit 2bcde3f
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 90 deletions.
35 changes: 30 additions & 5 deletions app/assets/javascripts/angular/controllers/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@
$scope.bigScope.step = $scope.bigScope.step + 1;

// remove background image
if($scope.bigScope.results.steps === $scope.bigScope.step){
if($scope.bigScope.results.steps.length === $scope.bigScope.step){
angular.element(document.getElementById('page-wrapper')).css("background-image", '');
}
};
Expand Down Expand Up @@ -842,6 +842,7 @@
// record order
for(var exampleAnnotationIndex = 0;exampleAnnotationIndex < annotations.length;exampleAnnotationIndex++){
$scope.currentStepResults.annotationExampleOrder.push({
'index': exampleAnnotationIndex,
'step': $scope.step,
'annotationId': annotations[exampleAnnotationIndex].id,
'locationName': $scope.currentLocation.name,
Expand All @@ -859,12 +860,15 @@

$scope.addMediaUrlsToAnnotations(annotations);

$scope.addResponseCounts(annotations);

// random order
baw.shuffle(annotations);

// record order
for(var verifyAnnotationIndex = 0;verifyAnnotationIndex < annotations.length;verifyAnnotationIndex++){
$scope.currentStepResults.annotationVerifyOrder.push({
'index': verifyAnnotationIndex,
'step': $scope.step,
'annotationId': annotations[verifyAnnotationIndex].id,
'locationName': $scope.currentLocation.name,
Expand All @@ -888,6 +892,17 @@
});
};

$scope.addResponseCounts = function(annotations){
angular.forEach(annotations, function (value, key) {
/*
.filter(function (element, index, array) {
return element.type == ANNOTATION_TYPE_EXAMPLE && element.speciesCommonName == speciesCommonName;
}
$scope.annotatonResponseCounts['id']
*/
});
};

$scope.addMarkerClick = function (map, marker, content) {
google.maps.event.addListener(marker, 'click', function () {
$scope.showMarkerInfo(map, marker, content);
Expand Down Expand Up @@ -1029,7 +1044,7 @@

// change the background image
angular.element(document.getElementById('page-wrapper'))
.css("background-image", "url('/experiment_assets/bird_tour/" + toLocation.backgroundImageName + "')");
.css("background-image", "url('" + $scope.getImagePath(toLocation.backgroundImageName) + "')");

$scope.showMarkerInfo($scope.transitionMap, toDetails.marker, toDetails.content);

Expand All @@ -1041,6 +1056,10 @@
}
};

$scope.getImagePath = function(imageFileName){
return '/experiment_assets/bird_tour/' + imageFileName;
};

//================
// perform set up
//=================
Expand Down Expand Up @@ -1068,6 +1087,9 @@
$scope.bigScope.results.locationSpeciesOrder = locationSpeciesOrder;
console.log('[Bird Tour Experiment] location and species order.', JSON.stringify(locationSpeciesOrder, undefined, 4));

// store annotation response counts
$scope.annotatonResponseCounts = $scope.bigScope.spec.additionalResources.annotationsResponseCounts;

//================
// now copy in the steps and configure the locations and species
//=================
Expand Down Expand Up @@ -1221,16 +1243,19 @@
toLatLng = new google.maps.LatLng(toLocation.lat, toLocation.long);
var toContent = String.format(
'<div><h1>{0}</h1>' +
'<div style="float:right;margin:00 5px 5px;"><img style="width:200px;" src="{8}"><a href="{10}" class="mapAttribution" target="_blank">Source: {9}</a></div>' +
'<p>{1}</p>' +
'<a href="{3}" class="mapAttribution">Source: {2}</a>' +
'<a href="{3}" class="mapAttribution" target="_blank">Source: {2}</a>' +
'<h2>{4}</h2>' +
'<p>{5}</p>' +
'<a href="{7}" class="mapAttribution">Source: {6}</a>' +
'<a href="{7}" class="mapAttribution" target="_blank">Source: {6}</a>' +
'</div>',
toLocation.name,
toLocation.locationDescription, toLocation.locationDescriptionAttribution, toLocation.locationDescriptionAttributionLink,
toLocation.environmentType,
toLocation.environmentDescription, toLocation.environmentDescriptionAttribution, toLocation.environmentDescriptionAttributionLink
toLocation.environmentDescription, toLocation.environmentDescriptionAttribution, toLocation.environmentDescriptionAttributionLink,
$scope.getImagePath(toLocation.backgroundImageName),
toLocation.backgroundImageAttribution, toLocation.backgroundImageAttributionLink
);
var toMarker = new google.maps.Marker({
position: toLatLng,
Expand Down
6 changes: 3 additions & 3 deletions app/assets/templates/tour_experiment.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ <h3 class="sectionTitle">Currently at {{currentLocation.name}}
<div class="sideInfo">
<div>
<h3 class="sectionTitle">Photograph</h3>
<a ng-href="{{currentSpecies.faunaImageAttributionLink}}"
<a ng-href="{{currentSpecies.faunaImageAttributionLink}}" target="_blank"
class="attribution">Source:
{{currentSpecies.faunaImageAttribution}}</a>
<img class="simpleImage" alt="Species image"
Expand All @@ -453,7 +453,7 @@ <h3 class="sectionTitle">Photograph</h3>
</div>
<div>
<h3 class="sectionTitle">Distribution</h3>
<a ng-href="{{currentSpecies.distributionImageAttributionLink}}"
<a ng-href="{{currentSpecies.distributionImageAttributionLink}}" target="_blank"
class="attribution">Source:
{{currentSpecies.distributionImageAttribution}}</a>
<img class="simpleImage" alt="Species distribution map"
Expand All @@ -468,7 +468,7 @@ <h2 class="sectionTitle" style="margin-top:8px;">{{currentSpecies.commonName}} <
class="extraInfo">{{currentSpecies.scientificName}}</em>
<em class="extraInfo backgroundExtraInfo">(Conservation Status:
{{currentSpecies.conservationStatus}})</em></h2>
<a ng-href="{{currentSpecies.descriptionAttributionLink}}"
<a ng-href="{{currentSpecies.descriptionAttributionLink}}" target="_blank"
class="attribution">Source:
{{currentSpecies.descriptionAttribution}}</a>

Expand Down
89 changes: 65 additions & 24 deletions app/controllers/experiments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def new
EXPERIMENTS_SAVE_DIRECTORY = BawSite::Application.config.custom_experiment_path
EXPERIMENTS_ASSETS_DIRECTORY = File.join(Rails.root, 'public', 'experiment_assets')
RAPID_SCAN_COUNTS = 'rapid_scan_counts.json'
BIRD_TOUR_COUNTS = 'bird_tour_counts.json'
ANNOTATION_RESPONSE_COUNTS = 'bird_tour_annotation_counts.json'

def create
success = nil
Expand All @@ -47,10 +49,16 @@ def create
success = true

if params[:experiment] && params[:experiment] == 'Virtual bird tour experiment'
if params[:code]
update_bird_tour_counts(params[:code])
if params[:location_species_order]
update_bird_tour_counts(params[:location_species_order])
else
raise "No code found in json packet for update_bird_tour_counts!"
raise "No code found in json for update_bird_tour_counts!"
end

if params[:steps]
bird_tour_annotation_counts(params[:steps])
else
raise "No code found in json for update_bird_tour_counts!"
end
end

Expand Down Expand Up @@ -113,38 +121,71 @@ def update_rapid_scan_counts(code)

end

def update_bird_tour_counts()
responses_store = {}
responses_file = File.join(EXPERIMENTS_SAVE_DIRECTORY, 'bird_tour_annotation_responses.json')
# updates counts for how many times the location and species orders have been completed.
def update_bird_tour_counts(order)
# open file
filename = File.join(EXPERIMENTS_ASSETS_DIRECTORY, BIRD_TOUR_COUNTS)
file_contents = File.open(filename, 'r') { |file| file.read }

# load the existing file (if the file exists)
if File.exists? responses_file
responses_store = JSON.parse(IO.read(responses_file))
end
# parse JSON
counts = JSON.parse(file_contents)

# modify JSON
# increment location order counts
location_order_code = order[:locations].join('').to_s

current = counts['locations'][location_order_code]['count']
current = current + 1
counts['locations'][location_order_code]['count'] = current

# increment species order counts
order[:species].each { |key, value|
species_key = key.to_s
species_value = value.join('')
if counts['species'].has_key?(species_key)
current = counts['species'][species_key][species_value]['count']
current = current + 1
counts['species'][species_key][species_value]['count'] += current
end
}

# write JSON
json_output = JSON.pretty_generate(counts)

File.open(filename, 'w') { |file| file.write json_output }
end

# updates counts for annotation responses - yes, no, unsure
def bird_tour_annotation_counts(steps)
# open file
filename = File.join(EXPERIMENTS_ASSETS_DIRECTORY, ANNOTATION_RESPONSE_COUNTS)
file_contents = File.open(filename, 'r') { |file| file.read }

# parse JSON
counts = JSON.parse(file_contents)

# modify JSON
# pull out the responses for each annotations, and increment the response counts
params[:steps].each do |step|
step[:responses].each do |annotationId, response|
unless responses_store.keys.any? { |key| key.include? annotationId }
steps.each do |step|
if step.has_key?(:responses)
# check that the step contains responses, transitions will not
step[:responses].each do |annotation_id, response|
new_annotation_id = annotation_id.gsub('response','')
unless counts.keys.any? { |key| key == new_annotation_id }
# add the annotation id to the store if it is not already in there
responses_store[annotationId] = { 'no' => 0, 'yes' => 0, 'unsure' => 0 }
counts[new_annotation_id] = { 'no' => 0, 'yes' => 0, 'unsure' => 0 }
end

# increment the response count for each response given: yes, no, or unsure.
responses_store[annotationId][response.to_s] += 1
counts[new_annotation_id][response.to_s] += 1
end
end
end

# overwrite the file

FileUtils.makedirs EXPERIMENTS_SAVE_DIRECTORY
File.open(responses_file, 'w') { |file|
file.write responses_store.to_json
}
end

def load_bird_tour_counts()
# write JSON
json_output = JSON.pretty_generate(counts)

File.open(filename, 'w') { |file| file.write json_output }
end

end
5 changes: 3 additions & 2 deletions public/experiment_assets/bird_tour.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"surveyLink": "nowhere-yet",
"experimentResultsPostUri": "/experiments",
"additionalResources" : {
"experiment_combination_counts": "/experiment_assets/bird_tour_counts.json"
"experiment_combination_counts": "/experiment_assets/bird_tour_counts.json",
"annotations_response_counts": "/experiment_assets/bird_tour_annotation_counts.json"
},
"experimentSteps": [
{
Expand Down Expand Up @@ -137,7 +138,7 @@
"distributionImageAttribution": "Birdlife International",
"distributionImageAttributionLink": "http://bie.ala.org.au/species/urn:lsid:biodiversity.org.au:afd.taxon:d3c27152-9e37-49df-837c-051262010644",
"description": "The Red-browed Pardalote (Pardalotus rubricatus) occupies the northern two-thirds of Australia. It is a fraction larger than the Forty-spotted Pardalote, at 10 to 12 cm, and is the least conspicuously coloured, being paler and combining the spotted skull-cap of the Spotted Pardalote with the striped wings of the Striated Pardalote. It is rare in the eastern part of its range but common in the northwest, where it prefers dry woodlands, mulga, and the trees along creek beds. The red-browed pardalote builds its nest underground at the end of a tunnel.",
"descriptionAttribution": "Wikipedia Article Contributors",
"descriptionAttribution": "Wikipedia",
"descriptionAttributionLink": "http://en.wikipedia.org/wiki/Red-browed_Pardalote",
"annotationIds":[]
},
Expand Down
52 changes: 52 additions & 0 deletions public/experiment_assets/bird_tour_annotation_counts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"response1021502": {
"no": 0,
"yes": 1,
"unsure": 0
},
"response1021503": {
"no": 1,
"yes": 0,
"unsure": 0
},
"response102150": {
"no": 0,
"yes": 0,
"unsure": 1
},
"response1021501": {
"no": 0,
"yes": 0,
"unsure": 1
},
"response1021504": {
"no": 1,
"yes": 0,
"unsure": 0
},
"1021502": {
"no": 2,
"yes": 0,
"unsure": 1
},
"1021501": {
"no": 2,
"yes": 1,
"unsure": 0
},
"102150": {
"no": 1,
"yes": 0,
"unsure": 2
},
"1021504": {
"no": 2,
"yes": 0,
"unsure": 1
},
"1021503": {
"no": 1,
"yes": 1,
"unsure": 1
}
}
Loading

0 comments on commit 2bcde3f

Please sign in to comment.