示例#1
0
def keep(in_nii, keep_labels, keep_csv_filename, out_filename, merge=None):

    in_label_nii = labels.read_nifti_file(in_nii, 'Label file does not exist')
    in_label_array = in_label_nii.get_data()

    if len(keep_csv_filename):
        csv_keep_labels = labels.read_labels_from_csv(keep_csv_filename)
    else:
        csv_keep_labels = []

    all_labels = set(labels.get_labels(None, in_label_array))
    keep_labels = set(
        labels.get_labels(keep_labels + csv_keep_labels, in_label_array))
    remove_labels = sorted(list(all_labels.symmetric_difference(keep_labels)))

    out_label_array = in_label_array

    for ii in remove_labels:
        mask = in_label_array == ii
        out_label_array[mask] = 0

    if merge is not None and merge != 0:  # isinstance(map, (int, long, float)) and float(map) !=0:
        out_label_array = merge * (out_label_array > 0)

    nb.save(nb.Nifti1Image(out_label_array, None, in_label_nii.get_header()),
            out_filename)
示例#2
0
def load_data():
    dir = 'training2017CSV/'
    records = [f for f in listdir(dir) if isfile(join(dir, f)) if(f.find('.csv') != -1)]
    records.sort() 
    # print(records)
    y, f = lb.get_labels()
    # print(y)
    # print(f)
    # print(type(y))
    # records = ['A00001.csv','A00002.csv']
    # records = records[0:100]
    # print(records)
    x_train = np.array([])
    for r in records:
        test = pd.read_csv(dir + r, index_col=0,nrows=2714)
        # print(test)
        test = test.values.tolist()
        # print(test)
        # test = test[0:2714]
        # test = test[:,1]
        x = np.array([])
        for t in test:
            # print(t[1])
            x = np.append(x,t)
        # print(x)
        # x = x[0:2714]
        x_train = np.append(x_train,x)
        print(dir + r)

    x_train = x_train.reshape(-1, 2714)
    x_train = tf.keras.utils.normalize(x_train, axis = 1)
    # print(x_train)  
    return y,x_train
示例#3
0
def remove(in_nii,
           remove_labels,
           remove_csv_filename,
           out_filename,
           merge=None):

    in_label_nii = labels.read_nifti_file(in_nii, 'Label file does not exist')
    in_label_array = in_label_nii.get_data()

    if len(remove_csv_filename):
        csv_remove_labels = labels.read_labels_from_csv(remove_csv_filename)
    else:
        csv_remove_labels = []

    remove_labels = set(
        labels.get_labels(remove_labels + csv_remove_labels, in_label_array))

    out_label_array = in_label_array

    for ii in remove_labels:
        mask = in_label_array == ii
        out_label_array[mask] = 0

    if merge is not None and merge != 0:  # isinstance(map, (int, long, float)) and float(map) !=0:
        out_label_array = merge * (out_label_array > 0)

    nibabel.save(
        nibabel.Nifti1Image(out_label_array, None, in_label_nii.get_header()),
        out_filename)
示例#4
0
def build_model_for_corpus(corpus):
    """ Build an appropriate Keras NN model depending on the corpus """
    keras_model = None
    no_of_labels = -1

    if corpus == 'keywords':
        keras_model = berger_cnn(embedding_size=100, output_length=1000)
        no_of_labels = 1000
    elif corpus == 'categories':
        keras_model = berger_cnn(embedding_size=50, output_length=14)
        no_of_labels = 14

    model_path = os.path.join(DATA_DIR, corpus, 'model.pickle')
    keras_model.load_weights(model_path)

    w2v_path = os.path.join(DATA_DIR, corpus, 'word2vec.pickle')
    w2v_model = Word2Vec.load(w2v_path)

    scaler_path = os.path.join(DATA_DIR, corpus, 'scaler.pickle')
    scaler = load_from_disk(scaler_path)

    labels = get_labels(no_of_labels)

    model = MagpieModel(
        keras_model=keras_model,
        word2vec_model=w2v_model,
        scaler=scaler,
        labels=labels,
    )

    return model
