Daniel@0: :- module(dml_c3, Daniel@0: [ csv_op_chart/3 Daniel@0: , cla_op_chart/3 Daniel@0: , cla_label_lang/3 Daniel@0: , c3_bar/3, c3_bar/2 Daniel@0: , c3_plot/3 Daniel@0: , c3_steps/3 Daniel@0: , c3_csteps/3 Daniel@0: , c3_hist/5 Daniel@0: ]). Daniel@0: Daniel@0: :- use_module(library(computations)). Daniel@0: :- use_module(library(mlserver)). Daniel@0: :- use_rendering(c3). Daniel@0: Daniel@0: csv_op_chart(Op,Result,Chart) :- op_chart(Op,Result,Chart), !. Daniel@0: csv_op_chart(Op,X,unrecognised(Op,X)). Daniel@0: Daniel@0: op_chart(pitch_hist(_Weighting),Pairs,Ch) :- Daniel@0: unzip(Pairs,NoteNums,Counts), Daniel@0: maplist(computations:pitch_number_name,NoteNums,Pitches), Daniel@0: c3_bar(pitch-Pitches,counts-Counts,Ch1), Daniel@0: Ch=Ch1.put(axis/x/type,categorical). Daniel@0: Daniel@0: op_chart(freq_hist(Map,_),Counts,Ch) :- c3_hist(bars,pitch,Map,Counts,Ch). Daniel@0: op_chart(freq_hist_r(Map,_),Counts,Ch) :- c3_hist(bars,pitch,Map,Counts,Ch). Daniel@0: op_chart(tempo,Pairs,Ch) :- unzip(Pairs,T,X), c3_plot(time-T,tempo-X,Ch). Daniel@0: op_chart(uniform_tempo(_),T-X,Ch) :- c3_plot(time-T,tempo-X,Ch). Daniel@0: op_chart(uniform_tempo_r(_),T-X,Ch) :- c3_plot(time-T,tempo-X,Ch). Daniel@0: op_chart(uniform_tempo(_,_),T-X,Ch) :- c3_plot(time-T,tempo-X,Ch). Daniel@0: op_chart(uniform_tempo_r(_,_),T-X,Ch) :- c3_plot(time-T,tempo-X,Ch). Daniel@0: op_chart(tempo_hist_r(_DT,Map),_-Counts,Ch) :- c3_hist(steps,tempo,Map,Counts,Ch). Daniel@0: op_chart(tempo_hist(_DT,Map),_-Counts,Ch) :- c3_hist(steps,tempo,Map,Counts,Ch). Daniel@0: op_chart(normalised_tempo(N),_T-X,Ch) :- tempo_curve(N,X,Ch). Daniel@0: op_chart(normalised_tempo_r(N),_T-X,Ch) :- tempo_curve(N,X,Ch). Daniel@0: Daniel@0: cla_op_chart(Op,X,Chart) :- cla_chart(Op,X,Chart), !. Daniel@0: cla_op_chart(Op,X,unrecognised(Op,X)). Daniel@0: Daniel@0: cla_chart(Op,X,Chart) :- Daniel@0: is_dict(X), _{result:R} :< X, !, Daniel@0: cla_chart(Op,R,Chart). Daniel@0: Daniel@0: cla_chart(collection_pitch_histogram(_),X,Chart) :- Daniel@0: c3_bar(notenum-X.values,counts-X.counts,Chart). Daniel@0: Daniel@0: cla_chart(collection_freq_histogram(Min,Max,Quant,_),X,Chart) :- % matlab Daniel@0: c3_hist(bars,pitch,binmap(Min,Max,(Max-Min)*Quant+1),X.counts,Chart). Daniel@0: Daniel@0: cla_chart(collection_freq_histogram(_Lang,Min,Max,Quant,_),X,Chart) :- Daniel@0: c3_hist(bars,pitch,binmap(Min,Max,(Max-Min)*Quant+1),X.counts,Chart). Daniel@0: Daniel@0: cla_chart(collection_tempo_histogram(_,Min,Max,N),X,Chart) :- % matlab Daniel@0: c3_hist(steps,tempo,expmap(Min,Max,N),X.counts,Chart). Daniel@0: Daniel@0: cla_chart(collection_tempo_histogram(_Lang,_,Min,Max,N),X,Chart) :- Daniel@0: c3_hist(steps,tempo,expmap(Min,Max,N),X.counts,Chart). Daniel@0: Daniel@0: cla_chart(collection_tempo_curve(N),X,Chart) :- % matlab Daniel@0: tempo_curve(N,X.means,Chart). Daniel@0: Daniel@0: cla_chart(collection_tempo_curve(_Lang,N),X,Chart) :- Daniel@0: tempo_curve(N,X.means,Chart). Daniel@0: Daniel@0: cla_chart(py_hist(tagged(tonic), key_tonic_hist:aggregate, []),X,Chart) :- Daniel@0: c3_bar(key-X.values,counts-X.counts,Chart1), Daniel@0: Chart=Chart1.put(axis/x/type,categorical). Daniel@0: Daniel@0: cla_chart(py_hist(tagged(transcription), semitone_hist:aggregate, []),X,Chart) :- Daniel@0: c3_bar('pitch class'-X.values,counts-X.counts,Chart1), Daniel@0: Chart=Chart1.put(axis/x/type,categorical). Daniel@0: Daniel@0: cla_chart(tuning_stats, X, Chart) :- Daniel@0: Hist=X.stats.hist, Daniel@0: c3_hist(steps,frequency,edgemap(Hist.edges),Hist.counts,Chart). Daniel@0: Daniel@0: cla_chart(py_cla(tagged(transcription(1)), tuning_stats:per_file, []),X, Chart) :- Daniel@0: cla_chart(tuning_stats, X, Chart). Daniel@0: Daniel@0: Daniel@0: tempo_curve(N,X,Chart) :- Daniel@0: numlist(1,N,T), Daniel@0: c3_csteps(time-T,tempo-X,Chart). Daniel@0: Daniel@0: c3_hist(bars,XLabel,Map,Counts,Chart) :- Daniel@0: array_list(centres(Map),Centres1), Daniel@0: maplist(fix(2),Centres1,Centres), Daniel@0: c3_bar(XLabel-Centres,counts-Counts,Chart). Daniel@0: Daniel@0: c3_hist(steps,XLabel,Map,Counts,Chart) :- Daniel@0: array_list(edges(Map),Edges1), Daniel@0: maplist(fix(2),Edges1,Edges), Daniel@0: append(Counts,[0],C), Daniel@0: c3_steps(XLabel-Edges,counts-C,Chart). Daniel@0: Daniel@0: Daniel@0: fix(N,X,Y) :- Q is 10^N, Y is round(X*Q)/Q. Daniel@0: Daniel@0: :- public c3:put//2 Daniel@0: , c3:axis//2 Daniel@0: , c3:bar//2 Daniel@0: , c3:area//2 Daniel@0: , c3:zoom//1 Daniel@0: , c3:legend//1 Daniel@0: , c3:subchart//1 Daniel@0: , c3:put//2 Daniel@0: , c3:scat//2 Daniel@0: . Daniel@0: Daniel@0: c3:put(Path,Val,D1,D1.put(Path,Val)). Daniel@0: c3:axis(Name,Label,D1,D2) :- axis_pos(Name,Pos), D2=D1.put(axis/Name/label,_{text:Label,position:Pos}). Daniel@0: c3:axes(Lab1,Lab2) --> c3:axis(x,Lab1), c3:axis(y,Lab2). Daniel@0: c3:legend(F) --> c3:put(legend/show,F). Daniel@0: c3:subchart(F) --> c3:put(subchart/show,F). Daniel@0: c3:zoom(F) --> c3:put(zoom/enabled,F). Daniel@0: Daniel@0: c3:scat(XLabel,YLabel) --> c3:init(scatter,XLabel,YLabel). Daniel@0: c3:bar(XLabel,YLabel) --> c3:init(bar,XLabel,YLabel). Daniel@0: c3:area(XLabel,YLabel) --> c3:init(area,XLabel,YLabel). Daniel@0: Daniel@0: c3:init(Type,XLabel,YLabel) --> Daniel@0: c3:axes(XLabel,YLabel), Daniel@0: c3:put(data,_{columns:[],type:Type}), Daniel@0: c3:put(axis/x/tick/fit,false). Daniel@0: Daniel@0: axis_pos(x,'outer-center'). Daniel@0: axis_pos(y,'outer-middle'). Daniel@0: Daniel@0: c3_plot(XL-XV,YL-YV, c3{data:_{x:XL,columns:[[XL|XV],[YL|YV]]}}.axes(XL,YL).legend(false)). Daniel@0: c3_steps(XL-XV,YL-YV, c3{ line:_{step:_{type:'step-after'}}, Daniel@0: data:_{type:'area-step',x:XL, columns:[[XL|XV],[YL|YV]]} Daniel@0: }.axes(XL,YL).legend(false)). Daniel@0: c3_csteps(XL-XV,YL-YV, c3{data:_{type:step,x:XL,columns:[[XL|XV],[YL|YV]]}}.axes(XL,YL).legend(false)). Daniel@0: c3_bar(Label-Vals,c3{data:_{type:bar, columns:[[Label|Vals]]}}.axis(y,Label)). Daniel@0: c3_bar(XL-XV,YL-YV,c3{data:_{type:bar, x:XL, columns:[[XL|XV],[YL|YV]]}}.axes(XL,YL).legend(false)). Daniel@0: Daniel@0: Daniel@0: cla_label_lang(collection_pitch_histogram(_),pitch_histogram,pl). Daniel@0: cla_label_lang(collection_freq_histogram(_,_,_,_),freq_histogram,ml). Daniel@0: cla_label_lang(collection_freq_histogram(Lang,_,_,_,_),freq_histogram,Lang). Daniel@0: cla_label_lang(collection_tempo_histogram(_,_,_,_),tempo_histogram,ml). % matlab Daniel@0: cla_label_lang(collection_tempo_histogram(Lang,_,_,_,_),tempo_histogram,Lang). Daniel@0: cla_label_lang(collection_tempo_curve(_),tempo_curve,ml). % matlab Daniel@0: cla_label_lang(collection_tempo_curve(Lang,_),tempo_curve,Lang). Daniel@0: cla_label_lang(py_hist(tagged(tonic), key_tonic_hist:aggregate, []),tonic_histogram,py). Daniel@0: cla_label_lang(py_hist(tagged(transcription), semitone_hist:aggregate, _),pc_histogram,py). Daniel@0: cla_label_lang(tuning_stats, tuning_stats, py). Daniel@0: cla_label_lang(py_cla(tagged(transcription(1)), tuning_stats:per_file, _),tuning_stats, py). Daniel@0: cla_label_lang(py_cla(keys_chords,chord_seq_key_relative:aggregate,_),key_relative_chord_seq,py). Daniel@0: cla_label_lang(py_cla(similarity_bundle,similarity:per_file,_), similarity, py). Daniel@0: Daniel@0: