aboutsummaryrefslogtreecommitdiff
path: root/code/sunlab/common/data/utilities.py
blob: 6b4e6f33dbfc4b7a74b03db75b5fc6a1a6be3073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from .shape_dataset import ShapeDataset
from ..scaler.max_abs_scaler import MaxAbsScaler


def import_10x(
    filename,
    magnification=10,
    batch_size=None,
    shuffle=False,
    val_split=0.0,
    scaler=None,
    sort_columns=None,
):
    """# Import a 10x Image Dataset
    
    Pixel-to-micron: ???"""
    magnification = 10
    dataset = ShapeDataset(
        filename,
        batch_size=batch_size,
        shuffle=shuffle,
        pre_scale=magnification,
        val_split=val_split,
        scaler=scaler,
        sort_columns=sort_columns,
    )
    return dataset


def import_20x(
    filename,
    magnification=10,
    batch_size=None,
    shuffle=False,
    val_split=0.0,
    scaler=None,
    sort_columns=None,
):
    """# Import a 20x Image Dataset
    
    Pixel-to-micron: ???"""
    magnification = 20
    dataset = ShapeDataset(
        filename,
        batch_size=batch_size,
        shuffle=shuffle,
        pre_scale=magnification,
        val_split=val_split,
        scaler=scaler,
        sort_columns=sort_columns,
    )
    return dataset


def import_dataset(
    filename,
    magnification,
    batch_size=None,
    shuffle=False,
    val_split=0.0,
    scaler=None,
    sort_columns=None,
):
    """# Import a dataset

    Requires a magnificaiton to be specified"""
    dataset = ShapeDataset(
        filename,
        pre_scale=magnification,
        batch_size=batch_size,
        shuffle=shuffle,
        val_split=val_split,
        scaler=scaler,
        sort_columns=sort_columns,
    )
    return dataset


def import_full_dataset(fname, magnification=20, scaler=None):
    """# Import a Full Dataset

    If a classification file exists(.txt with a 'Class' header and 'frame','cellnumber' headers), also import it"""
    from os.path import isfile
    import pandas as pd
    import numpy as np

    cfname = fname
    tfname = cfname[:-3] + "txt"
    columns = [
        "frame",
        "cellnumber",
        "x-cent",
        "y-cent",
        "actinedge",
        "filopodia",
        "bleb",
        "lamellipodia",
    ]
    if isfile(tfname):
        dataset = import_dataset(cfname, magnification=magnification, scaler=scaler)
        class_df = np.loadtxt(tfname, skiprows=1)
        class_df = pd.DataFrame(class_df, columns=columns)
        full_df = pd.merge(
            dataset.dataframe,
            class_df,
            left_on=["Frames", "CellNum"],
            right_on=["frame", "cellnumber"],
        )
        full_df["Class"] = np.argmax(
            class_df[["actinedge", "filopodia", "bleb", "lamellipodia"]].to_numpy(),
            axis=-1,
        )
        dataset.labels = full_df["Class"].to_numpy()
    else:
        dataset = import_dataset(cfname, magnification=magnification, scaler=scaler)
        full_df = dataset.dataframe
    dataset.dataframe = full_df
    dataset.filter_off()
    return dataset