|
| 1 | +# Copyright 2020 resspect software |
| 2 | +# Author: Amanda Wasserman |
| 3 | +# |
| 4 | +# created on 18 March 2024 |
| 5 | +# |
| 6 | +# Licensed GNU General Public License v3.0; |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# https://www.gnu.org/licenses/gpl-3.0.en.html |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +__all__ = ["update_pool_stash", "update_training_stash"] |
| 19 | + |
| 20 | +### Need to remove old objs at some point (once they are no longer hot) |
| 21 | +# Need to remove objects that have been moved from pool to training |
| 22 | +def update_pool_stash(current_stash_path: str, new_night_path: str): |
| 23 | + #should we store old features somewhere? makes it easier to add training objs |
| 24 | + #would want to add current MJD, maybe first MJD, and peak MJD |
| 25 | + |
| 26 | + #read in current stash as list of strings |
| 27 | + with open(current_stash_path, 'r') as f: |
| 28 | + current_stash = f.readlines() |
| 29 | + |
| 30 | + #read in new night as list of strings |
| 31 | + with open(new_night_path, 'r') as f: |
| 32 | + new_night = f.readlines() |
| 33 | + |
| 34 | + #if id is already in stash, replace it. else append it |
| 35 | + for obj in new_night: |
| 36 | + replaced = 0 |
| 37 | + id = obj.split(',')[0] |
| 38 | + for old_obj in current_stash: |
| 39 | + if id in old_obj: |
| 40 | + current_stash[current_stash.index(old_obj)] = obj |
| 41 | + replaced = 1 |
| 42 | + break |
| 43 | + if replaced == 0: |
| 44 | + current_stash.append(obj) |
| 45 | + |
| 46 | + #write new stash |
| 47 | + with open(current_stash_path, 'w') as f: |
| 48 | + for obj in current_stash: |
| 49 | + f.write(obj) |
| 50 | + |
| 51 | +def update_training_stash_with_new_classification(current_training_stash_path: str, new_obj_ids: list, |
| 52 | + new_obj_classes: list, new_obj_redshifts: list, current_stash_path: str): |
| 53 | + #add new obj id and class for each point on the training obj light curve going forward |
| 54 | + #(how do we want to do this? add A,B,C, etc to the end of the id?) |
| 55 | + with open(current_stash_path, 'r') as f: |
| 56 | + current_stash = f.readlines() |
| 57 | + |
| 58 | + with open(current_training_stash_path, 'r') as f: |
| 59 | + training_stash = f.readlines() |
| 60 | + |
| 61 | + #find obj in current stash |
| 62 | + for idx, obj in new_obj_ids: |
| 63 | + for old_obj in current_stash: |
| 64 | + if idx in old_obj: |
| 65 | + line = old_obj.split(',') |
| 66 | + break |
| 67 | + line[1] = new_obj_redshifts[idx] |
| 68 | + line[2] = new_obj_classes[idx] |
| 69 | + train_to_append = ','.join(line) |
| 70 | + training_stash.append(train_to_append) |
| 71 | + |
| 72 | + #write new training stash |
| 73 | + with open(current_training_stash_path, 'w') as f: |
| 74 | + for obj in training_stash: |
| 75 | + f.write(obj) |
| 76 | + |
| 77 | + |
| 78 | +def update_training_stash_with_new_features(new_night_path: str, current_training_stash_path: str): |
| 79 | + #add new obj id and features for each point on the training obj light curve going forward |
| 80 | + #(how do we want to do this? add A,B,C, etc to the end of the id?) |
| 81 | + with open(new_night_path, 'r') as f: |
| 82 | + new_night = f.readlines() |
| 83 | + |
| 84 | + with open(current_training_stash_path, 'r') as f: |
| 85 | + training_stash = f.readlines() |
| 86 | + |
| 87 | + #append training set with new features |
| 88 | + for obj in new_night: |
| 89 | + id = obj.split(',')[0] |
| 90 | + for old_obj in training_stash: |
| 91 | + if id in old_obj: |
| 92 | + #append new features |
| 93 | + old_split = old_obj.split(',') |
| 94 | + split = [id+mjd, old_split[1], old_split[2], 'N/A', 'N/A'] ###we need mjd (or something) to differentiate the ids |
| 95 | + split.extend(obj.split(',')[5:]) |
| 96 | + training_stash.append(','.join(split)) |
| 97 | + |
| 98 | + #write new stash |
| 99 | + with open(current_training_stash_path, 'w') as f: |
| 100 | + for obj in training_stash: |
| 101 | + f.write(obj) |
| 102 | + |
| 103 | +def remove_training_from_pool(current_stash_path: str, new_obj_ids: list): |
| 104 | + #remove objects that have been moved from pool to training |
| 105 | + with open(current_stash_path, 'r') as f: |
| 106 | + current_stash = f.readlines() |
| 107 | + |
| 108 | + for id in new_obj_ids: |
| 109 | + for obj in current_stash: |
| 110 | + if id in obj: |
| 111 | + current_stash.remove(obj) |
| 112 | + |
| 113 | + #write new stash |
| 114 | + with open(current_stash_path, 'w') as f: |
| 115 | + for obj in current_stash: |
| 116 | + f.write(obj) |
| 117 | + |
| 118 | +def remove_not_hot_objects(current_stash_path: str): |
| 119 | + #remove objects that are no longer hot |
| 120 | + #need MJDs |
| 121 | + #make sure we're not getting old objects from rob or we'll be wasting computing power adding/removing |
| 122 | + with open(current_stash_path, 'r') as f: |
| 123 | + current_stash = f.readlines() |
| 124 | + |
| 125 | + for obj in current_stash: |
| 126 | + if obj[current_mjd-discovery_mjd] >5 0: |
| 127 | + current_stash.remove(obj) |
| 128 | + |
| 129 | + #write new stash |
| 130 | + with open(current_stash_path, 'w') as f: |
| 131 | + for obj in current_stash: |
| 132 | + f.write(obj) |
| 133 | + |
0 commit comments