File size: 9,900 Bytes
bbeb72c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Helper functions to reduce RAM utilization
# Please install dependencies before:
# pip install -r requirements.txt

# Import necessary libraries
import os
import pickle
import tarfile
import pandas as pd
import numpy as np

from tqdm import tqdm
from tqdm import trange
from pathlib import Path
from sklearn.model_selection import train_test_split


def delete_temporary_files():
    if Path('tmp_dataset_X.pkl').exists():
        os.remove('tmp_dataset_X.pkl')
    if Path('tmp_dataset_y.pkl').exists():
        os.remove('tmp_dataset_y.pkl')
    if Path('tmp_dataset_X.npy').exists():
        os.remove('tmp_dataset_X.npy')
    if Path('tmp_dataset_y.npy').exists():
        os.remove('tmp_dataset_y.npy')

    if Path('tmp_X_train.pkl').exists():
        os.remove('tmp_X_train.npy')
    if Path('tmp_y_train.pkl').exists():
        os.remove('tmp_y_train.npy')
    if Path('tmp_X_test.npy').exists():
        os.remove('tmp_X_test.npy')
    if Path('tmp_y_test.npy').exists():
        os.remove('tmp_y_test.npy')



def load_dataset(file_path='dataset.tar.bz2'):
    """
    Decompress and load already prepared dataset.

    Parameters:
        file_path (str): Path to the compressed version of the prepared dataset (default dataset.tar.bz2)
    
    Returns:
        X_train (np.memmap): Memory-mapped NumPy array of the X training data
        X_test (np.memmap): Memory-mapped NumPy array of the X test data
        y_train (np.memmap): Memory-mapped NumPy array of the y training data
        y_test (np.memmap): Memory-mapped NumPy array of the y test data
    """

    # Return the pepared dataset if it already exists
    if Path('X_train.npy').exists() and Path('y_train.npy').exists() and Path('X_test.npy').exists() and Path('y_test.npy').exists():
        X_train = np.load('X_train.npy', mmap_mode='r')
        y_train = np.load('y_train.npy', mmap_mode='r')
        X_test = np.load('X_test.npy', mmap_mode='r')
        y_test = np.load('y_test.npy', mmap_mode='r')

        return X_train, X_test, y_train, y_test
    
    # Decompress memory mapped files
    if Path(file_path).exists():
        with tarfile.open(file_path) as dataset:
            dataset.extractall(path='.')
        
        # Load the dataset
        dataset_X = np.load('dataset_X.npy', mmap_mode='r')
        dataset_y = np.load('dataset_y.npy', mmap_mode='r')

        # Create a train test split with memory mapped files
        X_train, X_test, y_train, y_test = train_test_split_memmapped(dataset_X, dataset_y)

        return X_train, X_test, y_train, y_test
    else:
        print('ERROR: file not found')

def convert_and_load_dataset(file_path='dataset.csv.bz2'):
    """
    Converts a CSV dataset into a NumPy memory-mapped dataset and load it.

    This function transforms a given CSV dataset into a memory-mapped NumPy array. 
    Memory-mapping helps to reduce RAM usage by loading the dataset in smaller chunks. 
    However, it requires additional disk space during the conversion process.

    Parameters:
        file_path (str): Path to the CSV dataset file (default dataset.csv.bz2)
    
    Returns:
        X_train (np.memmap): Memory-mapped NumPy array of the X training data
        X_test (np.memmap): Memory-mapped NumPy array of the X test data
        y_train (np.memmap): Memory-mapped NumPy array of the y training data
        y_test (np.memmap): Memory-mapped NumPy array of the y test data
    """

    # Return the pepared dataset if it already exists
    if Path('X_train.npy').exists() and Path('y_train.npy').exists() and Path('X_test.npy').exists() and Path('y_test.npy').exists():
        return load_dataset()

    # Load and prepare dataset
    delete_temporary_files()
    with open('tmp_dataset_X.pkl', 'ab') as tmp_dataset_X, open('tmp_dataset_y.pkl', 'ab') as tmp_dataset_y:
        shape = None
        num_of_chunks = 0

        # Load the dataset from a local file path
        # Replace with Huggingface dataset call if applicable
        for real_data_chunk in tqdm(pd.read_csv(file_path, compression='bz2', chunksize=4096), desc='Read and Prepare Dataset'):
            # Select relevant columns (replace these with actual column names from your dataset)
            # Here we assume that the dataset contains sensor readings like gyroscope and accelerometer data
            relevant_columns = ['gyro_x', 'gyro_y', 'gyro_z', 'acc_x', 'acc_y', 'acc_z', 'upright']
            sensordata_chunk = real_data_chunk[relevant_columns]

            # Split the data into features (X) and labels (y)
            # 'fall_label' is assumed to be the column indicating whether a fall occurred
            X_chunk = np.array(sensordata_chunk.drop(columns=['upright'])) # Replace 'fall_label' with the actual label column
            y_chunk = np.array(sensordata_chunk['upright'])
            
            if shape is None:
                # Preview the dataset
                print('\n' + str(real_data_chunk.head()))

            if shape is None:
                shape = np.array(X_chunk.shape)
            else:
                shape[0] += X_chunk.shape[0]

            pickle.dump(X_chunk, tmp_dataset_X)
            pickle.dump(y_chunk, tmp_dataset_y)
            num_of_chunks += 1

    # Convert dataset into a memory-mapped array stored in a binary file on disk.
    X_idx = 0
    y_idx = 0
    dataset_X = np.memmap('tmp_dataset_X.npy', mode='w+', dtype=np.float32, shape=(shape[0], shape[1], 1))
    dataset_y = np.memmap('tmp_dataset_y.npy', mode='w+', dtype=np.float32, shape=(shape[0], 1))
    with open('tmp_dataset_X.pkl', 'rb') as tmp_dataset_X, open('tmp_dataset_y.pkl', 'rb') as tmp_dataset_y:
        for _ in trange(0, num_of_chunks, 1, desc='Convert Dataset'):
            X_chunk = pickle.load(tmp_dataset_X)
            y_chunk = pickle.load(tmp_dataset_y)

            # Reshape data for LSTM input (assuming time-series data)
            # Adjust the reshaping based on your dataset structure
            for X_data in X_chunk:
                dataset_X[X_idx] = np.expand_dims(X_data, axis=-1)
                X_idx += 1
            for y_data in y_chunk:
                dataset_y[y_idx] = np.expand_dims(y_data, axis=-1)
                y_idx += 1

    # Delete temporary files
    os.remove('tmp_dataset_X.pkl')
    os.remove('tmp_dataset_y.pkl')

    # Save the memory-mapped arrays
    with open('dataset_X.npy', 'wb') as dataset_x_file, open('dataset_y.npy', 'wb') as dataset_y_file:
        np.save(dataset_x_file, dataset_X, allow_pickle=False, fix_imports=True)
        np.save(dataset_y_file, dataset_y, allow_pickle=False, fix_imports=True)
    
    # Delete temporary files
    dataset_X._mmap.close()
    dataset_y._mmap.close()
    os.remove('tmp_dataset_X.npy')
    os.remove('tmp_dataset_y.npy')

    # Reload memory-mapped arrays
    dataset_X = np.load('dataset_X.npy', mmap_mode='r')
    dataset_y = np.load('dataset_y.npy', mmap_mode='r')

    # Create a train test split with memory mapped files
    X_train, X_test, y_train, y_test = train_test_split_memmapped(dataset_X, dataset_y)

    return X_train, X_test, y_train, y_test


