Ejemplo n.º 1
0
def get_data():
    global RES, INPUT_RES, OUTPUT_RES

    m_x = []
    m_y = []

    for object_index in range(120):
        for series_index in range(24):
            series = []
            for i in range(3):
                path = f"coil-20/obj{object_index // 6 + 1}__{series_index * 3 + i}.png" if object_index % 6 == 0 else \
                    f"coil-100/obj{object_index - (object_index + 1) // 6 + 1}__{(series_index * 3 + i) * 5}.png"
                image = resample(cv.imread(path, cv.IMREAD_GRAYSCALE), INPUT_RES / RES, 0).astype('float32') / 255
                series.append(image)
                if i == 1:
                    image = resample(cv.imread(path, cv.IMREAD_GRAYSCALE), OUTPUT_RES / RES, 0).astype('float32') / 255
                    m_y.append(image)
            m_x.append(series)

    m_x_train, m_x_test, m_y_train, m_y_test = train_test_split(m_x, m_y, test_size=0.15, shuffle=False)
    m_x_train = np.array(m_x_train)
    m_y_train = np.array(m_y_train)
    m_x_test = np.array(m_x_test)
    m_y_test = np.array(m_y_test)

    m_y_train = m_y_train.reshape((m_y_train.shape[0], OUTPUT_RES * OUTPUT_RES))
    m_y_test = m_y_test.reshape((m_y_test.shape[0], OUTPUT_RES * OUTPUT_RES))

    m_x = np.array(m_x)
    m_y = np.array(m_y)
    m_y = m_y.reshape((m_y.shape[0], OUTPUT_RES * OUTPUT_RES))

    return m_x, m_y, m_x_train, m_x_test, m_y_train, m_y_test
Ejemplo n.º 2
0
feat_df = feat_df[~feat_df.index.duplicated(keep='last')]
print("Pre-Processing Runtime: " + str(time.time() - start_time)) # Loop Runtime

# =============================================================================
#                                 RESAMPLING
# =============================================================================

target_hz = 40
overlap = 2
window_size = target_hz * 2
hop = int(window_size/overlap)
sep = 'Sensus OS' # 'Device ID' or 'Sensus OS'
datetime_range = [['2018-9-13 17:31:35','2018-9-13 17:48:35'],['2018-9-14 14:34:32','2018-9-14 15:49:19'],['2018-9-14 19:27:19','2018-9-14 19:57:48']] # enter precise data collection datetime ranges

start_time = time.time()
resamp_df, keys = re.resample(feat_df,target_hz,sep,datetime_range)
print("Resampling Runtime: " + str(time.time() - start_time)) # Loop Runtime

    
# =============================================================================
#                             FEATURE EXTRACTION
# =============================================================================


target_path = '/Users/sm7gc/Desktop/WASH/Featurized/09-14-18/OS&Participant' if sep == 'Sensus OS' else '/Users/sm7gc/Desktop/WASH/Featurized/09-14-18/Device'
shutil.rmtree(target_path)
os.makedirs(target_path)

features = {} # dictionary for calculated features

for k in range(len(keys)):
if __name__ == "__main__":
    
    import read_segy as rsegy
    import resampling as rs

    segy = _read_segy('data/seis/CX_ZJ_ori.SGY', headonly=True)
#    time_start = 900.0
#    time_stop = 1500.0
#    time_step = 2.0 # ms
#    inline = 3662
#    xline = 1938
    trace = rsegy.extract_seismic(segy, line=3662, xline=1938, 
                time_start = 810, time_stop= 1500, time_step=2, name='amp')
    trace_res = rs.resample(trace.time, trace.amp, 
                                    time_start_re = 820,
                                    time_stop_re = 1400,
                                    time_step_re = 1,
                                    kind="quadratic", plot=False, 
                                    logname='amp')
    cc,ff = spectrum_cwt(trace_res.time, trace_res.amp/10000, 
                    colorscale=1, widths=100, wavelet='morl', freqmax=100)
    freqs,amplitude = spectrum_fft_filter(trace_res.time, trace_res.amp/10000, 
                        fft_size = 2000, 
                    freq_set=True, freq_min=0, freq_max=100, 
                    withfilter=False, filter_value=60, filter_slop=10,  
                    btype='low', analog=False, output='ba')

    plt.figure(figsize=(3,8))
    plt.plot(trace_res.amp/10000, trace_res.time, 'k') #'steelblue'

    plt.xlabel(u"Amplitude")
    plt.ylabel(u"Time(ms)")
Ejemplo n.º 4
0
import faux
import resampling
import os
import numpy as np

