<jupyter_start><jupyter_text>Single Cell Export and Visualization<jupyter_code>def plot_cells(df, ncol=6, title=''):
    n = len(df)
    plt.figure()
    nrow = -(-n//ncol)
    fig, axs = plt.subplots(nrow, ncol)
    fig.suptitle(title)
    fig.set_size_inches(ncol * 6, nrow * 6)
    axs = axs.ravel()
    for i in range(len(axs)):
        axs[i].axis('off')
    for i in range(n):
        r = df.iloc[i]
        axs[i].imshow(r['image'])
        axs[i].set_title('N{} C{}'.format(r['nucleus_plasmid_count'], r['cytoplasm_plasmid_count']))

def disp():
    if not enable_single_cell_data:
        return None
    
    # Set display settings for overlay on single cell images
    thresh = exp_conf.cytometry_params['quantification_params']['spot_count_params']['threshold']
    ranges = {
        'proc_Plasmid': [thresh, 255],
        'proc_PHA': [0, 150],
        'cyto_nucleus_boundary': [0, 1],
        'cyto_cell_boundary': [0, 1]
    }
    colors = {
        'proc_DAPI': 'blue', 
        'proc_PHA': 'red', 
        'proc_Plasmid': 'green',
        'cyto_nucleus_boundary': 'cyan',
        'cyto_cell_boundary': 'magenta'
    }
    
    # Attach cell images to cytometry data frame
    dfsc = cytokit_data.get_single_cell_image_data(
        exp_output_dir, df, 'best_z_segm',
        ranges=ranges, colors=colors,
        image_size=(212, 212)
    )
    assert dfsc['image'].notnull().all()
    
    # Extract summary data 
    cols = [
        'id', 'rid', 'tile_x', 'tile_y', 'x', 'y', 'z', 
        'cell_diameter', 'nucleus_diameter',
        'cell_circularity', 'nucleus_circularity',
        'cs:Plasmid:nucleus_fraction', 
        'cs:Plasmid:nucleus_plasmid_count',
        'cs:Plasmid:cytoplasm_plasmid_count',
        'image'
    ]
    dfsc = dfsc[cols].rename(columns=lambda c: c.replace('cs:Plasmid:', ''))
    
    # Cherry-pick cells with ideal characteristics for visualization
    plot_cells(
        dfsc[(dfsc['nucleus_plasmid_count'] > 0) & (dfsc['cytoplasm_plasmid_count'] == 0)]\
        .sort_values('nucleus_plasmid_count').head(24),
        title='Cells w/ nucleus plasmid only:'
    )
    plot_cells(
        dfsc[(dfsc['cytoplasm_plasmid_count'] > 0) & (dfsc['nucleus_plasmid_count'] == 0)]\
        .sort_values('cytoplasm_plasmid_count').head(24),
        title='Cells w/ cytoplasm plasmid only:'
    )
    
    # Export to csv if export dir given
    if export_dir:
        dfsc['image'] = dfsc['image'].apply(encoding.base64_encode_image)
        print('Single cell data frame info:')
        print(dfsc.info())
        print('Exporting data frame to csv')
        path = osp.join(export_dir, 'data.csv')
        dfsc.to_csv(path, index=False)
    
disp()<jupyter_output>Single cell data frame info:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 72 entries, 0 to 71
Data columns (total 15 columns):
id                         72 non-null int64
rid                        72 non-null int64
tile_x                     72 non-null int64
tile_y                     72 non-null int64
x                          72 non-null float64
y                          72 non-null float64
z                          72 non-null int64
cell_diameter              72 non-null float64
nucleus_diameter           72 non-null float64
cell_circularity           72 non-null float64
nucleus_circularity        72 non-null float64
nucleus_fraction           72 non-null object
nucleus_plasmid_count      72 non-null int64
cytoplasm_plasmid_count    72 non-null int64
image                      72 non-null object
dtypes: float64(6), int64(7), object(2)
memory usage: 8.5+ KB
None
Exporting data frame to csv<jupyter_text>Individual Plasmid Analysis<jupyter_code>def from_csv(v):
    if pd.isnull(v) or (len(v) == 0):
        return []
    return [float(x) for x in v.split(',')]

dfs = []
for i, r in df.iterrows():
    nuc_frac = np.array(from_csv(r['cs:Plasmid:nucleus_fraction']))
    assert np.all(nuc_frac <= 1)
    assert np.all(nuc_frac >= 0)
    for i in range(len(nuc_frac)):
        dfs.append((r['rid'], nuc_frac[i]))
dfs = pd.DataFrame(dfs, columns=['rid', 'nuc_frac'])
dfs.head()
if enable_graphics:
    dfs['nuc_frac'].hist(bins=16)
def get_threshold_curve(dfs):
    df = []
    for thresh in np.arange(.1, 1., .05):
        p = (dfs['nuc_frac'] > thresh).mean()
        df.append((100*thresh, 100*p))
    return pd.DataFrame(df, columns=['threshold', 'p'])
    
dfsfrac = get_threshold_curve(dfs)
pm.record('nucleus_fraction_threshold', dfsfrac.set_index('threshold')['p'].to_json())

def disp():
    if not enable_graphics:
        return None
    return (
        pn.ggplot(dfsfrac, pn.aes(x='threshold', y='p')) + 
        pn.geom_line() + 
        pn.ylim(0, 100) + 
        pn.xlab('Nucleus Fraction Threshold (%)') + 
        pn.ylab('Plasmids in Nucleus (%)') +
        pn.ggtitle(exp_name)
    )
disp()
cts = (dfs['nuc_frac'] > nucleus_plasmid_threshold).value_counts()
pm.record('plasmids_in_nucleus:count', cts.to_json())
cts
cts = (dfs['nuc_frac'] > nucleus_plasmid_threshold).value_counts(normalize=True)
pm.record('plasmids_in_nucleus:frac', cts.to_json())
cts
desc = df['nucleus_diameter'].describe()
pm.record('nucleus_diameter', desc.to_json())
desc
desc = df['cell_diameter'].describe()
pm.record('cell_diameter', desc.to_json())
desc
desc = (df['nucleus_size'] / df['cell_size']).describe()
pm.record('nucleus_to_cell_ratio', desc.to_json())
desc
if enable_graphics:
    (df['nucleus_size'] / df['cell_size']).hist(bins=32)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1415591495547883, 'fraction_numerical': 0.02235144466654552, 'mean_word_length': 2.934238741958542, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '208290', 'license': 'Apache-2.0', 'n_tokens_mistral': 2161, 'n_tokens_neox': 1958, 'n_words': 389, 'path': 'notebooks/figure-3b_s2/20181005-d37-unsti-lab-60x-15x15.ipynb', 'repo_name': 'hammerlab/t-cell-electroporation', 'size': '5503'}