<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR%  <>
#
# Distributed under terms of the %LICENSE% license.

"""
This module is used to train liver model with intensity.

First use organ_localizator module to train intensity independent model.
"""

import logging

logger = logging.getLogger(__name__)

import os
import sys
import os.path as op
sys.path.append(op.join(op.dirname(os.path.abspath(__file__)), "../../pysegbase/"))
import argparse
import glob
import traceback
import numpy as np

import io3d
from imtools import qmisc

def add_fv_extern_into_modelparams(modelparams):
    # import PyQt4; PyQt4.QtCore.pyqtRemoveInputHook()
    # import ipdb; ipdb.set_trace()

    if "fv_type" in modelparams.keys() and modelparams['fv_type'] == 'fv_extern':
        if type(modelparams['fv_extern']) == str:
            fv_extern_str = modelparams['fv_extern']
            if fv_extern_str == "intensity_localization_fv":
                modelparams['fv_extern'] = intensity_localization_fv
    return modelparams

def intensity_localization_fv(data3dr, voxelsize_mm, seeds=None, unique_cls=None):        # scale
    """
    Use organ_localizator features plus intensity features

    :param data3dr:
    :param voxelsize_mm:
    :param seeds:
    :param unique_cls:
    :return:
    """
    import scipy
    import numpy as np
    import os.path as op
    try:
        from lisa import organ_localizator
    except:
        import organ_localizator

#         print "po importech"
    fv = []
    f0 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=1).reshape(-1, 1)
    f1 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=3).reshape(-1, 1)
    #f2 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=5).reshape(-1, 1) - f0
    #f3 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=10).reshape(-1, 1) - f0
    #f4 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=20).reshape(-1, 1) - f0
    # position
    #ss = lisa.body_navigation.BodyNavigation(data3dr, voxelsize_mm)
    #ss.feature_function(data3d, voxelsize_mm)
    #fd1 = ss.dist_to_lungs().reshape(-1, 1)
    #fd2 = ss.dist_to_spine().reshape(-1, 1)
    #fd3 = ss.dist_sagittal().reshape(-1, 1)
    #fd4 = ss.dist_coronal().reshape(-1, 1)
    #fd5 = ss.dist_to_surface().reshape(-1, 1)
    #fd6 = ss.dist_diaphragm().reshape(-1, 1)

#         print "pred f6"
    f6 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[20, 1, 1]).reshape(-1, 1) - f1
    f7 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[1, 20, 1]).reshape(-1, 1) - f1
    f8 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[1, 1, 20]).reshape(-1, 1) - f1

#         print "pred organ_localizator"
    ol = organ_localizator.OrganLocalizator()
    ol.load(op.expanduser("~/lisa_data/liver.ol.p"))

    fdall = ol.feature_function(data3dr, voxelsize_mm)


    middle_liver = ol.predict_w(data3dr, voxelsize_mm, 0.85)
    mn = np.median(data3dr[middle_liver==1])
    fdn = np.ones(f0.shape) * mn


    # print "fv shapes ", f0.shape, fd2.shape, fd3.shape
    fv = np.concatenate([
            f0,
            f1,
#                 f2, f3, f4,
#                 fd1, fd2, fd3, fd4, fd5, fd6,
            fdall,
            f6, f7, f8,
            fdn,<commit_msg>change parameters of liver model - removed features, gaussian sigma = 0.5<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR%  <>
#
# Distributed under terms of the %LICENSE% license.

"""
This module is used to train liver model with intensity.

First use organ_localizator module to train intensity independent model.
"""

import logging

logger = logging.getLogger(__name__)

import os
import sys
import os.path as op
sys.path.append(op.join(op.dirname(os.path.abspath(__file__)), "../../pysegbase/"))
import argparse
import glob
import traceback
import numpy as np

import io3d
from imtools import qmisc

def add_fv_extern_into_modelparams(modelparams):
    # import PyQt4; PyQt4.QtCore.pyqtRemoveInputHook()
    # import ipdb; ipdb.set_trace()

    if "fv_type" in modelparams.keys() and modelparams['fv_type'] == 'fv_extern':
        if type(modelparams['fv_extern']) == str:
            fv_extern_str = modelparams['fv_extern']
            if fv_extern_str == "intensity_localization_fv":
                modelparams['fv_extern'] = intensity_localization_fv
    return modelparams

def intensity_localization_fv(data3dr, voxelsize_mm, seeds=None, unique_cls=None):        # scale
    """
    Use organ_localizator features plus intensity features

    :param data3dr:
    :param voxelsize_mm:
    :param seeds:
    :param unique_cls:
    :return:
    """
    import scipy
    import numpy as np
    import os.path as op
    try:
        from lisa import organ_localizator
    except:
        import organ_localizator

#         print "po importech"
    fv = []
    f0 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=0.5).reshape(-1, 1)
    f1 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=3).reshape(-1, 1)
    #f2 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=5).reshape(-1, 1) - f0
    #f3 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=10).reshape(-1, 1) - f0
    #f4 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=20).reshape(-1, 1) - f0
    # position
    #ss = lisa.body_navigation.BodyNavigation(data3dr, voxelsize_mm)
    #ss.feature_function(data3d, voxelsize_mm)
    #fd1 = ss.dist_to_lungs().reshape(-1, 1)
    #fd2 = ss.dist_to_spine().reshape(-1, 1)
    #fd3 = ss.dist_sagittal().reshape(-1, 1)
    #fd4 = ss.dist_coronal().reshape(-1, 1)
    #fd5 = ss.dist_to_surface().reshape(-1, 1)
    #fd6 = ss.dist_diaphragm().reshape(-1, 1)

#         print "pred f6"
#     f6 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[20, 1, 1]).reshape(-1, 1) - f1
#     f7 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[1, 20, 1]).reshape(-1, 1) - f1
#     f8 = scipy.ndimage.filters.gaussian_filter(data3dr, sigma=[1, 1, 20]).reshape(-1, 1) - f1

#         print "pred organ_localizator"
    ol = organ_localizator.OrganLocalizator()
    ol.load(op.expanduser("~/lisa_data/liver.ol.p"))

    fdall = ol.feature_function(data3dr, voxelsize_mm)


    middle_liver = ol.predict_w(data3dr, voxelsize_mm, 0.85)
    mn = np.median(data3dr[middle_liver==1])
    fdn = np.ones(f0.shape) * mn


    # print "fv shapes ", f0.shape, fd2.shape, fd3.shape
    fv = np.concatenate([
            f0,
            f1,
#                 f2, f3, f4,
#                 fd1, fd2, fd3, fd4, fd5, fd6,
            fdall,
            # f6, f7, f8,
            fdn,
{'fraction_non_alphanumeric': 0.13227752639517346, 'fraction_numerical': 0.03363499245852187, 'mean_word_length': 3.090684762492289, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1635526', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2429, 'n_words': 504}