text:
# based off of Aaron's SVM network in http://compneuro.uwaterloo.ca/publications/voelker2016a.html

from constants import *
from dataman import create_feed_net

import nengo
from nengolib.synapses import HeteroSynapse, Bandpass
from sklearn import svm
import numpy as np

def multisynapse(src, dest, sub_features):

    synapses = [Bandpass(freq, Q) for (freq, Q) in sub_features]
    syn = nengo.Node(size_in=1, output=HeteroSynapse(synapses, dt=dt))

    nengo.Connection(src, syn, synapse=None)
    nengo.Connection(
        syn, dest.neurons, synapse=None,
        function=lambda x, transform=np.squeeze(dest.encoders): transform*x)

def svm_freq(t_len, dims, n_classes, alif=False):

    tau = 0.1
    # setup the feature craziness
    freq_range = (0, 500)
    Q_range = (2, 50)
    features_per_dim = 400
    feat_pops = []
    feat_list = []
    enc_list = []

    def train(dataset, corset):

        # make a model and run it to get spiking data
        # as acquired when passed through multiple bandpass filters
        train_model = nengo.Network()
        with train_model:
            feed_net = create_feed_net(dataset, corset, t_len, dims, n_classes)

            if alif:
                state = nengo.networks.EnsembleArray(n_neurons, dims, seed=SEED, neuron_type=nengo.AdaptiveLIF())
            else:
                state = nengo.networks.EnsembleArray(n_neurons, dims, seed=SEED)

            nengo.Connection(feed_net.q_in, state.input, synapse=None)

            for dim in range(dims):
                # this declaration needed for the multisynapse transform
                encoders = nengo.dists.UniformHypersphere(
                            surface=True).sample(features_per_dim, 1)
                enc_list.append(encoders)
                feat_pop = nengo.Ensemble(features_per_dim, 1, encoders=encoders, seed=SEED+dim)
                feat_pops.append(feat_pop)

                sub_features = zip(
                    nengo.dists.Uniform(*freq_range).sample(features_per_dim),
                    nengo.dists.Uniform(*Q_range).sample(features_per_dim)
                )
                feat_list.append(sub_features)
                multisynapse(state.ensembles[dim], feat_pop, sub_features)

            p_target = nengo.Probe(feed_net.get_ans, sample_every=sample_every, synapse=None)

            p_features = [
                nengo.Probe(
                    feat_pop.neurons, sample_every=sample_every, synapse=tau)
                for feat_pop in feat_pops]

        print("training simulation start")
        sim_train = nengo.Simulator(train_model)
        with sim_train:
            sim_train.run((t_len + PAUSE)*dataset.shape[0])
        print("training simulation done")

        # TODO: Enable logging and close the files here

        # pass the feature data and the target to train an SVM
        print("Training SVM")

        feature_num = features_per_dim*dims

        # decode the labels into 1D (should be done in numpy)
        yv = []
        for tar_val in sim_train.data[p_target]:
            if np.any(tar_val == 1):
                yv.append(np.argmax(tar_val))
            else:
                yv.append(0)
        yv = np.array(yv)

        xv = np.zeros((yv.shape[0], feature_num))
        for p_i, p_feat in enumerate(p_features):
            xv[:, p_i*400:(p_i+1)*400] = sim_train.data[p_feat]

        clf = svm.LinearSVC().fit(xv, yv)
        # Need to use clf.coef_, clf.intercept_

        # this basically just forms the coefficients into weights, it's basically a reshape operation
        weights = np.zeros((len(feat_list), features_per_dim, n_classes))
        for i in xrange(feature_num):
            weights[i // features_per_dim, i % features_per_dim] = clf.coef_[:, i]

        return weights, clf.intercept_

    def test(weights, intercept, testset=None):
        # run the test data with the SVM

        test_model = nengo.Network()
        with test_model:
            feed_net = create_feed_net(testset[0], testset[1], t_len, dims, n_classes)

            if alif:
                state = nengo.networks.EnsembleArray(n_neurons, dims, seed=SEED, neuron_type=nengo.AdaptiveLIF())
            else:
                state = nengo.networks.EnsembleArray(n_neurons, dims, seed=SEED)

            nengo.Connection(feed_net.q_in, state.input, synapse=None)

            # synapses for smoothing, because that's important according to Aaron's paper
            predict_tau = 0.005
            lowpass = 0.05
            tau_ratio = predict_tau / lowpass

            scores = nengo.Node(size_in=n_classes)
            predict = nengo.networks.EnsembleArray(n_neurons, n_classes, seed=SEED+1)

            nengo.Connection(scores, predict.input, transform=tau_ratio,
                             synapse=predict_tau)
            nengo.Connection(predict.input, predict.output,
                             transform=1-tau_ratio, synapse=predict_tau)

            for dim in range(dims):
                feat_pop = nengo.Ensemble(features_per_dim, 1, encoders=enc_list[dim],
                                          seed=SEED+dim)
                multisynapse(state.ensembles[dim], feat_pop, feat_list[dim])

                nengo.Connection(feat_pop.neurons, scores,
                                 transform=weights[dim].T, synapse=None)

            bias = nengo.Node(output=[1], label="bias")
            nengo.Connection(bias, scores,
                             transform=intercept[:, None], synapse=None)

            p_out = nengo.Probe(predict.output, sample_every=sample_every)
            p_correct = nengo.Probe(feed_net.get_ans, sample_every=sample_every)

        print("test simulation start")
        sim_test = nengo.Simulator(test_model)
        with sim_test:
            sim_test.run((t_len + PAUSE)*testset[0].shape[0])
        print("test simulation done")

        # TODO: Enable logging and close the files here

        return sim_test.data[p_out], sim_test.data[p_correct]

    return train, test


text:
import numpy as np
from Simulator import SimulatorEnv
from drone_network import DroneNetwork
from tensorflow.keras.optimizers import Adam
from rl.agents import DDPGAgent
from rl.memory import SequentialMemory

# Get the environment and extract the number of actions.
env = SimulatorEnv()
np.random.seed(123)
env.seed(123)
assert len(env.action_space.shape) == 1
nb_actions = env.action_space.shape[0]

n = DroneNetwork(nb_actions=nb_actions, observation_shape=env.observation_space.shape)

# Next, we build a very simple model.
actor = n.create_actor()
critic = n.create_critic()
action_input = n.get_action_input()
critic.summary()
memory = SequentialMemory(limit=100000, window_length=1)

agent = DDPGAgent(nb_actions=nb_actions, actor=actor, critic=critic, critic_action_input=action_input,
                  memory=memory)

agent.compile(Adam(lr=.001, clipnorm=1.), metrics=['mae'])

agent.fit(env, nb_steps=15000, visualize=True, verbose=2, nb_max_episode_steps=200)
agent.save_weights('ddpg_{}_weights.h5f'.format('drone'), overwrite=True)
print('testing')
agent.test(env, nb_episodes=10, visualize=True, nb_max_episode_steps=200)
env.close()

ext: py

max_issues_repo_name: SwapnilMad/Drone-Reinforcement-Learning

max_forks_repo_name: SwapnilMad/Drone-Reinforcement-Learning

reason: import numpy


text:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import inception_resnet_v2
from tensorflow.contrib.slim.nets import inception
import numpy as np
import pandas as pd
from PIL import Image
import PIL

import argparse
import sys

slim = tf.contrib.slim
parser = argparse.ArgumentParser()

parser.add_argument('--input_dir',type=str, default='./dev_data/')
parser.add_argument('--output_dir',type=str, default='./output/')
parser.add_argument('--dev_dir',type=str, default='./dev_data/')
parser.add_argument("-iw", "--is_wrong", help="wrong",action="store_true")
args = parser.parse_args()
dev=pd.read_csv(os.path.join(args.dev_dir,'dev.csv'))
if args.is_wrong:
    try:
        wrong_list=pd.read_csv('./wrong_list.csv',header=None)
        dev=dev[dev.filename.isin(wrong_list[0])].reset_index(drop=True)
    except:
        sys.exit(0)
else:
    try:
        wrong_list=pd.read_csv('./wrong_list.csv',header=None)
        t_dev=dev[dev.filename.isin(wrong_list[0])].reset_index(drop=True)
        if len(t_dev)==0: sys.exit(0)
    except:
        pass

num_classes=110
batch_size=1
x=tf.placeholder(tf.float32, shape=[1,299,299,3])
x_input=x/255*2-1

with slim.arg_scope(inception.inception_v3_arg_scope()):
    logits_inception_v3, end_points_inception_v3 = inception.inception_v3(x_input, num_classes=110, is_training=False, scope='InceptionV3')

with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
    logits_inception_resnet_v2, end_points_inception_resnet_v2 = inception_resnet_v2.inception_resnet_v2(x_input, num_classes=110, is_training=False, scope='InceptionResnetV2')

model_checkpoint_map = {
    'resnet_v1_50': os.path.join('./checkpoints/', 'resnet_v1_50','model.ckpt-49800'),
    'vgg_16': os.path.join('./checkpoints/', 'vgg_16', 'vgg_16.ckpt'),
    'InceptionV3': os.path.join('./checkpoints/', 'inception_v3', 'inception_v3.ckpt'),
    'InceptionResnetV2': os.path.join('./checkpoints/', 'inception_resnet_v2', 'model.ckpt-193019'),
}
s2 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
s3 = tf.train.Saver(slim.get_model_variables(scope='InceptionResnetV2'))

r1=[]
r2=[]
d=[]

with tf.Session() as sess:
    s2.restore(sess, model_checkpoint_map['InceptionV3'])
    s3.restore(sess, model_checkpoint_map['InceptionResnetV2'])
    for _,row in dev.iterrows():
        adv_image = Image.open(os.path.join(args.output_dir,row.filename), mode='r')
        raw_image = Image.open(os.path.join(args.input_dir,row.filename), mode='r')
        adv_image_arr = np.array(adv_image).astype(np.int32)
        raw_image_arr = np.array(raw_image).astype(np.int32)
        diff = adv_image_arr .reshape((-1, 3)) - raw_image_arr.reshape((-1, 3))
        distance = np.mean(np.sqrt(np.sum((diff ** 2), axis=1)))
        d.append(distance)
        adv_image = np.array(adv_image).astype(np.float32).reshape(1,299,299,3)
        a,b=sess.run([logits_inception_v3,logits_inception_resnet_v2],feed_dict={x:adv_image})
        r1.append(a)
        r2.append(b)
r1=np.concatenate(r1)
r2=np.concatenate(r2)
r1=r1.argmax(1)
r2=r2.argmax(1)
result=pd.DataFrame({'r1':r1,'r2':r2,'d':d})
result=dev.join(result)
r1_score=result[result.targetedLabel==result.r1].d.mean()*len(result[result.targetedLabel==result.r1])/len(dev)+64*(1-len(result[result.targetedLabel==result.r1])/len(dev))
r2_score=result[result.targetedLabel==result.r2].d.mean()*len(result[result.targetedLabel==result.r2])/len(dev)+64*(1-len(result[result.targetedLabel==result.r2])/len(dev))
result[~((result.targetedLabel==result.r1)&(result.targetedLabel==result.r2))].filename.to_csv('wrong_list.csv',index=False)
#wrong_list=pd.read_csv('./wrong_list.csv',header=None)[0]
t=result[(result.targetedLabel==result.r1)&(result.targetedLabel==result.r2)]
print(t.d.mean())#,file=open("output.txt", "a+"))
print('logits_inception_v3',r1_score,len(dev)-len(result[result.targetedLabel==result.r1]))#,file=open("output.txt", "a+"))
print('logits_inception_resnet_v2',r2_score,len(dev)-len(result[result.targetedLabel==result.r2]))
print('union',len(t))
print((r1_score+r2_score)/2)#,file=open("output.txt", "a+"))

max_stars_repo_name: wufanyou/EPGD

max_issues_repo_name: wufanyou/EPGD

max_forks_repo_name: wufanyou/EPGD


text:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import src.mylib.mfile as mfile
import src.mylib.mcalc as mcalc
import src.mylib.iching as iching

start= 12

dat = mfile.loadOneSymbol("CHRIS/CME_GC1", "../db/Metals.db", 'weekly')
dat = dat.reset_index()
dat = dat[['Close']]
#dat = mcalc.m_pct(dat, False)
dat = dat.iloc[start:96+start]

#fn = lambda x: (1.0 if x > 0.0 else 0.0)
#xx = dat['Close'].apply(fn)
xx = dat['Close']
yy = xx.copy()
yy[ (yy.index+start+1) % 16 != 0 ] = np.nan

print(xx)
plt.plot(xx.index, xx, color='blue')
plt.scatter(yy.index, yy, s=50, edgecolors='none')
plt.show()


text:
"""numpy基礎
単位行列を作成する方法(eye関数)

[説明ページ]
https://tech.nkhn37.net/numpy-ndarray-create-basic/#eye
"""
import numpy as np

data_array = np.eye(3)

print(data_array)
print(type(data_array))
print(data_array.dtype)


text:
![Callysto.ca Banner](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-top.jpg?raw=true)

%%html

 Code is hidden for ease of viewing. Click the Show/Hide button to see. </>
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form>

#!pip install --upgrade --force-reinstall --user git+git://github.com/callysto/nbplus.git#egg=geogebra\&subdirectory=geogebra

try:
    from geogebra.ggb import *
except ImportError:
    !pip install --upgrade --force-reinstall --user git+git://github.com/callysto/nbplus.git#egg=geogebra\&subdirectory=geogebra
    from importlib import reload
    import site
    reload(site)
    from geogebra.ggb import *
ggb = GGB()

from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

import IPython
from IPython.display import HTML,display, Math, Latex, clear_output

import math
import random
import numpy as np

import plotly.offline as py
from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go

# Binary decimal conversion

## Introduction

A *positional numeral system* is a way of writing numbers using place value.

Our usual way of writing numbers is an example:

In a number like 1234, the 4 is in the 'ones' spot, the 3 in the 'tens' spot, and so on.

Each position represents a power of 10, so we call this the *decimal*, or base-10 system.

This notebook will look at positional numeral systems in general, but a focus on two familiar examples:

The decimal (base-10) system we all know and love, and the binary (base-2) system.

Topics include:
- the *base* of the numeral system
- how the choice of base effects the *digits* we use to write a number
- how to convert between decimal and binary representations of a number.

###  Big Ideas

Here are some key takeaways you should have in mind as you progress through this notebook:

* Computers understand numbers in binary, while humans understand decimal.
* Conversion from decimal to binary is fundamental to human-computer interaction.
* Understanding this process requires only basic arithmetic: addition, subtraction, and powers.

## Background information

### Numeral systems

In mathematical terminology, a **_number system_** refers to the type of number being used.

Number systems include the integers, the rational numbers (fractions), and real numbers.

A [**_numeral system_**](https://en.wikipedia.org/wiki/Numeral_system) refers to the way we write those numbers down.

The numeral system we use when we write a number like 3127 is called the *decimal system*.

You've gotten so used to using the decimal system that you understand the number 3127 without thinking!

Take a moment to remind yourself that the *position* of each digit represents a value: 1, 10, 100, etc.

The digit in that position then tells us how many of that quantity we have.

In 3127, there are 3 thousands, one hundred, two tens, and seven ones.

But our method of expressing numbers is by no means universal.

Another numeral system you've probably encountered is that of Roman numerals.

The Roman system uses symbols instead of position to represent certain quantities.

We count how many times each symbol appears to know how many of that quantity we have.

For example, the Roman numeral system uses I for 1, V for 5, X for 10, C for 100, and D for 1000, among others.

In the Roman system, the number 3127 is written as MMMCXXVII:

$$\begin{aligned}
3127&=3000+100+20+7\\
& = \underbrace{1000+1000+1000}_{\text{M M M}}+\underbrace{100}_{\text{C}}+\underbrace{10+10}_{\text{X X}}+\underbrace{5}_{\text{V}}+\underbrace{1+1}_{\text{I I}}.
\end{aligned}$$

Note that in the Roman system, there is no symbol for zero: a number like 210 has no ones, so we don't write any.

The number 210 is written CCX. Many early numeral systems did not have a zero.

The invention (discovery?) of zero is usually credited to India, around the 4th or 5th century.

However, zero was also used in [Mayan calendars](https://en.wikipedia.org/wiki/0#History).

The number zero is an interesting topic in its own right, but it's not the subject of this notebook.

If you're curious, you might enjoy this [Scientific American article](https://www.scientificamerican.com/article/history-of-zero/).

Binary and decimal numbers are two examples of *positional numeral systems*, where the relative placement of digits tells us about the values they represent.

One of the earliest examples of a positional system is the remarkable Babylonian [sexagesimal (base-60) system](https://en.wikipedia.org/wiki/Babylonian_numerals).

Our numeral system traces its origins back to India and the Middle East, and is known as the [Hindu-Arabic numeral system](https://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system).

This is a decimal system, where positions represent powers of 10, and symbols represent numbers from 0 to 9.

For example, as we mentioned above, the expression 3127 is convenient shorthand for

$$3\times 1000 + 1\times 1000 + 2\times 10 + 7\times 1.$$

Notice that 1000, 100, 10, and 1 are all powers of 10: $1=10^0, 10=10^1, 100=10^2$, and $1000=10^3$.

When each position represents successive powers of some number, we call that number the *base* of the system.

The decimal system uses base 10. The binary system, which is essential to computing, uses base 2.

### Why Binary?

Why do computers use binary? First, we might ask: why do we use decimal?

We’ve been using decimal so long, you may not have wondered why we chose the base-10 number system for our everyday number needs.

(There's a pretty good chance that it has something to do with humans having 10 fingers.)

Regardless of what led to it, tricks we’ve learned along the way have solidified base-10’s place in our heart.

Everyone can count by 10s. We even round large numbers to the nearest multiple of 10. We’re obsessed with 10!

Computers, being electronic machines, only understand two things:

'ON' or '1': electricity is flowing (through circuit, gate, transistor, whatever).

'OFF' or '0': electricity is not flowing.

In a computer, everything must be represented as collections of ONs and OFFs.
Unfortunately, this scheme is difficult for humans to process.

To make things easier on ourselves, humans represent "computerian" as binary numbers:

ON is represented by a 1, and OFF is represented by a 0.

So instead of "OFF ON ON OFF", we humans can read or write this as "0110".
|

###  Base and Digits in a Positional System

The **base** in a positional numeral system like Hindu-Arabic is the number whose powers we represent by the position of the digits in a number.

Note that the value of the base is also the number of different symbols we need for digits.

For example, the decimal (base-10) system uses 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

In some computer applications (like many WiFi passwords) a hexadecimal (base-16) system is used, with digits

> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f.

Of course, we also use "base" to refer to the number that gets multiplied when applying an exponent.

Examples:

* In $8^{2}$, 8 is the base, and the result is $8 × 8 = 64$.
* In $5^{3}$, 5 is the base, and the result is $5 × 5 × 5 = 125$.

This isn't really a coincidence, since we use powers of the base to reconstruct numbers from positional notation.

The way our notation works is as follows:

 - Position of a digit (read from right to left) tells us which power of the base to use.
 - The digit in that position tells us what to multiply that power by.

Any integer greater than 1 can be used as a base. (Why can't we use 1?)

For a hypothetical "base-$r$" system, we would have $r$ symbols to represent the digits, and each position represents a power of $r$.

For example, we could imagine a base-26 system with each letter of the alphabet representing a digit.

(It's doubtful anyone would want to *use* this system, but we can imagine it!)

In this system, the word "number" would actually *be* a number!

If we assign each letter its place value in the alphabet, we have
$$
n = 14, u = 21, m = 13, b = 2, e = 5, r = 18,
$$

and
![](images/number.png)

This doesn't seem very practical, however, so let's sum up the key points and move on to numbers in decimal and binary.

* The base of a positional numeral system is equal to the number of digits (symbols) needed to represent numbers in that system.
* The position of a digit tells us what the corresponding power of the base should be.
* We multiply that power by the value of the digit, for each digit present.
* Finally, we add everything up to get our number.

For a number $a_na_{n-1}\cdots a_2a_1a_0$ with digits $a_0, a_1, \ldots, a_n$ written in base $r$, we get the number

$$a_nr^n+a_{n-1}r^{n-1}+\cdots a_2r^2+a_1r+a_0.$$

We won't do it here, but see if you can convince yourself any number can be written *uniquely* in any base. We never miss a number, and we never have two different ways of writing down the same number.

While working with numbers of different bases, its best to put the base as a subscript to the number to avoid confusion like $(Number)_{Base}$. For example in order to represent a binary number we place 2 as a subscript like $(11)_{2}$ and 10 to denote decimal numbers like $(11)_{10}$.

#### An answer to an impractical problem.

In the example above, were you left wondering what (base-10) number is "number" in (base-26)?

We can ask the computer to calculate it for us:

14*26**5 + 21*26**4 + 13*26**3 + 2*26**2 + 5*26 + 18

### Counting in binary

We can count in decimal endlessly, even in our sleep, but how can we count in binary?

Zero and one in base-two should look pretty familiar: 0 and 1. From there things get decidedly binary.

In decimal, we start in the 'ones' position, and count until we run out of digits:
$$0, 1, 2, 3, 4, 5, 6, 7, 8, 9.$$
After 9, we hit the next power of 10, so we add 1 to the 'tens' position, and reset the'ones' to 0.

Then we go back to counting in the 'ones' position until we reach 9 again:
$$10, 11, 12, 13, 14, 15, 16, 17, 18, 19.$$
Now, we increase the 'tens' digit by 1 again, and reset the 'ones', getting 20.

We continue like this until we reach 99, and then we're ready to add a 1 in the 'hundreds' position.

When we do, the digits in both the 'tens' and 'ones' positions reset to zero, and then we continue the process.

Counting in binary is similar, with two important differences:
1. Positions represent powers of 2, not 10: $ 2^0=1, 2^1 = 2, 2^2 = 4, 2^3 = 8, 2^4 = 16, 2^5 = 32, 2^6=64$, etc.
2. Digits are only 0 and 1, so the 'change position and reset to zero' step is a lot more frequent!

Remember that we’ve only got those two digits, so as we do in decimal, when we run out of symbols we shift one column to the left, add a 1, and turn all of the digits to the right of that column to 0.

So after 1 we get 10, then 11, then 100. Let’s start counting…

| Decimal | Binary |
|:-------:|:------:|
|    0    |  0000  |
|    1    |  0001  |
|    2    |  0010  |
|    3    |  0011  |
|    4    |  0100  |
|    5    |  0101  |
|    6    |  0110  |
|    7    |  0111  |
|    8    |  1000  |
|    9    |  1001  |
|    10   |  1010  |
|    11   |  1011  |
|    12   |  1100  |
|    13   |  1101  |
|    14   |  1110  |
|    15   |  1111  |

## Converting between decimal and binary notation.

### Converting binary to decimal

Mathematically, this is straightforward. The digits in binary are called **_bits_**.

Each position represents a power of 2. We multiply each power of 2 by the bit in that position, and then add it all up.

#### Example:
To convert the number 1101 from binary to decimal, we compute as follows:
$$\begin{aligned}
1101 &= 1 * 2^{3} + 1 * 2^{2} + 0 * 2^{1} + 1 * 2^{0} \\
    &= 1*8+1*4+0*2+1*1\\
    &= 8+4+1\\&=13
\end{aligned}$$

### Interactive Method

We will now demonstrate an interactive approach to doing this conversion.

Our implementation uses buttons on a computer screen, but you can do this "in real life" as well!

For demonstration we start off with a 5 bit number; that is, a 5 digit binary number, which can represent up to 31 in decimal.

1. Represent each binary digit with a single card, so there will be 5 cards.

2. Mark one side of each card with a power of 2: our five cards will be 1, 2, 4, 8, and 16.

Mark the other side with a 0.

3. Arrange the cards to reflect our positional system: Our order is 16, 8, 4, 2, 1.

4. The side with the power of 2 represents 1 in binary and the other side represents 0.

5. Start with the 0 on each card. Choose a binary number, then flip the cards whose digit is 1.

For example, if we chose the number 11001, we flip the 16, the 8, and the 1.

6. Now to convert to decimal we just add up the numbers showing in the cards!

For 11001 we have $16+8+0+0+1=25$, so 11001 in binary is equivalent to 25 in decimal.

#### Applet Instructions:
1. Create a number in binary: clicking on a button with a 0 turns it into a 1.
2. The corresponding power of 2 below will change colour, and the 0 in front will change to a 1.
3. The decimal equivalent will appear at the bottom.

%%HTML

<!DOCTYPE html>

<meta name="viewport" content="width=device-width, initial-scale=1">

Enter a binary number

<button id="card1" onclick="myFunction(this.id);myFunction1(this.id, 'Decimal_card_16', 'box1');" class= "button">0</button>
<button id="card2" onclick="myFunction(this.id);myFunction1(this.id,'Decimal_card_8', 'box2' );" class= "button">0</button>
<button id="card3" onclick="myFunction(this.id);myFunction1(this.id,'Decimal_card_4', 'box3');" class= "button">0</button>
<button id="card4" onclick="myFunction(this.id);myFunction1(this.id,'Decimal_card_2', 'box4');" class= "button">0</button>
<button id="card5" onclick="myFunction(this.id);myFunction1(this.id,'Decimal_card_1', 'box5');" class= "button">0</button>

Decimal Equivalent of the given binary number

<input type="text" id = "box1" value="0" class ="txtbox" />   &#215

<button id="Decimal_card_16" class= "button ">16</button>  +

<input type="text" id = "box2"  value="0" class ="txtbox"  />  &#215

<button id="Decimal_card_8"  class= "button ">8</button> +

<input type="text" id = "box3" value="0" class ="txtbox"  />  &#215

<button id="Decimal_card_4"  class= "button ">4</button>+

<input type="text" id = "box4" value="0" class ="txtbox"  />  &#215

<button id="Decimal_card_2"  class= "button ">2</button>+

<input type="text"  id = "box5" value="0" class ="txtbox"  />  &#215

<button id="Decimal_card_1"  class= "button ">1</button> =

<input type="text"  id = "box6" value="" class ="txtbox"  />

The decimal equivalent is:

### A remark on notation

When we're working with more than one base system at the same time (for example, when converting from decimal to binary) we need a way of specifying what base we're using.

A common notation is to place the digits in parentheses, with the base as a subscript.

For example, we can write $(10110)_2$ to indicate that we're expressing a number in binary.

This lets us write equations, like $(10110)_2 = (22)_{10}$, to indicate that we've expressed the same number in two different bases.

Some people use notation like $\mathbf{Bin}\,10110 = \mathbf{Dec}\, 22$ instead.

This notation is a little clumsier, but it can be used for a [great joke](https://armchairdissident.wordpress.com/2009/04/15/a-long-explanation-of-a-damned-fine-joke/).

### Converting Decimal to Binary

In the other direction, our goal is to flip the right cards to add up to a given number.

Let's say we want the binary representation for 13.

We could simply employ trial and error, flipping cards and checking each time to see if we have it right.

This isn't very efficient though. Instead, let's think like a computer scientist, and devise an algorithm.

1. First, we definitely don't want to add any numbers *greater* than our number. So 16 is out: we set that card to 0.
2. Next, we're going to want the biggest power of 2 that's *less* than our number. So 8 is in: we set that card to 1.
3. Now we ask ourselves, what do we have left to add? This is a subtraction problem! We want 13 and we have 8, so we still have $13-8=5$ left to add.
4. To continue, we take the value that's left -- 5 -- and repeat steps 2 and 3. Since 4 is less than 5, we flip that card to 1, and find the difference: $5-4=1$.
5. We keep repeating, until there's nothing left to add. But all we have left is 1, so we flip that card, and we're done!

**Note:** the steps we just described give an example of what's called a *recursive algorithm*.

In a recursive algorithm, we repeat a series of steps until a desired goal is reached.

Many computer programs employ recursive algorithms to accomplish their task.

### Visualizing the Algorithm

#### Instructions:
1. Enter a value for n between 1 and 31 in the input box.
2. A graph will be generated to show the original numbers as a blue dot.
3. The red dots will represent the powers of 2 we need to make the remainder 0.
4. Below the graph, the steps are also shown for further clarification. For Example if the input number is 25, then the steps will be
        the first step : 25 - 16 = 9
        the next step : 9 - 8 = 1
        the final step : 1 - 1 = 0

From the steps we know that we need 16, 8, 1 as powers of 2 to make the remainder 0.

* So The binary for $(25)_{10}$ is $(11001)_{2}$.

py.init_notebook_mode(connected=True)

def plot_stored_values(store_plot_value, value):

    value_list = []
    value_list.append(value)

    N = len(store_plot_value)
    random_y = np.zeros(N)

    # Create a trace
    trace = go.Scatter(
        x = store_plot_value,
        y = random_y,
        marker = dict(color = 'rgb(128, 0, 0)',),
        mode = 'markers',
        name =  "Derived Numbers of power of 2"
     )

    trace_value = go.Scatter(
        x = value_list,
        y = [0],
        marker = dict(color = 'rgb(0, 0, 128)',),
        mode = 'markers',
        name = "Original number"
     )

    data = [trace , trace_value]
    layout = go.Layout(
        title ='Decimal Value numbers',
        xaxis =dict(
        title='Decimal Values',
        titlefont=dict(
            family='Courier New, monospace',
            size=18,
            color='#7f7f7f'
            )
        ),
        yaxis=dict(
        title='',
        titlefont=dict(
            family='Courier New, monospace',
            size=18,
            color='#7f7f7f'
            )
        )
    )
    fig = go.Figure(data=data, layout=layout)
    py.iplot(fig, filename='basic-line')

#function for finding the power of 2
def power_two_1(n):
    return int(math.log(n, 2))

def power_two(x):
    return x.bit_length()-1

#function for calculating the algorithm value
store_plot_value = []
def Calculate_Value(x):
    store_plot_value = []

    power_of_2 = power_two(x)
    #print(power_of_2)
    first_value = 2 ** power_of_2
    store_plot_value.append(first_value)
    plot_value = x - 2 ** power_of_2

    #store_plot_value.append(plot_value)
    while(plot_value > 0):

        power_of_2 = power_two(plot_value)
        #print(power_of_2)
        store_value =  2 ** power_of_2
        store_plot_value.append(store_value)
        plot_value = plot_value - 2 ** power_of_2

    #plot_stored_values(store_plot_value)
    return store_plot_value

Algorithm_value = widgets.BoundedIntText(
    value = 0,
    min = 0,
    max = 256,
    step = 1,
    description = 'Enter value:',
    disabled = False
)

def on_value_change(change):

    clear_output(wait= False)
    display(Algorithm_value)
    Plot_values = []
    #Calculate_Value(change['new'])
    Plot_values = Calculate_Value(change['new'])
    plot_stored_values(Plot_values, change['new'])

    visual_value = change['new'] - Plot_values[0]

    interim_value = visual_value
    print("the first step : {} - {} = {}".format(change['new'],Plot_values[0], interim_value ))

    for i in Plot_values[1:]:
        temp = interim_value
        interim_value = interim_value - i
        print("the next step : {} - {} = {}".format(temp, i , interim_value ))
    print("From the steps we see that we need", Plot_values, " as powers of 2 to make the remainder 0.")

display(Algorithm_value)
Algorithm_value.observe(on_value_change, names='value')

### Try doing the algorithm yourself!

#### Instructions:
1. Enter a value for n between 1 and 31 in the input box.
2. When you click on a power of 2, it will be subtracted from your number.
3. Keep working until the amount r that remains is zero.
4. Your number in binary will be shown at the bottom.

#ggb.material('VJJpYMQ2').draw()

%%html

## Practice Questions

Now that you've seen a few examples, it's time to try some on your own.

But remember: two problems worth of practice is not enough to master this skill.

Make sure you take the time to work through additional problems from your teacher or textbook.
### Convert the following Decimal Numbers to binary
##### Question 1
$(57)_{10} = (A)_2$

a = widgets.IntText(value=0, description='A =', disabled=False)

IPython.display.display(a)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(a)
    if a.value == 111001:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

a.observe(check, 'value')

%%html

Solution Question 1.  <button id = "A"
onclick="toggle('answer1');">Click Here</button> to see the answer.

As $64(2^6)$ is closest power of 2 for 57, so there will be atleast 6 digits for binary.

the first step : 57 - 32 = 25

the next step : 25 - 16 = 9

the next step : 9 - 8 = 1

the last step : 1 - 1 = 0

so, the solution is:
 $1*32+ 1*16+ 1*8+ 0*4 + 0*2 + 1*1 $= $(111001)_2$

##### Question 2
$(79)_{10} = (B)_2$

b = widgets.IntText(value=0, description='B =', disabled=False)
IPython.display.display(b)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(b)
    if b.value == 1001111:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

b.observe(check, 'value')

%%html

Solution Question 2.  <button id = "B"
onclick="toggle('answer2');">Click Here</button> to see the answer.

As $128(2^7)$ is closest power of 2 for 79, so there will be atleast 7 digits for binary.

the first step : 79 - 64 = 15

the next step : 15 - 8 = 7

the next step : 7 - 4 = 3

the next step : 3 - 2 = 1

the last step : 1 - 1 = 0

so, the solution is:
 $1*64+ 0*32+ 0*16+ 1*8+ 1*4 + 1*2 + 1*1 $= $(1001111)_2$

##### Question 3
$(39)_{10} = (C)_2$

c = widgets.IntText(value=0, description='C =', disabled=False)
IPython.display.display(c)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(c)
    if c.value == 100111:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

c.observe(check, 'value')

%%html

Solution Question 3. <button id = "C"
onclick="toggle('answer3');">Click Here</button> to see the answer.

As $64(2^6)$ is closest power of 2 for 39, so there will be atleast 6 digits for binary.

the first step : 39 - 32 = 7

the next step : 7 - 4 = 3

the next step : 3 - 2 = 1

the last step : 1 - 1 = 0

so, the solution is:
 $1*32+ 0*16+ 0*8+ 1*4 + 1*2 + 1*1 $= $(100111)_2$

### Convert the following Binary Numbers to Decimal
##### Question 4
$(10101)_{2} = (D)_{10}$

d = widgets.IntText(value=0, description='D =', disabled=False)
IPython.display.display(d)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(d)
    if d.value == 21:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

d.observe(check, 'value')

%%html

Solution Question 4. <button id = "C"
onclick="toggle('answer4');">Click Here</button> to see the answer.

As $64(2^6)$ is closest power of 2 for 39, so there will be atleast 6 digits for binary.

the first step : 39 - 32 = 7

the next step : 7 - 4 = 3

the next step : 3 - 2 = 1

the last step : 1 - 1 = 0

so, the solution is:
 $1*32+ 0*16+ 0*8+ 1*4 + 1*2 + 1*1 $= $(100111)_2$

##### Question 5
$(01111)_{2} = (E)_{10}$

e = widgets.IntText(value=0, description='E =', disabled=False)
IPython.display.display(e)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(e)
    if e.value == 15:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

e.observe(check, 'value')

%%html

Solution Question 5.  <button id = "D"
onclick="toggle('answer5');">Click Here</button> to see the answer.

$(10101)_2 = 1 * 2^{4} + 0 * 2^{3} + 1 * 2^{2} + 0* 2{1} + 1*2{0}$

           = $1*16+ 0*8+ 1*4 + 0*2 + 1*1$

           = $(21)_{10}$

##### Question 6
$(100100)_{2} = (F)_{10}$

f = widgets.IntText(value=0, description='F =', disabled=False)
IPython.display.display(f)
def check(q):
    IPython.display.clear_output(wait=False)
    IPython.display.display(f)
    if f.value == 36:
        print("That's right! Great work!")
    else:
        print("Sorry, try again or practice some more using the tools above")

f.observe(check, 'value')

%%html

Solution Question 6. <button id = "D"
onclick="toggle('answer6');">Click Here</button> to see the answer.

$(100100)_2 = 1 * 2^{5} + 0 * 2^{4} + 0 * 2^{3} + 1 * 2^{2} + 0* 2{1} + 1*2{0}$

           = $1*32+ 0*16+ 0*8+ 1*4 + 0*2 + 0*1$

           = $(36)_{10}$

## Conclusion

There are many different ways to represent the numbers we use every day.

The best way of representing a number can depend on what we're trying to do.

Digital computers typically use two states rather than more because:

* It is usually easy to distinguish two states;
* Two is enough to represent anything you like as a sequence of bits; and
* Computers don't mind long sequences.

Since we live in a digital era surrounded by machines, we must learn how machines understand the living world.

[![Callysto.ca License](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-bottom.jpg?raw=true)](https://github.com/callysto/curriculum-notebooks/blob/master/LICENSE.md)

max_stars_repo_name: BryceHaley/curriculum-jbook

max_issues_repo_name: callysto/curriculum-jbook

max_forks_repo_name: callysto/curriculum-jbook


text:
import numpy as np
import numbers, random, skimage, warnings, cv2
from skimage.filters import threshold_otsu
from scipy.ndimage.interpolation import rotate, zoom
from scipy.ndimage.morphology import binary_fill_holes
import scipy.ndimage.measurements as measure
import SimpleITK as sitk
import elasticdeform as edf

"""
Adapted from medical torch for 3D volumes of numpy arrays.
"""

class MTTransform(object):
    def __call__(self, sample):
        raise NotImplementedError("You need to implement the transform() method.")

    def undo_transform(self, sample):
        raise NotImplementedError("You need to implement the undo_transform() method.")

class UndoCompose(object):
    def __init__(self, compose):
        self.transforms = compose.transforms

    def __call__(self, sample):
        for t in self.transforms:
            img = t.undo_transform(sample)
        return img

class UndoTransform(object):
    def __init__(self, transform):
        self.transform = transform

    def __call__(self, sample):
        return self.transform.undo_transform(sample)

class Compose(object):

    """

    Composes several transforms together.
    Modiefied to edit both image & mask at one time.
    Args:
        transforms (list of ``Transform`` objects): list of transforms to compose.
    Example:
        >>>     transforms.Compose([
        >>>     transforms.CenterCrop(10),
        >>>     transforms.ToTensor(),
        >>> ])

    """

    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, img, mask):
        assert mask.max() > 0
        max_ = mask.max()
        max_img = img.max()
        min_img = img.min()

        shape = mask.shape

        for i, t in enumerate(self.transforms):
            img, mask = t(img, mask)
            mask = np.round(mask)
            # make sure transformation doesn't mess up mask...
            # do the masks look like how they ought to look?
            # print(count, max_, mask.max())
            # check transformations doing right thing //
            if i < len(self.transforms)-2:

                # assert mask.max() == max_
                assert mask.min() == 0

                try:
                    assert img.max() == max_img
                except Exception:
                    warnings.warn(f'Max value of img changed at transform {i}.')
                    max_img = img.max()
                try:
                    assert mask.max() == max_
                except Exception:
                    warnings.warn(f'Max value of mask changed at transform {i}.')
                    max_ = mask.max()
                    # max_img = img.max()
                try:
                    assert img.min() == min_img
                except Exception:
                    warnings.warn(f'Min value of img changed at transform {i}.')
                    min_img = img.min()

            else:

                try:
                    assert mask.max() == max_
                except Exception:
                    warnings.warn('Cropped out max class value.')

        return img, mask

    def __repr__(self):
        format_string = self.__class__.__name__ + "("
        for t in self.transforms:
            format_string += "\n"
            format_string += "    {0}".format(t)
        format_string += "\n)"
        return format_string

class sitkZoom3D(MTTransform):
    def __init__(self, spacing=np.arange(0.96, 1.04, 0.01)):
        self.spacing = np.random.choice(spacing)

    @staticmethod
    def resample(image, mode, new_spacing=np.array((1.0, 1.0, 3.0))):

        # originally taken from https://github.com/SimpleITK/SimpleITK/issues/561
        resample = sitk.ResampleImageFilter()
        if mode == "linear":
            resample.SetInterpolator = sitk.sitkLinear  # use linear to resample image
            image = sitk.SmoothingRecursiveGaussian(image, 2.0)
        else:
            # use sitkNearestNeighbor interpolation
            # best for masks, no gaussian smoothing required...
            resample.SetInterpolator = sitk.sitkNearestNeighbor

        orig_size = np.array(image.GetSize(), dtype=np.int)
        orig_spacing = np.array(image.GetSpacing())
        resample.SetOutputDirection(image.GetDirection())
        resample.SetOutputOrigin(image.GetOrigin())
        new_spacing = new_spacing
        resample.SetOutputSpacing(new_spacing)

        # new_spacing[:2] = orig_spacing[:2]
        # resample.SetOutputPixelType = sitk_image.GetPixelIDValue()
        new_size = orig_size * (orig_spacing / new_spacing)
        new_size = np.ceil(new_size).astype(np.int)  #  Image dimensions are in integers
        new_size = [int(s) for s in new_size]
        resample.SetSize(new_size)

        # we can use this with or without gaussian smoothing, prevent analising

        image = resample.Execute(image)

        return image

    def __call__(self, img, mask=None):
        # calculate new spacing ...
        # might be better and a whole lot faster than numpy...
        zoom_by = self.spacing
        new_spacing = np.array((zoom_by, zoom_by, 3.0))
        img = self.resample(img, mode="linear", new_spacing=new_spacing)

        if mask is not None:
            mask = self.resample(mask, mode="nearest", new_spacing=new_spacing)
            return img, mask
        else:
            return img

class RandomZoom3D(MTTransform):
    def __init__(self, p=1.0, zoom_factors=np.arange(0.9, 1.1, 0.01)):

        self.zoom_by = np.random.choice(zoom_factors)
        self.p = p

    @staticmethod
    def clipped_zoom(img, zoom_factor, method="linear"):
        """
        Center zoom in/out of the given image and returning an enlarged/shrinked view of
        the image without changing dimensions
        Args:
            img : Image array
            zoom_factor : amount of zoom as a ratio (0 to Inf)
        """

        if len(img.shape) == 3:
            img = img.transpose(1, 2, 0)

        # if method == 'li':
        #     a = np.sum(img)
        #     if a < 1000:
        #         # do not downsample...
        #         # change zoom factor...
        #         zoom_factor = np.random.choice(np.arange(1.00, 1.06, 0.01))

        height, width = img.shape[:2]  # It's also the final desired shape
        new_height, new_width = int(height * zoom_factor), int(width * zoom_factor)

        ### Crop only the part that will remain in the result (more efficient)
        # Centered bbox of the final desired size in resized (larger/smaller) image coordinates
        y1, x1 = max(0, new_height - height) // 2, max(0, new_width - width) // 2
        y2, x2 = y1 + height, x1 + width
        bbox = np.array([y1, x1, y2, x2])
        # Map back to original image coordinates
        bbox = (bbox / zoom_factor).astype(np.int)
        y1, x1, y2, x2 = bbox
        cropped_img = img[y1:y2, x1:x2]

        # Handle padding when downscaling
        resize_height, resize_width = min(new_height, height), min(new_width, width)
        pad_height1, pad_width1 = (
            (height - resize_height) // 2,
            (width - resize_width) // 2,
        )
        pad_height2, pad_width2 = (
            (height - resize_height) - pad_height1,
            (width - resize_width) - pad_width1,
        )
        pad_spec = [(pad_height1, pad_height2), (pad_width1, pad_width2)] + [(0, 0)] * (
            img.ndim - 2
        )

        if method == "linear":
            result = cv2.resize(
                cropped_img,
                (resize_width, resize_height),
                interpolation=cv2.INTER_LINEAR,
            )
        else:
            result = cv2.resize(
                cropped_img,
                (resize_width, resize_height),
                interpolation=cv2.INTER_NEAREST,
            )
        result = np.pad(result, pad_spec, mode="constant")  #'constant'
        assert result.shape[0] == height and result.shape[1] == width

        if len(img.shape) == 3:
            result = result.transpose(2, 0, 1)

        return result

    def __call__(self, img, mask=None):
        flip = random.random()
        if flip < self.p:
            img = self.clipped_zoom(img.copy(), self.zoom_by, method="nearest")
            if mask is not None:
                mask = self.clipped_zoom(mask.copy(), self.zoom_by, method="nearest")
                return img, mask
            else:
                return img
        else:
            return img, mask

        # Previous clipped_zoom function...
        # """
        #     Taken from: https://stackoverflow.com/questions/37119071/scipy-rotate-and-zoom-an-image-without-changing-its-dimensions
        #     """
        # much slower than open-cv
        #
        # if len(img.shape) == 3:
        #     img = img.transpose(1, 2, 0)
        #
        # if method == "linear":
        #     # bilinear interpolation
        #     mode = 1
        # else:
        #     # nearest neighbour interpolation
        #     mode = 0
        #
        # h, w = img.shape[:2]
        #
        # # For multichannel images we don't want to apply the zoom factor to the RGB
        # # dimension, so instead we create a tuple of zoom factors, one per array
        # # dimension, with 1's for any trailing dimensions after the width and height.
        # zoom_tuple = (zoom_factor,) * 2 + (1,) * (img.ndim - 2)
        #
        # # Zooming out
        # if zoom_factor < 1:
        #
        #     # Bounding box of the zoomed-out image within the output array
        #     zh = int(np.round(h * zoom_factor))
        #     zw = int(np.round(w * zoom_factor))
        #     top = (h - zh) // 2
        #     left = (w - zw) // 2
        #
        #     # Zero-padding
        #     out = np.zeros_like(img)
        #     out[top : top + zh, left : left + zw] = zoom(img, zoom_tuple, order=mode)
        #
        # # Zooming in
        # elif zoom_factor > 1:
        #
        #     # Bounding box of the zoomed-in region within the input array
        #     zh = int(np.round(h / zoom_factor))
        #     zw = int(np.round(w / zoom_factor))
        #     top = (h - zh) // 2
        #     left = (w - zw) // 2
        #
        #     out = zoom(img[top : top + zh, left : left + zw], zoom_tuple, order=mode)
        #
        #     # `out` might still be slightly larger than `img` due to rounding, so
        #     # trim off any extra pixels at the edges
        #     trim_top = (out.shape[0] - h) // 2
        #     trim_left = (out.shape[1] - w) // 2
        #     out = out[trim_top : trim_top + h, trim_left : trim_left + w]
        #
        # # If zoom_factor == 1, just return the input array
        # else:
        #     out = img
        #
        # if len(img.shape) == 3:
        #     return out.transpose(2, 0, 1)
        # else:
        #     return out

# define function that saves
class RandomCrop3D(MTTransform):
    def __init__(self, window=5, factor=512, mode="train", data="RADCURE", crop_as="3D"):

        # can we add this to overide norm?
        # for volume just change the window...
        self.factor = factor
        self.window = window
        self.mode = mode
        self.data = data
        self.crop_as = crop_as

    @staticmethod
    def segment_head(img):
        # function to make (fake) external for center cropping of image...
        otsu = threshold_otsu(img)  # Compute the Ostu threshold
        binary_img = np.array(img > otsu, dtype=int )  # Convert image into binary mask (numbers bigger then otsu set to 1, else 0)
        fill = binary_fill_holes(binary_img)  # Fill any regions of 0s that are enclosed by 1s
        return fill

    def get_shifts(self, img):
        # Assumes there is NO external contour then use this...
        # image if '3D' is a binary or similar mask...
        # if mask is not avaliable use image with otsu thresholding...
        # slice_ = img[self.window]
        # fill = self.segment_head(img)
        # assert img.max() > 0
        # only do this if of type tensor...
        try:
            shape = img.shape
        except Exception:
            shape = img.size()
            img = img.cpu().numpy()

        if len(shape)==4:
            img = img[0,:,:,:]
            shape = img.shape
        elif len(shape)==5:
            img=img[0,0,:,:,:]
            shape = img.shape

        if self.mode == 'test':
            img = img[shape[0]//2:shape[0]-shape[0]//3]
            img = self.segment_head(img)
            com = measure.center_of_mass(img)
            img = img[int(com[0])]
            # prodcues a mask, where we take COM from...

        com = measure.center_of_mass(img)
        # com = [com[0], com[1], com[2]]
        # assert len(com) == 3
        self.center = com

    def get_params(self, img):
        if len(img.shape) == 3:
            self.z, self.y, self.x = img.shape
        elif len(img.shape) == 4:
            self.b, self.z, self.y, self.x = img.shape
        elif len(img.shape) == 5:
            self.b, self.c, self.z, self.y, self.x = img.shape
        else:
            self.y, self.x = img.shape
        assert self.x == self.y

    # @staticmethod
    def get_crop(self, img, mask=None):

        try:
            shape = img.shape
        except Exception:
            shape = img.size()

        if self.mode != "test":
            centerz = np.int(self.center[0]) if self.center is not None else self.z // 2
            centerx = np.int(self.center[2]) if self.center is not None else self.x // 2
            centery = np.int(self.center[1]) if self.center is not None else self.y // 2
        else:
            centerx = np.int(self.center[1]) if self.center is not None else self.x // 2
            centery = np.int(self.center[0]) if self.center is not None else self.y // 2

        startx = np.int(centerx) - (self.factor // 2)
        starty = np.int(centery) - (self.factor // 2)

        if self.mode == "train":
            assert len(self.center) == 3
            a = np.arange(-64, 64)
            startx += np.random.choice(a)
            starty += np.random.choice(a)

            try:
                assert startx > 0
            except Exception:
                warnings.warn('COM of mask < 1/4 of crop factor in x.')
                startx = 1
            try:
                assert starty > 0
            except Exception:
                warnings.warn('COM of mask < 1/4 of crop factor in y.')
                starty = 1
            try:
                assert startx < (self.x - self.factor//2 - 1)
            except Exception:
                warnings.warn('Startx needs to be changed for effective crop.')
                startx = self.x - self.factor//2 - 2
            try:
                assert starty < (self.y - self.factor//2 - 1)
            except Exception:
                warnings.warn('Starty needs to be changed for effective crop.')
                starty = self.y - self.factor//2 - 2

        else:
            try:
                assert startx > 0
            except Exception:
                warnings.warn('COM of mask < 1/4 of crop factor in x.')
                startx = 1
            try:
                assert starty > 0
            except Exception:
                warnings.warn('COM of mask < 1/4 of crop factor in y.')
                starty = 1
            try:
                assert startx < (self.x - self.factor//2 - 1)
            except Exception:
                warnings.warn('Startx needs to be changed for effective crop.')
                startx = self.x - self.factor//2 - 2
            try:
                assert starty < (self.y - self.factor//2 - 1)
            except Exception:
                warnings.warn('Starty needs to be changed for effective crop.')
                starty = self.y - self.factor//2 - 2

        # Use during training.
        # for vlidation stay cropped around GTV...
        # ie, comment out for testing...
        if self.mode != "test":
            if shape[0] > self.window*2: # 128
                warnings.warn(f'Cropping images/masks from {shape[0]} to 120.')
                # self.window = 56 # 64
                a = np.arange(-50, 50) # (64,64)
                if self.mode == 'train':
                    centerz += np.random.choice(a)
                end = shape[0] - self.window

                if centerz < self.window - 1:
                    centerz = self.window + self.window // 2
                elif centerz > end:
                    centerz = centerz - self.window // 2

                bottom = centerz - self.window  # //2 # input 54//2
                top = centerz + self.window  # //2+1 # input 54 //2

                try:
                    assert bottom > 0
                except Exception:
                    warnings.warn('Cropping starting from z==1.')
                    bottom = 0
                    top = 0 + self.window*2
                try:
                    assert top < shape[0]
                except Exception:
                    warnings.warn(f'Cropping ending at {shape[0]}.')
                    bottom = shape[0] - 1 - self.window*2
                    top = shape[0] - 1

                if len(shape) == 3:

                    img = img[int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]
                    if mask is not None:
                        mask = mask[int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]

                elif len(shape) == 4:

                    img = img[:, int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]
                    if mask is not None:
                        mask = mask[:, int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]

                elif len(shape) == 5:

                    img = img[ :, :, int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]
                    if mask is not None:
                        mask = mask[ :, :, int(bottom): int(top), starty: starty + self.factor, startx: startx + self.factor]
                else:

                    img = img[starty: starty + self.factor, startx: startx + self.factor]
                    if mask is not None:
                        mask = mask[starty: starty + self.factor, startx: startx + self.factor]

                if mask is not None:
                    return img, mask
                else:
                    return img

        if len(shape) == 3:
            if self.mode=='test':
                starty+=-15
            img = img[:, starty: starty + self.factor, startx: startx + self.factor]
            if mask is not None:
                mask = mask[:, starty: starty + self.factor, startx: startx + self.factor]

        elif len(shape) == 4:

            img = img[:, :, starty: starty + self.factor, startx: startx + self.factor]
            if mask is not None:
                mask = mask[:, :, starty: starty + self.factor, startx: startx + self.factor]

        elif len(shape) == 5:

            img = img[ :, :, :, starty: starty + self.factor, startx: startx + self.factor]
            if mask is not None:
                mask = mask[ :, :, :, starty: starty + self.factor, startx: startx + self.factor]
        else:
            img = img[starty: starty + self.factor, startx: startx + self.factor]
            if mask is not None:
                mask = mask[starty: starty + self.factor, startx: startx + self.factor]

        if mask is not None:
            if self.mode == 'test':
                return img, mask, (starty, startx)
            else:
                return img, mask
        else:
            if self.mode == 'test':
                return img, (starty, startx)
            else:
                return img

    def __call__(self, img, mask=None, mask2=None):

        # initiate parameters (get shifing coeff)

        if self.mode=='test':
            self.get_params(img)
            self.get_shifts(mask2)
        else:
            self.get_params(img)
            self.get_shifts(mask)

        if mask is not None:
            assert mask.max() > 0
            if self.mode == 'test':
                img, mask, center = self.get_crop(img, mask)
                return img, mask, center
            else:
                img, mask = self.get_crop(img, mask)
                if mask.shape != (img.shape[0] ,self.factor, self.factor):
                    warnings.warn(f'Bad mask shape...recropping. {mask.shape}')
                return img, mask
        else:
            img, c = self.get_crop(img)
            return img, c

class RandomRotation3D(MTTransform):
    """Make a rotation of the volume's values.
    :param degrees: Maximum rotation's degrees.
    :param axis: Axis of the rotation.
    """

    def __init__(self, degrees=5, axis=(1, 2), p=1.):
        if isinstance(degrees, numbers.Number):

            if degrees < 0:
                raise ValueError("If degrees is a single number, it must be positive.")

            self.degrees = (-degrees, degrees)
        else:
            if len(degrees) != 2:
                raise ValueError("If degrees is a sequence, it must be of len 2.")
            self.degrees = degrees

        self.axis = axis
        self.p = p

    @staticmethod
    def get_params(degrees):
        angle = np.random.uniform(degrees[0], degrees[1])
        return angle

    def __call__(self, img, mask=None):
        flip = random.random()
        if flip < self.p:
            angle = self.get_params(self.degrees)
            img = rotate(img, angle, axes=self.axis, reshape=False, order=0)
            if mask is not None:
                if len(mask.shape) == 2:
                    self.axis = (0, 1)
                mask = rotate(mask, angle, axes=self.axis, reshape=False, order=0)
                return img, mask
            else:
                return img
        else:
            return img, mask

class RandomFlip3D(MTTransform):

    """Make a symmetric inversion of the different values of each dimensions.
    (randomized)
    """

    def __init__(self, axis=2, flip_labels=True):
        self.axis = 2
        # TRUE FOR OAR SEGMENTATION...
        self.flip_labels=flip_labels

    def __call__(self, img, mask=None):

        self.coin = random.random()
        if self.coin > 0.5:
            # flip image
            img = np.flip(img, axis=self.axis).copy()
            if mask is not None:
                if len(mask.shape) == 2:
                    self.axis = 1
                elif len(mask.shape) ==4:
                    self.axis += 1
                mask = np.flip(mask, axis=self.axis).copy()
                mask_ = np.zeros(mask.shape)
                # we would also need to flip mask labels...
                if self.flip_labels is True:
                    flipped_chosen = [0,1,2,3,4,5,7,6,9,8,11,10,13,12,15,14,17,16,18,19]
                    for i, val in enumerate(flipped_chosen):
                        mask_[mask==i] = val
                else:
                    pass

                return img, mask_
            else:
                return img
        else:
            # do nothing...
            if mask is not None:
                return img, mask
            else:
                return img

class ElasticTransform3D(MTTransform):
    def __init__( self, sigma=25, points=3, axis=(1, 2), order=0, p=1.0,\
                  mode="nearest", prefilter=True):

        self.sigma = sigma
        self.pts = points
        self.ax = axis
        self.mode = mode
        self.prefilt = prefilter
        self.order = order
        self.p = p

    @staticmethod
    def choose_sig(sigma):
        a = np.arange(sigma//2, sigma+1)
        sigma = np.random.choice(a)
        return sigma

    def __call__(self, image, mask=None):
        # only run augmentation if flip below
        # certain probability...
        flip = random.random()
        if flip < self.p:
            sigma = self.choose_sig(self.sigma)
            if mask is not None:
                # can test this out...
                # self.mode = ["constant", "nearest"]
                # self.order = [1, 0]
                # want to make ure the same random deform is applied...
                if len(mask.shape) == 2:
                    self.ax = [(1, 2), (0, 1)]

                image, mask = edf.deform_random_grid(
                    [image, mask],
                    sigma=sigma,
                    points=self.pts,
                    axis=self.ax,
                    mode=self.mode,
                    order=self.order,
                    prefilter=self.prefilt,
                )

                return image, mask

            else:
                image = edf.deform_random_grid(
                    image,
                    sigma=sigma,
                    points=self.pts,
                    axis=self.ax,
                    mode=self.mode,
                    order=self.order,
                    prefilter=self.prefilt,
                )
                return image
        else:
            return image, mask

class AdditiveGaussianNoise(MTTransform):
    def __init__(self, mean=0.0, std=0.01):
        self.mean = mean
        self.std = std

    def __call__(self, img, mask=None):
        # rdict = {}
        # input_data = sample['input']
        if np.random.randn() > 0.5:
            shape = img.shape
            noise = np.random.normal(self.mean, self.std, shape)
            img = img + noise

        if mask is not None:
            return img, mask
        else:
            return img

class Clahe(MTTransform):
    def __init__(self, clip_limit=3.0, kernel_size=(8, 8), return_="3D"):
        # Default values are based upon the following paper:
        # https://arxiv.org/abs/1804.09400 (3D Consistent Cardiac Segmentation)

        self.clip_limit = clip_limit
        self.kernel_size = kernel_size

    def __call__(self, img, mask=None):

        if not isinstance(img, np.ndarray):
            raise TypeError("Input sample must be a numpy array.")

        input = np.copy(img)

        if len(input.shape) == 3:
            images = []
            for i, slice in enumerate(input):
                images.append(
                    skimage.exposure.equalize_adapthist(
                        slice, kernel_size=self.kernel_size, clip_limit=self.clip_limit
                    )
                )
            images = np.stack(images)
            return images

        elif len(input.shape) == 2:
            array = skimage.exposure.equalize_adapthist(
                input, kernel_size=self.kernel_size, clip_limit=self.clip_limit
            )

            return array

        else:
            raise ValueError("Input sample must be a 3D or 2D numpy array.")

class HistogramClipping(MTTransform):
    def __init__(
        self,
        percent=False,
        min_percentile=84.0,
        max_percentile=99.0,
        # old clipping messed s**t up, note -296 196 not good.
        min_hu=-500,
        max_hu=1000,
    ):

        self.min_percentile = min_percentile
        self.max_percentile = max_percentile
        self.min_hu = min_hu
        self.max_hu = max_hu
        self.percent = percent

    def __call__(self, img, mask=None):

        array = np.copy(img)

        if self.percent is True:
            percentile1 = np.percentile(array, self.min_percentile)
            percentile2 = np.percentile(array, self.max_percentile)
            array[array <= percentile1] = percentile1
            array[array >= percentile2] = percentile2
        else:
            array[array <= self.min_hu] = self.min_hu
            array[array >= self.max_hu] = self.max_hu

        if mask is not None:
            return array, mask
        else:
            return array

class NormBabe(MTTransform):

    def __init__(self, mean=False, std=False, min=-196.0, max=296.0, type="standard"):

        self.mean = mean
        self.std = std
        self.min = min
        self.max = max
        self.type = type

    def normalize(self, image):
        # will normalize between zero and 1
        image = (image - self.min) / (self.max - self.min)
        image[image > 1] = 1.0
        image[image < 0] = 0.0

        return image

    def __call__(self, img, mask=None):

        array = np.copy(img)
        if self.type == "standard":
            array = (array - self.mean) / self.std
        else:
            array = self.normalize(array)

        if mask is not None:

            return array, mask

        else:

            return array

class Normalize(MTTransform):

    """
    Normalize a tensor image with mean and standard deviation.
    :param mean: mean value.
    :param std: standard deviation value.
    """

    def __init__(self, mean=None, std=None, verbose=False, min=-200, max=300):

        self.mean = mean
        self.std = std
        self.verbose = verbose
        self.min = min
        self.max = max

    def __call__(self, img, mask=None):

        if self.verbose is not False:
            mean = np.mean(img)
            std = np.std(img)
            return mean, std
        else:
            if self.mean is not None and self.std is not None:
                warnings.warn("Using global normalization.")
                image = (img - self.mean) / self.std
            else:
                warnings.warn("Using image based normalization.")
                image = (img - np.mean(img)) / np.std(img)

            if mask is not None:
                return image, mask
            else:
                return image

max_stars_repo_name: bhklab/ptl-oar-segmentation

max_issues_repo_name: bhklab/ptl-oar-segmentation

max_forks_repo_name: bhklab/ptl-oar-segmentation

reason: import numpy,import scipy,from scipy