<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <h2>Goal
Step1:  Start with MTBLS315, the malaria vs fever dataset. Could get ~0.85 AUC for whole dataset.
Step2:  Almost everything is below 30sec rt-window
Step3:  <h2> Show me the distribution of features from alzheimers dataset </h2>
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.colors as colors
%matplotlib inline
Explanation: <h2>Goal:</h2>
Write functions to subdivide an m/z : rt space into rt bins. See how this affects classification performance
End of explanation
# Get the data
### Subdivide the data into a feature table
local_path = '/home/irockafe/Dropbox (MIT)/Alm_Lab/projects/'
data_path = local_path + '/revo_healthcare/data/processed/MTBLS315/'\
'uhplc_pos/xcms_camera_results.csv'
## Import the data and remove extraneous columns
df = pd.read_csv(data_path, index_col=0)
df.shape
df.head()
# Show me a distribution of retention time widths
rt_width = df['rtmax']-df['rtmin']
#sns.violinplot(rt_width, inner='box')
#sns.rugplot(rt_width)
rt_width.shape
sns.kdeplot(rt_width, kernel='gau')
plt.ylabel('Probability')
plt.xlabel('retention-width')
plt.title('Distribution of retention-time widths')
# Show me a distribution of intensity values
intensities = df['X1001_P'] 
normalized_intensities = df['X1001_P'].div(df['X1001_P'].max())
#sns.kdeplot(df['X1001_P'])
sns.kdeplot(intensities)
plt.xlabel("Normalized Intensity")
plt.ylabel('probability \(not correct, but w/e\)')
plt.title('Distribution of intensities')
Explanation: Start with MTBLS315, the malaria vs fever dataset. Could get ~0.85 AUC for whole dataset.
End of explanation
# Show me a scatterplot of m/z rt dots
# distribution along mass-axis and rt axist
plt.scatter(df['mz'], df['rt'], s=normalized_intensities*100,)
plt.xlabel('mz')
plt.ylabel('rt')
plt.title('mz vs. rt')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
def plot_mz_rt(df, path, rt_bounds):
    # the random data
    x = df['rt']
    y = df['mz']
    print np.max(x)
    print np.max(y)
    nullfmt = NullFormatter()         # no labels
    # definitions for the axes
    left, width = 0.1, 0.65
    bottom, height = 0.1, 0.65
    bottom_h = left_h = left + width + 0.02
    rect_scatter = [left, bottom, width, height]
    rect_histx = [left, bottom_h, width, 0.2]
    rect_histy = [left_h, bottom, 0.2, height]
    # start with a rectangular Figure
    #fig = plt.figure(1, figsize=(8, 8))
    fig = plt.figure(1, figsize=(10,10))
    
    axScatter = plt.axes(rect_scatter)
    axHistx = plt.axes(rect_histx)
    axHisty = plt.axes(rect_histy)
    # no labels
    axHistx.xaxis.set_major_formatter(nullfmt)
    axHisty.yaxis.set_major_formatter(nullfmt)
    # the scatter plot:
    axScatter.scatter(x, y, s=1)
    # now determine nice limits by hand:
    binwidth = 0.25
    #xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y))])
    #lim = (int(xymax/binwidth) + 1) * binwidth
    x_min = np.min(x)-50
    x_max = np.max(x)+50
    axScatter.set_xlim(x_min, x_max )
    y_min = np.min(y)-50
    y_max = np.max(y)+50
    axScatter.set_ylim(y_min, y_max)
    # Add vertical red line between 750-1050 retention time
    '''
    plt.plot([0,1], [0,1], linestyle = '--', lw=2, color='r',
                        label='Luck', alpha=0.5)
    '''
    print 'ymin: ', y_min
    # Add vertical/horizontal lines to scatter and histograms
    axScatter.axvline(x=rt_bounds[0], lw=2, color='r', alpha=0.5)
    axScatter.axvline(x=rt_bounds[1], lw=2, color='r', alpha=0.5)
    #axHistx.axvline(x=rt_bounds[0], lw=2, color='r', alpha=0.5)
    #axHistx.axvline(x=rt_bounds[1], lw=2, color='r', alpha=0.5)
    #bins = np.arange(-lim, lim + binwidth, binwidth)
    bins = 100
    axHistx.hist(x, bins=bins)
    axHisty.hist(y, bins=bins, orientation='horizontal')
    axHistx.set_xlim(axScatter.get_xlim())
    axHisty.set_ylim(axScatter.get_ylim())
    axScatter.set_ylabel('m/z', fontsize=30)
    axScatter.set_xlabel('Retention Time', fontsize=30)
    axHistx.set_ylabel('# of Features', fontsize=20)
    axHisty.set_xlabel('# of Features', fontsize=20)
        
    plt.savefig(path, 
                format='pdf')
    plt.show()
