def transfer_session(src: Path, dst: Path, force: bool = False): print(src, dst) # log.info(f"Attempting to copy from {src} to {dst}...") src = Path(folders.session_path(src)) dst_sess = Path(folders.session_path(dst)) if src is None: return if dst_sess is None: dst = dst / Path(*src.parts[-3:]) src_flag_file = src / "transfer_me.flag" if not src_flag_file.exists(): return flag = flags.read_flag_file(src_flag_file) if isinstance(flag, list): raise (NotImplementedError) else: if force: shutil.rmtree(dst, ignore_errors=True) log.info(f"Copying all files from {src} to {dst} ...") shutil.copytree(src, dst, ignore=ig(str(src_flag_file.name))) # If folder was created delete the src_flag_file if (dst / 'raw_video_data').exists(): log.info( f"{Path(*src.parts[-3:]) / 'raw_video_data'} copied to {dst.parent.parent.parent}" ) src_flag_file.unlink()
def test_session_path(self): self.assertIsNone(folders.session_path(self.no_session)) self.assertEqual(folders.session_path(self.std_session), self.std_session) self.assertEqual(folders.session_path(self.nonstd_session), self.nonstd_session) self.assertEqual(folders.session_path(self.nonstd_file), 'path/to/iblrig_data/sub_name/1977-08-01/1') self.assertEqual(folders.session_path(self.std_file), self.std_session)
def rename_session(session_path: str) -> Path: session_path = Path(folders.session_path(session_path)) if session_path is None: return mouse = session_path.parts[-3] date = session_path.parts[-2] sess = session_path.parts[-1] new_mouse = input( f"Please insert mouse NAME [current value: {mouse}]> ") or mouse new_date = input( f"Please insert new session DATE [current value: {date}]> ") or date new_sess = input( f"Please insert new session NUMBER [current value: {sess}]> ") or sess new_session_path = Path( *session_path.parts[:-3]) / new_mouse / new_date / new_sess shutil.move(str(session_path), str(new_session_path)) print(session_path, '--> renamed to:') print(new_session_path) return new_session_path
def find_pairs(root_data_folder): """Find all passive sessions that needs transfer and where to""" root_data_folder = Path(root_data_folder) settings_files = list(root_data_folder.rglob("_iblrig_taskSettings.raw.json")) n_settings_files = len(settings_files) if n_settings_files == 0: log.warning(f"Found {n_settings_files} sessions") else: log.info(f"Found {n_settings_files} sessions") # Load the corresponding ephys session path form settings file if exists pairs = [] for sf in settings_files: # Get session path form settings file source_spath = session_path(sf) if source_spath is None: continue # Find the root_data_path for session subjects_folder_path = Path(*Path(source_spath).parts[:-3]) # Load reference to corresponding ephys session (ces) which comes in windows format ces = load_settings_file(sf, key="CORRESPONDING_EPHYS_SESSION") # if CORRESPONDING_EPHYS_SESSION does not exist, it's not a passive session if ces is None: continue # Convert windows path to corresponsding session name (csn) in native Path format csn = Path(*PureWindowsPath(ces).parts[-3:]) target_spath = str(subjects_folder_path / csn) pairs.append((source_spath, target_spath)) # Remove sessions that are already transfered i.e. source and destination files are == from_to_pairs = [(x, y) for x, y in pairs if x != y] n_pairs = len(from_to_pairs) if n_pairs == 0: log.warning(f"Found {n_pairs} passive sessions to move") else: log.info(f"Found {n_pairs} passive sessions to move") return from_to_pairs