# Pacientes id
id = 1
data_path = "C:/Users/Darwin/Documents/NSCLC-RADIOMICS-INTEROBSERVER1/interobs06/02-18-2019-CT-43086/01133/"
# Carpeta donde se almacenara los resultados
output_path = "C:/Users/Darwin/Documents/ResultadosNSCL/"

patient = faux.load_scan(data_path)

imgs_to_process = np.load(output_path + 'fullimages_{}.npy'.format(id))

print("Slice Thickness: %f" % patient[0].SliceThickness)
print("Pixel Spacing (row, col): (%f, %f) " %
      (patient[0].PixelSpacing[0], patient[0].PixelSpacing[1]))

print("Shape before resampling\t", imgs_to_process.shape)

imgs_after_resamp, spacing = resampling.resample(imgs_to_process, patient,
                                                 [1, 1, 1])

print("Shape after resampling\t", imgs_after_resamp.shape)

np.save(output_path + "imgs_after_resamp_%d.npy" % (id), imgs_after_resamp)
Ejemplo n.º 5
0
def IDBMIOT(S, A, k1, k2):
    #c, p1, p2, m, D <- S & A
    c = pd.Series.sort_values(S['Class'].value_counts())
    p1 = len(A.numeric)
    p2 = len(A.nominal)
    m = len(c)
    D = c

    #resampling, get orate(c)
    orates = resample(c, D, S)
    orate = orates.orate

    print("Oversampling rate: " + str(orate))

    S = orates.samples
    S = S.dropna()

    c = pd.Series.sort_values(S['Class'].value_counts())
    m = len(c)
    D = c
    minorityClasses = c.drop(c.index[m - 1])
    c = minorityClasses

    retransform_sample = {}

    #iterate on class
    for c1 in c.keys():
        #instance of class c1
        xs = S[S['Class'] == c1]
        class_sample_instance = len(xs)

        samples = chooseSample(S, c1, k1, k2)
        _sample = samples
        """#_samples computation
        #_sample = pd.get_dummies(samples, columns=["Class"], prefix=["class"])

        #mean
        #us = xs.mean(axis=0)

        
         #eclipse curve
        ec = 0
        it = 0 
        for i in class_sample_instance:
            ec = ec + math.pow((xs.iloc(it) - us),2) 
            it = it + 1
        
        os = math.sqrt( (1/class_sample_instance) * ec)
        #normalization
        _sample = (_sample - us)/os

        #compute N & M 
        N = 1/class_sample_instance 
        adiag = []
        g = 0
        while g < len(A.numeric):
            adiag.append(1)
            g = g + 1
        h = 0 
        while h < (m * len(A.nominal)):
            k=0
            while k<len(c.keys()):
                v = _sample.iloc[len(A.numeric)+k]
                adiag.append(2/len(v)) 
                k = k + 1
            h = h +1
        M = np.diag(adiag) """

        #get features and label
        X = _sample.drop('Class', axis=1)
        y = _sample['Class']

        #Standardise
        x_std = StandardScaler().fit_transform(X)

        #coefficent of vector varriance
        features = x_std.T
        covariance_matrix = np.cov(features)
        V = covariance_matrix

        #convert samples into Principle component
        pca = PCA(n_components=2)
        principalComponents = pca.fit_transform(x_std)
        principalDf = pd.DataFrame(
            data=principalComponents,
            columns=['principal component 1', 'principal component 2'])

        #principalDf['Class'] = _sample['Class']

        cf = _sample[['Class']]
        #finalDf = pd.append([principalDf, _sample[['Class']]], axis = 1)
        finalDf = principalDf.join(cf)

        newSampleInPC = pd.DataFrame()
        newSampleInOC = pd.DataFrame()

        #sample generation
        if (orate[c1] > 0):
            j = 0
            while j < orate[c1]:

                #random sample choose
                maxValue = finalDf[finalDf['Class'] == c1].max()[0]
                randomSample = finalDf[np.isclose(
                    finalDf['principal component 1'], maxValue)]

                #cal random sample
                rs = sum(random.sample(range(1, p1 + m + k2), p1 + m))
                randomSample['principal component 1'] = randomSample[
                    'principal component 1'] + rs
                newSampleInPC = newSampleInPC.append(randomSample)

                j = j + 1

        if not newSampleInPC.empty:

            ff = newSampleInPC.drop('Class', axis=1)
            # get new sample
            newSampleInOC = pca.inverse_transform(ff)

            # transform
            column_label = X.columns
            retransform_sample = pd.DataFrame(newSampleInOC,
                                              columns=column_label)
            retransform_sample['Class'] = c1

    #Addition of resamples into orginal sample
    """ if retransform_sample.empty is False:
        #print("No new sample were generated!")
        raise Exception('No new sample were generated!')
    else: """
    S.append(retransform_sample, ignore_index=True)

    #return samples
    return S