B ]!@sddlZddlmZddlZddlZGdddZGdddZ GdddZ Gdd d Z Gd d d Z Gd d d Z ee e gZe eZdS)Nc@seZdZdZdZddZdS)EmptyBarRateMetricCreatorzEmpty Bar Ratez4The ratio of empty bars to the total number of bars.cCs6t|jdkrtdtt|dkdtjdS)Nz'Input pianoroll must have 5 dimensions.g?))r)lenshape ValueErrornpmeananyastypefloat32)self pianorollr5/Users/mzliang/Downloads/Lab 2/utils/metrics_utils.pycomputesz!EmptyBarRateMetricCreator.computeN)__name__ __module__ __qualname__label descriptionrrrrrrsrc@seZdZdZdZddZdS)UniquePitchCountMetricCreatorzUnique Pitch Countz*The number of unique pitches used per bar.cCsFt|jdkrtdtt|dd}tjjt|j|ddS)Nrz'Input pianoroll must have 5 dimensions.r)rrr)axis) rrr r r sumlinalgnormones)rr pitch_histrrrr%sz%UniquePitchCountMetricCreator.computeN)rrrrrrrrrrr!src@s$eZdZdZdZddZddZdS)"UniquePitchClassCountMetricCreatorzUnique Pitch Class Countu Average number of notes in each bar after projecting to chroma space. Chroma features or Pitch Class Profiles are a distribution of the signal’s energy across a predefined set of pitch classes. c Cst|jdkrtd|jdd}|rFt|ddddd|fdf}t|d|jd|jd d|jddt|dk|jd f}t|d S) z,Return the chroma features (not normalized).rz'Input pianoroll must have 5 dimensions.r )rrrrr)rrr r padreshapeintr)rr remainderreshapedrrr _to_chroma3sz-UniquePitchClassCountMetricCreator._to_chromacCsPt|jdkrtd||}tt|dd}tjjt |j|ddS)Nrz'Input pianoroll must have 5 dimensions.r)rrr)r) rrr r)r r rrrr)rrchroma_pianorollrrrrrAs  z*UniquePitchClassCountMetricCreator.computeN)rrrrrr)rrrrrr +sr c@s&eZdZdZdZd ddZddZdS) PolyphonicRateMetricCreatorzPolyphonic Ratez The ratio of the number of time steps where the number of pitches being played is larger than `threshold` to the total number of time steps. rcCs ||_dS)N) threshold)rr,rrr__init__Psz$PolyphonicRateMetricCreator.__init__cCsFt|jdkrtdtt|d|jkd}t||jddS)Nrz'Input pianoroll must have 5 dimensions.rr)rr)rrr r count_nonzeror,r )rrn_polyrrrrSsz#PolyphonicRateMetricCreator.computeN)r)rrrrrr-rrrrrr+Js r+c@s.eZdZdZdZddZd ddZdd Zd S) InScaleRateMetricCreatorz In Scale Ratez The ratio of the average number of notes in a bar, which are in C major key which is the most common key found in music to the total number of notes. c Cst|jdkrtd|jdd}|rFt|ddddd|fdf}t|d|jd|jd d|jddt|dk|jd f}t|d S) z,Return the chroma features (not normalized).rz'Input pianoroll must have 5 dimensions.rr!)rrrr"rrr#)rrr r r$r%r&r)rrr'r(rrrr)`sz#InScaleRateMetricCreator._to_chromarcCsHtddddddddddddg ggt}tt|| ddtjS)z@Return a scale mask for the given key. Default to C major scale.rrrr")r arraybool expand_dimsrollr r)rkey a_scale_maskrrr _scale_maskns(z$InScaleRateMetricCreator._scale_maskcCsLt|jdkrtd||}t|t|dd}|t|dS)Nrz'Input pianoroll must have 5 dimensions.r)rrr)rrrr)rrr r)r rr7)rrr*Zin_scalerrrrss  z InScaleRateMetricCreator.computeN)r)rrrrrr)r7rrrrrr0Ys  r0c@s<eZdZddZddZddZddZd d Zd d Zd S)MusicQualityMetricsManagercCs||_|dS)N)metrics_creators initialize)rr9rrrr-}sz#MusicQualityMetricsManager.__init__cCs*i|_x|jD]}dgd|j|<qWdS)N) reference per_iteration)metricsr9)rZmetrics_creatorrrrr:s z%MusicQualityMetricsManager.initializecCs,|dddddddddddfS)N)r"rr#l)r%)rrrrr_reshape_pianorollsz-MusicQualityMetricsManager._reshape_pianorollcCsD||}x4|jD]&}||}|j|d||fqWdS)Nr<)rBr=keysrappend)rr iterationreshaped_pianorollmetric_creatormetricrrrappend_metrics_for_iterations  z7MusicQualityMetricsManager.append_metrics_for_iterationcCs:||}x*|jD]}||}||j|d<qWdS)Nr;)rBr=rCr)rrrFrGrHrrrset_reference_metricss  z0MusicQualityMetricsManager.set_reference_metricsc sd}tttjt|j|ddd\}}|tdxt |D]҉xt |jD]\}}||j dddd ||j dd dd |j|}||j d d |d Dt t|d |ddddd||jdd |d Dfdd |d Dddq\WqLWx2t |D]&d}|dj|ddq,Wx4t |jD]&\}}|j}||dj|ddqbWtdS)Nr#T)<)sharexfigsizelogbothmajorrL)rwhich labelsizeminorcSsg|] }|dqS)rr).0xrrr sz;MusicQualityMetricsManager.plot_metrics..r<r;r gffffff?) linewidthalphacSsg|] }|dqS)rr)rUrVrrrrWscsg|]}|dqS)rr)rUrV)instrument_idxrrrWs)rZzIterations (Instrument {})r()xlabelfontsizer)ylabelr_)pltionsnssetsubplotsrr= tight_layoutxscalerange enumerate tick_paramsplotr rscatterformat set_xlabelr set_ylabelshow)rZnum_instrumentsfigaxsZ metric_idxrGZ metric_datarr)r\r plot_metricss8     z'MusicQualityMetricsManager.plot_metricsN) rrrr-r:rBrIrJrsrrrrr8{s r8)numpyr matplotlib.pyplotpyplotrapickleseabornrcrrr r+r0r8ZDEFAULT_METRICS_CREATORSZmetrics_managerrrrrs   "G