示例#5
0
def train(train_dir, test_dir=None, nn='berger_cnn', nb_epochs=NB_EPOCHS,
          batch_size=BATCH_SIZE, verbose=1):
    # Figure out whether we're predicting categories or keywords
    if NO_OF_LABELS == 14:
        scaler_path = CATEGORY_SCALER
        w2v_path = CATEGORY_WORD2VEC
    else:
        scaler_path = KEYWORD_SCALER
        w2v_path = KEYWORD_WORD2VEC

    model = MagpieModel(
        word2vec_model=Word2Vec.load(w2v_path),
        scaler=load_from_disk(scaler_path),
    )

    logger = CustomLogger(nn)
    model_checkpoint = ModelCheckpoint(
        os.path.join(logger.log_dir, 'keras_model'),
        save_best_only=True,
    )

    history = model.train(
        train_dir,
        get_labels(NO_OF_LABELS),
        test_dir=test_dir,
        nn_model=nn,
        callbacks=[logger, model_checkpoint],
        batch_size=batch_size,
        nb_epochs=nb_epochs,
        verbose=verbose,
    )

    finish_logging(logger, history)

    return history, model
示例#6
0
def keep(in_nii, keep_labels, keep_csv_filename, out_filename):

    in_label_nii = labels.read_nifti_file(in_nii, 'Label file does not exist')
    in_label_array = in_label_nii.get_data()

    if len(keep_csv_filename):
        csv_keep_labels = labels.read_labels_from_csv(keep_csv_filename)
    else:
        csv_keep_labels = []

    all_labels = set(labels.get_labels(None, in_label_array))
    keep_labels = set(
        labels.get_labels(keep_labels + csv_keep_labels, in_label_array))
    remove_labels = sorted(list(all_labels.symmetric_difference(keep_labels)))

    out_label_array = in_label_array

    for ii in remove_labels:
        mask = in_label_array == ii
        out_label_array[mask] = 0

    nb.save(nb.Nifti1Image(out_label_array, None, in_label_nii.get_header()),
            out_filename)
def load_data():
    # dir = 'training2017CSV/'
    dir = 'test/'
    counter = 0
    # ==> Find all csv files and save them in records list:
    records = [
        f for f in listdir(dir) if isfile(join(dir, f))
        if (f.find('.csv') != -1)
    ]
    records.sort()
    # print(records)
    print(len(records))
    #  =========================== Create Labels ======================================================================
    y, f = lb.get_labels()
    print(y)
    print(f)
    #  ================================================================================================================
    # x = np.array([])h
    # =========================== Create train test =====================================================================================
    x_train = np.array([])
    y_train = np.array([])
    file = np.array([])
    for r in records:
        # print(r)
        test = pd.read_csv(dir + r)
        test = test.values.tolist()  # --> convert test dataframe to list
        # print(r)
        if len(test) == 9000:
            for char in '.csv':
                r = r.replace(char, '')
            # print(r)
            x = np.array([])
            for t in test:
                # print(t[1])
                x = np.append(x, t[1])
            # print(x)
            x_train = np.append(x_train, x)
            # print(x_train,counter)
            # TODO : add switch mode eg: N --> 0 etc
            y_train = np.append(y_train, y[f.index(r)])
            file = np.append(file, r)
            counter = counter + 1

    x_train = x_train.reshape(counter, 9000)
    for i in range(0, counter):
        print(file[i], y_train[i])

    return x_train, y_train
示例#8
0
def create_strings(imgurl):
    labels = get_labels(imgurl)
    face = faceDetect(imgurl)
    celebstr = celeb(imgurl)

    labelstr = "Objects detected in the image are:\n"
    facestr = ""

    for i in labels:
        labelstr += i['Name'] + " with confidence " + str(
            i['Confidence']) + ",\n"

    if face:
        facestr += "The attributes for faces found in the image are as follows: \n\n" + face[
            0] + ",\n\n"
        facestr += "Number of faces detected in the image are" + str(
            face[1]) + "\n\n"
    else:
        facestr += " \n\n"

    return [labelstr, facestr, celebstr]
def extract(in_filename,
            in_csv,
            in_extract_labels,
            out_filename,
            verbose=False,
            merge=None):

    # Read in label array

    in_nii = labels.read_nifti_file(in_filename, 'Label file does not exist')
    in_array = in_nii.get_data()

    if len(in_csv):
        csv_extract_labels = labels.read_labels_from_csv(in_csv)
    else:
        csv_extract_labels = []