def train_test_split_memmapped(dataset_X, dataset_y, test_size=0.2, random_state=42):
    """
    Create memory-mapped files for train and test datasets.

    Parameters:
        dataset_X (np.memmap): X part of the complete dataset
        dataset_y (np.memmap): y part of the complete dataset
        test_size (float): Propotion of the dataset used for the test split (default 0.2)
        random_state (int): Random state used for repeatability (default 42)

    Returns:
        X_train (np.memmap): Memory-mapped NumPy array of the X training data
        X_test (np.memmap): Memory-mapped NumPy array of the X test data
        y_train (np.memmap): Memory-mapped NumPy array of the y training data
        y_test (np.memmap): Memory-mapped NumPy array of the y test data
    """
    delete_temporary_files()

    # Split data into training and test sets
    idxs = np.arange(dataset_X.shape[0])
    train_idx, test_idx = train_test_split(idxs, test_size=test_size, random_state=random_state)

    # Create memory-mapped files for train and test sets
    X_train = np.memmap('tmp_X_train.npy', dtype=dataset_X.dtype, mode='w+', shape=(len(train_idx), dataset_X.shape[1], 1))
    y_train = np.memmap('tmp_y_train.npy', dtype=dataset_y.dtype, mode='w+', shape=(len(train_idx), dataset_y.shape[1]))
    X_test = np.memmap('tmp_X_test.npy', dtype=dataset_X.dtype, mode='w+', shape=(len(test_idx), dataset_X.shape[1], 1))
    y_test = np.memmap('tmp_y_test.npy', dtype=dataset_y.dtype, mode='w+', shape=(len(test_idx), dataset_y.shape[1]))

    # Assign values to the train and test memmap arrays
    X_train[:] = dataset_X[train_idx]
    y_train[:] = dataset_y[train_idx]
    X_test[:] = dataset_X[test_idx]
    y_test[:] = dataset_y[test_idx]

    # Save the memory-mapped arrays
    with open('X_train.npy', 'wb') as X_train_file, open('y_train.npy', 'wb') as y_train_file, open('X_test.npy', 'wb') as X_test_file, open('y_test.npy', 'wb') as y_test_file:
        np.save(X_train_file, X_train, allow_pickle=False, fix_imports=True)
        np.save(y_train_file, y_train, allow_pickle=False, fix_imports=True)
        np.save(X_test_file, X_test, allow_pickle=False, fix_imports=True)
        np.save(y_test_file, y_test, allow_pickle=False, fix_imports=True)
    
    X_train._mmap.close()
    y_train._mmap.close()
    X_test._mmap.close()
    y_test._mmap.close()

    # Delete temporary files
    os.remove('tmp_X_train.npy')
    os.remove('tmp_y_train.npy')
    os.remove('tmp_X_test.npy')
    os.remove('tmp_y_test.npy')

    X_train = np.load('X_train.npy', mmap_mode='r')
    y_train = np.load('y_train.npy', mmap_mode='r')
    X_test = np.load('X_test.npy', mmap_mode='r')
    y_test = np.load('y_test.npy', mmap_mode='r')
    
    return X_train, X_test, y_train, y_test