o
    Uݢg                     @   s   d Z ddlZddlZddlmZmZmZ dZ	dZ
dZdZdZd	Zd
ZdZddiZdejjfddZdd Zdd ZdejejB ejB ejB defddZdS )z0Utility functions for dealing with altair plots.    N)is_bool_dtypeis_datetime64_any_dtypeis_numeric_dtypelayerhconcatvconcatdatanameformatdatasetsparsetypecsvdtypec                 C   s,   t | rdS t| rdS t| rdS td)zConvert pandas dtype to vega-lite dtype.

    Vega-lite CSV parse
    dtypes are - https://vega.github.io/vega-lite-api/api/csv.html#parse .
    booleandatenumberzExpected code to be unreachable)r   r   r   
ValueError)r    r   f/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/altair_utils.pyget_vega_dtype   s   r   c                 C   s   t | v r| t  D ]}t|| qt| v r | t D ]}t|| qt| v r0| t D ]}t|| q(t| v rXt| t vr>td| t t }|| | t t< | t t  d7  < dS dS )zcSanitise data names in chart dict.

    Uses name_to_format_dict to get column types to parse.
    z:Found nameless data in Altair JSON. Not expected VEGA out..csvN)LAYERsanitise_data_namesHCONCATVCONCATDATA	DATA_NAMEr   DATA_FORMAT)
chart_dictname_to_format_dictZ
layer_dictZhconcat_dictZvconcat_dictZold_data_namer   r   r   r   &   s    r   c                 C   s   t | t  }i }|D ]8}t ||< tj| t | }|jdd}dd |j	
 D || t< | d}|| t |< | t | qt| | dS )zClean up the chart dict.F)indexc                 S   s2   i | ]\}}t |st|st|r|t|qS r   )r   r   r   r   ).0xyr   r   r   
<dictcomp>G   s    z'sanitise_chart_dict.<locals>.<dictcomp>r   N)listDATASETS_KEYkeysDATA_FORMAT_CSV_DICTcopypd	DataFrame	from_dictto_csvdtypesitemsCSV_PARSE_KEYpopr   )r   Zraw_dataset_namesr    Zraw_datasetdf
csv_stringZnew_dataset_namer   r   r   sanitise_chart_dict=   s   
r5   chartreturnc                 C   s   |   }t| |S )a  Convert an altair chart to a Dict.

    We store data in CSV format rather than as a JSON. This avoids having to
    write out field names for all rows and usually saves us a lot in
    storage.

    Args:
        chart (alt.Chart | alt.LayerChart | alt.HConcatChart | alt.VConcatChart):
            Altair Chart of various flavours.

    Returns:
        dict: Dict corresponding to the chart.
    )to_dictr5   )r6   r   r   r   r   chart_to_jsonR   s   r9   )__doc__altairaltpandasr+   pandas.api.typesr   r   r   r   r   r   r   r   r   r'   r1   r)   r,   r/   r   r   r5   Chart
LayerChartHConcatChartVConcatChartdictr9   r   r   r   r   <module>   s*   