#     print ( inArgs.extract.shape)
#     print ( csv_extract_labels.shape)

    requested_labels = in_extract_labels + csv_extract_labels

    extract_labels = labels.get_labels(requested_labels, in_array)

    if verbose:
        print('Requested labels for extraction not found',
              list(set(requested_labels) - set(extract_labels)))

    out_array = np.zeros(in_array.shape, dtype=np.int8)

    for ii in extract_labels:
        mask = in_array == ii
        out_array[mask] = in_array[mask]

    if merge is not None and merge != 0:  # isinstance(map, (int, long, float)) and float(map) !=0:
        out_array = merge * (out_array > 0)

    nb.save(nb.Nifti1Image(out_array, None, in_nii.get_header()), out_filename)

    return
示例#10
0
def batch_train(train_dir,
                test_dir=None,
                nn='berger_cnn',
                nb_epochs=NB_EPOCHS,
                batch_size=BATCH_SIZE,
                verbose=1):

    # Figure out whether we're predicting categories or keywords
    if NO_OF_LABELS == 14:
        scaler_path = CATEGORY_SCALER
        w2v_path = CATEGORY_WORD2VEC
    else:
        scaler_path = KEYWORD_SCALER
        w2v_path = KEYWORD_WORD2VEC

    model = MagpieModel(
        word2vec_model=Word2Vec.load(w2v_path),
        scaler=load_from_disk(scaler_path),
    )

    logger = CustomLogger(nn)
    model_checkpoint = ModelCheckpoint(
        os.path.join(logger.log_dir, 'keras_model'),
        save_best_only=True,
    )

    history = model.batch_train(
        train_dir,
        get_labels(NO_OF_LABELS),
        test_dir=test_dir,
        nn_model=nn,
        callbacks=[logger, model_checkpoint],
        batch_size=batch_size,
        nb_epochs=nb_epochs,
        verbose=verbose,
    )

    finish_logging(logger, history)

    return history, model
示例#11
0
def remove(in_nii, remove_labels, remove_csv_filename, out_filename):

    in_label_nii = labels.read_nifti_file(in_nii, 'Label file does not exist')
    in_label_array = in_label_nii.get_data()

    if len(remove_csv_filename):
        csv_remove_labels = labels.read_labels_from_csv(remove_csv_filename)
    else:
        csv_remove_labels = []

    remove_labels = set(
        labels.get_labels(remove_labels + csv_remove_labels, in_label_array))

    out_label_array = in_label_array

    for ii in remove_labels:
        mask = in_label_array == ii
        out_label_array[mask] = 0

    nibabel.save(
        nibabel.Nifti1Image(out_label_array, None, in_label_nii.get_header()),
        out_filename)
示例#12
0
def getCleanMarksDF(week_number):
    marks_WS = gspread.authorize(getCredentials()).open(
        f"Voti_Fantacalcio_Stagione_2018-19_Giornata_{week_number}").worksheet("Fantagazzetta")

    # GET ALL VALUES

    labels = lb.get_labels()
    x = marks_WS.get_all_values()[4:]
    marks_DF = pd.DataFrame(x, columns=labels)

    # STORE TEAMS THA HAS ALREADY PLAYED

    codeCol = marks_DF[lb.code]
    alreadyPlayedTeams = codeCol.loc[marks_DF[lb.code].isin(
        util.italian_teams)]
    alreadyPlayedTeams = alreadyPlayedTeams.values.tolist()

    # REMOVE USELESS COLUMNS

    marks_DF.pop(lb.code)
    marks_DF.pop(lb.gdv)
    marks_DF.pop(lb.gdp)

    # REMOVE USELESS ROWS

    roles = rl.get_roles()
    marks_DF = marks_DF.loc[marks_DF[lb.role].isin(roles)]
    marks_DF = marks_DF.loc[marks_DF[lb.mark] != '6*']

    # ARRANGE COLUMNS TYPES

    floatLabels = lb.get_float_labels()
    intLabels = lb.get_int_labels()

    marks_DF[floatLabels] = marks_DF[floatLabels].astype(float)
    marks_DF[intLabels] = marks_DF[intLabels].astype(int)

    # SUM THE TWO KINDS OF ASSISTS

    marks_DF[lb.ass] += marks_DF[lb.asf]  # TODO check
    marks_DF.pop(lb.asf)

    # CALCULATE GOAL BONUS

    marks_DF[lb.golBns] = 0.0
    marks_DF.loc[marks_DF[lb.gf] > 0, [lb.golBns]] = \
        marks_DF[lb.gf] * marks_DF[lb.role].apply(bn.get_gol_value) + \
        marks_DF[lb.gf].apply(bn.get_gol_bonus)

    # CALCULATE ASSIST BONUS

    marks_DF[lb.assBns] = 0.0
    marks_DF.loc[marks_DF[lb.ass] > 0, [lb.assBns]] = \
        marks_DF[lb.ass] * marks_DF[lb.role].apply(bn.get_ass_value) + \
        marks_DF[lb.ass].apply(bn.get_ass_bonus)

    # CALCULATE TOTAL BONUS

    marks_DF[lb.bonus] = getTotalBonus(marks_DF)

    return marks_DF
