def test_region_by_identifier(self):

        from jicbioimage.segment import SegmentedImage

        input_array = np.array([[0, 0, 0],
                                [1, 1, 1],
                                [2, 2, 2]])

        segmented_image = SegmentedImage.from_array(input_array)

        with self.assertRaises(ValueError):
            segmented_image.region_by_identifier(0)

        with self.assertRaises(ValueError):
            segmented_image.region_by_identifier(0.5)

        with self.assertRaises(ValueError):
            segmented_image.region_by_identifier(-1)

        from jicbioimage.segment import Region

        selected_region = segmented_image.region_by_identifier(1)

        self.assertTrue(isinstance(selected_region, Region))

        expected_output = Region.select_from_array(input_array, 1)
        self.assertTrue(np.array_equal(selected_region,
                                       expected_output))
    def test_dilate(self):
        from jicbioimage.segment import Region

        test_array = np.array([[0, 0, 0, 0, 0],
                               [0, 0, 1, 1, 0],
                               [0, 1, 1, 0, 0],
                               [0, 1, 0, 0, 0],
                               [0, 0, 0, 0, 0]])

        region = Region(test_array)

        dilate_array = np.array([[0, 0, 1, 1, 0],
                                 [0, 1, 1, 1, 1],
                                 [1, 1, 1, 1, 0],
                                 [1, 1, 1, 0, 0],
                                 [0, 1, 0, 0, 0]], dtype=bool)

        self.assertTrue(np.array_equal(region.dilate(),
                                       dilate_array))
    def test_region_select_from_array(self):
        from jicbioimage.segment import Region

        id_array = np.array([[0, 0, 0],
                             [1, 1, 1],
                             [2, 2, 2]])

        region_1 = Region.select_from_array(id_array, 1)

        self.assertFalse(region_1[0, 0])
        self.assertTrue(region_1[1, 0])
        self.assertFalse(region_1[2, 0])

        self.assertEqual(region_1.area, 3)
def main():
    # Parse the command line arguments.
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("input_file", help="Input file")
    parser.add_argument("mask_file", help="Mask file")
    parser.add_argument("parameters_file", help="Parameters file")
    parser.add_argument("output_dir", help="Output directory")
    parser.add_argument("--debug", default=False, action="store_true",
                        help="Write out intermediate images")
    args = parser.parse_args()

    # Check that the input file exists.
    if not os.path.isfile(args.input_file):
        parser.error("{} not a file".format(args.input_file))
    if not os.path.isfile(args.parameters_file):
        parser.error("{} not a file".format(args.parameters_file))

    # Read in the parameters.
    params = Parameters.from_file(args.parameters_file)

    # Create the output directory if it does not exist.
    if not os.path.isdir(args.output_dir):
        os.mkdir(args.output_dir)
    AutoName.directory = args.output_dir

    # Only write out intermediate images in debug mode.
    if not args.debug:
        AutoWrite.on = False

    # Setup a logger for the script.
    log_fname = "audit.log"
    log_fpath = os.path.join(args.output_dir, log_fname)
    logging_level = logging.INFO
    if args.debug:
        logging_level = logging.DEBUG
    logging.basicConfig(filename=log_fpath, level=logging_level)

    # Log some basic information about the script that is running.
    logging.info("Script name: {}".format(__file__))
    logging.info("Script version: {}".format(__version__))
    logging.info("Parameters: {}".format(params))

    # Run the analysis.
    mask_im = Image.from_file(args.mask_file)
    mask = Region.select_from_array(mask_im, 0)
    identity(mask)
    analyse_file(args.input_file, mask, args.output_dir, **params)
    def test_background(self):

        from jicbioimage.segment import SegmentedImage

        input_array = np.array([[0, 0, 0],
                                [1, 1, 1],
                                [2, 2, 2]])

        segmented_image = SegmentedImage.from_array(input_array)

        from jicbioimage.segment import Region

        background = segmented_image.background

        expected_output = Region.select_from_array(input_array, 0)
        self.assertTrue(np.array_equal(background,
                                       expected_output))