<commit_before>#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Experiment s \"barevným\" modelem jater.

Pro spuštění zkuste help:

python experiments/20130919_liver_statistics.py --help

Měřené vlastnosti se přidávají do get_features().
Pro přidání dalších dat, editujte příslušný yaml soubor.

"""

# import funkcí z jiného adresáře
import sys
import os.path

path_to_script = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(path_to_script, "../src/"))
sys.path.append(os.path.join(path_to_script, "../extern/pyseg_base/src"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/py3DSeedEditor/"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/lbp/"))
#sys.path.append(os.path.join(path_to_script, "../extern/"))
#import featurevector

import logging
logger = logging.getLogger(__name__)

import json
#import apdb
#  apdb.set_trace();
#import scipy.io
import numpy as np
#import scipy
#from scipy import sparse
#import traceback
import itertools

# ----------------- my scripts --------
import py3DSeedEditor
#import dcmreaddata1 as dcmr
#import dcmreaddata as dcmr
import argparse
#import py3DSeedEditor

import misc
import datareader
import matplotlib.pyplot as plt
import experiments


def feat_hist_by_segmentation(data3d_orig, data3d_seg, voxelsize_mm=[1],
                              visualization=True):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    hist1, bin_edges1 = np.histogram(data3d_orig[data3d_seg > 0], bins=bins)
    hist2, bin_edges2 = np.histogram(data3d_orig[data3d_seg <= 0], bins=bins)
    #import pdb; pdb.set_trace()
    if visualization:
        plt_liver = plt.step(bin_edges1[1:], hist1)
        plt_rest = plt.step(bin_edges2[1:], hist2)
        plt.legend([plt_liver, plt_rest], ['Liver', 'Other tissue'])
        #plt.plot(bin_edges1[1:], hist1, bin_edges2[1:], hist2)
        plt.show()

    vvolume = np.prod(voxelsize_mm)
    fv_hist = {'hist1': hist1 * vvolume,
               'hist2': hist2 * vvolume,
               'bins': bins
               }
    return fv_hist


def feat_hist(data3d_orig):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    bins = range(-512, 512, 10)
    bins = range(-512, 512, 64)
    bins = range(-512, 512, 100)
    hist1, bin_edges1 = np.histogram(data3d_orig, bins=bins)
    return hist1


def super_feat_hist(data3d_orig, parameter):
    return feat_hist(data3d_orig)
    #print parameter


def lbp(data3d_orig, data3d_seg, visualization=True):
    import realtime_lbp as real_lib
    realLbp = real_lib.loadRealtimeLbpLibrary()
    lbpRef = np.zeros([1, 256])
    lbpRef = real_lib.realTimeLbpImNp(realLbp, data3d_orig[:, :, 1])
    return lbpRef


def lbp3d(data3d_orig, filename, visualization=True):
    from lbp import lbp3d
    lib3d = lbp3d.loadLibrary()
    f = open(filename, 'r')
    maskJSON = json.load(f)
    mask = maskJSON['mask']
    lbp3d.coordToPoints(
        mask,
        data3d_orig.shape[2],
        data3d_orig.shape[1])
    res = lbp3d.compute(
        lib3d,
        data3d_orig,
        mask)
    return res


def f_lbp3d(data3d_orig):
    return lbp3d(
        data3d_orig,
        '/home/petr/Dokumenty/git/lbpLibrary/masks/mask3D_8_4.json',
        True)
<commit_msg>Update tiled_liver_statistics.py<commit_after>#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Experiment s \"barevným\" modelem jater.

Pro spuštění zkuste help:

python experiments/20130919_liver_statistics.py --help

Měřené vlastnosti se přidávají do get_features().
Pro přidání dalších dat, editujte příslušný yaml soubor.

"""

# import funkcí z jiného adresáře
import sys
import os.path

path_to_script = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(path_to_script, "../src/"))
sys.path.append(os.path.join(path_to_script, "../extern/pyseg_base/src"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/py3DSeedEditor/"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/lbp/"))
#sys.path.append(os.path.join(path_to_script, "../extern/"))
#import featurevector

import logging
logger = logging.getLogger(__name__)

import json
#import apdb
#  apdb.set_trace();
#import scipy.io
import numpy as np
#import scipy
#from scipy import sparse
#import traceback
import itertools

# ----------------- my scripts --------
import py3DSeedEditor
#import dcmreaddata1 as dcmr
#import dcmreaddata as dcmr
import argparse
#import py3DSeedEditor

import misc
import datareader
import matplotlib.pyplot as plt
import experiments


def feat_hist_by_segmentation(data3d_orig, data3d_seg, voxelsize_mm=[1],
                              visualization=True):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    hist1, bin_edges1 = np.histogram(data3d_orig[data3d_seg > 0], bins=bins)
    hist2, bin_edges2 = np.histogram(data3d_orig[data3d_seg <= 0], bins=bins)
    #import pdb; pdb.set_trace()
    if visualization:
        plt_liver = plt.step(bin_edges1[1:], hist1)
        plt_rest = plt.step(bin_edges2[1:], hist2)
        plt.legend([plt_liver, plt_rest], ['Liver', 'Other tissue'])
        #plt.plot(bin_edges1[1:], hist1, bin_edges2[1:], hist2)
        plt.show()

    vvolume = np.prod(voxelsize_mm)
    fv_hist = {'hist1': hist1 * vvolume,
               'hist2': hist2 * vvolume,
               'bins': bins
               }
    return fv_hist


def feat_hist(data3d_orig):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    bins = range(-512, 512, 10)
    bins = range(-512, 512, 64)
    bins = range(-512, 512, 100)
    hist1, bin_edges1 = np.histogram(data3d_orig, bins=bins)
    return hist1


def super_feat_hist(data3d_orig, parameter):
    return feat_hist(data3d_orig)
    #print parameter


def lbp(data3d_orig, data3d_seg, visualization=True):
    import realtime_lbp as real_lib
    realLbp = real_lib.loadRealtimeLbpLibrary()
    lbpRef = np.zeros([1, 256])
    lbpRef = real_lib.realTimeLbpImNp(realLbp, data3d_orig[:, :, 1])
    return lbpRef


def lbp3d(data3d_orig, filename, visualization=True):
    from lbp import lbp3d
    lib3d = lbp3d.load()
    f = open(filename, 'r')
    maskJSON = json.load(f)
    mask = maskJSON['mask']
    lbp3d.coordToPoints(
        mask,
        data3d_orig.shape[2],
        data3d_orig.shape[1])
    res = lbp3d.compute(
        lib3d,
        data3d_orig,
        mask)
    return res


def f_lbp3d(data3d_orig):
    return lbp3d(
        data3d_orig,
        '/home/petr/Dokumenty/git/lbpLibrary/masks/mask3D_8_4.json',
        True)

{'fraction_non_alphanumeric': 0.12806168732990625, 'fraction_numerical': 0.04052010885999395, 'mean_word_length': 3.050826699326393, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5612823', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2472, 'n_words': 501}