示例#13
0
        "https://www.googleapis.com/auth/drive"
    ])

gc = gspread.authorize(credentials)

# WORKSHEET VOTI

try:
    marksWS = gc.open("Voti_Fantacalcio_Stagione_2018-19_Giornata_").worksheet(
        "Fantagazzetta")
except SpreadsheetNotFound:
    input("SpreadsheetNotFound:")

# GET ALL VALUES

labels = lb.get_labels()
x = marksWS.get_all_values()[4:]
marksDF = pd.DataFrame(x, columns=labels)

# STORE TEAMS THA HAS ALREADY PLAYED

codeCol = marksDF[lb.code]
alreadyPlayedTeams = codeCol.loc[marksDF[lb.code].isin(util.italian_teams)]
alreadyPlayedTeams = alreadyPlayedTeams.values.tolist()

# REMOVE USELESS COLUMNS

marksDF.pop(lb.code)
marksDF.pop(lb.gdv)
marksDF.pop(lb.gdp)
示例#14
0
traces01 = readIn(lines01, '\t')
traces02 = readIn(lines02, '\t')

#make them into dataframe
df01 = sqlContext.createDataFrame(traces01)
df02 = sqlContext.createDataFrame(traces02)

#convert them to vectors
df_conv01 = convDf(df01)

#prepare for ml
df_prepped01 = prep(df_conv01)
df_prepped02 = df02.select("name").distinct()

#function to apply labels
df_labeled = get_labels(df_prepped01, df_prepped02)
df_labeled = df_labeled.na.drop().drop("version_idx")
cols_for_ml = df_prepped01.drop("name").drop("version_idx").schema.names

#pipline stages
#index the label
labelIndexer = mlf.StringIndexer(inputCol="Label", outputCol="Label_idx")
#vectorise the input
toVec = mlf.VectorAssembler(inputCols=cols_for_ml, outputCol="Features")
#classify
classifier = DecisionTreeClassifier(labelCol="Label_idx",
                                    featuresCol="Features",
                                    maxDepth=10,
                                    maxBins=200)

#create pipline of the stages and use it to train and test
     parser.add_argument("--debug",         help="Debug flag",      action="store_true", default=False )

     inArgs = parser.parse_args()


     if inArgs.debug:

         print("inArgs.in_nii   = " +  inArgs.in_nii + '\n')
         print("inArgs.debug     = " +  str(inArgs.debug))
         print("inArgs.verbose   = " +  str(inArgs.verbose))


     label_nii    = labels.read_nifti_file( inArgs.in_nii, 'Label file does not exist' )
     label_array  = label_nii.get_data()

     labels = labels.get_labels( inArgs.labels, label_array)

     df_stats  = pd.DataFrame(columns=('label', 'com_x', 'com_y', 'com_z', 
                                       'label_volume', 'ellipsoid_volume',
                                       'center_x', 'center_y', 'center_z',
                                       'r1', 'r2', 'r3', 'fa' ))
     if inArgs.stats is not None:
         stats_list = inArgs.stats
     else:
         stats_list = list(df_stats.columns.values)

     if inArgs.verbose:
         jj=0
         pd.set_option('expand_frame_repr', False)

     for ii in labels:
示例#16
0
def measure(label_nii_filename,
            image_nii_filename,
            requested_labels=[],
            verbose_flag=False,
            verbose_nlines=10,
            verbose_all_flag=False):

    # Load arrays

    label_nii = labels.read_nifti_file(label_nii_filename,
                                       'Label file does not exist')
    image_nii = labels.read_nifti_file(image_nii_filename,
                                       'Image file does not exist')

    # System Checks to verify that the Array Size and Dimensions are compatible

    image_array = image_nii.get_data()
    label_array = label_nii.get_data()

    if len(image_array.shape) < 2 or len(image_array.shape) > 4:
        sys.exit('Only supports 3D and 4D image arrays')

