Skip to content

Commit

Permalink
Merge pull request #569 from oist/hotfix/fix-roi-edit
Browse files Browse the repository at this point in the history
fix errors on ROI edit
  • Loading branch information
ReiHashimoto authored Sep 29, 2023
2 parents 6bd688c + cf97b3a commit 3db78b0
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 80 deletions.
14 changes: 7 additions & 7 deletions frontend/src/components/Workspace/Visualize/Plot/ImagePlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ const ImagePlotChart = React.memo<{
name: 'roi',
hovertemplate: isAddRoi ? 'none' : 'cell id: %{z}',
// hoverinfo: isAddRoi || pointClick.length ? 'none' : undefined,
colorscale: [...Array(timeDataMaxIndex)].map((_, i) => {
colorscale: [...Array(timeDataMaxIndex + 1)].map((_, i) => {
const new_i = Math.floor(((i % 10) * 10 + i / 10) % 100)
const offset: number = i / (timeDataMaxIndex - 1)
const offset: number = i / timeDataMaxIndex
const rgba = colorscaleRoi[new_i]
const hex = rgba2hex(rgba, roiAlpha)
return [offset, hex]
Expand Down Expand Up @@ -356,7 +356,7 @@ const ImagePlotChart = React.memo<{
z: Number(point.z),
})
}
if (point.curveNumber >= 1 && point.z > 0) {
if (point.curveNumber >= 1 && point.z >= 0) {
dispatch(
setImageItemClikedDataId({
itemId,
Expand All @@ -367,12 +367,12 @@ const ImagePlotChart = React.memo<{
}

const setSelectRoi = (point: PointClick) => {
if (!point.z) return
if (typeof point.z !== 'number' || point.z === -1) return
const newPoints = [...pointClick, point]
const newRoi = roiDataState.map((roi) => {
return roi.map((element) => {
if (newPoints.some((p) => p.z === element)) {
return 0
return -1
}
return element
})
Expand Down Expand Up @@ -480,7 +480,7 @@ const ImagePlotChart = React.memo<{
dispatch(resetAllOrderList())
try {
await mergeRoiApi(roiFilePath, {
ids: pointClick.map((point) => point.z - 1),
ids: pointClick.map((point) => point.z),
})
} catch {}
setLoadingApi(false)
Expand All @@ -495,7 +495,7 @@ const ImagePlotChart = React.memo<{
dispatch(resetAllOrderList())
try {
await deleteRoiApi(roiFilePath, {
ids: pointClick.map((point) => point.z - 1),
ids: pointClick.map((point) => point.z),
})
} catch {}
setLoadingApi(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const TimeSeriesPlotImple = React.memo(() => {
return Object.fromEntries(
dataKeys.map((key) => {
let y = newDataXrange.map((x) => newTimeSeriesData[key]?.[x])
const i = Number(key) - 1
const i = Number(key)
const new_i = Math.floor((i % 10) * 10 + i / 10) % 100
if (drawOrderList.includes(key) && offset) {
const activeIdx: number = drawOrderList.findIndex((v) => v === key)
Expand Down
11 changes: 4 additions & 7 deletions studio/app/common/core/rules/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,10 @@ def save_all_nwb(cls, save_path, all_nwbfile):
timeout = 30 # ロック取得のタイムアウト時間(秒)
with FileLock(lock_path, timeout=timeout):
# ロックが取得できたら、ファイルに書き込みを行う
try:
if os.path.exists(save_path):
overwrite_nwbfile(save_path, nwbconfig)
else:
save_nwb(save_path, input_nwbfile, nwbconfig)
except Exception as e:
print(e)
if os.path.exists(save_path):
overwrite_nwbfile(save_path, nwbconfig)
else:
save_nwb(save_path, input_nwbfile, nwbconfig)

@classmethod
def execute_function(cls, path, params, nwb_params, output_dir, input_info):
Expand Down
7 changes: 2 additions & 5 deletions studio/app/optinist/core/edit_ROI/edit_ROI.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ def excute(cls, config):
last_output_info = cls.__update_pickle_for_roi_edition(
last_output_path, func_name, output_info
)
rule_type = os.path.splitext(os.path.basename(last_output_path))[0]
whole_nwb_path = join_filepath(
[workflow_dirpath, f"whole_{rule_type}.nwb"]
)
whole_nwb_path = join_filepath([workflow_dirpath, "whole.nwb"])

Runner.save_all_nwb(whole_nwb_path, last_output_info["nwbfile"])

Expand All @@ -122,7 +119,7 @@ def excute(cls, config):
v.save_json(node_dirpath)

if k == "nwbfile":
nwb_files = glob(join_filepath([node_dirpath, "*.nwb"]))
nwb_files = glob(join_filepath([node_dirpath, "[!tmp_]*.nwb"]))
if len(nwb_files) > 0:
overwrite_nwb(v, node_dirpath, os.path.basename(nwb_files[0]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start caiman add_roi:", function_id)

# load data
cnmf_data = np.load(f"{node_dirpath}/caiman_cnmf.npy", allow_pickle=True).item()
is_cell = cnmf_data.get("is_cell")
Expand Down Expand Up @@ -48,7 +51,7 @@ def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
file_name="cell_roi",
),
"cnmf_data": CaimanCnmfData(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data, function_id),
}

return info
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
def excute_delete_roi(node_dirpath, ids):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start caiman delete_roi:", function_id)

# load data
cnmf_data = np.load(f"{node_dirpath}/caiman_cnmf.npy", allow_pickle=True).item()
is_cell = cnmf_data.get("is_cell")
Expand All @@ -30,6 +33,6 @@ def excute_delete_roi(node_dirpath, ids):
file_name="cell_roi",
),
"cnmf_data": CaimanCnmfData(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data, function_id),
}
return info
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
def execute_merge_roi(node_dirpath: str, ids: list):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start caiman merge_roi:", function_id)

# load data
cnmf_data = np.load(f"{node_dirpath}/caiman_cnmf.npy", allow_pickle=True).item()
is_cell = cnmf_data.get("is_cell")
Expand Down Expand Up @@ -49,7 +52,7 @@ def execute_merge_roi(node_dirpath: str, ids: list):
file_name="cell_roi",
),
"cnmf_data": CaimanCnmfData(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data),
"nwbfile": set_nwbfile(cnmf_data, function_id),
}

return info
30 changes: 17 additions & 13 deletions studio/app/optinist/core/edit_ROI/wrappers/caiman_edit_roi/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from studio.app.optinist.core.nwb.nwb import NWBDATASET


def set_nwbfile(cnmf_data):
def set_nwbfile(cnmf_data, function_id):
im = cnmf_data.get("im")
is_cell = cnmf_data.get("is_cell")
fluorescence = cnmf_data.get("fluorescence")
Expand All @@ -16,11 +16,11 @@ def set_nwbfile(cnmf_data):
kargs["image_mask"] = im[i, :]
roi_list.append(kargs)

nwbfile[NWBDATASET.ROI] = {"roi_list": roi_list}
nwbfile[NWBDATASET.ROI] = {function_id: roi_list}

# iscellを追加
nwbfile[NWBDATASET.COLUMN] = {
"roi_column": {
function_id: {
"name": "iscell",
"discription": "two columns - iscell & probcell",
"data": is_cell,
Expand All @@ -30,20 +30,24 @@ def set_nwbfile(cnmf_data):
# Fluorescence

nwbfile[NWBDATASET.FLUORESCENCE] = {
"RoiResponseSeries": {
"table_name": "ROIs",
"region": list(range(n_cells)),
"name": "RoiResponseSeries",
"data": fluorescence.T,
"unit": "lumens",
},
function_id: {
"Fluorescence": {
"table_name": "ROIs",
"region": list(range(n_cells)),
"name": "Fluorescence",
"data": fluorescence.T,
"unit": "lumens",
},
}
}

# NWB追加
nwbfile[NWBDATASET.POSTPROCESS] = {
"add_roi": cnmf_data.get("add_roi", []),
"delete_roi": cnmf_data.get("delete_roi", []),
"merge_roi": cnmf_data.get("merge_roi", []),
function_id: {
"add_roi": cnmf_data.get("add_roi", []),
"delete_roi": cnmf_data.get("delete_roi", []),
"merge_roi": cnmf_data.get("merge_roi", []),
}
}

return nwbfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start lccd add_roi:", function_id)

lccd_data = np.load(
os.path.join(node_dirpath, "lccd.npy"), allow_pickle=True
).item()
Expand Down Expand Up @@ -67,7 +70,9 @@ def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
),
"fluorescence": FluoData(timeseries, file_name="fluorescence"),
"dff": FluoData(timeseries_dff, file_name="dff"),
"nwbfile": set_nwbfile(lccd_data, roi_list, fluorescence=timeseries),
"nwbfile": set_nwbfile(
lccd_data, roi_list, function_id, fluorescence=timeseries
),
}

return info
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
def excute_delete_roi(node_dirpath, ids):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start lccd delete_roi:", function_id)

lccd_data = np.load(
os.path.join(node_dirpath, "lccd.npy"), allow_pickle=True
).item()
Expand Down Expand Up @@ -67,7 +70,7 @@ def excute_delete_roi(node_dirpath, ids):
),
"fluorescence": FluoData(timeseries, file_name="fluorescence"),
"dff": FluoData(timeseries_dff, file_name="dff"),
"nwbfile": set_nwbfile(lccd_data, roi_list),
"nwbfile": set_nwbfile(lccd_data, roi_list, function_id),
}

return info
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
def execute_merge_roi(node_dirpath, ids):
import numpy as np

function_id = node_dirpath.split("/")[-1]
print("start lccd merge_roi:", function_id)

lccd_data = np.load(
os.path.join(node_dirpath, "lccd.npy"), allow_pickle=True
).item()
Expand Down Expand Up @@ -73,7 +76,9 @@ def execute_merge_roi(node_dirpath, ids):
),
"fluorescence": FluoData(timeseries, file_name="fluorescence"),
"dff": FluoData(timeseries_dff, file_name="dff"),
"nwbfile": set_nwbfile(lccd_data, roi_list, fluorescence=timeseries),
"nwbfile": set_nwbfile(
lccd_data, roi_list, function_id, fluorescence=timeseries
),
}

return info
31 changes: 18 additions & 13 deletions studio/app/optinist/core/edit_ROI/wrappers/lccd_edit_roi/utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from studio.app.optinist.core.nwb.nwb import NWBDATASET


def set_nwbfile(lccd_data, roi_list, fluorescence=None):
def set_nwbfile(lccd_data, roi_list, function_id, fluorescence=None):
nwbfile = {}

nwbfile[NWBDATASET.ROI] = {"roi_list": roi_list}
nwbfile[NWBDATASET.ROI] = {function_id: roi_list}

if fluorescence is not None:
nwbfile[NWBDATASET.FLUORESCENCE] = {}
nwbfile[NWBDATASET.FLUORESCENCE]["Fluorescence"] = {
"table_name": "Fluorescence",
"region": list(range(len(fluorescence))),
"name": "Fluorescence",
"data": fluorescence,
"unit": "lumens",
nwbfile[NWBDATASET.FLUORESCENCE] = {
function_id: {
"Fluorescence": {
"table_name": "ROIs",
"region": list(range(len(fluorescence))),
"name": "Fluorescence",
"data": fluorescence,
"unit": "lumens",
}
}
}

nwbfile[NWBDATASET.COLUMN] = {
"roi_column": {
function_id: {
"name": "iscell",
"discription": "two columns - iscell & probcell",
"data": lccd_data.get("is_cell"),
Expand All @@ -26,9 +29,11 @@ def set_nwbfile(lccd_data, roi_list, fluorescence=None):

# NWB追加
nwbfile[NWBDATASET.POSTPROCESS] = {
"add_roi": lccd_data.get("add_roi", []),
"delete_roi": lccd_data.get("delete_roi", []),
"merge_roi": lccd_data.get("merge_roi", []),
function_id: {
"add_roi": lccd_data.get("add_roi", []),
"delete_roi": lccd_data.get("delete_roi", []),
"merge_roi": lccd_data.get("merge_roi", []),
}
}

return nwbfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
from suite2p import ROI

function_id = node_dirpath.split("/")[-1]
print("start suite2p add_roi:", function_id)

ops = np.load(os.path.join(node_dirpath, "suite2p.npy"), allow_pickle=True).item()
iscell = ops.get("iscell")
stat = ops.get("stat")
Expand Down Expand Up @@ -59,6 +62,6 @@ def execute_add_ROI(node_dirpath, posx, posy, sizex, sizey):
"cell_roi": RoiData(
np.nanmax(im[iscell], axis=0), output_dir=node_dirpath, file_name="cell_roi"
),
"nwbfile": set_nwbfile(ops),
"nwbfile": set_nwbfile(ops, function_id),
}
return info
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


def excute_delete_roi(node_dirpath, ids):
function_id = node_dirpath.split("/")[-1]
print("start suite2p delete_roi:", function_id)

ops = np.load(os.path.join(node_dirpath, "suite2p.npy"), allow_pickle=True).item()
iscell = ops.get("iscell")
delete_roi = ops.get("delete_roi", [])
Expand All @@ -32,7 +35,7 @@ def excute_delete_roi(node_dirpath, ids):
"cell_roi": RoiData(
np.nanmax(im[iscell], axis=0), output_dir=node_dirpath, file_name="cell_roi"
),
"nwbfile": set_nwbfile(ops),
"nwbfile": set_nwbfile(ops, function_id),
}

return info
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def execute_merge_roi(node_dirpath, ids):
from suite2p import ROI
from suite2p.detection.stats import median_pix, roi_stats

function_id = node_dirpath.split("/")[-1]
print("start suite2p merge_roi:", function_id)

ops = np.load(os.path.join(node_dirpath, "suite2p.npy"), allow_pickle=True).item()
iscell = ops.get("iscell")
stat_orig = ops.get("stat")
Expand Down Expand Up @@ -127,6 +130,6 @@ def execute_merge_roi(node_dirpath, ids):
"cell_roi": RoiData(
np.nanmax(im[iscell], axis=0), output_dir=node_dirpath, file_name="cell_roi"
),
"nwbfile": set_nwbfile(ops),
"nwbfile": set_nwbfile(ops, function_id),
}
return info
Loading

0 comments on commit 3db78b0

Please sign in to comment.