#!/usr/bin/env python

# Copyright 2016-2023 Biomedical Imaging Group Rotterdam, Departments of
# Medical Informatics and Radiology, Erasmus MC, Rotterdam, The Netherlands
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pandas as pd
import numpy as np
import os
import SimpleITK as sitk

currentdir = os.path.dirname(os.path.realpath(__file__))


def create_random_features(n_objects=7, n_features=10):
    """
    Create n_objects sets of random features and save in files. Format based
    on PREDICT python package.
    """
    # Create some input values for all objects
    feature_labels = [f'rf_randomlabel_{i}' for i in range(n_features)]
    image_type = 'None'
    parameters = {'Random': 'True'}
    panda_labels = ['image_type', 'parameters', 'feature_values',
                    'feature_labels']

    for i in range(n_objects):
        # Create output name and random feature values and labels
        if i < float(n_objects) / 2.0:
            feature_values = [np.random.normal(loc=5.0, scale=2.0) for i in range(n_features)]
        else:
            feature_values = [np.random.normal(loc=10.0, scale=2.0) for i in range(n_features)]

        output = os.path.join(currentdir, f'examplefeatures_Patient-{str(i).zfill(3)}.hdf5')

        # Convert to pandas Series and save as hdf5
        panda_data = pd.Series([image_type, parameters, feature_values,
                                feature_labels],
                               index=panda_labels,
                               name='Image features'
                               )

        print(f'Saving image features for object {i}.')
        panda_data.to_hdf(output, 'image_features')
        
        
def create_random_imageandmask(size=512, slices=20):
    # Create the image
    image = np.rand.rand((size, size, slices))
    
    # Create a sphere mask in the center
    ra = range(-int(slices/2), int(slices/2)+1)
    x, y, z = np.meshgrid(ra, ra, ra)
    radius = np.sqrt(x**2 + y**2 + z**2)
    mask = radius.astype(int) == int(size/4)

    # Convert the images to ITK objects
    image = sitk.GetImageFromArray(image.astype(np.float32))
    mask = sitk.GetImageFromArray(mask.astype(int))

    return image, mask
    

if __name__ == "__main__":
    create_random_features()
    create_random_imageandmask()