Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cellacdc/acdc_bioio_bioformats/_read_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
metadataXML = bioformats.get_omexml_metadata(raw_filepath)
metadata = bioformats.OMEXML().init_from_metadata(metadataXML)

print(metadata)

os.makedirs(bioio_sample_data_folderpath, exist_ok=True)
metadataXML_filepath = os.path.join(
bioio_sample_data_folderpath, 'metadataXML.txt'
Expand Down
6 changes: 4 additions & 2 deletions cellacdc/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3052,10 +3052,11 @@ def __init__(
label = QLabel(txt)
entriesLayout.addWidget(label, row, 0, alignment=Qt.AlignRight)
entriesLayout.addWidget(self.SizeS_SB, row, 1)
self.SizeS_SB.valueChanged.connect(self.SizeSvalueChanged)


if rawDataStruct == 0:
row += 1
self.SizeS_SB.setValue(1)
self.SizeS_SB.setDisabled(True)
self.posSelector = widgets.ExpandableListBox()
positions = ['All positions']
positions.extend([f'Position_{i+1}' for i in range(SizeS)])
Expand All @@ -3064,6 +3065,7 @@ def __init__(
label = QLabel(txt)
entriesLayout.addWidget(label, row, 0, alignment=Qt.AlignRight)
entriesLayout.addWidget(self.posSelector, row, 1)
self.SizeS_SB.valueChanged.connect(self.SizeSvalueChanged)

row += 1
self.LensNA_DSB = QDoubleSpinBox()
Expand Down
148 changes: 92 additions & 56 deletions cellacdc/dataStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class bioFormatsWorker(QObject):
progressPbar = Signal(int)
initPbar = Signal(int)
criticalError = Signal(str, str, str)
filesExisting = Signal(str)
confirmMetadata = Signal(
str, float, int, int, int, int,
float, str, float, float, float,
Expand All @@ -86,7 +85,9 @@ def __init__(
self, raw_src_path, rawFilenames, exp_dst_path,
mutex, waitCond, rawDataStruct,
bioformats_backend: Literal['bioio', 'python-bioformats'],
lazy_load=True, move_raw_microscopy_files=True
lazy_load=True, move_raw_microscopy_files=True,
overwrite=False, add_files=False, create_new=False,
start_pos_n=1
):
QObject.__init__(self)
self.raw_src_path = raw_src_path
Expand All @@ -98,9 +99,11 @@ def __init__(
self.overWriteMetadata = False
self.trustMetadataReader = False
self.rawDataStruct = rawDataStruct
self.overwritePos = False
self.addFiles = False
self.overwritePos = overwrite
self.addFiles = add_files
self.createNew = create_new
self.cancel = False
self.start_pos_n = start_pos_n
self.bioformats_backend = bioformats_backend
self.lazy_load = lazy_load
self.move_raw_microscopy_files = move_raw_microscopy_files
Expand Down Expand Up @@ -505,7 +508,7 @@ def readMetadata(self, raw_src_path, filename):
self.PhysicalSizeZ = PhysicalSizeZ
# self.chNames = chNames
self.emWavelens = emWavelens
return
return False

sampleImgData = None
while True:
Expand Down Expand Up @@ -563,42 +566,42 @@ def readMetadata(self, raw_src_path, filename):
self.saveChannels = self.metadataWin.saveChannels
self.emWavelens = self.metadataWin.emWavelens
self.addImageName = self.metadataWin.addImageName

return False

def saveToPosFolder(
self, p, raw_src_path, exp_dst_path, filename, series, p_idx=0
self, p, raw_src_path, exp_dst_path, filename, series, pos_n,
p_idx=0,
):
rawFilePath = os.path.join(raw_src_path, filename)

if os.path.basename(raw_src_path) == 'raw_microscopy_files':
raw_src_path = os.path.dirname(raw_src_path)
pos_name = f'Position_{p+1}'

in_file_pos_name = f'Position_{p+1}'
savePos = (
'All Positions' in self.selectedPos or pos_name in self.selectedPos
'All Positions' in self.selectedPos
or in_file_pos_name in self.selectedPos
)
if not savePos:
return False

pos_path = os.path.join(exp_dst_path, f'Position_{p+1}')
pos_path = os.path.join(exp_dst_path, f'Position_{pos_n}')
images_path = os.path.join(pos_path, 'Images')

if os.path.exists(images_path) and self.askReplacePosFiles:
self.askReplacePosFiles = False
self.mutex.lock()
self.filesExisting.emit(pos_path)
self.waitCond.wait(self.mutex)
self.mutex.unlock()
if self.cancel:
return True

if os.path.exists(images_path) and self.overwritePos:
shutil.rmtree(images_path)

if os.path.exists(images_path) and self.createNew:
images_path = re.sub(
r'Position_\d+', f'Position_{pos_n}', images_path
)

if not os.path.exists(images_path):
os.makedirs(images_path, exist_ok=True)

self.saveData(
images_path, rawFilePath, filename, p, series, p_idx=p_idx
images_path, rawFilePath, filename, p, series, pos_n, p_idx=p_idx,
)

return False
Expand Down Expand Up @@ -776,16 +779,19 @@ def saveImgDataChannel(
shutil.move(tempFilepath, filePath)
shutil.rmtree(tempDir)

def saveData(self, images_path, rawFilePath, filename, p, series, p_idx=0):
def saveData(
self, images_path, rawFilePath, filename, p, series, pos_n,
p_idx=0
):
if self.bioformats_backend == 'bioio':
from cellacdc import acdc_bioio_bioformats as bioformats
else:
import javabridge
from cellacdc import bioformats
s0p = str(p+1).zfill(self.numPosDigits)

s0p = str(pos_n).zfill(self.numPosDigits)
self.progress.emit(
f'Position {p+1}/{self.numPos}: saving data to {images_path}...'
f'Position {pos_n}/{self.numPos}: saving data to {images_path}...'
)
filenameNOext, ext = os.path.splitext(filename)

Expand Down Expand Up @@ -920,7 +926,7 @@ def saveData(self, images_path, rawFilePath, filename, p, series, p_idx=0):
if not saveCh:
continue

rawFilename = f'{basename}{p+1}_{chName}'
rawFilename = f'{basename}{pos_n}_{chName}'
pos_rawFilenames.append(rawFilename)
raw_src_path = os.path.dirname(rawFilePath)
rawFilePath = [
Expand Down Expand Up @@ -981,7 +987,7 @@ def saveData(self, images_path, rawFilePath, filename, p, series, p_idx=0):
if self.moveOtherFiles or self.copyOtherFiles:
# Move the other files present in the folder if they
# contain "otherFilename" in the name
otherFilename = f'{basename}{p+1}'
otherFilename = f'{basename}{pos_n}'
rawFilePath = set()
for f in myutils.listdir(raw_src_path):
notRawFile = all(
Expand Down Expand Up @@ -1027,39 +1033,42 @@ def run(self):
self.cancelled = False
self.isCriticalError = False
for p, filename in enumerate(self.rawFilenames):
pos_n = p + self.start_pos_n
if self.rawDataStruct == 0:
if not self.overWriteMetadata:
abort = self.readMetadata(raw_src_path, filename)
if abort:
cancel = self.readMetadata(raw_src_path, filename)
if cancel:
self.cancelled = True
break

self.numPos = self.SizeS
self.numPosDigits = len(str(self.numPos))
if p == 0:
self.initPbar.emit(self.numPos*self.SizeC)
for p in range(self.SizeS):
abort = self.saveToPosFolder(
p, raw_src_path, exp_dst_path, filename, p

for in_file_p in range(self.SizeS):
cancel = self.saveToPosFolder(
in_file_p, raw_src_path, exp_dst_path, filename,
in_file_p, pos_n
)
if abort:
if cancel:
self.cancelled = True
break

elif self.rawDataStruct == 1:
if not self.overWriteMetadata:
abort = self.readMetadata(raw_src_path, filename)
if abort:
cancel = self.readMetadata(raw_src_path, filename)
if cancel:
self.cancelled = True
break
self.numPos = len(self.rawFilenames)
self.numPosDigits = len(str(self.numPos))
if p == 0:
self.initPbar.emit(self.numPos*self.SizeC)
abort = self.saveToPosFolder(
p, raw_src_path, exp_dst_path, filename, 0
cancel = self.saveToPosFolder(
p, raw_src_path, exp_dst_path, filename, 0, pos_n
)
if abort:
if cancel:
self.cancelled = True
break

Expand Down Expand Up @@ -1088,7 +1097,7 @@ def run(self):
for p_idx, pos in enumerate(self.posNums):
p = pos-1
abort = self.saveToPosFolder(
p, raw_src_path, exp_dst_path, self.basename,
p, raw_src_path, exp_dst_path, self.basename, pos_n,
0, p_idx=p_idx
)
if abort:
Expand Down Expand Up @@ -1565,7 +1574,14 @@ def main(self):
if not exp_dst_path:
self.close()
return

out = self.askPosFoldersExisting(exp_dst_path)
if out is None:
self.close()
return

overwrite, add_files, create_new, start_pos_n = out

self.log('Instructing to move raw data...')
loadEntirePosIntoRam = self.askHowToLoadData()
if loadEntirePosIntoRam is None:
Expand Down Expand Up @@ -1599,7 +1615,11 @@ def main(self):
self.mutex, self.waitCond, rawDataStruct,
self.bioformats_backend,
lazy_load=not self.loadEntirePosIntoRam,
move_raw_microscopy_files=move_raw_microscopy_files
move_raw_microscopy_files=move_raw_microscopy_files,
overwrite=overwrite,
add_files=add_files,
create_new=create_new,
start_pos_n=start_pos_n,
)
if self.rawDataStruct == 2:
self.worker.basename = self.basename
Expand All @@ -1621,7 +1641,6 @@ def main(self):
self.worker.critical.connect(self.workerCritical)
self.worker.criticalError.connect(self.criticalBioFormats)
self.worker.confirmMetadata.connect(self.askConfirmMetadata)
self.worker.filesExisting.connect(self.askReplacePos)
self.thread.started.connect(self.worker.run)

self.thread.start()
Expand Down Expand Up @@ -2016,6 +2035,7 @@ def warnMultipleFiles(self, files):
return []
else:
files = [win.selectedItemText]
return files

def attemptSeparateMultiChannel(self, rawFilenames):
self.chNames = set()
Expand Down Expand Up @@ -2130,25 +2150,41 @@ def askConfirmMetadata(
self.worker.metadataWin = self.metadataWin
self.waitCond.wakeAll()

def askReplacePos(self, pos_path):
msg = widgets.myMessageBox()
def askPosFoldersExisting(self, exp_dst_path):
pos_foldernames = myutils.get_pos_foldernames(exp_dst_path)
if not pos_foldernames:
return False, False, False, 1

msg = widgets.myMessageBox(wrapText=False)
txt = html_utils.paragraph(
f'The following folder <b>already exists</b>.<br><br>'
f'<code>{pos_path}</code><br><br>'
'Do you want to <b>overwrite</b> all of its content or '
'<b>add files</b> to it?'
'The selected destination folder <b>already contains Position folders</b>.<br><br>'
'Do you want to <b>overwrite</b> all of its content, '
'<b>add files</b> to the existing Position folders,<br>'
'or <b>create new</b> Position folders?'
)
cancelButton, overwriteButton, addFilesButton = msg.warning(
self, 'Replace files?', txt,
buttonsTexts=('Cancel', 'Overwrite', 'Add files')
_, overwriteButton, addFilesButton, createNewButton = msg.warning(
self, 'Warning: existing Position folders detected!', txt,
buttonsTexts=(
'Cancel',
'Overwrite',
'Add files',
widgets.newFilePushButton('Create new'),
),
path_to_browse=exp_dst_path
)
if msg.cancel:
self.worker.cancel = True
elif overwriteButton == msg.clickedButton:
self.worker.overwritePos = True
elif addFilesButton == msg.clickedButton:
self.worker.addFiles = True
self.waitCond.wakeAll()
return

overwrite = overwriteButton == msg.clickedButton
add_files = addFilesButton == msg.clickedButton
create_new = createNewButton == msg.clickedButton

start_pos_n = 1
if create_new:
pos_ns = [int(pos.split('_')[-1]) for pos in pos_foldernames]
start_pos_n = max(pos_ns) + 1

return overwrite, add_files, create_new, start_pos_n

def closeEvent(self, event):
self.logger.info('Closing data structure logger...')
Expand Down
Loading