#    if not len(label_array.shape) == 3:
#        sys.exit('Only supports 3D label arrays')

    label_ndim = len(label_array.shape)
    image_ndim = len(image_array.shape)

    ndim = min([label_ndim, image_ndim])

    if not image_array.shape[0:ndim] == label_array.shape[0:ndim]:
        sys.exit(
            'Image array and label array do not have the same voxel dimensions'
        )

    # Find a set of acceptable labels

    label_list = labels.get_labels(requested_labels, label_array)

    # Permute array or expand so desired stats is along first dimension

    if image_ndim == 4:
        nVolumes = int(image_array.shape[3])
        image_array = numpy.transpose(image_array, [3, 0, 1, 2])

    elif image_ndim == 3:
        image_array = numpy.transpose(image_array, [2, 0, 1])

    else:
        nVolumes = 1
        image_array = numpy.expand_dims(image_array, axis=0)

    label_array = numpy.expand_dims(label_array, axis=0)

    # Gather stats

    if verbose_flag:
        ii_verbose = 0
        pandas.set_option('expand_frame_repr', False)

    df_stats = pandas.DataFrame(columns=('label', 'x_com', 'y_com', 'z_com',
                                         'time', 'mean', 'std', 'min', 'max'))

    nlabels = len(label_list)

    for ii, ii_label in enumerate(label_list):

        for jj in range(0, nVolumes):

            if label_ndim == 3:
                mask = label_array[0, :, :, :] == ii_label
            else:
                mask = label_array[0, :, :] == ii_label

            label_mean = numpy.mean(image_array[jj][mask])
            label_std = numpy.std(image_array[jj][mask])
            label_min = numpy.min(image_array[jj][mask])
            label_max = numpy.max(image_array[jj][mask])

            if label_ndim == 3:
                x_com, y_com, z_com = ndimage.measurements.center_of_mass(mask)
            else:
                z_com = 0
                x_com, y_com = ndimage.measurements.center_of_mass(mask)

            stats = [
                ii_label, x_com, y_com, z_com, jj, label_mean, label_std,
                label_min, label_max
            ]

            if verbose_flag and nlabels > 3 * verbose_nlines:
                if ii_verbose == (verbose_nlines - 1):
                    df_verbose = df_stats.tail(verbose_nlines)
                    print('\n')
                    print(
                        df_verbose.to_string(
                            formatters={
                                'label': '{:,.0f}'.format,
                                'volume': '{:,.0f}'.format,
                                'time': '{:,.0f}'.format,
                                'mean': '{:,.3f}'.format,
                                'std': '{:,.3f}'.format,
                                'min': '{:,.3f}'.format,
                                'max': '{:,.3f}'.format,
                                'x_com': '{:,.0f}'.format,
                                'y_com': '{:,.0f}'.format,
                                'z_com': '{:,.0f}'.format
                            }))
                    ii_verbose = 0
                else:
                    ii_verbose += 1

            df_stats.loc[len(df_stats)] = stats

    if verbose_flag:

        if verbose_all_flag:
            pandas.set_option('display.max_rows', len(df_stats))

        print('\n')

        # ['label' 'x_com' 'y_com' 'z_com' 'time' 'mean' 'std' 'min' 'max']
        print(
            df_stats.to_string(
                formatters={
                    'label': '{:,.0f}'.format,
                    'time': '{:,.0f}'.format,
                    'mean': '{:,.3f}'.format,
                    'std': '{:,.3f}'.format,
                    'min': '{:,.3f}'.format,
                    'max': '{:,.3f}'.format,
                    'x_com': '{:,.0f}'.format,
                    'y_com': '{:,.0f}'.format,
                    'z_com': '{:,.0f}'.format
                }))
        print('\n')

    return df_stats