my_path = '/home/irockafe/Dropbox (MIT)/'\
    'Alm_Lab/projects/revo_healthcare/'\
    'presentations/eric_bose/poop.pdf'
plot_mz_rt(df, my_path, (750, 1050))
print 'Maximum retention time', x_max
# divide feature table into slices of retention time
def get_rt_slice(df, rt_bounds):
    '''
    PURPOSE: 
        Given a tidy feature table with 'mz' and 'rt' column headers, 
        retain only the features whose rt is between rt_left 
        and rt_right
    INPUT: 
        df - a tidy pandas dataframe with 'mz' and 'rt' column 
            headers
        rt_left, rt_right: the boundaries of your rt_slice, in seconds
    '''
    out_df = df.loc[ (df['rt'] > rt_bounds[0]) & 
                    (df['rt'] < rt_bounds[1])]
    return out_df
def sliding_window_rt(df, rt_width, step=rt_width*0.25):
    # get range of values [(0, rt_width)
    #get_rt_slice(df, )
    rt_min = np.min(df['rt'])
    rt_max = np.max(df['rt'])
    # define the ranges 
    left_bound = np.arange(rt_min, rt_max, step)
    right_bound = left_bound + rt_width
    rt_bounds = zip(left_bound, right_bound)
    for rt_slice in rt_bounds:
        rt_window = get_rt_slice(df, rt_slice)
        #print rt_window.head()
        print 'shape', rt_window.shape
        raise hee
        # TODO Send to ml pipeline here? Or separate function?
print type(np.float64(3.5137499999999999))
a = get_rt_slice(df, (750, 1050))
print 'Original dataframe shape: ', df.shape
print '\n Shape:', a.shape, '\n\n\n\n'
print df
sliding_window_rt(df, 100)
# Convert selected slice to feature table, X, get labels y
Explanation: Almost everything is below 30sec rt-window
End of explanation
### Subdivide the data into a feature table
local_path = '/home/irockafe/Dropbox (MIT)/Alm_Lab/'\
'projects'
data_path = local_path + '/revo_healthcare/data/processed/MTBLS72/positive_mode/'\
'mtbls_no_retcor_bw2.csv'
## Import the data and remove extraneous columns
df = pd.read_csv(data_path, index_col=0)
df.shape
df.head()
# Make a new index of mz:rt
mz = df.loc[:,"mz"].astype('str')
rt = df.loc[:,"rt"].astype('str')
idx = mz+':'+rt
df.index = idx
df.head()
a = get_rt_slice(df, (0,100))
print df.shape
print a.shape
my_path = '/home/irockafe/Dropbox (MIT)/'\
    'Alm_Lab/projects/revo_healthcare/'\
    'presentations/eric_bose/alzheimers_mz_rt_scatter.pdf'
print np.max(df['rt'])
plot_mz_rt(df, my_path, (550, 670))
# Make chromatography gradient images
fig = plt.figure(1, figsize=(8,8))
    
#axScatter = plt.axes()
plt.plot([0,1500], [0, 1], lw=4, label='Normal',
        alpha=0.4)
#plt.xlim([0, 30])
#plt.ylim([0,1])
plt.ylabel("% Hydrophobic Solvent", fontsize=20)
plt.xlabel('Time', fontsize=20)
plt.plot([650, 750, 1050], [750.0/1500, 750.0/1500, 1050.0/1500], 
         lw=4, color='r', label='Faster')
plt.xlim([0, 1400])
plt.ylim([0,1])
plt.ylabel("% Hydrophobic Solvent", fontsize=20)
plt.xlabel('Retention Time', fontsize=20)
plt.legend(fontsize=12)
plt.axvline(x=750, lw=2, color='r', alpha=0.3)
plt.axvline(x=1060, lw=2, color='r',alpha=0.3)
plt.savefig('/home/irockafe/Dropbox (MIT)/Alm_Lab/projects/revo_healthcare/presentations/eric_bose/gradient_example.pdf')
plt.show()
# Make chromatography gradient images
fig = plt.figure()
    
#axScatter = plt.axes()
Explanation: <h2> Show me the distribution of features from alzheimers dataset </h2>
End of explanation
<END_TASK>