annotate dml-cla/python/places_hist.py @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 # Part of DML (Digital Music Laboratory)
Daniel@0 2 # Copyright 2014-2015 Daniel Wolff, City University
Daniel@0 3
Daniel@0 4 # This program is free software; you can redistribute it and/or
Daniel@0 5 # modify it under the terms of the GNU General Public License
Daniel@0 6 # as published by the Free Software Foundation; either version 2
Daniel@0 7 # of the License, or (at your option) any later version.
Daniel@0 8 #
Daniel@0 9 # This program is distributed in the hope that it will be useful,
Daniel@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 # GNU General Public License for more details.
Daniel@0 13 #
Daniel@0 14 # You should have received a copy of the GNU General Public
Daniel@0 15 # License along with this library; if not, write to the Free Software
Daniel@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17
Daniel@0 18 # -*- coding: utf-8 -*-
Daniel@0 19 __author__='wolffd'
Daniel@0 20
Daniel@0 21
Daniel@0 22 from rdflib import RDF, RDFS
Daniel@0 23 from csvutils import *
Daniel@0 24 from aggregate import *
Daniel@0 25 from n3Parser import get_rdf_graph_from_n3
Daniel@0 26 import numpy
Daniel@0 27 from scipy.spatial import distance
Daniel@0 28 from collections import defaultdict
Daniel@0 29
Daniel@0 30
Daniel@0 31
Daniel@0 32 def per_file(inputs,opts={}):
Daniel@0 33 places = []
Daniel@0 34 lists = []
Daniel@0 35
Daniel@0 36 #print_status(str(inputs))
Daniel@0 37 # simtype = opts['sim_type']
Daniel@0 38
Daniel@0 39 def accum(item):
Daniel@0 40
Daniel@0 41 # add uri if everything went well
Daniel@0 42 places.append(item['place'])
Daniel@0 43 lists.append(item['list'])
Daniel@0 44
Daniel@0 45 # accumulation
Daniel@0 46 st=for_each(inputs,accum)
Daniel@0 47
Daniel@0 48 # get the histogram
Daniel@0 49 (histo,index) = histogram(places)
Daniel@0 50
Daniel@0 51 # get the songs for each place
Daniel@0 52 list = []
Daniel@0 53 for row in index.values():
Daniel@0 54 list += [lists[i] for i in row]
Daniel@0 55
Daniel@0 56 return { 'result': { 'hist': histo }, #AK requested this removed, 'lists': list},
Daniel@0 57 'stats' : st }
Daniel@0 58
Daniel@0 59
Daniel@0 60
Daniel@0 61 # histogram the returns revers index as well
Daniel@0 62 def histogram(strin = []):
Daniel@0 63 # build histogram
Daniel@0 64 histo = dict()
Daniel@0 65 index = defaultdict(list)
Daniel@0 66 for num, row in enumerate(strin):
Daniel@0 67 histo[row] = histo.get(row, 0) + 1
Daniel@0 68 index[row] += [num]
Daniel@0 69
Daniel@0 70 # return most frequent key
Daniel@0 71 return ({'counts':histo.values(), 'places':histo.keys()}, index)