示例#17
0
def properties(in_label_nii_filename, label_list, background, stats, out_filename, limits_volume_voxels=[0, numpy.inf],
            limits_bb_volume_voxels=[0, numpy.inf], limits_fill_factor=[0,1], sort='volume', 
            verbose=False, verbose_nlines=20):

    label_nii = labels.read_nifti_file( in_label_nii_filename, 'labels.properties.py: Label file does not exist. ')

    single_voxel_volume_mm3 = query_voxel_volume_mm3( label_nii )

    label_array = label_nii.get_data()

    label_list = labels.get_labels(label_list, label_array, background)

    df_stats = pd.DataFrame(columns=(
    'label', 'volume_voxels', 'volume_mm3', 'com_x', 'com_y', 'com_z', 'com_t', 'com_in', 'bb_dx', 'bb_dy', 'bb_dz', 'bb_dt', 'bb_dmin', 'bb_volume_voxels',
    'fill_factor'))

    stats_list = ['label', ] + list(stats)

    if verbose:
        jj = 0
        pd.set_option('expand_frame_repr', False)

    for ii in label_list:

        # Create logical mask

        mask = (label_array == ii)
        ndim = len(mask.shape)

        # Calculate Volume
        label_volume_voxels = int(numpy.sum(mask))
        label_volume_mm3    = single_voxel_volume_mm3 * label_volume_voxels

        if check_limits(label_volume_voxels, limits_volume_voxels):

            bb_dx, bb_dy, bb_dz, bb_dt, bb_dmin, bb_volume_voxels = calculate_bounding_box(mask)

            if check_limits(bb_volume_voxels, limits_bb_volume_voxels):

                label_x_com, label_y_com, label_z_com, label_t_com = calculate_center_of_mass(mask)

                if ndim == 4:
                    label_com_in = mask[label_x_com, label_y_com, label_z_com, label_t_com]
                elif ndim==3:
                    label_t_com = 0
                    label_com_in = mask[label_x_com, label_y_com, label_z_com]
                else:
                    label_z_com = label_t_com = 0
                    label_com_in = mask[label_x_com, label_y_com]

                fill_factor = label_volume_voxels / bb_volume_voxels

                if check_limits(fill_factor, limits_fill_factor):

                    label_stats = [ii, label_volume_voxels, label_volume_mm3, label_x_com, label_y_com, label_z_com, label_t_com, label_com_in, bb_dx, bb_dy,
                                   bb_dz, bb_dt, bb_dmin, bb_volume_voxels, fill_factor]

                    df_stats.loc[len(df_stats)] = label_stats

                    if verbose:
                        if jj == (verbose_nlines - 1):
                            print('\n')
                            df_verbose = df_stats.tail(verbose_nlines)
                            df_verbose = df_verbose[stats_list]
                            print(df_verbose.to_string(
                                formatters={'volume_voxels': '{:,.0f}'.format, 'volume_mm3': '{:,.3f}'.format,
                                            'com_x': '{:,.0f}'.format,
                                            'com_y': '{:,.0f}'.format, 'com_z': '{:,.0f}'.format,
                                            'com_t': '{:,.0f}'.format,
                                            'com_in': '{:,.0f}'.format, 'bb_dx': '{:,.0f}'.format,
                                            'bb_dy': '{:,.0f}'.format, 'bb_dz': '{:,.0f}'.format, 'bb_dt': '{:,.0f}'.format,
                                            'bb_volume_voxels': '{:,.0f}'.format,
                                            'fill_factor': '{:,.3f}'.format 
                                            }
                                ))

                            jj = 0
                        else:
                            jj += 1

    df_sorted = df_stats.sort_values([sort], ascending=[1]).reset_index(drop=True)

    if verbose:
        print('\n')
        print(df_sorted[stats_list])
        print('\n')

    if not out_filename == None:
        df_sorted[stats_list].to_csv(out_filename, index=False)
示例#18
0
def list_labels(in_filename):
    in_label_nii = labels.read_nifti_file(in_filename,
                                          'Label file does not exist')
    label_list = labels.get_labels(None, in_label_nii.get_data())
    return label_list
示例#19
0
def overlap( label_nii_filename, label2_nii_filename, requested_labels=[], verbose_flag=False, verbose_nlines=10, verbose_all_flag=False ):

    # Load arrays

    label_nii = labels.read_nifti_file( label_nii_filename, 'Label file does not exist' )
    label2_nii = labels.read_nifti_file( label2_nii_filename, 'Image file does not exist' )

    label_single_voxel_volume_mm3 = query_voxel_volume_mm3(label_nii)
    label2_single_voxel_volume_mm3 = query_voxel_volume_mm3(label2_nii)

    # System Checks to verify that the Array Size and Dimensions are compatible

    label_array = label_nii.get_data()
    label2_array = label2_nii.get_data()

    if len(label2_array.shape) < 2 or len(label2_array.shape) > 4:
        sys.exit('Only supports 3D and 4D image arrays')

