Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 Daniel Wolff, City University Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: # -*- coding: utf-8 -*- Daniel@0: __author__='wolffd' Daniel@0: Daniel@0: Daniel@0: from rdflib import RDF, RDFS Daniel@0: from csvutils import * Daniel@0: from aggregate import * Daniel@0: from n3Parser import get_rdf_graph_from_n3 Daniel@0: import numpy Daniel@0: from scipy.spatial import distance Daniel@0: from collections import defaultdict Daniel@0: Daniel@0: Daniel@0: Daniel@0: def per_file(inputs,opts={}): Daniel@0: places = [] Daniel@0: lists = [] Daniel@0: Daniel@0: #print_status(str(inputs)) Daniel@0: # simtype = opts['sim_type'] Daniel@0: Daniel@0: def accum(item): Daniel@0: Daniel@0: # add uri if everything went well Daniel@0: places.append(item['place']) Daniel@0: lists.append(item['list']) Daniel@0: Daniel@0: # accumulation Daniel@0: st=for_each(inputs,accum) Daniel@0: Daniel@0: # get the histogram Daniel@0: (histo,index) = histogram(places) Daniel@0: Daniel@0: # get the songs for each place Daniel@0: list = [] Daniel@0: for row in index.values(): Daniel@0: list += [lists[i] for i in row] Daniel@0: Daniel@0: return { 'result': { 'hist': histo }, #AK requested this removed, 'lists': list}, Daniel@0: 'stats' : st } Daniel@0: Daniel@0: Daniel@0: Daniel@0: # histogram the returns revers index as well Daniel@0: def histogram(strin = []): Daniel@0: # build histogram Daniel@0: histo = dict() Daniel@0: index = defaultdict(list) Daniel@0: for num, row in enumerate(strin): Daniel@0: histo[row] = histo.get(row, 0) + 1 Daniel@0: index[row] += [num] Daniel@0: Daniel@0: # return most frequent key Daniel@0: return ({'counts':histo.values(), 'places':histo.keys()}, index)