File size: 2,801 Bytes
4b876a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import astropy.table
import numpy as np
import pandas as pd

def load(path,
         x_cols=('psfMag_u', 'psfMag_g', 'psfMag_r', 'psfMag_i', 'psfMag_z'),
         y_col='redshift',
         class_col='class',
         class_val='Galaxy'):

    # Cast x_cols to list so Pandas doesn't complain…
    x_cols_l = list(x_cols)

    if '.h5' in path or '.hdf' in path:
        # We have an HDF5 file
        data = pd.read_hdf(path)
        return data, x_cols_l, y_col

    elif '.fits' in path:
        x_cols_l = ['umag', 'gmag', 'rmag', 'imag', 'zmag']
        y_col = 'z'
        dat = astropy.table.Table.read(path, format='fits')
        data = dat.to_pandas()
        data = data[x_cols_l + [y_col]]
        return data, x_cols_l, y_col

    else:
        # We have a CSV file

        data_iter = pd.read_csv(
            path,
            iterator=True,
            chunksize=100000,
            usecols=x_cols_l + [y_col, class_col])

        # Filter out anything that is not a galaxy without loading the
        # whole file into memory.
        data = pd.concat(chunk[chunk[class_col] == class_val]
                         for chunk in data_iter)

        return data[x_cols_l + [y_col]], x_cols_l, y_col


def split(data, train_n, test_n):
    data, x_cols, y_col = data

    X_data = data[x_cols].as_matrix()
    y_data = data[y_col].as_matrix()
    assert X_data.shape[0] == y_data.shape[0] == data.shape[0]
    assert X_data.shape[1] == data.shape[1] - 1
    assert len(y_data.shape) == 1

    # Shuffle data
    indices = list(range(data.shape[0]))
    np.random.seed(seed=12)
    np.random.shuffle(indices)
    X_data = X_data[indices]
    y_data = y_data[indices]

    train_X = X_data[:train_n]
    test_X = X_data[train_n:train_n+test_n]
    train_y = y_data[:train_n]
    test_y = y_data[train_n:train_n+test_n]

    assert train_X.shape == (train_n, len(x_cols))
    assert train_y.shape == (train_n,)
    assert test_X.shape == (test_n, len(x_cols))
    assert test_y.shape == (test_n,)

    return (train_X, train_y), (test_X, test_y)


def save_as_hdf5(path, data):
    data.to_hdf(path, '👀')


if __name__ == '__main__':
    # TODO: Use argparse if this gets any longer...

    import sys
    if len(sys.argv) == 3 and sys.argv[2] == '--test':
        # Tiny test
        data = load(sys.argv[1])
        data = split(data, 2, 1)
        print(data)
    elif len(sys.argv) == 4 and sys.argv[2] == '--to-hdf5':
        data, _, _ = load(sys.argv[1])
        save_as_hdf5(sys.argv[3], data)
    elif len(sys.argv) == 3 and sys.argv[2] == '--time':
        from time import time
        start_time = time()
        data = load(sys.argv[1])
        total_time = time() - start_time
        print('Took {}s'.format(total_time))
    else:
        print('No options specified.')