#    if not len(label_array.shape) == 3:
#        sys.exit('Only supports 3D label arrays')

    label_ndim = len(label_array.shape)
    label2_ndim = len(label2_array.shape)

    ndim = min([label_ndim, label2_ndim])

    if not label2_array.shape[0:ndim] == label_array.shape[0:ndim]:
        sys.exit('Image array and label array do not have the same voxel dimensions')

    # Find a set of acceptable labels

    label_list = labels.get_labels( requested_labels, label_array)
    label2_list = labels.get_labels([], label2_array)

    # Gather stats

    if verbose_flag:
        ii_verbose=0
        pandas.set_option('expand_frame_repr', False)

    df_stats              = pandas.DataFrame(columns=('label1', 'label2', 'volume1_mm3',  'volume2_mm3', 'volume12_mm3',
                                                      'fraction12', 'x12_com', 'y12_com', 'z12_com'))

    for ii, ii_label in enumerate(label_list):

        mask1 = label_array[0, ...] == ii_label
        _, label1_volume_mm3 = measure_volume(mask1, label_single_voxel_volume_mm3)

        overlap_label2_list = list(numpy.unique(mask1 * label2_array))
        print(overlap_label2_list)

        for jj, jj_label in enumerate(overlap_label2_list):
        
            mask2 = label2_array[0,...] == jj_label
            mask12 = mask1 * mask2

            _, label2_volume_mm3 = measure_volume( mask2, label_single_voxel_volume_mm3)
            _, label12_volume_mm3 = measure_volume(mask12, label_single_voxel_volume_mm3)

            fraction12 = label12_volume_mm3 / label1_volume_mm3

            x12_com, y12_com, z12_com =  ndimage.measurements.center_of_mass(mask12)

            stats  = [ii_label, jj_label, label1_volume_mm3, label2_volume_mm3, label2_volume_mm3,
                      fraction12, x12_com, y12_com, z12_com ]

            if verbose_flag:
                if ii_verbose==(verbose_nlines-1):
                    df_verbose =  df_stats.tail(verbose_nlines) 
                    print('\n')
                    print (df_verbose.to_string(formatters={'label1':'{:,.0f}'.format, 'label2':'{:,.0f}'.format
                                                            'volume1':'{:,.0f}'.format, 'volume2':'{:,.0f}'.format,
                                                            'volume12':'{:,.0f}'.format,
                                                            'fraction12':'{:,.3f}'.format,
                                                            'x12_com':'{:,.0f}'.format,
                                                            'y12_com':'{:,.0f}'.format,
                                                            'z12_com':'{:,.0f}'.format}  ))
                    ii_verbose = 0
                else:
                    ii_verbose += 1
                    
            df_stats.loc[len(df_stats)] = stats
示例#20
0
import os
import sys
from crop_func import crop_func
from get_directories import get_directories
import labels as lbl

labels_path = str(sys.argv[1])  # path to label.txt files
data_path = str(sys.argv[2])  # path to images
save_path = str(sys.argv[3])  # path to save the cropped directory

labels_dir, data_dir = get_directories(labels_path, data_path)
all_labels = lbl.get_labels(labels_path, labels_dir)

print("all labels...")
idx = 0
for i in all_labels:
    print(F"File #{idx}")
    for row in i:
        print(row)
    idx += 1

print()

print("Attempting to create cropped directory")
if os.path.exists(save_path):
    print(F"{save_path} already exists.")
else:
    try:
        os.mkdir(save_path)
    except OSError:
        print(F"Creation of the directory '{save_path}' failed.")
示例#21
0
 def get(self, video_id):
     return get_labels(video_id)
示例#22
0
# -*- coding: UTF-8 -*-

"""
Constructs an index.html for debugging the translation and region information.
"""

from regions import regions as get_regions
from names import names as get_names
from labels import labels as get_labels
regions = get_regions()
names = get_names()
labels = get_labels()

def language_abbr(language):
    return """<em><abbr title="%s">%s</abbr>.</em>""" % (
        {
            "E": "English",
            "S": "Sindarin",
            "Q": "Quenya",
            "N": "Ñoldorin",
        }.get(language, language),
        language
    )

def label_html(language):
    key = language["Canonical"], language["Language"], "Tengwar", "General Use"
    if key not in labels:
        return ""
    label = labels[key]
    return """
        <img class="label" src="http://3rin.gs/labels/%s-%st-thumbnail.png" title="%s in the Tengwar">