|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +# ------------------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | +import json |
| 24 | +import pickle |
| 25 | +import numpy as np |
| 26 | +import h5py |
| 27 | + |
| 28 | +from systemds.scuro.representations.unimodal import UnimodalRepresentation |
| 29 | + |
| 30 | + |
| 31 | +class NPY(UnimodalRepresentation): |
| 32 | + def __init__(self): |
| 33 | + super().__init__("NPY") |
| 34 | + |
| 35 | + def parse_all(self, filepath, indices, get_sequences=False): |
| 36 | + data = np.load(filepath, allow_pickle=True) |
| 37 | + |
| 38 | + if indices is not None: |
| 39 | + return np.array([data[index] for index in indices]) |
| 40 | + else: |
| 41 | + return np.array([data[index] for index in data]) |
| 42 | + |
| 43 | + |
| 44 | +class Pickle(UnimodalRepresentation): |
| 45 | + def __init__(self): |
| 46 | + super().__init__("Pickle") |
| 47 | + |
| 48 | + def parse_all(self, file_path, indices, get_sequences=False): |
| 49 | + with open(file_path, "rb") as f: |
| 50 | + data = pickle.load(f) |
| 51 | + |
| 52 | + embeddings = [] |
| 53 | + for n, idx in enumerate(indices): |
| 54 | + embeddings.append(data[idx]) |
| 55 | + |
| 56 | + return np.array(embeddings) |
| 57 | + |
| 58 | + |
| 59 | +class HDF5(UnimodalRepresentation): |
| 60 | + def __init__(self): |
| 61 | + super().__init__("HDF5") |
| 62 | + |
| 63 | + def parse_all(self, filepath, indices=None, get_sequences=False): |
| 64 | + data = h5py.File(filepath) |
| 65 | + |
| 66 | + if get_sequences: |
| 67 | + max_emb = 0 |
| 68 | + for index in indices: |
| 69 | + if max_emb < len(data[index][()]): |
| 70 | + max_emb = len(data[index][()]) |
| 71 | + |
| 72 | + emb = [] |
| 73 | + if indices is not None: |
| 74 | + for index in indices: |
| 75 | + emb_i = data[index].tolist() |
| 76 | + for i in range(len(emb_i), max_emb): |
| 77 | + emb_i.append([0 for x in range(0, len(emb_i[0]))]) |
| 78 | + emb.append(emb_i) |
| 79 | + |
| 80 | + return np.array(emb) |
| 81 | + else: |
| 82 | + if indices is not None: |
| 83 | + return np.array([np.mean(data[index], axis=0) for index in indices]) |
| 84 | + else: |
| 85 | + return np.array([np.mean(data[index][()], axis=0) for index in data]) |
| 86 | + |
| 87 | + |
| 88 | +class JSON(UnimodalRepresentation): |
| 89 | + def __init__(self): |
| 90 | + super().__init__("JSON") |
| 91 | + |
| 92 | + def parse_all(self, filepath, indices): |
| 93 | + with open(filepath) as file: |
| 94 | + return json.load(file) |
0 commit comments