|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | + |
| 4 | +import datajoint as dj |
| 5 | +import pytest |
| 6 | + |
| 7 | +logger = dj.logger |
| 8 | +_tear_down = True |
| 9 | + |
| 10 | +# ---------------------- FIXTURES ---------------------- |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(autouse=True, scope="session") |
| 14 | +def dj_config(): |
| 15 | + """If dj_local_config exists, load""" |
| 16 | + if pathlib.Path("./dj_local_conf.json").exists(): |
| 17 | + dj.config.load("./dj_local_conf.json") |
| 18 | + dj.config.update( |
| 19 | + { |
| 20 | + "safemode": False, |
| 21 | + "database.host": os.environ.get("DJ_HOST") or dj.config["database.host"], |
| 22 | + "database.password": os.environ.get("DJ_PASS") |
| 23 | + or dj.config["database.password"], |
| 24 | + "database.user": os.environ.get("DJ_USER") or dj.config["database.user"], |
| 25 | + } |
| 26 | + ) |
| 27 | + return |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True, scope="session") |
| 31 | +def pipeline(): |
| 32 | + import tutorial_pipeline as pipeline |
| 33 | + |
| 34 | + yield { |
| 35 | + "lab": pipeline.lab, |
| 36 | + "subject": pipeline.subject, |
| 37 | + "session": pipeline.session, |
| 38 | + "probe": pipeline.probe, |
| 39 | + "ephys": pipeline.ephys, |
| 40 | + "get_ephys_root_data_dir": pipeline.get_ephys_root_data_dir, |
| 41 | + } |
| 42 | + |
| 43 | + if _tear_down: |
| 44 | + pipeline.subject.Subject.delete() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(scope="session") |
| 48 | +def insert_upstreams(pipeline): |
| 49 | + |
| 50 | + subject = pipeline["subject"] |
| 51 | + session = pipeline["session"] |
| 52 | + probe = pipeline["probe"] |
| 53 | + ephys = pipeline["ephys"] |
| 54 | + |
| 55 | + subject.Subject.insert1( |
| 56 | + dict(subject="subject5", subject_birth_date="2023-01-01", sex="U") |
| 57 | + ) |
| 58 | + |
| 59 | + session_key = dict(subject="subject5", session_datetime="2023-01-01 00:00:00") |
| 60 | + session_dir = "raw/subject5/session1" |
| 61 | + |
| 62 | + session.SessionDirectory.insert1(dict(**session_key, session_dir=session_dir)) |
| 63 | + probe.Probe.insert1(dict(probe="714000838", probe_type="neuropixels 1.0 - 3B")) |
| 64 | + ephys.ProbeInsertion.insert1( |
| 65 | + dict( |
| 66 | + session_key, |
| 67 | + insertion_number=1, |
| 68 | + probe="714000838", |
| 69 | + ) |
| 70 | + ) |
| 71 | + yield |
| 72 | + |
| 73 | + if _tear_down: |
| 74 | + subject.Subject.delete() |
| 75 | + probe.Probe.delete() |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(scope="session") |
| 79 | +def populate_ephys_recording(pipeline, insert_upstream): |
| 80 | + ephys = pipeline["ephys"] |
| 81 | + ephys.EphysRecording.populate() |
| 82 | + |
| 83 | + yield |
| 84 | + |
| 85 | + if _tear_down: |
| 86 | + ephys.EphysRecording.delete() |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture(scope="session") |
| 90 | +def insert_clustering_task(pipeline, populate_ephys_recording): |
| 91 | + ephys = pipeline["ephys"] |
| 92 | + params_ks = { |
| 93 | + "fs": 30000, |
| 94 | + "fshigh": 150, |
| 95 | + "minfr_goodchannels": 0.1, |
| 96 | + "Th": [10, 4], |
| 97 | + "lam": 10, |
| 98 | + "AUCsplit": 0.9, |
| 99 | + "minFR": 0.02, |
| 100 | + "momentum": [20, 400], |
| 101 | + "sigmaMask": 30, |
| 102 | + "ThPr": 8, |
| 103 | + "spkTh": -6, |
| 104 | + "reorder": 1, |
| 105 | + "nskip": 25, |
| 106 | + "GPU": 1, |
| 107 | + "Nfilt": 1024, |
| 108 | + "nfilt_factor": 4, |
| 109 | + "ntbuff": 64, |
| 110 | + "whiteningRange": 32, |
| 111 | + "nSkipCov": 25, |
| 112 | + "scaleproc": 200, |
| 113 | + "nPCs": 3, |
| 114 | + "useRAM": 0, |
| 115 | + } |
| 116 | + ephys.ClusteringParamSet.insert_new_params( |
| 117 | + clustering_method="kilosort2", |
| 118 | + paramset_idx=0, |
| 119 | + params=params_ks, |
| 120 | + paramset_desc="Spike sorting using Kilosort2", |
| 121 | + ) |
| 122 | + |
| 123 | + session_key = dict(subject="subject5", session_datetime="2023-01-01 00:00:00") |
| 124 | + |
| 125 | + ephys.ClusteringTask.insert1( |
| 126 | + dict( |
| 127 | + session_key, |
| 128 | + insertion_number=1, |
| 129 | + paramset_idx=0, |
| 130 | + task_mode="load", # load or trigger |
| 131 | + clustering_output_dir="processed/subject5/session1/probe_1/kilosort2-5_1", |
| 132 | + ) |
| 133 | + ) |
| 134 | + |
| 135 | + yield |
| 136 | + |
| 137 | + if _tear_down: |
| 138 | + ephys.ClusteringParamSet.delete() |
| 139 | + |
| 140 | + |
| 141 | +@pytest.fixture(scope="session") |
| 142 | +def processing(pipeline, populate_ephys_recording): |
| 143 | + |
| 144 | + ephys = pipeline["ephys"] |
| 145 | + ephys.CuratedClustering.populate() |
| 146 | + ephys.LFP.populate() |
| 147 | + ephys.WaveformSet.populate() |
| 148 | + |
| 149 | + yield |
| 150 | + |
| 151 | + if _tear_down: |
| 152 | + ephys.CuratedClustering.delete() |
| 153 | + ephys.LFP.delete() |
0 commit comments