<commit_before># @File(label="Select a directory", style="directory") parent_dir
# @Dataset dataset
# @DatasetIOService io
# @OpService ops
# @DatasetService ds

import os
from ij.plugin.frame import RoiManager

def get_axis(axis_type):
	from net.imagej.axis import Axes
	return {'X': Axes.X, 'Y': Axes.Y, 'Z': Axes.Z, 'TIME': Axes.TIME,
		     'CHANNEL': Axes.CHANNEL}.get(axis_type, Axes.Z)
 
def crop(ops, data, intervals):
	from net.imglib2.util import Intervals
	
	intervals_start = [data.min(d) for d in range(0, data.numDimensions())]
	intervals_end = [data.max(d) for d in range(0, data.numDimensions())]
 
	for axis_type, interval in intervals.items():
		index = data.dimensionIndex(get_axis(axis_type))
		intervals_start[index] = interval[0]
		intervals_end[index] = interval[1]
 
	intervals = Intervals.createMinMax(*intervals_start + intervals_end)
	output = ops.run("transform.crop", data, intervals, True)
	return output

rm = RoiManager.getInstance()
filename = dataset.getName()

roi_path = os.path.join(parent_dir.toString(), os.path.splitext(filename)[0] + ".zip")
image_path = os.path.join(parent_dir.toString(), os.path.splitext(filename)[0] + ".tif")

rm.runCommand("Deselect")
rm.runCommand("Save", roi_path)
print("ROIs saved at {}".format(roi_path))

# Crop the image
z_position = rm.getRoi(0).getZPosition()
intervals = {'Z': [z_position, z_position]}
output = crop(ops, dataset, intervals)
output = ops.run("transform.dropSingletonDimensionsView", output)
output = ds.create(output)

io.save(output, image_path)
print("Image saved at {}".format(image_path))<commit_msg>fix ROI_and_Image_saver.py<commit_after># @File(label="Select a directory", style="directory") parent_dir
# @Dataset dataset
# @DatasetIOService io
# @OpService ops
# @DatasetService ds

import os
from ij.plugin.frame import RoiManager

def get_axis(axis_type):
	from net.imagej.axis import Axes
	return {'X': Axes.X, 'Y': Axes.Y, 'Z': Axes.Z, 'TIME': Axes.TIME,
		     'CHANNEL': Axes.CHANNEL}.get(axis_type, Axes.Z)

def crop(ops, data, intervals):
	from net.imglib2.util import Intervals

	intervals_start = [data.min(d) for d in range(0, data.numDimensions())]
	intervals_end = [data.max(d) for d in range(0, data.numDimensions())]

	for axis_type, interval in intervals.items():
		index = data.dimensionIndex(get_axis(axis_type))
		intervals_start[index] = interval[0]
		intervals_end[index] = interval[1]

	intervals = Intervals.createMinMax(*intervals_start + intervals_end)
	output = ops.run("transform.crop", data, intervals, True)
	return output

rm = RoiManager.getInstance()
filename = dataset.getName()

roi_path = os.path.join(parent_dir.toString(), filename.replace(".lif", "") + ".zip")
image_path = os.path.join(parent_dir.toString(), filename.replace(".lif", "") + ".tif")

rm.runCommand("Deselect")
rm.runCommand("Save", roi_path)
print("ROIs saved at {}".format(roi_path))

# Crop the image
z_position = rm.getRoi(0).getZPosition()
intervals = {'Z': [z_position, z_position]}
output = crop(ops, dataset, intervals)
output = ops.run("transform.dropSingletonDimensionsView", output)
output = ds.create(output)

io.save(output, image_path)
print("Image saved at {}".format(image_path))
{'fraction_non_alphanumeric': 0.1529558961526431, 'fraction_numerical': 0.004379105411323116, 'mean_word_length': 5.93709327548807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1893347', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1120, 'n_words': 248}