repo_name
stringlengths
8
130
hexsha
list
file_path
list
code
list
apis
list
ChristianLin0420/DeepRL
[ "143a9bfebd264229d9d26fcdc070065225774e04" ]
[ "Homework 4/107062240_hw4_train.py" ]
[ "from osim.env import L2M2019Env\n\n'''\n TD3\n'''\nimport copy\nimport numpy as np\nimport pandas as pd\n\nimport argparse\nimport os\nimport os.path\nfrom os import path\nimport utils\nfrom collections import Iterable\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Implementation of Twin Delayed Deep Deterministic Policy Gradients (TD3)\n# Paper: https://arxiv.org/abs/1802.09477\n\ndef flatten_list(lis):\n for item in lis:\n if isinstance(item, Iterable) and not isinstance(item, str):\n for x in flatten(item):\n yield x\n else: \n yield item\n\ndef flatten(mydict):\n new_dict = {}\n for key, value in mydict.items():\n if type(value) == list:\n print(\"key: {}, value: {}\".format(key, value))\n new_dict[key] = flatten_list(value)\n else:\n new_dict[key] = value\n return new_dict\n\ndef FF(ss):\n state = flatten(ss)\n # print(state)\n\n new_state = []\n\n for v in state.values(): \n # print(type(v))\n if type(v) == dict:\n temp = pd.json_normalize(v, sep = '_')\n temp = list(temp.values)\n for item in temp:\n if type(item) == np.ndarray:\n for val in item:\n if type(val) == list:\n for t in val:\n new_state.append(float(t))\n else:\n new_state.append(float(val))\n else:\n new_state.append(item)\n else:\n # print(\"asdf\")\n for arr in v:\n for a in arr:\n for val in a:\n new_state.append(float(val))\n\n return new_state\n\nclass Actor(nn.Module):\n def __init__(self, state_dim, action_dim, max_action):\n super(Actor, self).__init__()\n\n self.l1 = nn.Linear(state_dim, 256)\n self.l2 = nn.Linear(256, 256)\n self.l3 = nn.Linear(256, action_dim)\n \n self.max_action = max_action\n \n\n def forward(self, state):\n a = F.relu(self.l1(state))\n a = F.relu(self.l2(a))\n return self.max_action * torch.tanh(self.l3(a))\n\n\nclass Critic(nn.Module):\n def __init__(self, state_dim, action_dim):\n super(Critic, self).__init__()\n\n # Q1 architecture\n self.l1 = nn.Linear(state_dim + action_dim, 256)\n self.l2 = nn.Linear(256, 256)\n self.l3 = nn.Linear(256, 1)\n\n # Q2 architecture\n self.l4 = nn.Linear(state_dim + action_dim, 256)\n self.l5 = nn.Linear(256, 256)\n self.l6 = nn.Linear(256, 1)\n\n\n def forward(self, state, action):\n sa = torch.cat([state, action], 1)\n\n q1 = F.relu(self.l1(sa))\n q1 = F.relu(self.l2(q1))\n q1 = self.l3(q1)\n\n q2 = F.relu(self.l4(sa))\n q2 = F.relu(self.l5(q2))\n q2 = self.l6(q2)\n return q1, q2\n\n\n def Q1(self, state, action):\n sa = torch.cat([state, action], 1)\n\n q1 = F.relu(self.l1(sa))\n q1 = F.relu(self.l2(q1))\n q1 = self.l3(q1)\n return q1\n\n\nclass TD3(object):\n def __init__(\n self,\n state_dim,\n action_dim,\n max_action,\n discount=0.99,\n tau=0.005,\n policy_noise=0.2,\n noise_clip=0.5,\n policy_freq=2\n ):\n\n self.actor = Actor(state_dim, action_dim, max_action).to(device)\n self.actor_target = copy.deepcopy(self.actor)\n self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr=3e-4)\n\n self.critic = Critic(state_dim, action_dim).to(device)\n self.critic_target = copy.deepcopy(self.critic)\n self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr=3e-4)\n\n self.max_action = max_action\n self.discount = discount\n self.tau = tau\n self.policy_noise = policy_noise\n self.noise_clip = noise_clip\n self.policy_freq = policy_freq\n\n self.total_it = 0\n\n def select_action(self, state):\n\n new_state = FF(state)\n\n new_state = np.array(new_state)\n # print(new_state)\n\n new_state = torch.FloatTensor(new_state.reshape(1, -1)).to(device)\n return self.actor(new_state).cpu().data.numpy().flatten()\n\n def train(self, replay_buffer, batch_size=256):\n self.total_it += 1\n\n # Sample replay buffer \n state, action, next_state, reward, not_done = replay_buffer.sample(batch_size)\n\n with torch.no_grad():\n # Select action according to policy and add clipped noise\n noise = (\n torch.randn_like(action) * self.policy_noise\n ).clamp(-self.noise_clip, self.noise_clip)\n \n next_action = (\n self.actor_target(next_state) + noise\n ).clamp(-self.max_action, self.max_action)\n\n # Compute the target Q value\n target_Q1, target_Q2 = self.critic_target(next_state, next_action)\n target_Q = torch.min(target_Q1, target_Q2)\n target_Q = reward + not_done * self.discount * target_Q\n\n # Get current Q estimates\n current_Q1, current_Q2 = self.critic(state, action)\n\n # Compute critic loss\n critic_loss = F.mse_loss(current_Q1, target_Q) + F.mse_loss(current_Q2, target_Q)\n\n # Optimize the critic\n self.critic_optimizer.zero_grad()\n critic_loss.backward()\n self.critic_optimizer.step()\n\n # Delayed policy updates\n if self.total_it % self.policy_freq == 0:\n\n # Compute actor losse\n actor_loss = -self.critic.Q1(state, self.actor(state)).mean()\n \n # Optimize the actor \n self.actor_optimizer.zero_grad()\n actor_loss.backward()\n self.actor_optimizer.step()\n\n # Update the frozen target models\n for param, target_param in zip(self.critic.parameters(), self.critic_target.parameters()):\n target_param.data.copy_(self.tau * param.data + (1 - self.tau) * target_param.data)\n\n for param, target_param in zip(self.actor.parameters(), self.actor_target.parameters()):\n target_param.data.copy_(self.tau * param.data + (1 - self.tau) * target_param.data)\n\n\n def save(self, filename):\n torch.save(self.critic.state_dict(), filename + \"_critic\")\n torch.save(self.critic_optimizer.state_dict(), filename + \"_critic_optimizer\")\n \n torch.save(self.actor.state_dict(), filename + \"_actor\")\n torch.save(self.actor_optimizer.state_dict(), filename + \"_actor_optimizer\")\n\n\n def load(self, filename):\n self.critic.load_state_dict(torch.load(filename + \"_critic\"))\n self.critic_optimizer.load_state_dict(torch.load(filename + \"_critic_optimizer\"))\n self.critic_target = copy.deepcopy(self.critic)\n\n self.actor.load_state_dict(torch.load(filename + \"_actor\"))\n self.actor_optimizer.load_state_dict(torch.load(filename + \"_actor_optimizer\"))\n self.actor_target = copy.deepcopy(self.actor)\n\n def check_existed_files(self, filename):\n print (\"File exists:\"+str(path.exists(filename + \"_critic\")))\n print (\"File exists:\" + str(path.exists(filename + \"_critic_optimizer\")))\n print (\"File exists:\"+str(path.exists(filename + \"_actor\")))\n print (\"File exists:\" + str(path.exists(filename + \"_actor_optimizer\")))\n\n'''\n REPLAYBUFFER\n'''\n\nclass ReplayBuffer(object):\n def __init__(self, state_dim, action_dim, max_size=int(1e6)):\n self.max_size = max_size\n self.ptr = 0\n self.size = 0\n\n self.state = np.zeros((max_size, state_dim))\n self.action = np.zeros((max_size, action_dim))\n self.next_state = np.zeros((max_size, state_dim))\n self.reward = np.zeros((max_size, 1))\n self.not_done = np.zeros((max_size, 1))\n\n self.device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n\n def add(self, state, action, next_state, reward, done): \n new_state = FF(state)\n new_next_state = FF(next_state)\n self.state[self.ptr] = new_state\n self.action[self.ptr] = action\n self.next_state[self.ptr] = new_next_state\n self.reward[self.ptr] = reward\n self.not_done[self.ptr] = 1. - done\n\n self.ptr = (self.ptr + 1) % self.max_size\n self.size = min(self.size + 1, self.max_size)\n\n\n def sample(self, batch_size):\n ind = np.random.randint(0, self.size, size=batch_size)\n\n return (\n torch.FloatTensor(self.state[ind]).to(self.device),\n torch.FloatTensor(self.action[ind]).to(self.device),\n torch.FloatTensor(self.next_state[ind]).to(self.device),\n torch.FloatTensor(self.reward[ind]).to(self.device),\n torch.FloatTensor(self.not_done[ind]).to(self.device)\n )\n\n\n'''\n MAIN\n'''\n\n# Runs policy for X episodes and returns average reward\n# A fixed seed is used for the eval environment\ndef eval_policy(policy, env_name, seed, eval_episodes=5):\n eval_env = L2M2019Env(visualize=True)\n eval_env.seed(seed + 100)\n\n avg_reward = 0.\n for _ in range(eval_episodes):\n state, done = eval_env.reset(), False\n while not done:\n action = policy.select_action(state)\n\n for i in range(len(action)):\n if action[i] > 1:\n action[i] = 1\n elif action[i] < 0: \n action[i] = 0\n\n # print(action)\n state, reward, done, _ = eval_env.step(action)\n avg_reward += reward\n\n avg_reward /= eval_episodes\n\n print(\"---------------------------------------\")\n print(f\"Evaluation over {eval_episodes} episodes: {avg_reward:.3f}\")\n print(\"---------------------------------------\")\n return avg_reward\n\n\nif __name__ == \"__main__\":\n \n parser = argparse.ArgumentParser()\n parser.add_argument(\"--policy\", default=\"TD3\") # Policy name (TD3, DDPG or OurDDPG)\n parser.add_argument(\"--env\", default=\"HalfCheetah-v2\") # OpenAI gym environment name\n parser.add_argument(\"--seed\", default=0, type=int) # Sets Gym, PyTorch and Numpy seeds\n parser.add_argument(\"--start_timesteps\", default=10e2, type=int)# Time steps initial random policy is used\n parser.add_argument(\"--eval_freq\", default=1e4, type=int) # How often (time steps) we evaluate\n parser.add_argument(\"--max_timesteps\", default=5e7, type=int) # Max time steps to run environment\n parser.add_argument(\"--expl_noise\", default=0.1) # Std of Gaussian exploration noise\n parser.add_argument(\"--batch_size\", default=256, type=int) # Batch size for both actor and critic\n parser.add_argument(\"--discount\", default=0.99) # Discount factor\n parser.add_argument(\"--tau\", default=0.005) # Target network update rate\n parser.add_argument(\"--policy_noise\", default=0.2) # Noise added to target policy during critic update\n parser.add_argument(\"--noise_clip\", default=0.5) # Range to clip target policy noise\n parser.add_argument(\"--policy_freq\", default=2, type=int) # Frequency of delayed policy updates\n parser.add_argument(\"--save_model\", action=\"store_true\") # Save model and optimizer parameters\n parser.add_argument(\"--load_model\", default=\"true\") # Model load file name, \"\" doesn't load, \"default\" uses file_name\n args = parser.parse_args()\n\n file_name = f\"{args.policy}_{args.env}_{args.seed}\"\n print(\"---------------------------------------\")\n print(f\"Policy: {args.policy}, Env: {args.env}, Seed: {args.seed}\")\n print(\"---------------------------------------\")\n\n if not os.path.exists(\"./results\"):\n os.makedirs(\"./results\")\n\n if args.save_model and not os.path.exists(\"./models\"):\n os.makedirs(\"./models\")\n\n env = L2M2019Env(visualize=True)\n\n # Set seeds\n env.seed(args.seed)\n # env.action_space.seed(seed)\n torch.manual_seed(args.seed)\n np.random.seed(args.seed)\n \n state_dim = env.observation_space.shape[0]\n action_dim = env.action_space.shape[0] \n max_action = float(env.action_space.high[0])\n\n kwargs = {\n \"state_dim\": state_dim,\n \"action_dim\": action_dim,\n \"max_action\": max_action,\n \"discount\": args.discount,\n \"tau\": args.tau,\n }\n\n # Initialize policy\n if args.policy == \"TD3\":\n # Target policy smoothing is scaled wrt the action scale\n kwargs[\"policy_noise\"] = args.policy_noise * max_action\n kwargs[\"noise_clip\"] = args.noise_clip * max_action\n kwargs[\"policy_freq\"] = args.policy_freq\n policy = TD3(**kwargs)\n\n if args.load_model != \"\":\n policy.check_existed_files(\"models/107062240_HW4_data\")\n policy_file = file_name if args.load_model == \"default\" else args.load_model\n policy.load(\"models/107062240_HW4_data\")\n\n replay_buffer = ReplayBuffer(state_dim, action_dim)\n \n # Evaluate untrained policy\n evaluations = [eval_policy(policy, args.env, args.seed)]\n\n state, done = env.reset(), False\n episode_reward = 0\n episode_timesteps = 0\n episode_num = 0\n\n for t in range(int(args.max_timesteps)):\n episode_timesteps += 1\n\n # Select action randomly or according to policy\n if t < args.start_timesteps:\n action = env.action_space.sample()\n else:\n action = (\n policy.select_action(state)\n + np.random.normal(0, max_action * args.expl_noise, size=action_dim)\n ).clip(-max_action, max_action)\n\n for i in range(len(action)):\n if action[i] > 1:\n action[i] = 1\n elif action[i] < 0: \n action[i] = 0\n\n # print(action)\n\n # Perform action\n next_state, reward, done, _ = env.step(action) \n done_bool = float(done) if episode_timesteps < 500 else 0\n\n # Store data in replay buffer\n replay_buffer.add(state, action, next_state, reward, done_bool)\n\n state = next_state\n episode_reward += reward \n\n # Train agent after collecting sufficient data\n if t >= args.start_timesteps:\n # print(\"Start training ........ \")\n policy.train(replay_buffer, args.batch_size)\n\n if done: \n # +1 to account for 0 indexing. +0 on ep_timesteps since it will increment +1 even if done=True\n print(f\"Total T: {t+1} Episode Num: {episode_num+1} Episode T: {episode_timesteps} Reward: {episode_reward:.3f}\")\n # Reset environment\n state, done = env.reset(), False\n episode_reward = 0\n episode_timesteps = 0\n episode_num += 1 \n\n # Evaluate episode\n if (t + 1) % args.eval_freq == 0:\n print(\"save new model!\")\n evaluations.append(eval_policy(policy, args.env, args.seed))\n np.save(f\"./results/{file_name}\", evaluations)\n policy.save(\"models/107062240_HW4_data\")\n" ]
[ [ "numpy.save", "torch.min", "torch.nn.functional.mse_loss", "torch.nn.Linear", "torch.load", "numpy.zeros", "torch.FloatTensor", "torch.randn_like", "torch.manual_seed", "torch.no_grad", "numpy.random.seed", "pandas.json_normalize", "numpy.random.normal", "torch.cuda.is_available", "numpy.array", "numpy.random.randint", "torch.cat" ] ]
gptix/DS--Data-Engineering-
[ "2975e4b538aed3e477bea56f0b09601452ed4837" ]
[ "recommender_vanilla.py" ]
[ "import numpy as np\nimport pandas as pd\nfrom sklearn.neighbors import NearestNeighbors\nfrom sklearn.feature_extraction.text import CountVectorizer\n\n\n### Import and shape data\npath_base = 'gitstuff/buildweek2/DS--Data-Engineering-/'\ndf = pd.read_csv(path_base + 'data/cannabis.csv')\n\n## select subset with high rating\ngood_enough = df[df['Rating'] >= 4.0]\n\n## replace blank flavor with \"\"\ngood_enough = df.replace(np.nan, '', regex=True)\n\ndef clean_string(strng):\n \"\"\"Remove commas and parentheses.\"\"\"\n s = strng.replace(\",\",\" \") # comma-> space\n s = s.replace(\"(\",\" \") # (-> space\n s = s.replace(\")\",\" \") # (-> space\n s = s.lower()\n return s\n\n\n## Clean and concatenate som fields to build strings for creating an embedding.\ncols = ['Type', 'Effects', 'Flavor', 'Description']\nfor col in cols:\n good_enough[col] = good_enough[col].apply(clean_string)\ngood_enough['text'] = good_enough['Type'] + \" \" + good_enough['Effects'] + \" \" + good_enough['Flavor']\n\n## Define a function to create a list of docs to be used to create a sparse matrix.\ndef gather_docs(df):\n \"\"\" Produces List of Documents from a dataframe.\n df: a Pandas dataframe that has the column 'text'.\n Returns a list of strings. \n \"\"\"\n docs = list(df['text'])\n return docs\n\ndocs = gather_docs(good_enough)\n\n\n## Instantiate vectorizer\nvect = CountVectorizer(stop_words='english', max_features=1000)\n\n## Fit Vectorizer\nvect.fit(docs)\n\n## Create a sparse document-term matrix\ndtm = vect.transform(docs)\n\n## Make a dataframe of a condensed version of the DTM, using feature names\ndtm = pd.DataFrame(dtm.todense(), columns=vect.get_feature_names())\n\n## Instantiate Nearestneighbors\nnn = NearestNeighbors(n_neighbors=5, algorithm='kd_tree')\n\n## Fit on Document-Term Matrix\nnn.fit(dtm)\n\n\ndef recommend(txt):\n \"\"\" Receives a string containing strain, effects, and flavors, and \n returns a 2-tuple of (array of scores, array of indexes) describing\n the best matches among strains modeled.\"\"\"\n \n clean_text = clean_string(txt)\n transformed_text = vect.transform([clean_text])\n return nn.kneighbors(transformed_text.todense())" ]
[ [ "pandas.read_csv", "sklearn.neighbors.NearestNeighbors", "sklearn.feature_extraction.text.CountVectorizer" ] ]
simra/msrflute
[ "c28e2e6bcfa9464b8640ccd625393bbed28491c3", "c28e2e6bcfa9464b8640ccd625393bbed28491c3" ]
[ "utils/preprocessing/create-json.py", "extensions/privacy/__init__.py" ]
[ "# Copyright (c) Microsoft Corporation.\n# Licensed under the MIT license.\n\nimport json\nimport time\nfrom tqdm import tqdm\nimport pandas as pd\n\npath = r'C:\\Users\\train.tsv'\n\ndef local_time():\n return str(time.strftime(\"%H:%M:%S\",time.localtime()))\n\n\nprint(local_time() + \" Starting script \" ) \ncolumns = ['author','num1','content','str1','str2','num2','subreddit']\ndf = pd.read_csv(path, sep='\\t', names=columns, header=None)\nprint(local_time() + \" File has been read \" )\n\ndf_authors = pd.DataFrame(df['author'])\ndf_content = pd.DataFrame(df['content'])\ndf_file = pd.concat([df_authors,df_content], axis=1)\nprint(local_time() + \" Data needed has been concatenated \")\n\n\nusers_group = df_file.groupby('author')\ngroup0 = df_file.groupby(['author','content'])\ngroup1 = pd.Series(users_group.size())\nusers = (group1.index).to_numpy() \nprint(local_time() + \" users been formatted \")\nnum_samples = group1.values \nprint(local_time() + \" num_samples has been formatted \")\nuser_data_dict= {}\n\nuser_data_dict= {i: {'x':list()} for i in tqdm(users)}\n\nfor i in tqdm(range(len(df_file))):\n if df_file['content'][i] not in user_data_dict[df_file['author'][i]]['x']:\n user_data_dict[df_file['author'][i]]['x'].append(df_file['content'][i])\n \n\nf = open(r'C:\\Users\\train.json', \"w\")\nnew_data = {'users': users.tolist(), 'num_samples': num_samples.tolist(), 'user_data': user_data_dict}\njson.dump(new_data,f)\nprint(local_time() + \" end of script \")", "# Copyright (c) Microsoft Corporation.\n# Licensed under the MIT license.\n\nimport numpy as np\nimport torch as T\nimport logging\nimport math\nimport json\nfrom utils import print_rank\nfrom azureml.core import Run\nfrom scipy.special import betainc, betaln\n\nrun = Run.get_context()\n\ndef compute_LDP_noise_std(eps, max_sensitivity, delta):\n return np.sqrt(2 * np.log(1.25 / delta)) * max_sensitivity / eps\n\n \ndef _beta2betainc_ratio(a, x):\n return 1 / betainc(a, a, x)\n\n\ndef _log_m1(d, alpha, gamma):\n return alpha * np.log(1 - gamma**2) - (d - 2) * np.log(2) - np.log(d - 1)\n\n\ndef _log_m2(p, tau, alpha):\n return np.log(p / (_beta2betainc_ratio(alpha, tau) - 1) - (1 - p)) + np.log(_beta2betainc_ratio(alpha, tau)) - betaln(alpha, alpha)\n\n\ndef _efficient_m(d, gamma, p):\n alpha = (d - 1) / 2\n tau = (1 + gamma) / 2 \n return np.exp(_log_m1(d, alpha, gamma) + _log_m2(p, tau, alpha))\n\n\ndef privacy_parameters(eps0, eps, d):\n exp_eps0 = np.exp(eps0)\n exp_eps = np.exp(eps)\n if exp_eps0 == np.inf:\n p0 = 1\n else:\n p0 = exp_eps0 / (1 + exp_eps0)\n if exp_eps == np.inf:\n gamma = np.sqrt(np.pi / (2 * (d - 1)))\n else:\n gamma = ((exp_eps - 1) / (exp_eps + 1)) * np.sqrt(np.pi / (2 * (d - 1)))\n return p0, gamma\n\n\ndef private_unit2(grad, gamma, prob):\n np.testing.assert_almost_equal(grad.norm().cpu().item(), 1, decimal=5)\n assert prob >= 0.5\n assert (0 <= gamma <= 1)\n p = T.rand(())\n while True:\n # create a uniform distriubtion over d-sphere\n V = T.normal(0, 1, grad.shape, device=grad.device)\n V = V / V.norm()\n dot_prod = T.dot(V, grad)\n if (dot_prod >= gamma and p < prob) or (dot_prod < gamma and p >= prob):\n break\n d = grad.shape[0]\n m = _efficient_m(d, gamma, prob)\n return V / m\n\n\ndef add_gaussian_noise(grad, eps, max_grad, delta):\n sigma = compute_LDP_noise_std(eps, max_grad, delta)\n #sigma = np.sqrt(2 * np.log(1.25 / delta)) * max_grad / eps\n noisy_grad = sigma * T.randn(grad.shape, device=grad.device) + grad\n return noisy_grad, sigma\n\n\ndef add_private_unit2_noise(eps, grad):\n eps0 = 0.01 * eps\n eps1 = 0.99 * eps\n samp_prob, gamma = privacy_parameters(eps0, eps1, grad.shape[0])\n return private_unit2(grad, gamma, samp_prob)\n\n\ndef scalar_DP(r, eps, k, r_max):\n r = np.minimum(r, r_max)\n val = k * r / r_max\n f_val = math.floor(val)\n c_val = math.ceil(val)\n J = f_val if T.rand(()) < (c_val - val) else c_val\n exp_eps = np.exp(eps)\n rand_prob = exp_eps / (exp_eps + k)\n if T.rand(()) >= rand_prob:\n while True:\n J_ = T.randint(0, k + 1, ()).item()\n if J != J_:\n J = J_\n break\n a = ((exp_eps + k) / (exp_eps - 1)) * (r_max / k)\n b = (k * (k + 1)) / (2 * (exp_eps + k))\n return a * (J - b)\n\n\ndef laplace_noise(max_sens, eps, vocab_size):\n return np.random.laplace(0.0, max_sens/eps, vocab_size)\n\n\ndef unroll_network(named_params, select_grad=False):\n # Unroll the network as 1D vector and save original values indices\n params_ids, flat_params = {}, []\n cur_idx = 0\n for n, p in named_params:\n dat = p.grad if select_grad else p.data\n flat_params.append(dat.view(-1))\n next_idx = cur_idx + flat_params[-1].shape[0]\n params_ids[n] = (cur_idx, next_idx)\n cur_idx = next_idx\n return T.cat(flat_params), params_ids\n\n\ndef update_network(named_params, params_ids, flat_params, apply_to_grad=False):\n # Roll back the network parameters to layers\n for n, p in named_params:\n s_id, e_id = params_ids[n]\n if apply_to_grad:\n p.grad.copy_(flat_params[s_id : e_id].view(*p.grad.shape))\n else:\n p.data.copy_(flat_params[s_id : e_id].view(*p.data.shape))\n\n\ndef apply_global_dp(config, model, num_clients_curr_iter, select_grad=True, metric_logger=None):\n # Add global DP noise here\n dp_config = config.get('dp_config', None)\n if dp_config is not None and dp_config.get('enable_global_dp', False):\n # enable_local_dp must be enabled - client-side gradient clipping must be enabled.\n assert (dp_config['enable_local_dp'])\n # Unroll the network grads as 1D vectors\n flat_grad, params_ids = unroll_network(model.named_parameters(), select_grad=select_grad)\n\n sigma = dp_config['global_sigma']\n max_grad = dp_config['max_grad']\n noise_scale = sigma * max_grad / num_clients_curr_iter\n noise = T.normal(0, 1, flat_grad.shape, device=flat_grad.device) * noise_scale\n flat_noisy_grad = flat_grad + noise\n print_rank('Error from noise {} is {}. grad norm: {} noisy_grad norm: {}'.format(noise_scale, (\n flat_grad - flat_noisy_grad).norm(), flat_grad.norm(), flat_noisy_grad.norm()))\n\n # Return back to the network gradients\n update_network(model.named_parameters(), params_ids, flat_noisy_grad,\n apply_to_grad=select_grad)\n\n if metric_logger is None:\n metric_logger = Run.get_context().log\n metric_logger('Gradient Norm', flat_grad.norm().cpu().item())\n\n\ndef apply_local_dp(trainer, weight, dp_config, add_weight_noise):\n '''Apply client-side DP, possibly given a data-dependent aggregation weight\n\n Args:\n trainer (core.Trainer object): trainer on client.\n dp_config (dict): DP config on original config file.\n add_weight_noise (bool): whether noise should be added to aggregation weight.\n '''\n\n # Unroll the network grads as 1D vectors\n flat_grad, params_ids = unroll_network(trainer.model.named_parameters(), select_grad=True)\n grad_norm = flat_grad.norm().cpu().item()\n\n if dp_config['eps'] < 0:\n # clip, but don't add noise\n if grad_norm > dp_config['max_grad']:\n flat_grad = flat_grad * (dp_config['max_grad'] / grad_norm)\n update_network(trainer.model.named_parameters(), params_ids, flat_grad, apply_to_grad=True)\n\n else:\n # Get Gaussian LDP noise\n dp_eps = dp_config['eps']\n delta = dp_config.get('delta', 1e-7) # TODO pre-compute in config\n weight_ = weight\n\n # Scaling the weight down so we don't impact the noise too much\n weight = dp_config.get('weight_scaler', 1) * weight\n weight = min(dp_config['max_weight'], weight)\n flat_noisy_grad = dp_config['max_grad'] * (flat_grad / flat_grad.norm())\n max_sensitivity = np.sqrt(dp_config['max_grad']**2 + (dp_config['max_weight']**2 if add_weight_noise else 0.0))\n flat_noisy_grad = T.cat([flat_noisy_grad, T.tensor([weight], device=flat_noisy_grad.device)], dim=0)\n flat_noisy_grad, _ = add_gaussian_noise(flat_noisy_grad, dp_eps, max_sensitivity, delta)\n weight = min(max(flat_noisy_grad[-1].item(), dp_config['min_weight']), dp_config['max_weight'])\n\n # Scaling the weight back up after noise addition (This is a DP-protect transformation)\n weight = weight / dp_config.get('weight_scaler', 1)\n if not add_weight_noise:\n weight = weight_\n flat_noisy_grad = flat_noisy_grad[:-1]\n\n print_rank('Cosine error from noise {}'.format(T.nn.functional.cosine_similarity(flat_grad, flat_noisy_grad, dim=0)), loglevel=logging.DEBUG)\n print_rank('Error from noise is {}'.format((flat_grad-flat_noisy_grad).norm()), loglevel=logging.DEBUG)\n print_rank('weight is {} and noisy weight is {}'.format(weight_, weight), loglevel=logging.DEBUG)\n\n # Return back to the network\n update_network(trainer.model.named_parameters(), params_ids, flat_noisy_grad, apply_to_grad=True)\n\n return weight\n\n\ndef update_privacy_accountant(config, num_clients, curr_iter, num_clients_curr_iter):\n # Privacy accounting starts here\n # We will dump all the needed parameters to the log so as not to slow down training.\n dp_config = config.get('dp_config', None)\n if dp_config is not None and dp_config.get('enable_global_dp', False) or dp_config.get('enable_local_dp',\n False):\n from math import sqrt, exp, log\n import extensions.privacy.analysis as privacy_analysis\n\n K = 1 # from DP perspective each user is contributing one gradient\n B = num_clients_curr_iter # batch size\n n = num_clients\n T = curr_iter + 1\n _delta = dp_config.get('delta', min(1e-7, 1. / (n * log(n)))) # TODO should be precomputed in config\n if dp_config.get('global_sigma', None) is None:\n max_sensitivity = np.sqrt(dp_config['max_grad'] ** 2 + dp_config['max_weight'] ** 2)\n noise_scale = compute_LDP_noise_std(dp_config['eps'], max_sensitivity, _delta)\n global_sigma = noise_scale * np.sqrt(B) / max_sensitivity\n else: \n global_sigma = dp_config['global_sigma']\n noise_scale = global_sigma * dp_config['max_grad'] / B\n\n try:\n mu = K * B / n * sqrt(T * exp((1. / global_sigma) ** 2 - 1))\n except OverflowError:\n print_rank(f\"Error computing mu {global_sigma} {K} {B} {n} {T}\")\n mu = -1\n\n orders = ([1.25, 1.5, 1.75, 2., 2.25, 2.5, 3., 3.5, 4., 4.5] + list(range(5, 64)) + [128, 256, 512])\n q = B / n\n _sigma = global_sigma # was: noise_scale but we should apply the noise multiplier.\n rdp = privacy_analysis.compute_rdp(q, _sigma, T, orders)\n\n rdp_epsilon, opt_order = privacy_analysis.get_privacy_spent(orders, rdp, _delta)\n\n props = {\n 'dp_global_K': K, # gradients per user\n 'dp_global_B': B, # users per batch\n 'dp_global_n': n, # total users\n 'dp_global_T': T, # how many iterations\n 'dp_sigma': _sigma, # noise_multiplier. Should be combined global+local sigma.\n 'dp_global_mu': mu,\n # 'dp_epsilon_fdp': fdp_epsilon,\n 'dp_epsilon_rdp': rdp_epsilon,\n # 'dp_epsilon_exact': exact_eps,\n 'dp_opt_order': opt_order,\n 'dp_delta': _delta,\n 'dp_noise_scale': noise_scale # Note: not needed for accounting.\n }\n\n print_rank(f'DP accounting: {json.dumps(props)}')\n for k in props:\n run.log(k, props[k])\n\n return rdp_epsilon\n else:\n return None\n" ]
[ [ "pandas.read_csv", "pandas.DataFrame", "pandas.concat" ], [ "numpy.sqrt", "torch.dot", "torch.randint", "torch.randn", "torch.rand", "torch.tensor", "numpy.exp", "torch.normal", "scipy.special.betaln", "torch.nn.functional.cosine_similarity", "numpy.log", "scipy.special.betainc", "numpy.random.laplace", "torch.cat", "numpy.minimum" ] ]
hnguye12333/Python_
[ "6aebb00d18ff5f276e6d1cc006c5e664bf9252eb" ]
[ "discount_cashflows/discount_cashflows.py" ]
[ "path_cashflow = r\"./Cashflows.csv\"\npath_rate = r\"./Yield curve.csv\"\n\n# Implementation with Pandas\nimport pandas as pd\n\n# Read in the cash flows data and rate data as csv\ncashflow_df = pd.read_csv(path_cashflow)\nrate_df = pd.read_csv(path_rate)\n\n# Calculate discount factor from the rates\nrate_df[\"Discount factor\"] = 1 / (1 + rate_df[\"Interest rate\"])**rate_df[\"Year\"]\n\n# Join cash flows with rates\ncf_with_rate_df = cashflow_df.merge(rate_df, on=[\"Currency\", \"Year\"], how=\"left\")\n\n# Calculate present values\ncf_with_rate_df[\"Present value\"] = cf_with_rate_df[\"Cash flows\"] * cf_with_rate_df[\"Discount factor\"]\n\n# Groupby product and check the profitability\ncf_with_rate_df = cf_with_rate_df.groupby(\"Product\")[[\"Present value\"]].sum().reset_index()\n\n# -----\n\n# Implementation with Koalas\nimport databricks.koalas as ks\n\n# Read in the cash flows data and rate data as csv\ncashflow_df = ks.read_csv(path_cashflow)\nrate_df = ks.read_csv(path_rate)\n\n# Calculate discount factor from the rates\nrate_df[\"Discount factor\"] = 1 / (1 + rate_df[\"Interest rate\"])**rate_df[\"Year\"]\n\n# Join cash flows with rates\ncf_with_rate_df = cashflow_df.merge(rate_df, on=[\"Currency\", \"Year\"], how=\"left\")\n\n# Calculate present values\ncf_with_rate_df[\"Present value\"] = cf_with_rate_df[\"Cash flows\"] * cf_with_rate_df[\"Discount factor\"]\n\n# Groupby product and check the profitability\ncf_with_rate_df = cf_with_rate_df.groupby(\"Product\")[[\"Present value\"]].sum().reset_index()\n\n# -----\n\n# Implementation with PySpark\nfrom pyspark.sql import SparkSession\nfrom pyspark.sql import Window\nfrom pyspark.sql import functions as f\n\n# Define Spark settings\nbuilder = SparkSession.builder.appName(\"Discount_Cashflows\")\nspark = builder.getOrCreate()\n\n# Read in the cash flows data and rate data as csv\ncashflow_df = spark.read.csv(path_cashflow, header=True, inferSchema=True)\nrate_df = spark.read.csv(path_rate, header=True, inferSchema=True)\n\n# Calculate discount factor from the rates\nrate_df = rate_df.withColumn(\"Discount factor\", 1 / (1 + rate_df[\"Interest rate\"])**rate_df[\"Year\"])\n\n# Join cash flows with rates\ncf_with_rate_df = cashflow_df.join(f.broadcast(rate_df), on=[\"Currency\", \"Year\"], how=\"left\")\n\n# Calculate present values\ncf_with_rate_df = cf_with_rate_df.withColumn(\"Present value\", f.col(\"Cash flows\") * f.col(\"Discount factor\"))\n\n# Groupby product and check the profitability\ncf_with_rate_df = cf_with_rate_df.groupBy(\"Product\").agg(f.sum(\"Present value\").alias(\"Present value\"))\n\n\n" ]
[ [ "pandas.read_csv" ] ]
caspervdw/circletracking
[ "2d981a1bd3f2982d5d36932d7d5a38e912fcdba3" ]
[ "circletracking/algebraic.py" ]
[ "\"\"\" Functions for algebraic fitting \"\"\"\nfrom __future__ import (absolute_import, division, print_function,\n unicode_literals)\nimport six\nimport numpy as np\n\nMODE_DICT_ELLIPSE = {'circle': 'xy', 'ellipse_aligned': '0', 'ellipse': ''}\nMODE_DICT_ELLIPSOID = {'sphere': 'xyz', 'prolate': 'xy', 'oblate': 'xy',\n 'ellipsoid': '', 'ellipsoid_aligned': '0',\n 'prolate_aligned': '0xy', 'oblate_aligned': '0xy'}\n\n\ndef fit_ellipse(coords, mode=''):\n \"\"\" Fits an ellipse to datapoints using an algebraic method.\n\n This method is different from least squares minimization of the distnace\n between each datapoint and the fitted ellipse (so-called geometrical\n approach). For circles, this makes no difference. The higher the aspect\n ratio of the ellipse, the worse the approximation gets.\n\n Parameters\n ----------\n coords : numpy array of floats\n array of shape (N, 2) containing datapoints\n mode : {'circle', 'ellipse', 'ellipse_aligned'}\n 'ellipse' or None fits an arbitrary ellipse (default)\n 'circle' fits a circle\n 'ellipse_aligned' fits an ellipse with its axes aligned along [x y] axes\n\n Returns\n -------\n center, radii, angle\n\n References\n ----------\n .. [1] Bertoni B (2010) Multi-dimensional ellipsoidal fitting.\n \"\"\"\n if coords.shape[0] != 2:\n raise ValueError('Input data must have two columns!')\n if mode in MODE_DICT_ELLIPSE:\n mode = MODE_DICT_ELLIPSE[mode]\n\n x = coords[0, :, np.newaxis]\n y = coords[1, :, np.newaxis]\n\n if mode == '':\n D = np.hstack((x**2 - y**2, 2*x*y, 2*x, 2*y, np.ones_like(x)))\n elif mode == '0':\n D = np.hstack((x**2 - y**2, 2*x, 2*y, np.ones_like(x)))\n elif mode == 'xy':\n D = np.hstack((2*x, 2*y, np.ones_like(x)))\n\n d2 = x**2 + y**2 # the RHS of the llsq problem (y's)\n u = np.linalg.solve(np.dot(D.T, D), (np.dot(D.T, d2)))[:, 0]\n v = np.empty((6), dtype=u.dtype)\n\n if mode == '':\n v[0] = u[0] - 1\n v[1] = -u[0] - 1\n v[2:] = u[1:]\n elif mode == '0':\n v[0] = u[0] - 1\n v[1] = -u[0] - 1\n v[2] = 0\n v[3:] = u[1:]\n elif mode == 'xy':\n v[:2] = -1\n v[2] = 0\n v[3:] = u\n\n A = np.array([[v[0], v[2], v[3]],\n [v[2], v[1], v[4]],\n [v[3], v[4], v[5]]])\n # find the center of the ellipse\n center = -np.linalg.solve(A[:2, :2], v[3:5])\n\n # translate to the center\n T = np.identity(3, dtype=A.dtype)\n T[2, :2] = center\n R = np.dot(np.dot(T, A), T.T)\n\n # solve the eigenproblem\n evals, evecs = np.linalg.eig(R[:2, :2] / -R[2, 2])\n radius = (np.sqrt(1 / np.abs(evals)) * np.sign(evals))\n\n if mode == '':\n new_order = np.argmax(np.abs(evecs), 1)\n radius = radius[new_order]\n evecs = evecs[:, new_order]\n r11, r12, r21, r22 = evecs.T.flat\n angle = np.arctan(-r12/r11)\n else:\n angle = 0\n\n return radius, center, angle\n\n\ndef fit_ellipsoid(coords, mode='', return_mode=''):\n \"\"\"\n Fit an ellipsoid/sphere/paraboloid/hyperboloid to a set of xyz data points:\n\n Parameters\n ----------\n coords : ndarray\n Cartesian coordinates, 3 x n array\n mode : {'', 'xy', 'xz', 'xyz', '0', '0xy', '0xz'} t\n '' or None fits an arbitrary ellipsoid (default)\n 'xy' fits a spheroid with x- and y- radii equal\n 'xz' fits a spheroid with x- and z- radii equal\n 'xyz' fits a sphere\n '0' fits an ellipsoid with its axes aligned along [x y z] axes\n '0xy' the same with x- and y- radii equal\n '0xz' the same with x- and z- radii equal\n return_mode : {'', 'euler', 'skew'}\n '' returns the directions of the radii as 3x3 array\n 'euler' returns euler angles\n 'skew' returns skew in xy\n\n Returns\n -------\n radius : ndarray\n ellipsoid radii [zr, yr, xr]\n center : ndarray\n ellipsoid center coordinates [zc, yc, xc]\n value :\n return_mode == '': the radii directions as columns of the 3x3 matrix\n return_mode == 'euler':\n euler angles, applied in x, y, z order [z, y, x]\n the y value is the angle with the z axis (tilt)\n the z value is the angle around the z axis (rotation)\n the x value is the 3rd rotation, should be around 0\n return_mode == 'skew':\n skew in y, x order\n\n Notes\n -----\n Author: Yury Petrov, Oculus VR Date: September, 2015\n ported to python by Casper van der Wel, December 2015\n added euler angles and skew by Casper van der Wel\n \"\"\"\n if coords.shape[0] != 3:\n raise ValueError('Input data must have three columns!')\n if mode in MODE_DICT_ELLIPSOID:\n mode = MODE_DICT_ELLIPSOID[mode]\n if return_mode == 'skew' and 'xy' not in mode:\n raise ValueError('Cannot return skew when x, y radii are not equal')\n if return_mode == 'euler':\n raise ValueError('Euler mode is not implemented fully')\n z = coords[0, :, np.newaxis]\n y = coords[1, :, np.newaxis]\n x = coords[2, :, np.newaxis]\n\n # fit ellipsoid in the form Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx +\n # 2Hy + 2Iz + J = 0 and A + B + C = 3 constraint removing one extra param\n if mode == '':\n D = np.hstack((x**2 + y**2 - 2 * z**2, x**2 + z**2 - 2 * y**2,\n 2 * x * y, 2 * x * z, 2 * y * z, 2 * x, 2 * y, 2 * z,\n np.ones_like(x)))\n elif mode == 'xy':\n D = np.hstack((x**2 + y**2 - 2 * z**2, 2 * x * y, 2 * x * z, 2 * y * z,\n 2 * x, 2 * y, 2 * z, np.ones_like(x)))\n elif mode == 'xz':\n D = np.hstack((x**2 + z**2 - 2 * y**2, 2 * x * y, 2 * x * z, 2 * y * z,\n 2 * x, 2 * y, 2 * z, np.ones_like(x)))\n\n # fit ellipsoid in the form Ax^2 + By^2 + Cz^2 + 2Gx + 2Hy + 2Iz = 1\n elif mode == '0':\n D = np.hstack((x**2 + y**2 - 2 * z**2, x**2 + z**2 - 2 * y**2,\n 2 * x, 2 * y, 2 * z, np.ones_like(x)))\n\n # fit ellipsoid in the form Ax^2 + By^2 + Cz^2 + 2Gx + 2Hy + 2Iz = 1,\n # where A = B or B = C or A = C\n elif mode == '0xy':\n D = np.hstack((x**2 + y**2 - 2 * z**2, 2 * x, 2 * y, 2 * z,\n np.ones_like(x)))\n elif mode == '0xz':\n D = np.hstack((x**2 + z**2 - 2 * y**2, 2 * x, 2 * y, 2 * z,\n np.ones_like(x)))\n\n # fit sphere in the form A(x^2 + y^2 + z^2) + 2Gx + 2Hy + 2Iz = 1\n elif mode == 'xyz':\n D = np.hstack((2 * x, 2 * y, 2 * z, np.ones_like(x)))\n else:\n raise ValueError('Unknown mode \"{}\"'.format(mode))\n\n if D.shape[0] < D.shape[1]:\n raise ValueError('Not enough datapoints')\n\n # solve the normal system of equations\n d2 = x**2 + y**2 + z**2 # the RHS of the llsq problem (y's)\n u = np.linalg.solve(np.dot(D.T, D), (np.dot(D.T, d2)))[:, 0]\n\n # find the ellipsoid parameters\n # convert back to the conventional algebraic form\n v = np.empty((10), dtype=u.dtype)\n if mode == '':\n v[0] = u[0] + u[1] - 1\n v[1] = u[0] - 2 * u[1] - 1\n v[2] = u[1] - 2 * u[0] - 1\n v[3:10] = u[2:9]\n elif mode == 'xy':\n v[0] = u[0] - 1\n v[1] = u[0] - 1\n v[2] = -2 * u[0] - 1\n v[3:10] = u[1:8]\n elif mode == 'xz':\n v[0] = u[0] - 1\n v[1] = -2 * u[0] - 1\n v[2] = u[0] - 1\n v[3:10] = u[1:8]\n elif mode == '0':\n v[0] = u[0] + u[1] - 1\n v[1] = u[0] - 2 * u[1] - 1\n v[2] = u[1] - 2 * u[0] - 1\n v[3:6] = 0\n v[6:10] = u[2:6]\n elif mode == '0xy':\n v[0] = u[0] - 1\n v[1] = u[0] - 1\n v[2] = -2 * u[0] - 1\n v[3:6] = 0\n v[6:10] = u[2:6]\n elif mode == '0xz':\n v[0] = u[0] - 1\n v[1] = -2 * u[0] - 1\n v[2] = u[0] - 1\n v[3:6] = 0\n v[6:10] = u[2:6]\n elif mode == 'xyz':\n v[:3] = -1\n v[3:6] = 0\n v[6:10] = u[:4]\n\n # form the algebraic form of the ellipsoid\n A = np.array([[v[0], v[3], v[4], v[6]],\n [v[3], v[1], v[5], v[7]],\n [v[4], v[5], v[2], v[8]],\n [v[6], v[7], v[8], v[9]]])\n # find the center of the ellipsoid\n center = -np.linalg.solve(A[:3, :3], v[6:9])\n\n # form the corresponding translation matrix\n T = np.identity(4, dtype=A.dtype)\n T[3, :3] = center\n # translate to the center\n R = np.dot(np.dot(T, A), T.T)\n if return_mode == 'skew':\n # extract the xy skew (ignoring a parameter here!)\n skew_xy = -R[2, :2] / np.diag(R[:2, :2])\n radius = np.diag(R[:3, :3]) / R[3, 3]\n\n # do some trick to make radius_z be the unskewed radius\n radius[2] -= np.sum(radius[:2] * skew_xy**2)\n radius = np.sqrt(1 / np.abs(radius))\n return radius[::-1], center[::-1], skew_xy[::-1]\n\n # solve the eigenproblem\n evals, evecs = np.linalg.eig(R[:3, :3] / -R[3, 3])\n radii = (np.sqrt(1 / np.abs(evals)) * np.sign(evals))\n\n if return_mode == 'euler':\n # sort the vectors so that -> z, y, x\n new_order = np.argmax(np.abs(evecs), 1)\n radii = radii[new_order]\n evecs = evecs[:, new_order]\n\n # Discover Euler angle vector from 3x3 matrix\n cy_thresh = np.finfo(evecs.dtype).eps * 4\n r11, r12, r13, r21, r22, r23, r31, r32, r33 = evecs.T.flat\n # cy: sqrt((cos(y)*cos(z))**2 + (cos(x)*cos(y))**2)\n cy = np.sqrt(r33*r33 + r23*r23)\n if cy > cy_thresh: # cos(y) not close to zero, standard form\n # z: atan2(cos(y)*sin(z), cos(y)*cos(z)),\n # y: atan2(sin(y), cy), atan2(cos(y)*sin(x),\n # x: cos(x)*cos(y))\n angles = np.array([np.arctan(r12/r11), np.arctan(-r13/cy),\n np.arctan(r23/r33)])\n else: # cos(y) (close to) zero, so x -> 0.0 (see above)\n # so r21 -> sin(z), r22 -> cos(z) and\n # y: atan2(sin(y), cy)\n angles = np.array([np.arctan(-r21/r22), np.arctan(-r13/cy), 0.0])\n\n return radii[::-1], center[::-1], angles\n\n return radii[::-1], center[::-1], evecs[::-1]\n\n\ndef ellipse_grid(radius, center, rotation=0, skew=0, n=None, spacing=1):\n \"\"\" Returns points and normal (unit) vectors on an ellipse.\n\n Parameters\n ----------\n radius : tuple\n (yr, xr) the two principle radii of the ellipse\n center : tuple\n (yc, xc) the center coordinate of the ellipse\n rotation : float, optional\n angle of xr with the x-axis, in radians. Rotates clockwise in image.\n skew : float, optional\n skew: y -> y + skew * x\n n : int, optional\n number of points\n spacing : float, optional\n When `n` is not given then the spacing is determined by `spacing`.\n\n Returns\n -------\n two arrays of shape (2, N), being the coordinates and unit normals\n \"\"\"\n yr, xr = radius\n yc, xc = center\n if n is None:\n n = int(2*np.pi*np.sqrt((yr**2 + xr**2) / 2) / spacing)\n\n phi = np.linspace(-np.pi, np.pi, n, endpoint=False)\n pos = np.array([yr * np.sin(phi), xr * np.cos(phi)])\n\n normal = np.array([np.sin(phi) / yr, np.cos(phi) / xr])\n normal /= np.sqrt((normal**2).sum(0))\n\n mask = np.isfinite(pos).all(0) & np.isfinite(normal).all(0)\n pos = pos[:, mask]\n normal = normal[:, mask]\n\n if rotation != 0:\n R = np.array([[ np.cos(rotation), np.sin(rotation)],\n [-np.sin(rotation), np.cos(rotation)]])\n pos = np.dot(pos.T, R).T\n elif skew != 0:\n pos[0] += pos[1] * skew\n\n # translate\n pos[0] += yc\n pos[1] += xc\n return pos, normal # both in y_list, x_list format\n\n\ndef ellipsoid_grid(radius, center, spacing=1):\n \"\"\" Returns points and normal (unit) vectors on an ellipse on only\n integer values of z.\n\n Parameters\n ----------\n radius : tuple\n (zr, yr, xr) the three principle radii of the ellipsoid\n center : tuple\n (zc, yc, xc) the center coordinate of the ellipsoid\n spacing : float, optional\n Distance between points\n\n Returns\n -------\n two arrays of shape (3, N), being the coordinates and unit normals\n \"\"\"\n zc, yc, xc = center\n zr, yr, xr = radius\n\n pos = np.empty((3, 0))\n for z in range(int(zc - zr + 1), int(zc + zr) + 1):\n n = int(2*np.pi*np.sqrt((yr**2 + xr**2) / 2) / spacing)\n if n == 0:\n continue\n phi = np.linspace(-np.pi, np.pi, n, endpoint=False)\n factor = np.sqrt(1 - ((zc - z) / zr)**2) # = sin(arccos((zc/z)/zr))\n pos = np.append(pos,\n np.array([[float(z)] * n,\n yr * factor * np.sin(phi) + yc,\n xr * factor * np.cos(phi) + xc]),\n axis=1)\n normal = (pos - np.array(center)[:, np.newaxis]) / np.array(radius)[:, np.newaxis]\n normal /= np.sqrt((normal**2).sum(0))\n\n mask = np.isfinite(pos).all(0) & np.isfinite(normal).all(0)\n return pos[:, mask], normal[:, mask]\n\n\ndef max_linregress(arr, maxfit_size=2, threshold=0.1, axis=1):\n \"\"\" Locates maxima by fitting parabolas to values around the maximum.\n\n This function is optimized for two-dimensional numpy arrays. For each row\n in the array, the index of the maximum value is located. Then some values\n around it (given by ``maxfit_size``) are taken, the first (discrete)\n derivative is taken, and linear regression is done. This gives the location\n of the maximum with sub-pixel precision. Effectively, a parabola is fitted.\n\n Parameters\n ----------\n arr : ndarray\n maxfit_size : integer, optional\n Defines the fit region around the maximum value. By default, this value\n is 2, that is, two pixels before and two pixels after the maximum are\n used for the fit (a total of 5).\n threshold :\n Discard points when the average value of the fit region is lower than\n ``threshold`` times the maximum in the whole fit array. This helps\n discarding low-intensity peaks. Default 0.1: if the average intensity\n in the fitregion is below 10% of the global maximum, the point is\n discarded.\n axis : {0, 1}\n axis along which the maxima are fitted. Default 1.\n\n Returns\n -------\n ndarray with the locations of the maxima.\n Elements are NaN in all of the following cases:\n - any pixel in the fitregion is 0\n - the mean of the fitregion < threshold * global max\n - regression returned infinity\n - maximum is outside of the fit region.\n \"\"\"\n if axis == 0:\n arr = arr.T\n # identify the regions around the max value\n maxes = np.argmax(arr[:, maxfit_size:-maxfit_size],\n axis=1) + maxfit_size\n ind = maxes[:, np.newaxis] + range(-maxfit_size, maxfit_size+1)\n\n # must cast dtype from unsigned to signed integer\n dtype = np.dtype(arr.dtype)\n if dtype.kind == 'u':\n if dtype.itemsize == 1:\n dtype = np.int16\n elif dtype.itemsize == 2:\n dtype = np.int32\n else:\n dtype = np.int64\n else:\n dtype = arr.dtype\n\n fitregion = np.array([_int.take(_ind) for _int, _ind in zip(arr, ind)],\n dtype=dtype)\n\n # fit max using linear regression\n intdiff = np.diff(fitregion, 1)\n x_norm = np.arange(-maxfit_size + 0.5, maxfit_size + 0.5)\n y_mean = np.mean(intdiff, axis=1, keepdims=True)\n y_norm = intdiff - y_mean\n slope = np.sum(x_norm[np.newaxis, :] * y_norm, 1) / np.sum(x_norm * x_norm)\n slope[slope == 0] = np.nan # protect against division by zero\n r_dev = - y_mean[:, 0] / slope\n\n # mask invalid fits\n threshold = threshold * fitregion.max() # relative to global maximum\n with np.errstate(invalid='ignore'): # ignore comparison with np.nan\n valid = (np.isfinite(r_dev) & # finite result\n (fitregion > 0).all(1) & # all pixels in fitregion > 0\n (fitregion.mean(1) > threshold) & # fitregion mean > threshold\n (r_dev > -maxfit_size + 0.5) & # maximum inside fit region\n (r_dev < maxfit_size - 0.5))\n r_dev[~valid] = np.nan\n return r_dev + maxes\n\n\ndef max_edge(arr, threshold=0.5, axis=1):\n \"\"\" Find strongest decreasing edge on each row \"\"\"\n if axis == 0:\n arr = arr.T\n if np.issubdtype(arr.dtype, np.unsignedinteger):\n arr = arr.astype(np.int)\n derivative = -np.diff(arr)\n index = np.argmax(derivative, axis=1)\n values = np.max(derivative, axis=1)\n r_dev = index + 0.5\n r_dev[values < threshold * values.max()] = np.nan\n return r_dev\n" ]
[ [ "numpy.sum", "numpy.diff", "numpy.diag", "numpy.dtype", "numpy.issubdtype", "numpy.ones_like", "numpy.isfinite", "numpy.abs", "numpy.cos", "numpy.identity", "numpy.linspace", "numpy.linalg.eig", "numpy.mean", "numpy.argmax", "numpy.arange", "numpy.max", "numpy.finfo", "numpy.array", "numpy.linalg.solve", "numpy.sign", "numpy.empty", "numpy.arctan", "numpy.errstate", "numpy.sqrt", "numpy.sin", "numpy.dot" ] ]
fengwang/jax
[ "88f888d498ee5a063c7fbdf96ea593ab8bd01849" ]
[ "jax/_src/numpy/lax_numpy.py" ]
[ "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n# pytype: skip-file\n\"\"\"\nImplements the NumPy API, using the primitives in :mod:`jax.lax`.\n\nNumPy operations are implemented in Python in terms of the primitive operations\nin :mod:`jax.lax`. Since NumPy operations are not primitive and instead are\nimplemented in terms of :mod:`jax.lax` operations, we do not need to define\ntransformation rules such as gradient or batching rules. Instead,\ntransformations for NumPy primitives can be derived from the transformation\nrules for the underlying :code:`lax` primitives.\n\"\"\"\n\nimport abc\nimport builtins\nimport collections\nfrom functools import partial\nimport operator\nimport types\nfrom typing import Sequence, FrozenSet, Optional, Tuple, Union, Set, Type, Callable\nfrom textwrap import dedent as _dedent\nimport warnings\n\nimport numpy as np\nimport opt_einsum\n\nimport jax\nfrom jax import jit, custom_jvp\nfrom jax._src.numpy.vectorize import vectorize\nfrom jax._src.numpy.util import _wraps\nfrom jax import core\nfrom jax._src import dtypes\nfrom jax._src.api_util import _ensure_index_tuple\nfrom jax import errors\nfrom jax.core import UnshapedArray, ShapedArray, ConcreteArray, canonicalize_shape\nfrom jax.config import config\nfrom jax.interpreters import pxla\nfrom jax import lax\nfrom jax._src import device_array\nfrom jax._src.lax.lax import _array_copy\nfrom jax._src.ops import scatter\nfrom jax._src.util import (unzip2, prod as _prod, subvals, safe_zip, ceil_of_ratio,\n canonicalize_axis as _canonicalize_axis, maybe_named_axis)\nfrom jax.tree_util import tree_leaves, tree_flatten, tree_map\n\nnewaxis = None\n\n# Common docstring additions:\n\n_PRECISION_DOC = \"\"\"\\\nIn addition to the original NumPy arguments listed below, also supports\n``precision`` for extra control over matrix-multiplication precision\non supported devices. ``precision`` may be set to ``None``, which means\ndefault precision for the backend, a :class:`~jax.lax.Precision` enum value\n(``Precision.DEFAULT``, ``Precision.HIGH`` or ``Precision.HIGHEST``) or a tuple\nof two :class:`~jax.lax.Precision` enums indicating separate precision for each argument.\n\"\"\"\n\n# We replace some builtin names to follow Numpy's API, so we capture here.\n_abs = builtins.abs\n_all = builtins.all\n_any = builtins.any\n_max = builtins.max\n_min = builtins.min\n_sum = builtins.sum\n_divmod = builtins.divmod\n\n# NumPy constants\n\npi = np.pi\ne = np.e\neuler_gamma = np.euler_gamma\ninf = np.inf\nNINF = np.NINF\nPZERO = np.PZERO\nNZERO = np.NZERO\nnan = np.nan\n\n# NumPy utility functions\n\nget_printoptions = np.get_printoptions\nprintoptions = np.printoptions\nset_printoptions = np.set_printoptions\n\n# ndarray is defined as an virtual abstract base class.\n\nclass ArrayMeta(abc.ABCMeta):\n \"\"\"Metaclass for overriding ndarray isinstance checks.\"\"\"\n\n def __instancecheck__(self, instance):\n # Allow tracer instances with avals that are instances of UnshapedArray.\n # We could instead just declare Tracer an instance of the ndarray type, but\n # there can be traced values that are not arrays. The main downside here is\n # that isinstance(x, ndarray) might return true but\n # issubclass(type(x), ndarray) might return false for an array tracer.\n try:\n return (hasattr(instance, \"aval\") and\n isinstance(instance.aval, UnshapedArray))\n except AttributeError:\n super().__instancecheck__(instance)\n\n\nclass ndarray(metaclass=ArrayMeta):\n dtype: np.dtype\n ndim: int\n shape: Tuple[int, ...]\n size: int\n\n def __init__(shape, dtype=None, buffer=None, offset=0, strides=None,\n order=None):\n raise TypeError(\"jax.numpy.ndarray() should not be instantiated explicitly.\"\n \" Use jax.numpy.array, or jax.numpy.zeros instead.\")\n\n @abc.abstractmethod\n def __getitem__(self, key, indices_are_sorted=False,\n unique_indices=False): ...\n @abc.abstractmethod\n def __setitem__(self, key, value): ...\n @abc.abstractmethod\n def __len__(self): ...\n @abc.abstractmethod\n def __iter__(self): ...\n @abc.abstractmethod\n def __reversed__(self): ...\n\n # Comparisons\n @abc.abstractmethod\n def __lt__(self, other): ...\n @abc.abstractmethod\n def __le__(self, other): ...\n @abc.abstractmethod\n def __eq__(self, other): ...\n @abc.abstractmethod\n def __ne__(self, other): ...\n @abc.abstractmethod\n def __gt__(self, other): ...\n @abc.abstractmethod\n def __ge__(self, other): ...\n\n # Unary arithmetic\n\n @abc.abstractmethod\n def __neg__(self): ...\n @abc.abstractmethod\n def __pos__(self): ...\n @abc.abstractmethod\n def __abs__(self): ...\n @abc.abstractmethod\n def __invert__(self): ...\n\n # Binary arithmetic\n\n @abc.abstractmethod\n def __add__(self, other): ...\n @abc.abstractmethod\n def __sub__(self, other): ...\n @abc.abstractmethod\n def __mul__(self, other): ...\n @abc.abstractmethod\n def __matmul__(self, other): ...\n @abc.abstractmethod\n def __truediv__(self, other): ...\n @abc.abstractmethod\n def __floordiv__(self, other): ...\n @abc.abstractmethod\n def __mod__(self, other): ...\n @abc.abstractmethod\n def __divmod__(self, other): ...\n @abc.abstractmethod\n def __pow__(self, other): ...\n @abc.abstractmethod\n def __lshift__(self, other): ...\n @abc.abstractmethod\n def __rshift__(self, other): ...\n @abc.abstractmethod\n def __and__(self, other): ...\n @abc.abstractmethod\n def __xor__(self, other): ...\n @abc.abstractmethod\n def __or__(self, other): ...\n\n @abc.abstractmethod\n def __radd__(self, other): ...\n @abc.abstractmethod\n def __rsub__(self, other): ...\n @abc.abstractmethod\n def __rmul__(self, other): ...\n @abc.abstractmethod\n def __rmatmul__(self, other): ...\n @abc.abstractmethod\n def __rtruediv__(self, other): ...\n @abc.abstractmethod\n def __rfloordiv__(self, other): ...\n @abc.abstractmethod\n def __rmod__(self, other): ...\n @abc.abstractmethod\n def __rdivmod__(self, other): ...\n @abc.abstractmethod\n def __rpow__(self, other): ...\n @abc.abstractmethod\n def __rlshift__(self, other): ...\n @abc.abstractmethod\n def __rrshift__(self, other): ...\n @abc.abstractmethod\n def __rand__(self, other): ...\n @abc.abstractmethod\n def __rxor__(self, other): ...\n @abc.abstractmethod\n def __ror__(self, other): ...\n\n @abc.abstractmethod\n def __bool__(self): ...\n @abc.abstractmethod\n def __complex__(self): ...\n @abc.abstractmethod\n def __int__(self): ...\n @abc.abstractmethod\n def __float__(self): ...\n @abc.abstractmethod\n def __round__(self, ndigits=None): ...\n\n @abc.abstractmethod\n def __index__(self): ...\n\n # np.ndarray methods:\n @abc.abstractmethod\n def all(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None): ...\n @abc.abstractmethod\n def any(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None): ...\n @abc.abstractmethod\n def argmax(self, axis: Optional[int] = None, out=None): ...\n @abc.abstractmethod\n def argmin(self, axis: Optional[int] = None, out=None): ...\n @abc.abstractmethod\n def argpartition(self, kth, axis=-1, kind='introselect', order=None): ...\n @abc.abstractmethod\n def argsort(self, axis: Optional[int] = -1, kind='quicksort', order=None): ...\n @abc.abstractmethod\n def astype(self, dtype): ...\n @abc.abstractmethod\n def choose(self, choices, out=None, mode='raise'): ...\n @abc.abstractmethod\n def clip(self, a_min=None, a_max=None, out=None): ...\n @abc.abstractmethod\n def compress(self, condition, axis: Optional[int] = None, out=None): ...\n @abc.abstractmethod\n def conj(self): ...\n @abc.abstractmethod\n def conjugate(self): ...\n @abc.abstractmethod\n def copy(self): ...\n @abc.abstractmethod\n def cumprod(self, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None): ...\n @abc.abstractmethod\n def cumsum(self, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None): ...\n @abc.abstractmethod\n def diagonal(self, offset=0, axis1: int = 0, axis2: int = 1): ...\n @abc.abstractmethod\n def dot(self, b, *, precision=None): ...\n @abc.abstractmethod\n def flatten(self): ...\n @property\n @abc.abstractmethod\n def imag(self): ...\n @abc.abstractmethod\n def item(self, *args): ...\n @abc.abstractmethod\n def max(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None): ...\n @abc.abstractmethod\n def mean(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=False, *, where=None,): ...\n @abc.abstractmethod\n def min(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None): ...\n @property\n @abc.abstractmethod\n def nbytes(self): ...\n @abc.abstractmethod\n def nonzero(self, *, size=None, fill_value=None): ...\n @abc.abstractmethod\n def prod(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None, initial=None, where=None): ...\n @abc.abstractmethod\n def ptp(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=False,): ...\n @abc.abstractmethod\n def ravel(self, order='C'): ...\n @property\n @abc.abstractmethod\n def real(self): ...\n @abc.abstractmethod\n def repeat(self, repeats, axis: Optional[int] = None, *,\n total_repeat_length=None): ...\n @abc.abstractmethod\n def reshape(self, *args, order='C'): ...\n @abc.abstractmethod\n def round(self, decimals=0, out=None): ...\n @abc.abstractmethod\n def searchsorted(self, v, side='left', sorter=None): ...\n @abc.abstractmethod\n def sort(self, axis: Optional[int] = -1, kind='quicksort', order=None): ...\n @abc.abstractmethod\n def squeeze(self, axis: Optional[Union[int, Tuple[int, ...]]] = None): ...\n @abc.abstractmethod\n def std(self, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None, ddof=0, keepdims=False, *, where=None): ...\n @abc.abstractmethod\n def sum(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None, initial=None, where=None): ...\n @abc.abstractmethod\n def swapaxes(self, axis1: int, axis2: int): ...\n @abc.abstractmethod\n def take(self, indices, axis: Optional[int] = None, out=None,\n mode=None): ...\n @abc.abstractmethod\n def tobytes(self, order='C'): ...\n @abc.abstractmethod\n def tolist(self): ...\n @abc.abstractmethod\n def trace(self, offset=0, axis1: int = 0, axis2: int = 1, dtype=None,\n out=None): ...\n @abc.abstractmethod\n def transpose(self, *args): ...\n @abc.abstractmethod\n def var(self, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None, ddof=0, keepdims=False, *, where=None): ...\n @abc.abstractmethod\n def view(self, dtype=None, type=None): ...\n\n # Even though we don't always support the NumPy array protocol, e.g., for\n # tracer types, for type checking purposes we must declare support so we\n # implement the NumPy ArrayLike protocol.\n def __array__(self): ...\n\n # JAX extensions\n @property\n @abc.abstractmethod\n def at(self): ...\n @property\n @abc.abstractmethod\n def aval(self): ...\n @property\n @abc.abstractmethod\n def weak_type(self) -> bool: ...\n\n\nndarray.register(device_array.DeviceArray)\nfor t in device_array.device_array_types:\n ndarray.register(t)\nndarray.register(pxla._SDA_BASE_CLASS)\n\n\n\niscomplexobj = np.iscomplexobj\n\nshape = _shape = np.shape\nndim = _ndim = np.ndim\nsize = np.size\n_dtype = partial(dtypes.dtype, canonicalize=True)\n\n# At present JAX doesn't have a reason to distinguish between scalars and arrays\n# in its object system. Further, we want JAX scalars to have the same type\n# promotion behaviors as JAX arrays. Rather than introducing a new type of JAX\n# scalar object with JAX promotion behaviors, instead we make the JAX scalar\n# types return JAX arrays when instantiated.\n\nclass _ScalarMeta(type):\n def __hash__(self):\n return hash(self.dtype.type)\n\n def __eq__(self, other):\n return id(self) == id(other) or self.dtype.type == other\n\n def __ne__(self, other):\n return not (self == other)\n\n def __call__(self, x):\n return array(x, dtype=self.dtype)\n\n def __instancecheck__(self, instance):\n return isinstance(instance, self.dtype.type)\n\ndef _make_scalar_type(np_scalar_type):\n return _ScalarMeta(np_scalar_type.__name__, (object,),\n {\"dtype\": np.dtype(np_scalar_type)})\n\nbool_ = _make_scalar_type(np.bool_)\nuint8 = _make_scalar_type(np.uint8)\nuint16 = _make_scalar_type(np.uint16)\nuint32 = _make_scalar_type(np.uint32)\nuint64 = _make_scalar_type(np.uint64)\nint8 = _make_scalar_type(np.int8)\nint16 = _make_scalar_type(np.int16)\nint32 = _make_scalar_type(np.int32)\nint64 = _make_scalar_type(np.int64)\nbfloat16 = _make_scalar_type(dtypes.bfloat16)\nfloat16 = _make_scalar_type(np.float16)\nfloat32 = single = _make_scalar_type(np.float32)\nfloat64 = double = _make_scalar_type(np.float64)\ncomplex64 = csingle = _make_scalar_type(np.complex64)\ncomplex128 = cdouble = _make_scalar_type(np.complex128)\n\nint_ = int32 if dtypes.int_ == np.int32 else int64\nuint = uint32 if dtypes.uint == np.uint32 else uint64\nfloat_ = float32 if dtypes.float_ == np.float32 else float64\ncomplex_ = complex64 if dtypes.complex_ == np.complex64 else complex128\n\nnumber = np.number\ninexact = np.inexact\ncomplexfloating = np.complexfloating\nfloating = np.floating\ninteger = np.integer\nsignedinteger = np.signedinteger\nunsignedinteger = np.unsignedinteger\n\nflexible = np.flexible\ncharacter = np.character\nobject_ = np.object_\n\niinfo = dtypes.iinfo\nfinfo = dtypes.finfo\n\ndtype = np.dtype\ncan_cast = dtypes.can_cast\nissubsctype = dtypes.issubsctype\npromote_types = dtypes.promote_types\n\nComplexWarning = np.ComplexWarning\n\narray_str = np.array_str\narray_repr = np.array_repr\n\nsave = np.save\nsavez = np.savez\n\n@_wraps(np.dtype)\ndef _jnp_dtype(obj, align=False, copy=False):\n \"\"\"Similar to np.dtype, but respects JAX dtype defaults.\"\"\"\n if obj is None:\n obj = dtypes.float_\n elif isinstance(obj, type) and obj in dtypes.python_scalar_dtypes:\n obj = _DEFAULT_TYPEMAP[np.dtype(obj, align=align, copy=copy).type]\n return np.dtype(obj, align=align, copy=copy)\n\n### utility functions\n\n_DEFAULT_TYPEMAP = {\n np.bool_: bool_,\n np.int_: int_,\n np.float_: float_,\n np.complex_: complex_\n}\n\n_INT_DTYPES = {\n 16: np.int16,\n 32: np.int32,\n 64: np.int64,\n}\n\ndef _promote_shapes(fun_name, *args):\n \"\"\"Prepend implicit leading singleton dimensions for Numpy broadcasting.\"\"\"\n if len(args) < 2:\n return args\n else:\n shapes = [shape(arg) for arg in args]\n nonscalar_ranks = [len(shp) for shp in shapes if shp]\n if not nonscalar_ranks or len(set(nonscalar_ranks)) == 1:\n return args\n else:\n if config.jax_numpy_rank_promotion != \"allow\":\n _rank_promotion_warning_or_error(fun_name, shapes)\n result_rank = len(lax.broadcast_shapes(*shapes))\n return [broadcast_to(arg, (1,) * (result_rank - len(shp)) + shp)\n for arg, shp in zip(args, shapes)]\n\ndef _rank_promotion_warning_or_error(fun_name, shapes):\n if config.jax_numpy_rank_promotion == \"warn\":\n msg = (\"Following NumPy automatic rank promotion for {} on shapes {}. \"\n \"Set the jax_numpy_rank_promotion config option to 'allow' to \"\n \"disable this warning; for more information, see \"\n \"https://jax.readthedocs.io/en/latest/rank_promotion_warning.html.\")\n warnings.warn(msg.format(fun_name, ' '.join(map(str, shapes))))\n elif config.jax_numpy_rank_promotion == \"raise\":\n msg = (\"Operands could not be broadcast together for {} on shapes {} \"\n \"and with the config option jax_numpy_rank_promotion='raise'. \"\n \"For more information, see \"\n \"https://jax.readthedocs.io/en/latest/rank_promotion_warning.html.\")\n raise ValueError(msg.format(fun_name, ' '.join(map(str, shapes))))\n\ndef _promote_dtypes(*args):\n \"\"\"Convenience function to apply Numpy argument dtype promotion.\"\"\"\n # TODO(dougalm,mattjj): This is a performance bottleneck. Consider memoizing.\n if len(args) < 2:\n return args\n else:\n to_dtype, weak_type = dtypes._lattice_result_type(*args)\n to_dtype = dtypes.canonicalize_dtype(to_dtype)\n return [lax._convert_element_type(x, to_dtype, weak_type) for x in args]\n\ndef _promote_dtypes_inexact(*args):\n \"\"\"Convenience function to apply Numpy argument dtype promotion.\n\n Promotes arguments to an inexact type.\"\"\"\n to_dtype, weak_type = dtypes._lattice_result_type(*args)\n to_dtype = dtypes.canonicalize_dtype(to_dtype)\n to_dtype_inexact = _to_inexact_dtype(to_dtype)\n weak_type = (weak_type and to_dtype == to_dtype_inexact)\n return [lax._convert_element_type(x, to_dtype_inexact, weak_type) for x in args]\n\ndef _to_inexact_dtype(dtype):\n \"\"\"Promotes a dtype into an inexact dtype, if it is not already one.\"\"\"\n return dtype if issubdtype(dtype, inexact) else promote_types(dtype, float_)\n\ndef _complex_elem_type(dtype):\n \"\"\"Returns the float type of the real/imaginary parts of a complex dtype.\"\"\"\n return np.abs(np.zeros((), dtype)).dtype\n\ndef _result_dtype(op, *args):\n \"\"\"Compute result dtype of applying op to arguments with given dtypes.\"\"\"\n args = [np.ones((0,) * ndim(arg), _dtype(arg)) for arg in args]\n return _dtype(op(*args))\n\n\ndef _arraylike(x):\n return (isinstance(x, np.ndarray) or isinstance(x, ndarray) or\n hasattr(x, '__jax_array__') or isscalar(x))\n\n\ndef _stackable(*args):\n return _all(type(arg) in stackables for arg in args)\nstackables: Set[Type] = set()\n_register_stackable: Callable[[Type], None] = stackables.add\n\ndef _check_arraylike(fun_name, *args):\n \"\"\"Check if all args fit JAX's definition of arraylike.\"\"\"\n assert isinstance(fun_name, str), f\"fun_name must be a string. Got {fun_name}\"\n if _any(not _arraylike(arg) for arg in args):\n pos, arg = next((i, arg) for i, arg in enumerate(args)\n if not _arraylike(arg))\n msg = \"{} requires ndarray or scalar arguments, got {} at position {}.\"\n raise TypeError(msg.format(fun_name, type(arg), pos))\n\ndef _check_no_float0s(fun_name, *args):\n \"\"\"Check if none of the args have dtype float0.\"\"\"\n if _any(dtypes.dtype(arg) is dtypes.float0 for arg in args):\n raise TypeError(\n f\"Called {fun_name} with a float0 array. \"\n \"float0s do not support any operations by design because they \"\n \"are not compatible with non-trivial vector spaces. No implicit dtype \"\n \"conversion is done. You can use np.zeros_like(arr, dtype=np.float) \"\n \"to cast a float0 array to a regular zeros array. \\n\"\n \"If you didn't expect to get a float0 you might have accidentally \"\n \"taken a gradient with respect to an integer argument.\")\n\ndef _promote_args(fun_name, *args):\n \"\"\"Convenience function to apply Numpy argument shape and dtype promotion.\"\"\"\n _check_arraylike(fun_name, *args)\n _check_no_float0s(fun_name, *args)\n return _promote_shapes(fun_name, *_promote_dtypes(*args))\n\ndef _promote_args_inexact(fun_name, *args):\n \"\"\"Convenience function to apply Numpy argument shape and dtype promotion.\n\n Promotes non-inexact types to an inexact type.\"\"\"\n _check_arraylike(fun_name, *args)\n _check_no_float0s(fun_name, *args)\n return _promote_shapes(fun_name, *_promote_dtypes_inexact(*args))\n\ndef _convert_and_clip_integer(val, dtype):\n \"\"\"\n Convert integer-typed val to specified integer dtype, clipping to dtype\n range rather than wrapping.\n\n Args:\n val: value to be converted\n dtype: dtype of output\n\n Returns:\n equivalent of val in new dtype\n\n Examples\n --------\n Normal integer type conversion will wrap:\n\n >>> val = jnp.uint32(0xFFFFFFFF)\n >>> val.astype('int32')\n DeviceArray(-1, dtype=int32)\n\n This function clips to the values representable in the new type:\n\n >>> _convert_and_clip_integer(val, 'int32')\n DeviceArray(2147483647, dtype=int32)\n \"\"\"\n val = val if isinstance(val, ndarray) else asarray(val)\n dtype = dtypes.canonicalize_dtype(dtype)\n if not (issubdtype(dtype, integer) and issubdtype(val.dtype, integer)):\n raise TypeError(\"_convert_and_clip_integer only accepts integer dtypes.\")\n\n val_dtype = dtypes.canonicalize_dtype(val.dtype)\n if val_dtype != val.dtype:\n # TODO(jakevdp): this is a weird corner case; need to figure out how to handle it.\n # This happens in X32 mode and can either come from a jax value created in another\n # context, or a Python integer converted to int64.\n pass\n min_val = _constant_like(val, _max(iinfo(dtype).min, iinfo(val_dtype).min))\n max_val = _constant_like(val, _min(iinfo(dtype).max, iinfo(val_dtype).max))\n return clip(val, min_val, max_val).astype(dtype)\n\n\ndef _constant_like(x, const):\n return np.array(const, dtype=_dtype(x))\n\n@_wraps(np.load, update_doc=False)\ndef load(*args, **kwargs):\n # The main purpose of this wrapper is to recover bfloat16 data types.\n # Note: this will only work for files created via np.save(), not np.savez().\n out = np.load(*args, **kwargs)\n if isinstance(out, np.ndarray):\n # numpy does not recognize bfloat16, so arrays are serialized as void16\n if out.dtype == 'V2':\n out = out.view(bfloat16)\n out = asarray(out)\n return out\n\n### implementations of numpy functions in terms of lax\n\n@_wraps(np.fmin)\n@jit\ndef fmin(x1, x2):\n return where((x1 < x2) | isnan(x2), x1, x2)\n\n@_wraps(np.fmax)\n@jit\ndef fmax(x1, x2):\n return where((x1 > x2) | isnan(x2), x1, x2)\n\n@_wraps(np.issubdtype)\ndef issubdtype(arg1, arg2):\n return dtypes.issubdtype(arg1, arg2)\n\n@_wraps(np.isscalar)\ndef isscalar(element):\n if hasattr(element, '__jax_array__'):\n element = element.__jax_array__()\n return dtypes.is_python_scalar(element) or np.isscalar(element)\n\niterable = np.iterable\n\n@_wraps(np.result_type)\ndef result_type(*args):\n return dtypes.result_type(*args)\n\ndef _one_to_one_unop(numpy_fn, lax_fn, promote_to_inexact=False, lax_doc=False):\n if promote_to_inexact:\n fn = lambda x: lax_fn(*_promote_args_inexact(numpy_fn.__name__, x))\n else:\n fn = lambda x: lax_fn(*_promote_args(numpy_fn.__name__, x))\n fn = jit(fn, inline=True)\n if lax_doc:\n doc = _dedent('\\n\\n'.join(lax_fn.__doc__.split('\\n\\n')[1:])).strip()\n return _wraps(numpy_fn, lax_description=doc)(fn)\n else:\n return _wraps(numpy_fn)(fn)\n\ndef _one_to_one_binop(numpy_fn, lax_fn, promote_to_inexact=False, lax_doc=False):\n if promote_to_inexact:\n fn = lambda x1, x2: lax_fn(*_promote_args_inexact(numpy_fn.__name__, x1, x2))\n else:\n fn = lambda x1, x2: lax_fn(*_promote_args(numpy_fn.__name__, x1, x2))\n fn = jit(fn, inline=True)\n if lax_doc:\n doc = _dedent('\\n\\n'.join(lax_fn.__doc__.split('\\n\\n')[1:])).strip()\n return _wraps(numpy_fn, lax_description=doc)(fn)\n else:\n return _wraps(numpy_fn)(fn)\n\ndef _maybe_bool_binop(numpy_fn, lax_fn, bool_lax_fn, lax_doc=False):\n def fn(x1, x2):\n x1, x2 = _promote_args(numpy_fn.__name__, x1, x2)\n return lax_fn(x1, x2) if x1.dtype != bool_ else bool_lax_fn(x1, x2)\n fn = jit(fn, inline=True)\n if lax_doc:\n doc = _dedent('\\n\\n'.join(lax_fn.__doc__.split('\\n\\n')[1:])).strip()\n return _wraps(numpy_fn, lax_description=doc)(fn)\n else:\n return _wraps(numpy_fn)(fn)\n\nfabs = _one_to_one_unop(np.fabs, lax.abs, True)\nbitwise_not = _one_to_one_unop(np.bitwise_not, lax.bitwise_not)\ninvert = _one_to_one_unop(np.invert, lax.bitwise_not)\nnegative = _one_to_one_unop(np.negative, lax.neg)\npositive = _one_to_one_unop(np.positive, lambda x: x)\n\nfloor = _one_to_one_unop(np.floor, lax.floor, True)\nceil = _one_to_one_unop(np.ceil, lax.ceil, True)\nexp = _one_to_one_unop(np.exp, lax.exp, True)\nlog = _one_to_one_unop(np.log, lax.log, True)\nexpm1 = _one_to_one_unop(np.expm1, lax.expm1, True)\nlog1p = _one_to_one_unop(np.log1p, lax.log1p, True)\nsin = _one_to_one_unop(np.sin, lax.sin, True)\ncos = _one_to_one_unop(np.cos, lax.cos, True)\ntan = _one_to_one_unop(np.tan, lax.tan, True)\narcsin = _one_to_one_unop(np.arcsin, lax.asin, True)\narccos = _one_to_one_unop(np.arccos, lax.acos, True)\narctan = _one_to_one_unop(np.arctan, lax.atan, True)\nsinh = _one_to_one_unop(np.sinh, lax.sinh, True)\ncosh = _one_to_one_unop(np.cosh, lax.cosh, True)\narcsinh = _one_to_one_unop(np.arcsinh, lax.asinh, True)\ntanh = _one_to_one_unop(np.tanh, lax.tanh, True)\narcsinh = _one_to_one_unop(np.arcsinh, lax.asinh, True)\narctanh = _one_to_one_unop(np.arctanh, lax.atanh, True)\nsqrt = _one_to_one_unop(np.sqrt, lax.sqrt, True)\ncbrt = _one_to_one_unop(np.cbrt, lax.cbrt, True)\n\n\nadd = _maybe_bool_binop(np.add, lax.add, lax.bitwise_or)\nbitwise_and = _one_to_one_binop(np.bitwise_and, lax.bitwise_and)\nbitwise_or = _one_to_one_binop(np.bitwise_or, lax.bitwise_or)\nbitwise_xor = _one_to_one_binop(np.bitwise_xor, lax.bitwise_xor)\nleft_shift = _one_to_one_binop(np.left_shift, lax.shift_left)\nequal = _one_to_one_binop(np.equal, lax.eq)\nmultiply = _maybe_bool_binop(np.multiply, lax.mul, lax.bitwise_and)\nnot_equal = _one_to_one_binop(np.not_equal, lax.ne)\nsubtract = _one_to_one_binop(np.subtract, lax.sub)\narctan2 = _one_to_one_binop(np.arctan2, lax.atan2, True)\nminimum = _one_to_one_binop(np.minimum, lax.min)\nmaximum = _one_to_one_binop(np.maximum, lax.max)\nfloat_power = _one_to_one_binop(np.float_power, lax.pow, True)\nnextafter = _one_to_one_binop(np.nextafter, lax.nextafter, True, True)\n\n@_wraps(np.arccosh)\n@jit\ndef arccosh(x):\n # Note: arccosh is multi-valued for complex input, and lax.acosh uses a different\n # convention than np.arccosh.\n out = lax.acosh(*_promote_args_inexact(\"arccosh\", x))\n if issubdtype(out.dtype, np.complexfloating):\n out = where(real(out) < 0, lax.neg(out), out)\n return out\n\ndef _comparison_op(numpy_fn, lax_fn):\n # TODO(https://github.com/google/jax/issues/6713): decorate this function with\n # jit, after fixing a surprising interaction with remat(..., concrete=True).\n def fn(x1, x2):\n x1, x2 = _promote_args(numpy_fn.__name__, x1, x2)\n # Comparison on complex types are defined as a lexicographic ordering on\n # the (real, imag) pair.\n if issubdtype(_dtype(x1), complexfloating):\n rx = lax.real(x1)\n ry = lax.real(x2)\n return lax.select(lax.eq(rx, ry), lax_fn(lax.imag(x1), lax.imag(x2)),\n lax_fn(rx, ry))\n return lax_fn(x1, x2)\n return _wraps(numpy_fn)(fn)\n\ngreater_equal = _comparison_op(np.greater_equal, lax.ge)\ngreater = _comparison_op(np.greater, lax.gt)\nless_equal = _comparison_op(np.less_equal, lax.le)\nless = _comparison_op(np.less, lax.lt)\n\n\ndef _logical_op(np_op, bitwise_op):\n @_wraps(np_op, update_doc=False)\n @partial(jit, inline=True)\n def op(*args):\n zero = lambda x: lax.full_like(x, shape=(), fill_value=0)\n args = (x if issubdtype(_dtype(x), bool_) else lax.ne(x, zero(x))\n for x in args)\n return bitwise_op(*_promote_args(np_op.__name__, *args))\n return op\n\nlogical_and = _logical_op(np.logical_and, lax.bitwise_and)\nlogical_not = _logical_op(np.logical_not, lax.bitwise_not)\nlogical_or = _logical_op(np.logical_or, lax.bitwise_or)\nlogical_xor = _logical_op(np.logical_xor, lax.bitwise_xor)\n\n\n@_wraps(np.right_shift)\n@partial(jit, inline=True)\ndef right_shift(x1, x2):\n x1, x2 = _promote_args(np.right_shift.__name__, x1, x2)\n lax_fn = lax.shift_right_logical if \\\n np.issubdtype(x1.dtype, np.unsignedinteger) else lax.shift_right_arithmetic\n return lax_fn(x1, x2)\n\n\n@_wraps(np.absolute)\n@partial(jit, inline=True)\ndef absolute(x):\n _check_arraylike('absolute', x)\n dt = _dtype(x)\n return x if dt == bool_ or issubdtype(dt, unsignedinteger) else lax.abs(x)\nabs = _wraps(np.abs)(absolute)\n\n\n@_wraps(np.rint)\n@jit\ndef rint(x):\n _check_arraylike('rint', x)\n dtype = _dtype(x)\n if issubdtype(dtype, integer):\n return lax.convert_element_type(x, float_)\n if issubdtype(dtype, complexfloating):\n return lax.complex(rint(lax.real(x)), rint(lax.imag(x)))\n return lax.round(x, lax.RoundingMethod.TO_NEAREST_EVEN)\n\n\n@_wraps(np.sign)\n@jit\ndef sign(x):\n _check_arraylike('sign', x)\n dtype = _dtype(x)\n if issubdtype(dtype, complexfloating):\n re = lax.real(x)\n return lax.complex(\n lax.sign(where(re != 0, re, lax.imag(x))), _constant_like(re, 0))\n return lax.sign(x)\n\n\n@_wraps(np.copysign)\n@jit\ndef copysign(x1, x2):\n x1, x2 = _promote_args_inexact(\"copysign\", x1, x2)\n if issubdtype(_dtype(x1), complexfloating):\n raise TypeError(\"copysign does not support complex-valued inputs\")\n return where(signbit(x2), -lax.abs(x1), lax.abs(x1))\n\n\n@_wraps(np.true_divide)\n@partial(jit, inline=True)\ndef true_divide(x1, x2):\n x1, x2 = _promote_args_inexact(\"true_divide\", x1, x2)\n return lax.div(x1, x2)\n\ndivide = true_divide\n\n@_wraps(np.floor_divide)\n@jit\ndef floor_divide(x1, x2):\n x1, x2 = _promote_args(\"floor_divide\", x1, x2)\n dtype = _dtype(x1)\n if issubdtype(dtype, integer):\n quotient = lax.div(x1, x2)\n select = logical_and(lax.sign(x1) != lax.sign(x2), lax.rem(x1, x2) != 0)\n # TODO(mattjj): investigate why subtracting a scalar was causing promotion\n return where(select, quotient - np.array(1, _dtype(quotient)), quotient)\n elif issubdtype(dtype, complexfloating):\n x1r = lax.real(x1)\n x1i = lax.imag(x1)\n x2r = lax.real(x2)\n x2i = lax.imag(x2)\n which = lax.ge(lax.abs(x2r), lax.abs(x2i))\n rat1 = where(which, lax._const(x2i, 1), lax.div(x2r, x2i))\n rat2 = where(which, lax.div(x2i, x2r), lax._const(x2i, 1))\n out = lax.floor(lax.div(lax.add(lax.mul(x1r, rat1), lax.mul(x1i, rat2)),\n lax.add(lax.mul(x2r, rat1), lax.mul(x2i, rat2))))\n return lax.convert_element_type(out, dtype)\n else:\n return _float_divmod(x1, x2)[0]\n\n\n@_wraps(np.divmod)\n@jit\ndef divmod(x1, x2):\n x1, x2 = _promote_args(\"divmod\", x1, x2)\n if issubdtype(_dtype(x1), integer):\n return floor_divide(x1, x2), remainder(x1, x2)\n else:\n return _float_divmod(x1, x2)\n\n\ndef _float_divmod(x1, x2):\n # see float_divmod in floatobject.c of CPython\n mod = lax.rem(x1, x2)\n div = lax.div(lax.sub(x1, mod), x2)\n\n ind = lax.bitwise_and(mod != 0, lax.sign(x2) != lax.sign(mod))\n mod = lax.select(ind, mod + x2, mod)\n div = lax.select(ind, div - _constant_like(div, 1), div)\n\n return lax.round(div), mod\n\n\n@partial(jit, inline=True)\ndef _power(x1, x2):\n x1, x2 = _promote_args(\"power\", x1, x2)\n dtype = _dtype(x1)\n if not issubdtype(dtype, integer):\n return lax.pow(x1, x2)\n\n # Integer power => use binary exponentiation.\n\n # TODO(phawkins): add integer pow support to XLA.\n bits = 6 # Anything more would overflow for any x1 > 1\n zero = _constant_like(x2, 0)\n one = _constant_like(x2, 1)\n # Initialize acc carefully such that pow(0, x2) is zero for x2 != 0\n acc = where(lax.bitwise_and(lax.eq(x1, zero), lax.ne(x2, zero)), zero, one)\n for _ in range(bits):\n acc = where(lax.bitwise_and(x2, one), lax.mul(acc, x1), acc)\n x1 = lax.mul(x1, x1)\n x2 = lax.shift_right_logical(x2, one)\n return acc\n\n@_wraps(np.power)\ndef power(x1, x2):\n # Special case for concrete integer scalars: use binary exponentiation.\n # Using lax.pow may be imprecise for floating-point values; the goal of this\n # code path is to make sure we end up with a precise output for the common\n # pattern ``x ** 2`` or similar.\n if isinstance(core.get_aval(x2), ConcreteArray):\n try:\n x2 = operator.index(x2)\n except TypeError:\n pass\n else:\n return lax.integer_pow(x1, x2)\n return _power(x1, x2)\n\n@custom_jvp\n@_wraps(np.logaddexp)\n@jit\ndef logaddexp(x1, x2):\n x1, x2 = _promote_args_inexact(\"logaddexp\", x1, x2)\n amax = lax.max(x1, x2)\n if issubdtype(x1.dtype, np.floating):\n delta = lax.sub(x1, x2)\n return lax.select(isnan(delta),\n lax.add(x1, x2), # NaNs or infinities of the same sign.\n lax.add(amax, lax.log1p(lax.exp(lax.neg(lax.abs(delta))))))\n else:\n delta = lax.sub(lax.add(x1, x2), lax.mul(amax, _constant_like(amax, 2)))\n out = lax.add(amax, lax.log1p(lax.exp(delta)))\n return lax.complex(lax.real(out), _wrap_between(lax.imag(out), np.pi))\n\ndef _wrap_between(x, _a):\n \"\"\"Wraps `x` between `[-a, a]`.\"\"\"\n a = _constant_like(x, _a)\n two_a = _constant_like(x, 2 * _a)\n zero = _constant_like(x, 0)\n rem = lax.rem(lax.add(x, a), two_a)\n rem = lax.select(lax.lt(rem, zero), lax.add(rem, two_a), rem)\n return lax.sub(rem, a)\n\[email protected]\ndef _logaddexp_jvp(primals, tangents):\n x1, x2 = primals\n t1, t2 = tangents\n x1, x2, t1, t2 = _promote_args_inexact(\"logaddexp_jvp\", x1, x2, t1, t2)\n primal_out = logaddexp(x1, x2)\n tangent_out = lax.add(lax.mul(t1, exp(lax.sub(_replace_inf(x1), _replace_inf(primal_out)))),\n lax.mul(t2, exp(lax.sub(_replace_inf(x2), _replace_inf(primal_out)))))\n return primal_out, tangent_out\n\ndef _replace_inf(x):\n return lax.select(isposinf(real(x)), zeros_like(x), x)\n\n\n@custom_jvp\n@_wraps(np.logaddexp2)\n@jit\ndef logaddexp2(x1, x2):\n x1, x2 = _promote_args_inexact(\"logaddexp2\", x1, x2)\n amax = lax.max(x1, x2)\n if issubdtype(x1.dtype, np.floating):\n delta = lax.sub(x1, x2)\n return lax.select(isnan(delta),\n lax.add(x1, x2), # NaNs or infinities of the same sign.\n lax.add(amax, lax.div(lax.log1p(exp2(lax.neg(lax.abs(delta)))),\n _constant_like(x1, np.log(2)))))\n else:\n delta = lax.sub(lax.add(x1, x2), lax.mul(amax, _constant_like(amax, 2)))\n out = lax.add(amax, lax.div(lax.log1p(exp2(delta)), _constant_like(x1, np.log(2))))\n return lax.complex(lax.real(out), _wrap_between(lax.imag(out), np.pi / np.log(2)))\n\[email protected]\ndef _logaddexp2_jvp(primals, tangents):\n x1, x2 = primals\n t1, t2 = tangents\n x1, x2, t1, t2 = _promote_args_inexact(\"logaddexp2_jvp\", x1, x2, t1, t2)\n primal_out = logaddexp2(x1, x2)\n tangent_out = lax.add(lax.mul(t1, exp2(lax.sub(_replace_inf(x1), _replace_inf(primal_out)))),\n lax.mul(t2, exp2(lax.sub(_replace_inf(x2), _replace_inf(primal_out)))))\n return primal_out, tangent_out\n\n\n@_wraps(np.log2)\n@partial(jit, inline=True)\ndef log2(x):\n x, = _promote_args_inexact(\"log2\", x)\n return lax.div(lax.log(x), lax.log(_constant_like(x, 2)))\n\n\n@_wraps(np.log10)\n@partial(jit, inline=True)\ndef log10(x):\n x, = _promote_args_inexact(\"log10\", x)\n return lax.div(lax.log(x), lax.log(_constant_like(x, 10)))\n\n\n@_wraps(np.exp2)\n@partial(jit, inline=True)\ndef exp2(x):\n x, = _promote_args_inexact(\"exp2\", x)\n return lax.exp(lax.mul(lax.log(_constant_like(x, 2)), x))\n\n@_wraps(np.signbit)\n@jit\ndef signbit(x):\n x, = _promote_args(\"signbit\", x)\n dtype = _dtype(x)\n if issubdtype(dtype, integer):\n return lax.lt(x, _constant_like(x, 0))\n elif issubdtype(dtype, bool_):\n return full_like(x, False, dtype=bool_)\n elif not issubdtype(dtype, floating):\n raise ValueError(\n \"jax.numpy.signbit is not well defined for %s\" % dtype)\n\n # TPU supports BF16 but not S16 types, so as a workaround, convert BF16 to\n # F32.\n if dtype == bfloat16:\n dtype = float32\n x = lax.convert_element_type(x, float32)\n\n info = finfo(dtype)\n if info.bits not in _INT_DTYPES:\n raise NotImplementedError(\n \"jax.numpy.signbit only supports 16, 32, and 64-bit types.\")\n int_type = _INT_DTYPES[info.bits]\n x = lax.bitcast_convert_type(x, int_type)\n return lax.convert_element_type(x >> (info.nexp + info.nmant), np.bool_)\n\n\n@_wraps(np.trapz)\n@partial(jit, static_argnames=('axis',))\ndef trapz(y, x=None, dx=1.0, axis: int = -1):\n _check_arraylike('trapz', y)\n y = moveaxis(y, axis, -1)\n if x is not None:\n if ndim(x) == 1:\n dx = diff(x)\n else:\n dx = moveaxis(diff(x, axis=axis), axis, -1)\n return 0.5 * (dx * (y[..., 1:] + y[..., :-1])).sum(-1)\n\n\n@_wraps(np.trunc)\n@jit\ndef trunc(x):\n _check_arraylike('trunc', x)\n return where(lax.lt(x, lax._const(x, 0)), ceil(x), floor(x))\n\n\n@partial(jit, static_argnums=(2, 3, 4))\ndef _conv(x, y, mode, op, precision):\n if ndim(x) != 1 or ndim(y) != 1:\n raise ValueError(f\"{op}() only support 1-dimensional inputs.\")\n x, y = _promote_dtypes_inexact(x, y)\n if len(x) == 0 or len(y) == 0:\n raise ValueError(f\"{op}: inputs cannot be empty, got shapes {x.shape} and {y.shape}.\")\n\n out_order = slice(None)\n if op == 'correlate':\n y = conj(y)\n if len(x) < len(y):\n x, y = y, x\n out_order = slice(None, None, -1)\n elif op == 'convolve':\n if len(x) < len(y):\n x, y = y, x\n y = flip(y)\n\n if mode == 'valid':\n padding = [(0, 0)]\n elif mode == 'same':\n padding = [(y.shape[0] // 2, y.shape[0] - y.shape[0] // 2 - 1)]\n elif mode == 'full':\n padding = [(y.shape[0] - 1, y.shape[0] - 1)]\n else:\n raise ValueError(\"mode must be one of ['full', 'same', 'valid']\")\n\n result = lax.conv_general_dilated(x[None, None, :], y[None, None, :], (1,),\n padding, precision=precision)\n return result[0, 0, out_order]\n\n\n@_wraps(np.convolve, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('mode', 'precision'))\ndef convolve(a, v, mode='full', *, precision=None):\n _check_arraylike(\"convolve\", a, v)\n return _conv(a, v, mode, 'convolve', precision)\n\n\n@_wraps(np.correlate, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('mode', 'precision'))\ndef correlate(a, v, mode='valid', *, precision=None):\n _check_arraylike(\"correlate\", a, v)\n return _conv(a, v, mode, 'correlate', precision)\n\n\ndef _normalize_float(x):\n info = finfo(_dtype(x))\n cond = lax.abs(x) < info.tiny\n x1 = where(cond, x * lax._const(x, 1 << info.nmant), x)\n x2 = where(cond, lax._const(np.int32, -info.nmant), lax._const(np.int32, 0))\n int_type = _INT_DTYPES[info.bits]\n return lax.bitcast_convert_type(x1, int_type), x2\n\n\n@_wraps(np.ldexp)\n@jit\ndef ldexp(x1, x2):\n _check_arraylike(\"ldexp\", x1, x2)\n dtype = dtypes.canonicalize_dtype(_result_dtype(np.ldexp, x1, x2))\n x1, x2 = _promote_shapes(\"ldexp\", x1, x2)\n x1 = lax.convert_element_type(x1, dtype)\n\n info = finfo(dtype)\n mask = (1 << info.nexp) - 1\n bias = ((1 << info.nexp) - 1) >> 1\n\n int_type = _INT_DTYPES[info.bits]\n\n x, e = _normalize_float(x1)\n x2 += e + ((x >> info.nmant) & mask) - bias\n\n # find underflow/overflow before denormalization\n underflow_cond = x2 < -(bias + info.nmant)\n overflow_cond = x2 > bias\n\n m = ones_like(x, dtype=dtype)\n\n # denormals\n cond = x2 < -bias + 1\n x2 = where(cond, x2 + info.nmant, x2)\n m = where(cond, m / (1 << info.nmant), m)\n\n x2 = lax.convert_element_type(x2, np.int32)\n x &= ~(mask << info.nmant)\n x |= ((lax.convert_element_type(x2, int_type) + bias) << info.nmant)\n\n x = lax.convert_element_type(m, dtype) * lax.bitcast_convert_type(x, dtype)\n\n # underflow\n x = where(underflow_cond, zeros_like(x, dtype=dtype), x)\n # overflow\n x = where(overflow_cond, lax.sign(x1) * full_like(x, np.inf), x)\n # ldexp(x1, x2) = x1 for x1 = inf, -inf, nan, 0\n return where(isinf(x1) | isnan(x1) | (x1 == 0), x1, x)\n\n\n@_wraps(np.frexp)\n@jit\ndef frexp(x):\n _check_arraylike(\"frexp\", x)\n x = asarray(x)\n if issubdtype(x.dtype, complexfloating):\n raise TypeError(\"frexp does not support complex-valued inputs\")\n elif not issubdtype(x.dtype, floating):\n x = lax.convert_element_type(x, float_)\n\n dtype = _dtype(x)\n info = finfo(dtype)\n mask = (1 << info.nexp) - 1\n bias = ((1 << info.nexp) - 1) >> 1\n\n x1, x2 = _normalize_float(x)\n x2 += ((x1 >> info.nmant) & mask) - bias + 1\n x1 &= ~(mask << info.nmant)\n x1 |= (bias - 1) << info.nmant\n x1 = lax.bitcast_convert_type(x1, dtype)\n\n cond = isinf(x) | isnan(x) | (x == 0)\n x2 = where(cond, zeros_like(x2), x2)\n return where(cond, x, x1), lax.convert_element_type(x2, int32)\n\n\n@_wraps(np.remainder)\n@jit\ndef remainder(x1, x2):\n x1, x2 = _promote_args(\"remainder\", x1, x2)\n zero = _constant_like(x1, 0)\n trunc_mod = lax.rem(x1, x2)\n trunc_mod_not_zero = lax.ne(trunc_mod, zero)\n do_plus = lax.bitwise_and(\n lax.ne(lax.lt(trunc_mod, zero), lax.lt(x2, zero)), trunc_mod_not_zero)\n return lax.select(do_plus, lax.add(trunc_mod, x2), trunc_mod)\nmod = _wraps(np.mod)(remainder)\n\n\n@_wraps(np.fmod)\n@jit\ndef fmod(x1, x2):\n _check_arraylike(\"fmod\", x1, x2)\n if issubdtype(result_type(x1, x2), integer):\n x2 = where(x2 == 0, 1, x2)\n return lax.rem(*_promote_args(\"fmod\", x1, x2))\n\n\n@_wraps(np.square)\n@partial(jit, inline=True)\ndef square(x):\n _check_arraylike(\"square\", x)\n return lax.integer_pow(x, 2)\n\n\n@_wraps(np.deg2rad)\n@partial(jit, inline=True)\ndef deg2rad(x):\n x, = _promote_args_inexact(\"deg2rad\", x)\n return lax.mul(x, lax._const(x, pi / 180))\n\n\n@_wraps(np.rad2deg)\n@partial(jit, inline=True)\ndef rad2deg(x):\n x, = _promote_args_inexact(\"rad2deg\", x)\n return lax.mul(x, lax._const(x, 180 / pi))\n\n\ndegrees = rad2deg\nradians = deg2rad\n\n\n@_wraps(np.histogram_bin_edges)\ndef histogram_bin_edges(a, bins=10, range=None, weights=None):\n if isinstance(bins, str):\n raise NotImplementedError(\"string values for `bins` not implemented.\")\n _check_arraylike(\"histogram_bin_edges\", a, bins)\n a = ravel(a)\n b = asarray(bins)\n if b.ndim == 1:\n return b\n if range is None:\n range = [a.min(), a.max()]\n assert len(range) == 2\n range = asarray(range)\n range = (where(ptp(range) == 0, range[0] - 0.5, range[0]),\n where(ptp(range) == 0, range[1] + 0.5, range[1]))\n dtype = _dtype(a)\n if issubdtype(dtype, integer):\n dtype = promote_types(dtype, float32)\n return linspace(range[0], range[1], bins + 1, dtype=dtype)\n\n\n@_wraps(np.histogram)\ndef histogram(a, bins=10, range=None, weights=None, density=None):\n _check_arraylike(\"histogram\", a, bins)\n if weights is not None and a.shape != weights.shape:\n raise ValueError(\"weights should have the same shape as a.\")\n a = ravel(a)\n if weights is not None:\n weights = ravel(weights)\n else:\n weights = ones_like(a)\n bin_edges = histogram_bin_edges(a, bins, range, weights)\n bin_idx = searchsorted(bin_edges, a, side='right')\n bin_idx = where(a == bin_edges[-1], len(bin_edges) - 1, bin_idx)\n counts = bincount(bin_idx, weights, length=len(bin_edges))[1:]\n if density:\n bin_widths = diff(bin_edges)\n counts = counts / bin_widths / counts.sum()\n return counts, bin_edges\n\n@_wraps(np.histogram2d)\ndef histogram2d(x, y, bins=10, range=None, weights=None, density=None):\n _check_arraylike(\"histogram2d\", x, y)\n try:\n N = len(bins)\n except TypeError:\n N = 1\n\n if N != 1 and N != 2:\n x_edges = y_edges = asarray(bins)\n bins = [x_edges, y_edges]\n\n sample = transpose(asarray([x, y]))\n hist, edges = histogramdd(sample, bins, range, weights, density)\n return hist, edges[0], edges[1]\n\n@_wraps(np.histogramdd)\ndef histogramdd(sample, bins=10, range=None, weights=None, density=None):\n _check_arraylike(\"histogramdd\", sample)\n N, D = shape(sample)\n\n if weights is not None and weights.shape != (N,):\n raise ValueError(\"should have one weight for each sample.\")\n\n if range is not None and (\n len(range) != D or _any(r is not None and len(r) != 2 for r in range)):\n raise ValueError(f\"For sample.shape={(N, D)}, range must be a sequence \"\n f\"of {D} pairs or Nones; got range={range}\")\n\n try:\n num_bins = len(bins)\n if num_bins != D:\n raise ValueError(\"should be a bin for each dimension.\")\n except TypeError:\n # when bin_size is integer, the same bin is used for each dimension\n bins = D * [bins]\n\n bin_idx_by_dim = D*[None]\n nbins = np.empty(D, int)\n bin_edges_by_dim = D*[None]\n dedges = D*[None]\n\n for i in builtins.range(D):\n range_i = None if range is None else range[i]\n bin_edges = histogram_bin_edges(sample[:, i], bins[i], range_i, weights)\n bin_idx = searchsorted(bin_edges, sample[:, i], side='right')\n bin_idx = where(sample[:, i] == bin_edges[-1], bin_idx - 1, bin_idx)\n bin_idx_by_dim[i] = bin_idx\n nbins[i] = len(bin_edges) + 1\n bin_edges_by_dim[i] = bin_edges\n dedges[i] = diff(bin_edges_by_dim[i])\n\n xy = ravel_multi_index(bin_idx_by_dim, nbins, mode='clip')\n hist = bincount(xy, weights, length=nbins.prod())\n hist = reshape(hist, nbins)\n core = D*(slice(1, -1),)\n hist = hist[core]\n\n if density:\n hist /= hist.sum()\n for norm in ix_(*dedges):\n hist /= norm\n\n return hist, bin_edges_by_dim\n\n@_wraps(np.heaviside)\n@jit\ndef heaviside(x1, x2):\n _check_arraylike(\"heaviside\", x1, x2)\n x1, x2 = _promote_dtypes_inexact(x1, x2)\n zero = lax._const(x1, 0)\n return where(lax.lt(x1, zero), zero,\n where(lax.gt(x1, zero), lax._const(x1, 1), x2))\n\n\n@_wraps(np.hypot)\n@jit\ndef hypot(x1, x2):\n _check_arraylike(\"hypot\", x1, x2)\n x1, x2 = _promote_dtypes_inexact(x1, x2)\n x1 = lax.abs(x1)\n x2 = lax.abs(x2)\n x1, x2 = maximum(x1, x2), minimum(x1, x2)\n return lax.select(x1 == 0, x1, x1 * lax.sqrt(1 + lax.square(lax.div(x2, lax.select(x1 == 0, ones_like(x1), x1)))))\n\n\n@_wraps(np.reciprocal)\n@partial(jit, inline=True)\ndef reciprocal(x):\n _check_arraylike(\"reciprocal\", x)\n x, = _promote_dtypes_inexact(x)\n return lax.integer_pow(x, -1)\n\n\n@_wraps(np.sinc, update_doc=False)\n@jit\ndef sinc(x):\n _check_arraylike(\"sinc\", x)\n x, = _promote_dtypes_inexact(x)\n eq_zero = lax.eq(x, lax._const(x, 0))\n pi_x = lax.mul(lax._const(x, pi), x)\n safe_pi_x = where(eq_zero, lax._const(x, 1), pi_x)\n return where(eq_zero, _sinc_maclaurin(0, pi_x),\n lax.div(lax.sin(safe_pi_x), safe_pi_x))\n\n@partial(custom_jvp, nondiff_argnums=(0,))\ndef _sinc_maclaurin(k, x):\n # compute the kth derivative of x -> sin(x)/x evaluated at zero (since we\n # compute the monomial term in the jvp rule)\n if k % 2:\n return lax.full_like(x, 0)\n else:\n return lax.full_like(x, (-1) ** (k // 2) / (k + 1))\n\n@_sinc_maclaurin.defjvp\ndef _sinc_maclaurin_jvp(k, primals, tangents):\n (x,), (t,) = primals, tangents\n return _sinc_maclaurin(k, x), _sinc_maclaurin(k + 1, x) * t\n\n_ARRAY_VIEW_DOC = \"\"\"\nThe JAX version of this function may in some cases return a copy rather than a\nview of the input.\n\"\"\"\n\n@_wraps(np.transpose, lax_description=_ARRAY_VIEW_DOC)\ndef transpose(a, axes=None):\n _check_arraylike(\"transpose\", a)\n axes = np.arange(ndim(a))[::-1] if axes is None else axes\n return lax.transpose(a, axes)\n\n\n@_wraps(np.rot90, lax_description=_ARRAY_VIEW_DOC)\n@partial(jit, static_argnames=('k', 'axes'))\ndef rot90(m, k=1, axes=(0, 1)):\n _check_arraylike(\"rot90\", m)\n ax1, ax2 = axes\n ax1 = _canonicalize_axis(ax1, ndim(m))\n ax2 = _canonicalize_axis(ax2, ndim(m))\n if ax1 == ax2:\n raise ValueError(\"Axes must be different\") # same as numpy error\n k = k % 4\n if k == 0:\n return m\n elif k == 2:\n return flip(flip(m, ax1), ax2)\n else:\n perm = list(range(m.ndim))\n perm[ax1], perm[ax2] = perm[ax2], perm[ax1]\n if k == 1:\n return transpose(flip(m, ax2), perm)\n else:\n return flip(transpose(m, perm), ax2)\n\n\n@_wraps(np.flip, lax_description=_ARRAY_VIEW_DOC)\ndef flip(m, axis: Optional[Union[int, Tuple[int, ...]]] = None):\n return _flip(m, _ensure_optional_axes(axis))\n\n@partial(jit, static_argnames=('axis',))\ndef _flip(m, axis: Optional[Union[int, Tuple[int, ...]]] = None):\n _check_arraylike(\"flip\", m)\n if axis is None:\n return lax.rev(m, list(range(len(shape(m)))))\n axis = _ensure_index_tuple(axis)\n return lax.rev(m, [_canonicalize_axis(ax, ndim(m)) for ax in axis])\n\n\n@_wraps(np.fliplr, lax_description=_ARRAY_VIEW_DOC)\ndef fliplr(m):\n return _flip(m, 1)\n\n\n@_wraps(np.flipud, lax_description=_ARRAY_VIEW_DOC)\ndef flipud(m):\n return _flip(m, 0)\n\n\n@_wraps(np.conjugate)\n@partial(jit, inline=True)\ndef conjugate(x):\n _check_arraylike(\"conjugate\", x)\n return lax.conj(x) if iscomplexobj(x) else x\nconj = conjugate\n\n\n@_wraps(np.imag)\n@partial(jit, inline=True)\ndef imag(val):\n _check_arraylike(\"imag\", val)\n return lax.imag(val) if iscomplexobj(val) else zeros_like(val)\n\n\n@_wraps(np.real)\n@partial(jit, inline=True)\ndef real(val):\n _check_arraylike(\"real\", val)\n return lax.real(val) if iscomplexobj(val) else val\n\n\n@_wraps(np.iscomplex)\n@jit\ndef iscomplex(x):\n i = imag(x)\n return lax.ne(i, lax._const(i, 0))\n\n@_wraps(np.isreal)\n@jit\ndef isreal(x):\n i = imag(x)\n return lax.eq(i, lax._const(i, 0))\n\n@_wraps(np.angle)\n@jit\ndef angle(z):\n re = real(z)\n im = imag(z)\n dtype = _dtype(re)\n if not issubdtype(dtype, inexact) or (\n issubdtype(_dtype(z), floating) and ndim(z) == 0):\n dtype = dtypes.canonicalize_dtype(float_)\n re = lax.convert_element_type(re, dtype)\n im = lax.convert_element_type(im, dtype)\n return lax.atan2(im, re)\n\n\n@_wraps(np.diff)\n@partial(jit, static_argnames=('n', 'axis'))\ndef diff(a, n=1, axis: int = -1, prepend=None, append=None):\n _check_arraylike(\"diff\", a)\n n = core.concrete_or_error(operator.index, n, \"'n' argument of jnp.diff\")\n axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.diff\")\n if n == 0:\n return a\n if n < 0:\n raise ValueError(f\"order must be non-negative but got {n}\")\n if ndim(a) == 0:\n raise ValueError(f\"diff requires input that is at least one dimensional; got {a}\")\n\n nd = a.ndim\n axis = _canonicalize_axis(axis, nd)\n\n combined = []\n if prepend is not None:\n _check_arraylike(\"diff\", prepend)\n if isscalar(prepend):\n shape = list(a.shape)\n shape[axis] = 1\n prepend = broadcast_to(prepend, tuple(shape))\n combined.append(prepend)\n\n combined.append(a)\n\n if append is not None:\n _check_arraylike(\"diff\", append)\n if isscalar(append):\n shape = list(a.shape)\n shape[axis] = 1\n append = broadcast_to(append, tuple(shape))\n combined.append(append)\n\n if len(combined) > 1:\n a = concatenate(combined, axis)\n\n slice1 = [slice(None)] * nd\n slice2 = [slice(None)] * nd\n slice1[axis] = slice(1, None)\n slice2[axis] = slice(None, -1)\n slice1_tuple = tuple(slice1)\n slice2_tuple = tuple(slice2)\n\n op = not_equal if a.dtype == np.bool_ else subtract\n for _ in range(n):\n a = op(a[slice1_tuple], a[slice2_tuple])\n\n return a\n\n_EDIFF1D_DOC = \"\"\"\\\nUnlike NumPy's implementation of ediff1d, :py:func:`jax.numpy.ediff1d` will not\nissue an error if casting ``to_end`` or ``to_begin`` to the type of ``ary``\nloses precision.\n\"\"\"\n\n@_wraps(np.ediff1d, lax_description=_EDIFF1D_DOC)\n@jit\ndef ediff1d(ary, to_end=None, to_begin=None):\n _check_arraylike(\"ediff1d\", ary)\n ary = ravel(ary)\n result = lax.sub(ary[1:], ary[:-1])\n if to_begin is not None:\n _check_arraylike(\"ediff1d\", to_begin)\n result = concatenate((ravel(asarray(to_begin, dtype=ary.dtype)), result))\n if to_end is not None:\n _check_arraylike(\"ediff1d\", to_end)\n result = concatenate((result, ravel(asarray(to_end, dtype=ary.dtype))))\n return result\n\n\n@_wraps(np.gradient, skip_params=['edge_order'])\n@partial(jit, static_argnames=('axis', 'edge_order'))\ndef gradient(f, *varargs, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n edge_order=None):\n if edge_order is not None:\n raise NotImplementedError(\"The 'edge_order' argument to jnp.gradient is not supported.\")\n\n def gradient_along_axis(a, h, axis):\n sliced = partial(lax.slice_in_dim, a, axis=axis)\n a_grad = concatenate((\n (sliced(1, 2) - sliced(0, 1)), # upper edge\n (sliced(2, None) - sliced(None, -2)) * 0.5, # inner\n (sliced(-1, None) - sliced(-2, -1)), # lower edge\n ), axis)\n return a_grad / h\n\n a = f\n axis_tuple: Tuple[int, ...]\n if axis is None:\n axis_tuple = tuple(range(a.ndim))\n else:\n if isinstance(axis, int):\n axis = (axis,)\n elif not isinstance(axis, tuple) and not isinstance(axis, list):\n raise ValueError(\"Give `axis` either as int or iterable\")\n elif len(axis) == 0:\n return []\n axis_tuple = tuple(_canonicalize_axis(i, a.ndim) for i in axis)\n\n if _min([s for i, s in enumerate(a.shape) if i in axis_tuple]) < 2:\n raise ValueError(\"Shape of array too small to calculate \"\n \"a numerical gradient, \"\n \"at least 2 elements are required.\")\n len_axes = len(axis_tuple)\n n = len(varargs)\n if n == 0 or varargs is None:\n # no spacing\n dx = [1.0] * len_axes\n elif n == 1:\n # single value for all axes\n dx = list(varargs) * len_axes\n elif n == len_axes:\n dx = list(varargs)\n else:\n TypeError(\"Invalid number of spacing arguments %d\" % n)\n\n if ndim(dx[0]) != 0:\n raise NotImplementedError(\"Non-constant spacing not implemented\")\n\n # TODO: use jax.lax loop tools if possible\n a_grad = [gradient_along_axis(a, h, ax) for ax, h in zip(axis_tuple, dx)]\n\n if len(axis_tuple) == 1:\n a_grad = a_grad[0]\n\n return a_grad\n\n\n@_wraps(np.isrealobj)\ndef isrealobj(x):\n return not iscomplexobj(x)\n\n_POLYFIT_DOC = \"\"\"\\\nUnlike NumPy's implementation of polyfit, :py:func:`jax.numpy.polyfit` will not warn on rank reduction, which indicates an ill conditioned matrix\nAlso, it works best on rcond <= 10e-3 values.\n\"\"\"\n@_wraps(np.polyfit, lax_description=_POLYFIT_DOC)\n@partial(jit, static_argnames=('deg', 'rcond', 'full', 'cov'))\ndef polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):\n _check_arraylike(\"polyfit\", x, y)\n deg = core.concrete_or_error(int, deg, \"deg must be int\")\n order = deg + 1\n # check arguments\n if deg < 0:\n raise ValueError(\"expected deg >= 0\")\n if x.ndim != 1:\n raise TypeError(\"expected 1D vector for x\")\n if x.size == 0:\n raise TypeError(\"expected non-empty vector for x\")\n if y.ndim < 1 or y.ndim > 2:\n raise TypeError(\"expected 1D or 2D array for y\")\n if x.shape[0] != y.shape[0]:\n raise TypeError(\"expected x and y to have same length\")\n\n # set rcond\n if rcond is None:\n rcond = len(x)*finfo(x.dtype).eps\n rcond = core.concrete_or_error(float, rcond, \"rcond must be float\")\n # set up least squares equation for powers of x\n lhs = vander(x, order)\n rhs = y\n\n # apply weighting\n if w is not None:\n _check_arraylike(\"polyfit\", w)\n w, = _promote_dtypes_inexact(w)\n if w.ndim != 1:\n raise TypeError(\"expected a 1-d array for weights\")\n if w.shape[0] != y.shape[0]:\n raise TypeError(\"expected w and y to have the same length\")\n lhs *= w[:, newaxis]\n if rhs.ndim == 2:\n rhs *= w[:, newaxis]\n else:\n rhs *= w\n\n # scale lhs to improve condition number and solve\n scale = sqrt((lhs*lhs).sum(axis=0))\n lhs /= scale[newaxis,:]\n from jax._src.numpy import linalg\n c, resids, rank, s = linalg.lstsq(lhs, rhs, rcond)\n c = (c.T/scale).T # broadcast scale coefficients\n\n if full:\n return c, resids, rank, s, rcond\n elif cov:\n Vbase = linalg.inv(dot(lhs.T, lhs))\n Vbase /= outer(scale, scale)\n if cov == \"unscaled\":\n fac = 1\n else:\n if len(x) <= order:\n raise ValueError(\"the number of data points must exceed order \"\n \"to scale the covariance matrix\")\n fac = resids / (len(x) - order)\n fac = fac[0] #making np.array() of shape (1,) to int\n if y.ndim == 1:\n return c, Vbase * fac\n else:\n return c, Vbase[:,:, newaxis] * fac\n else:\n return c\n\n\n@_wraps(np.reshape, lax_description=_ARRAY_VIEW_DOC)\ndef reshape(a, newshape, order=\"C\"):\n _stackable(a) or _check_arraylike(\"reshape\", a)\n try:\n return a.reshape(newshape, order=order) # forward to method for ndarrays\n except AttributeError:\n return _reshape(a, newshape, order=order)\n\ndef _compute_newshape(a, newshape):\n \"\"\"Fixes a -1 value in newshape, if present.\"\"\"\n # other errors, like having more than one -1, are caught downstream, in\n # reshape_shape_rule.\n try: iter(newshape)\n except: iterable = False\n else: iterable = True\n newshape = core.canonicalize_shape(newshape if iterable else [newshape])\n return tuple(- core.divide_shape_sizes(np.shape(a), newshape)\n if core.symbolic_equal_dim(d, -1) else d\n for d in newshape)\n\n\ndef _reshape(a, *args, order=\"C\"):\n newshape = _compute_newshape(a, args[0] if len(args) == 1 else args)\n if order == \"C\":\n return lax.reshape(a, newshape, None)\n elif order == \"F\":\n dims = np.arange(ndim(a))[::-1]\n return lax.reshape(a, newshape[::-1], dims).T\n elif order == \"A\":\n raise NotImplementedError(\"np.reshape order=A is not implemented.\")\n else:\n raise ValueError(\"Unexpected value for 'order' argument: {}.\".format(order))\n\ndef _transpose(a, *args):\n if not args:\n axis = None\n elif len(args) == 1:\n axis = args[0] if args[0] is None else _ensure_index_tuple(args[0])\n else:\n axis = _ensure_index_tuple(args)\n return transpose(a, axis)\n\n@_wraps(np.ravel, lax_description=_ARRAY_VIEW_DOC)\n@partial(jit, static_argnames=('order',), inline=True)\ndef ravel(a, order=\"C\"):\n _stackable(a) or _check_arraylike(\"ravel\", a)\n if order == \"K\":\n raise NotImplementedError(\"Ravel not implemented for order='K'.\")\n return reshape(a, (size(a),), order)\n\n\n@_wraps(np.ravel_multi_index)\ndef ravel_multi_index(multi_index, dims, mode='raise', order='C'):\n assert len(multi_index) == len(dims), f\"len(multi_index)={len(multi_index)} != len(dims)={len(dims)}\"\n dims = tuple(core.concrete_or_error(int, d, \"in `dims` argument of ravel_multi_index().\") for d in dims)\n _check_arraylike(\"ravel_multi_index\", *multi_index)\n for index in multi_index:\n if mode == 'raise':\n core.concrete_or_error(array, index,\n \"The error occurred because ravel_multi_index was jit-compiled\"\n \" with mode='raise'. Use mode='wrap' or mode='clip' instead.\")\n if not issubdtype(_dtype(index), integer):\n raise TypeError(\"only int indices permitted\")\n if mode == \"raise\":\n if _any(any((i < 0) | (i >= d)) for i, d in zip(multi_index, dims)):\n raise ValueError(\"invalid entry in coordinates array\")\n elif mode == \"clip\":\n multi_index = [clip(i, 0, d - 1) for i, d in zip(multi_index, dims)]\n elif mode == \"wrap\":\n multi_index = [i % d for i, d in zip(multi_index, dims)]\n else:\n raise ValueError(f\"invalid mode={mode!r}. Expected 'raise', 'wrap', or 'clip'\")\n\n if order == \"F\":\n strides = np.cumprod((1,) + dims[:-1])\n elif order == \"C\":\n strides = np.cumprod((1,) + dims[1:][::-1])[::-1]\n else:\n raise ValueError(f\"invalid order={order!r}. Expected 'C' or 'F'\")\n\n result = array(0, dtype=dtypes.canonicalize_dtype(int_))\n for i, s in zip(multi_index, strides):\n result = result + i * s\n return result\n\n\n_UNRAVEL_INDEX_DOC = \"\"\"\\\nUnlike numpy's implementation of unravel_index, negative indices are accepted\nand out-of-bounds indices are clipped.\n\"\"\"\n\n@_wraps(np.unravel_index, lax_description=_UNRAVEL_INDEX_DOC)\ndef unravel_index(indices, shape):\n _check_arraylike(\"unravel_index\", indices)\n sizes = append(array(shape), 1)\n cumulative_sizes = cumprod(sizes[::-1])[::-1]\n total_size = cumulative_sizes[0]\n # Clip so raveling and unraveling an oob index will not change the behavior\n clipped_indices = clip(indices, -total_size, total_size - 1)\n # Add enough trailing dims to avoid conflict with clipped_indices\n cumulative_sizes = expand_dims(cumulative_sizes, range(1, 1 + _ndim(indices)))\n clipped_indices = expand_dims(clipped_indices, axis=0)\n idx = clipped_indices % cumulative_sizes[:-1] // cumulative_sizes[1:]\n # TODO(jakevdp): return tuple(idx) once it behaves properly (#3821)\n return tuple(lax.index_in_dim(idx, i, keepdims=False) for i in range(idx.shape[0]))\n\n@_wraps(np.resize)\n@partial(jit, static_argnames=('new_shape',))\ndef resize(a, new_shape):\n _check_arraylike(\"resize\", a)\n new_shape = _ensure_index_tuple(new_shape)\n\n if _any(dim_length < 0 for dim_length in new_shape):\n raise ValueError(\"all elements of `new_shape` must be non-negative\")\n\n a = ravel(a)\n\n new_size = _prod(new_shape)\n if a.size == 0 or new_size == 0:\n return zeros_like(a, shape=new_shape)\n\n repeats = ceil_of_ratio(new_size, a.size)\n a = tile(a, repeats)[:new_size]\n\n return reshape(a, new_shape)\n\n@_wraps(np.squeeze, lax_description=_ARRAY_VIEW_DOC)\ndef squeeze(a, axis: Optional[Union[int, Tuple[int, ...]]] = None):\n return _squeeze(a, _ensure_index_tuple(axis) if axis is not None else None)\n\n@partial(jit, static_argnames=('axis',), inline=True)\ndef _squeeze(a, axis):\n _check_arraylike(\"squeeze\", a)\n if axis is None:\n a_shape = shape(a)\n axis = tuple(i for i, d in enumerate(a_shape) if d == 1)\n return lax.squeeze(a, axis)\n\n\n@_wraps(np.expand_dims)\ndef expand_dims(a, axis: Union[int, Sequence[int]]):\n _check_arraylike(\"expand_dims\", a)\n return lax.expand_dims(a, _ensure_index_tuple(axis))\n\n\n@_wraps(np.swapaxes, lax_description=_ARRAY_VIEW_DOC)\n@partial(jit, static_argnames=('axis1', 'axis2'), inline=True)\ndef swapaxes(a, axis1: int, axis2: int):\n _check_arraylike(\"swapaxes\", a)\n perm = np.arange(ndim(a))\n perm[axis1], perm[axis2] = perm[axis2], perm[axis1]\n return lax.transpose(a, perm)\n\n\n@_wraps(np.moveaxis, lax_description=_ARRAY_VIEW_DOC)\ndef moveaxis(a, source: Union[int, Sequence[int]],\n destination: Union[int, Sequence[int]]):\n return _moveaxis(a, _ensure_index_tuple(source),\n _ensure_index_tuple(destination))\n\n@partial(jit, static_argnames=('source', 'destination'), inline=True)\ndef _moveaxis(a, source: Tuple[int, ...], destination: Tuple[int, ...]):\n _check_arraylike(\"moveaxis\", a)\n source = tuple(_canonicalize_axis(i, ndim(a)) for i in source)\n destination = tuple(_canonicalize_axis(i, ndim(a)) for i in destination)\n if len(source) != len(destination):\n raise ValueError(\"Inconsistent number of elements: {} vs {}\"\n .format(len(source), len(destination)))\n perm = [i for i in range(ndim(a)) if i not in source]\n for dest, src in sorted(zip(destination, source)):\n perm.insert(dest, src)\n return lax.transpose(a, perm)\n\n\n@_wraps(np.isclose)\n@partial(jit, static_argnames=('equal_nan',))\ndef isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):\n a, b = _promote_args(\"isclose\", a, b)\n dtype = _dtype(a)\n if issubdtype(dtype, inexact):\n if issubdtype(dtype, complexfloating):\n dtype = _complex_elem_type(dtype)\n rtol = lax.convert_element_type(rtol, dtype)\n atol = lax.convert_element_type(atol, dtype)\n out = lax.le(\n lax.abs(lax.sub(a, b)),\n lax.add(atol, lax.mul(rtol, lax.abs(b))))\n # This corrects the comparisons for infinite and nan values\n a_inf = isinf(a)\n b_inf = isinf(b)\n any_inf = logical_or(a_inf, b_inf)\n both_inf = logical_and(a_inf, b_inf)\n # Make all elements where either a or b are infinite to False\n out = logical_and(out, logical_not(any_inf))\n # Make all elements where both a or b are the same inf to True\n same_value = lax.eq(a, b)\n same_inf = logical_and(both_inf, same_value)\n out = logical_or(out, same_inf)\n\n # Make all elements where either a or b is NaN to False\n a_nan = isnan(a)\n b_nan = isnan(b)\n any_nan = logical_or(a_nan, b_nan)\n out = logical_and(out, logical_not(any_nan))\n if equal_nan:\n # Make all elements where both a and b is NaN to True\n both_nan = logical_and(a_nan, b_nan)\n out = logical_or(out, both_nan)\n return out\n else:\n return lax.eq(a, b)\n\n\n@_wraps(np.interp)\n@partial(jit, static_argnames=('period',))\ndef interp(x, xp, fp, left=None, right=None, period=None):\n if shape(xp) != shape(fp) or ndim(xp) != 1:\n raise ValueError(\"xp and fp must be one-dimensional arrays of equal size\")\n x, xp, fp = _promote_dtypes_inexact(x, xp, fp)\n if period is not None:\n if period == 0:\n raise ValueError(f\"period must be a non-zero value; got {period}\")\n period = abs(period)\n x = x % period\n xp = xp % period\n xp, fp = lax.sort_key_val(xp, fp)\n xp = concatenate([xp[-1:] - period, xp, xp[:1] + period])\n fp = concatenate([fp[-1:], fp, fp[:1]])\n\n i = clip(searchsorted(xp, x, side='right'), 1, len(xp) - 1)\n df = fp[i] - fp[i - 1]\n dx = xp[i] - xp[i - 1]\n delta = x - xp[i - 1]\n f = where((dx == 0), fp[i], fp[i - 1] + (delta / dx) * df)\n\n if period is None:\n f = where(x < xp[0], fp[0] if left is None else left, f)\n f = where(x > xp[-1], fp[-1] if right is None else right, f)\n return f\n\n\n@_wraps(np.in1d, lax_description=\"\"\"\nIn the JAX version, the `assume_unique` argument is not referenced.\n\"\"\")\n@partial(jit, static_argnames=('assume_unique', 'invert',))\ndef in1d(ar1, ar2, assume_unique=False, invert=False):\n _check_arraylike(\"in1d\", ar1, ar2)\n ar1 = ravel(ar1)\n ar2 = ravel(ar2)\n # Note: an algorithm based on searchsorted has better scaling, but in practice\n # is very slow on accelerators because it relies on lax control flow. If XLA\n # ever supports binary search natively, we should switch to this:\n # ar2 = jnp.sort(ar2)\n # ind = jnp.searchsorted(ar2, ar1)\n # if invert:\n # return ar1 != ar2[ind]\n # else:\n # return ar1 == ar2[ind]\n if invert:\n return (ar1[:, None] != ar2[None, :]).all(-1)\n else:\n return (ar1[:, None] == ar2[None, :]).any(-1)\n\n_SETDIFF1D_DOC = \"\"\"\\\nBecause the size of the output of ``setdiff1d`` is data-dependent, the function is not\ntypically compatible with JIT. The JAX version adds the optional `size` argument which\nspecifies the size of the output array: it must be specified statically for ``jnp.setdiff1d``\nto be compiled with non-static operands. If specified, the first `size` unique elements will\nbe returned; if there are fewer unique elements than `size` indicates, the return value will\nbe padded with the `fill_value`, which defaults to zero.\"\"\"\n\n@_wraps(np.setdiff1d, lax_description=_SETDIFF1D_DOC)\ndef setdiff1d(ar1, ar2, assume_unique=False, *, size=None, fill_value=None):\n _check_arraylike(\"setdiff1d\", ar1, ar2)\n if size is None:\n ar1 = core.concrete_or_error(None, ar1, \"The error arose in setdiff1d()\")\n else:\n size = core.concrete_or_error(operator.index, size, \"The error arose in setdiff1d()\")\n ar1 = asarray(ar1)\n fill_value = asarray(0 if fill_value is None else fill_value, dtype=ar1.dtype)\n if ar1.size == 0:\n return full_like(ar1, fill_value, shape=size or 0)\n if not assume_unique:\n ar1 = unique(ar1, size=size and ar1.size)\n mask = in1d(ar1, ar2, invert=True)\n if size is None:\n return ar1[mask]\n else:\n if not (assume_unique or size is None):\n # Set mask to zero at locations corresponding to unique() padding.\n n_unique = ar1.size + 1 - (ar1 == ar1[0]).sum()\n mask = where(arange(ar1.size) < n_unique, mask, False)\n return where(arange(size) < mask.sum(), ar1[where(mask, size=size)], fill_value)\n\n\n_UNION1D_DOC = \"\"\"\\\nBecause the size of the output of ``union1d`` is data-dependent, the function is not\ntypically compatible with JIT. The JAX version adds the optional `size` argument which\nspecifies the size of the output array: it must be specified statically for ``jnp.union1d``\nto be compiled with non-static operands. If specified, the first `size` unique elements\nwill be returned; if there are fewer unique elements than `size` indicates, the return\nvalue will be padded with `fill_value`, which defaults to the minimum value of the union.\"\"\"\n\n@_wraps(np.union1d, lax_description=_UNION1D_DOC)\ndef union1d(ar1, ar2, *, size=None, fill_value=None):\n _check_arraylike(\"union1d\", ar1, ar2)\n if size is None:\n ar1 = core.concrete_or_error(None, ar1, \"The error arose in union1d()\")\n ar2 = core.concrete_or_error(None, ar2, \"The error arose in union1d()\")\n else:\n size = core.concrete_or_error(operator.index, size, \"The error arose in union1d()\")\n return unique(concatenate((ar1, ar2), axis=None), size=size, fill_value=fill_value)\n\n\n@_wraps(np.setxor1d, lax_description=\"\"\"\nIn the JAX version, the input arrays are explicitly flattened regardless\nof assume_unique value.\n\"\"\")\ndef setxor1d(ar1, ar2, assume_unique=False):\n _check_arraylike(\"setxor1d\", ar1, ar2)\n ar1 = core.concrete_or_error(None, ar1, \"The error arose in setxor1d()\")\n ar2 = core.concrete_or_error(None, ar2, \"The error arose in setxor1d()\")\n\n ar1 = ravel(ar1)\n ar2 = ravel(ar2)\n\n if not assume_unique:\n ar1 = unique(ar1)\n ar2 = unique(ar2)\n\n aux = concatenate((ar1, ar2))\n if aux.size == 0:\n return aux\n\n aux = sort(aux)\n flag = concatenate((array([True]), aux[1:] != aux[:-1], array([True])))\n return aux[flag[1:] & flag[:-1]]\n\n\n@partial(jit, static_argnums=2)\ndef _intersect1d_sorted_mask(ar1, ar2, return_indices=False):\n \"\"\"\n Helper function for intersect1d which is jit-able\n \"\"\"\n ar = concatenate((ar1, ar2))\n if return_indices:\n iota = lax.broadcasted_iota(np.int64, shape(ar), dimension=0)\n aux, indices = lax.sort_key_val(ar, iota)\n else:\n aux = sort(ar)\n\n mask = aux[1:] == aux[:-1]\n if return_indices:\n return aux, mask, indices\n else:\n return aux, mask\n\n\n@_wraps(np.intersect1d)\ndef intersect1d(ar1, ar2, assume_unique=False, return_indices=False):\n _check_arraylike(\"intersect1d\", ar1, ar2)\n ar1 = core.concrete_or_error(None, ar1, \"The error arose in intersect1d()\")\n ar2 = core.concrete_or_error(None, ar2, \"The error arose in intersect1d()\")\n\n if not assume_unique:\n if return_indices:\n ar1, ind1 = unique(ar1, return_index=True)\n ar2, ind2 = unique(ar2, return_index=True)\n else:\n ar1 = unique(ar1)\n ar2 = unique(ar2)\n else:\n ar1 = ravel(ar1)\n ar2 = ravel(ar2)\n\n if return_indices:\n aux, mask, aux_sort_indices = _intersect1d_sorted_mask(ar1, ar2, return_indices)\n else:\n aux, mask = _intersect1d_sorted_mask(ar1, ar2, return_indices)\n\n int1d = aux[:-1][mask]\n\n if return_indices:\n ar1_indices = aux_sort_indices[:-1][mask]\n ar2_indices = aux_sort_indices[1:][mask] - ar1.size\n if not assume_unique:\n ar1_indices = ind1[ar1_indices]\n ar2_indices = ind2[ar2_indices]\n\n return int1d, ar1_indices, ar2_indices\n else:\n return int1d\n\n\n@_wraps(np.isin, lax_description=\"\"\"\nIn the JAX version, the `assume_unique` argument is not referenced.\n\"\"\")\ndef isin(element, test_elements, assume_unique=False, invert=False):\n result = in1d(element, test_elements, assume_unique=assume_unique, invert=invert)\n return result.reshape(shape(element))\n\n\n# The `jit` on `where` exists to avoid materializing constants in cases like\n# `np.where(np.zeros(1000), 7, 4)`. In op-by-op mode, we don't want to\n# materialize the broadcast forms of scalar arguments.\n@jit\ndef _where(condition, x=None, y=None):\n if x is None or y is None:\n raise ValueError(\"Either both or neither of the x and y arguments should \"\n \"be provided to jax.numpy.where, got {} and {}.\"\n .format(x, y))\n if not issubdtype(_dtype(condition), bool_):\n condition = lax.ne(condition, zeros_like(condition))\n x, y = _promote_dtypes(x, y)\n condition, x, y = broadcast_arrays(condition, x, y)\n return lax.select(condition, x, y) if not core.is_empty_shape(np.shape(x)) else x\n\n\n_WHERE_DOC = \"\"\"\\\nAt present, JAX does not support JIT-compilation of the single-argument form\nof :py:func:`jax.numpy.where` because its output shape is data-dependent. The\nthree-argument form does not have a data-dependent shape and can be JIT-compiled\nsuccessfully. Alternatively, you can specify the optional ``size`` keyword:\nif specified, the first ``size`` True elements will be returned; if there\nare fewer True elements than ``size`` indicates, the index arrays will be\npadded with ``fill_value`` (default is 0.)\n\"\"\"\n\n@_wraps(np.where, update_doc=False, lax_description=_WHERE_DOC)\ndef where(condition, x=None, y=None, *, size=None, fill_value=None):\n if x is None and y is None:\n _check_arraylike(\"where\", condition)\n return nonzero(condition, size=size, fill_value=fill_value)\n else:\n if size is not None or fill_value is not None:\n raise ValueError(\"size and fill_value arguments cannot be used in three-term where function.\")\n return _where(condition, x, y)\n\n\n@_wraps(np.select)\ndef select(condlist, choicelist, default=0):\n if len(condlist) != len(choicelist):\n msg = \"condlist must have length equal to choicelist ({} vs {})\"\n raise ValueError(msg.format(len(condlist), len(choicelist)))\n if len(condlist) == 0:\n raise ValueError(\"condlist must be non-empty\")\n choices = _promote_dtypes(default, *choicelist)\n choicelist = choices[1:]\n output = choices[0]\n for cond, choice in zip(condlist[::-1], choicelist[::-1]):\n output = where(cond, choice, output)\n return output\n\n\n@_wraps(np.bincount, lax_description=\"\"\"\\\nJax adds the optional `length` parameter which specifies the output length, and\ndefaults to ``x.max() + 1``. It must be specified for bincount to be compiled\nwith non-static operands. Values larger than the specified length will be discarded.\nIf `length` is specified, `minlength` will be ignored.\n\nAdditionally, while ``np.bincount`` raises an error if the input array contains\nnegative values, ``jax.numpy.bincount`` clips negative values to zero.\n\"\"\")\ndef bincount(x, weights=None, minlength=0, *, length=None):\n _check_arraylike(\"bincount\", x)\n if not issubdtype(_dtype(x), integer):\n msg = f\"x argument to bincount must have an integer type; got {x.dtype}\"\n raise TypeError(msg)\n if ndim(x) != 1:\n raise ValueError(\"only 1-dimensional input supported.\")\n minlength = core.concrete_or_error(operator.index, minlength,\n \"The error occurred because of argument 'minlength' of jnp.bincount.\")\n if length is None:\n x = core.concrete_or_error(asarray, x,\n \"The error occured because of argument 'x' of jnp.bincount. \"\n \"To avoid this error, pass a static `length` argument.\")\n length = _max(minlength, x.size and x.max() + 1)\n else:\n length = core.concrete_or_error(operator.index, length,\n \"The error occurred because of argument 'length' of jnp.bincount.\")\n if weights is None:\n weights = np.array(1, dtype=int_)\n elif shape(x) != shape(weights):\n raise ValueError(\"shape of weights must match shape of x.\")\n return zeros(length, _dtype(weights)).at[clip(x, 0)].add(weights)\n\n@_wraps(getattr(np, \"broadcast_shapes\", None))\ndef broadcast_shapes(*shapes):\n if not shapes:\n return ()\n shapes = [(shape,) if np.ndim(shape) == 0 else tuple(shape) for shape in shapes]\n return lax.broadcast_shapes(*shapes)\n\n@partial(jit, inline=True)\ndef broadcast_arrays(*args):\n \"\"\"Like Numpy's broadcast_arrays but doesn't return views.\"\"\"\n shapes = [shape(arg) for arg in args]\n if len(set(shapes)) == 1:\n return [arg if isinstance(arg, ndarray) or isscalar(arg) else array(arg)\n for arg in args]\n result_shape = lax.broadcast_shapes(*shapes)\n return [broadcast_to(arg, result_shape) for arg in args]\n\n\n@_wraps(np.broadcast_to, lax_description=\"\"\"\\\nThe JAX version does not necessarily return a view of the input.\n\"\"\")\ndef broadcast_to(arr, shape):\n if hasattr(arr, \"broadcast_to\"):\n return arr.broadcast_to(shape)\n arr = arr if isinstance(arr, ndarray) else array(arr)\n shape = (shape,) if ndim(shape) == 0 else shape\n shape = canonicalize_shape(shape) # check that shape is concrete\n arr_shape = _shape(arr)\n if core.symbolic_equal_shape(arr_shape, shape):\n return arr\n else:\n nlead = len(shape) - len(arr_shape)\n shape_tail = shape[nlead:]\n compatible = _all(core.symbolic_equal_one_of_dim(arr_d, [1, shape_d])\n for arr_d, shape_d in safe_zip(arr_shape, shape_tail))\n if nlead < 0 or not compatible:\n msg = \"Incompatible shapes for broadcasting: {} and requested shape {}\"\n raise ValueError(msg.format(arr_shape, shape))\n diff, = np.where(tuple(not core.symbolic_equal_dim(arr_d, shape_d)\n for arr_d, shape_d in safe_zip(arr_shape, shape_tail)))\n new_dims = tuple(range(nlead)) + tuple(nlead + diff)\n kept_dims = tuple(np.delete(np.arange(len(shape)), new_dims))\n return lax.broadcast_in_dim(squeeze(arr, tuple(diff)), shape, kept_dims)\n\n\ndef _split(op, ary, indices_or_sections, axis=0):\n axis = core.concrete_or_error(int, axis, f\"in jax.numpy.{op} argument `axis`\")\n size = ary.shape[axis]\n if isinstance(indices_or_sections, (tuple, list)):\n indices_or_sections = np.array(\n [core.concrete_or_error(np.int64, i_s, f\"in jax.numpy.{op} argument 1\")\n for i_s in indices_or_sections], np.int64)\n split_indices = np.concatenate([[np.int64(0)], indices_or_sections,\n [np.int64(size)]])\n elif (isinstance(indices_or_sections, (np.ndarray, ndarray)) and\n indices_or_sections.ndim > 0):\n indices_or_sections = np.array(\n [core.concrete_or_error(np.int64, i_s, f\"in jax.numpy.{op} argument 1\")\n for i_s in indices_or_sections], np.int64)\n split_indices = np.concatenate([[np.int64(0)], indices_or_sections,\n [np.int64(size)]])\n else:\n indices_or_sections = core.concrete_or_error(np.int64, indices_or_sections,\n f\"in jax.numpy.{op} argument 1\")\n part_size, r = _divmod(size, indices_or_sections)\n if r == 0:\n split_indices = np.arange(indices_or_sections + 1,\n dtype=np.int64) * part_size\n elif op == \"array_split\":\n split_indices = np.concatenate(\n [np.arange(r + 1, dtype=np.int64) * (part_size + 1),\n np.arange(indices_or_sections - r, dtype=np.int64) * part_size\n + ((r + 1) * (part_size + 1) - 1)])\n else:\n raise ValueError(\"array split does not result in an equal division\")\n starts, ends = [0] * ndim(ary), shape(ary)\n _subval = lambda x, i, v: subvals(x, [(i, v)])\n return [lax.slice(ary, _subval(starts, axis, start), _subval(ends, axis, end))\n for start, end in zip(split_indices[:-1], split_indices[1:])]\n\n@_wraps(np.split, lax_description=_ARRAY_VIEW_DOC)\ndef split(ary, indices_or_sections, axis: int = 0):\n return _split(\"split\", ary, indices_or_sections, axis=axis)\n\ndef _split_on_axis(np_fun, axis):\n @_wraps(np_fun, update_doc=False)\n def f(ary, indices_or_sections):\n return split(ary, indices_or_sections, axis=axis)\n return f\n\nvsplit = _split_on_axis(np.vsplit, axis=0)\nhsplit = _split_on_axis(np.hsplit, axis=1)\ndsplit = _split_on_axis(np.dsplit, axis=2)\n\n@_wraps(np.array_split)\ndef array_split(ary, indices_or_sections, axis: int = 0):\n return _split(\"array_split\", ary, indices_or_sections, axis=axis)\n\n@_wraps(np.clip, skip_params=['out'])\n@jit\ndef clip(a, a_min=None, a_max=None, out=None):\n _check_arraylike(\"clip\", a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.clip is not supported.\")\n if a_min is None and a_max is None:\n raise ValueError(\"At most one of a_min and a_max may be None\")\n if a_min is not None:\n a = maximum(a_min, a)\n if a_max is not None:\n a = minimum(a_max, a)\n return a\n\n@_wraps(np.around, skip_params=['out'])\n@partial(jit, static_argnames=('decimals',))\ndef round(a, decimals=0, out=None):\n _check_arraylike(\"round\", a)\n decimals = core.concrete_or_error(operator.index, decimals, \"'decimals' argument of jnp.round\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.round is not supported.\")\n dtype = _dtype(a)\n if issubdtype(dtype, integer):\n if decimals < 0:\n raise NotImplementedError(\n \"integer np.round not implemented for decimals < 0\")\n return a # no-op on integer types\n\n def _round_float(x):\n if decimals == 0:\n return lax.round(x, lax.RoundingMethod.TO_NEAREST_EVEN)\n\n # TODO(phawkins): the strategy of rescaling the value isn't necessarily a\n # good one since we may be left with an incorrectly rounded value at the\n # end due to precision problems. As a workaround for float16, convert to\n # float32,\n x = lax.convert_element_type(x, np.float32) if dtype == np.float16 else x\n factor = _constant_like(x, 10 ** decimals)\n out = lax.div(lax.round(lax.mul(x, factor),\n lax.RoundingMethod.TO_NEAREST_EVEN), factor)\n return lax.convert_element_type(out, dtype) if dtype == np.float16 else out\n\n if issubdtype(dtype, complexfloating):\n return lax.complex(_round_float(lax.real(a)), _round_float(lax.imag(a)))\n else:\n return _round_float(a)\naround = round\nround_ = round\n\n\n@_wraps(np.fix, skip_params=['out'])\n@jit\ndef fix(x, out=None):\n _check_arraylike(\"fix\", x)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.fix is not supported.\")\n zero = lax._const(x, 0)\n return where(lax.ge(x, zero), floor(x), ceil(x))\n\n\n@_wraps(np.modf, skip_params=['out'])\n@jit\ndef modf(x, out=None):\n _check_arraylike(\"modf\", x)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.modf is not supported.\")\n whole = fix(x)\n return x - whole, whole\n\n\n@_wraps(np.isfinite)\n@jit\ndef isfinite(x):\n _check_arraylike(\"isfinite\", x)\n dtype = _dtype(x)\n if issubdtype(dtype, floating):\n return lax.is_finite(x)\n elif issubdtype(dtype, complexfloating):\n return lax.bitwise_and(lax.is_finite(real(x)), lax.is_finite(imag(x)))\n else:\n return full_like(x, True, dtype=bool_)\n\n@_wraps(np.isinf)\n@jit\ndef isinf(x):\n _check_arraylike(\"isinf\", x)\n dtype = _dtype(x)\n if issubdtype(dtype, floating):\n return lax.eq(lax.abs(x), _constant_like(x, inf))\n elif issubdtype(dtype, complexfloating):\n re = lax.real(x)\n im = lax.imag(x)\n return lax.bitwise_or(lax.eq(lax.abs(re), _constant_like(re, inf)),\n lax.eq(lax.abs(im), _constant_like(im, inf)))\n else:\n return full_like(x, False, dtype=bool_)\n\ndef _isposneginf(infinity, x, out):\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to isneginf/isposinf is not supported.\")\n dtype = _dtype(x)\n if issubdtype(dtype, floating):\n return lax.eq(x, _constant_like(x, infinity))\n elif issubdtype(dtype, complexfloating):\n raise ValueError(\"isposinf/isneginf are not well defined for complex types\")\n else:\n return full_like(x, False, dtype=bool_)\n\nisposinf = _wraps(np.isposinf, skip_params=['out'])(\n lambda x, out=None: _isposneginf(inf, x, out)\n)\n\nisneginf = _wraps(np.isneginf, skip_params=['out'])(\n lambda x, out=None: _isposneginf(-inf, x, out)\n)\n\n@_wraps(np.isnan)\n@jit\ndef isnan(x):\n _check_arraylike(\"isnan\", x)\n return lax.ne(x, x)\n\n@_wraps(np.nan_to_num)\n@jit\ndef nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):\n del copy\n _check_arraylike(\"nan_to_num\", x)\n dtype = _dtype(x)\n if issubdtype(dtype, complexfloating):\n return lax.complex(\n nan_to_num(lax.real(x), nan=nan, posinf=posinf, neginf=neginf),\n nan_to_num(lax.imag(x), nan=nan, posinf=posinf, neginf=neginf))\n info = finfo(dtypes.canonicalize_dtype(dtype))\n posinf = info.max if posinf is None else posinf\n neginf = info.min if neginf is None else neginf\n x = where(isnan(x), array(nan, dtype=x.dtype), x)\n x = where(isposinf(x), array(posinf, dtype=x.dtype), x)\n x = where(isneginf(x), array(neginf, dtype=x.dtype), x)\n return x\n\n### Reducers\n\ndef _reduction(a, name, np_fun, op, init_val, has_identity=True,\n preproc=None, bool_op=None, upcast_f16_for_computation=False,\n axis=None, dtype=None, out=None, keepdims=False, initial=None,\n where_=None, parallel_reduce=None):\n bool_op = bool_op or op\n # Note: we must accept out=None as an argument, because numpy reductions delegate to\n # object methods. For example `np.sum(x)` will call `x.sum()` if the `sum()` method\n # exists, passing along all its arguments.\n if out is not None:\n raise NotImplementedError(f\"The 'out' argument to jnp.{name} is not supported.\")\n _check_arraylike(name, a)\n lax._check_user_dtype_supported(dtype, name)\n axis = core.concrete_or_error(None, axis, f\"axis argument to jnp.{name}().\")\n\n if initial is None and not has_identity:\n if not _all(core.greater_equal_dim(d, 1) for d in np.shape(a)):\n raise ValueError(f\"zero-size array to reduction operation {name} which has no identity\")\n if where_ is not None:\n raise ValueError(f\"reduction operation {name} does not have an identity, so to use a \"\n f\"where mask one has to specify 'initial'\")\n\n a = a if isinstance(a, ndarray) else asarray(a)\n a = preproc(a) if preproc else a\n pos_dims, dims = _reduction_dims(a, axis)\n result_dtype = dtypes.canonicalize_dtype(dtype or _dtype(np_fun(np.ones((), dtype=_dtype(a)))))\n if upcast_f16_for_computation and issubdtype(result_dtype, inexact):\n computation_dtype = promote_types(result_dtype, float32)\n else:\n computation_dtype = result_dtype\n a = lax.convert_element_type(a, computation_dtype)\n op = op if computation_dtype != np.bool_ else bool_op\n # NB: in XLA, init_val must be an identity for the op, so the user-specified\n # initial value must be applied afterward.\n init_val = _reduction_init_val(a, init_val)\n if where_ is not None:\n a = where(where_, a, init_val)\n if pos_dims is not dims:\n if parallel_reduce is None:\n raise NotImplementedError(f\"Named reductions not implemented for jnp.{name}()\")\n result = parallel_reduce(a, dims)\n else:\n result = lax.reduce(a, init_val, op, dims)\n if initial is not None:\n result = op(lax.convert_element_type(initial, a.dtype), result)\n if keepdims:\n result = expand_dims(result, pos_dims)\n return lax.convert_element_type(result, dtype or result_dtype)\n\ndef _canonicalize_axis_allow_named(x, rank):\n return maybe_named_axis(x, lambda i: _canonicalize_axis(i, rank), lambda name: name)\n\ndef _reduction_dims(a, axis):\n if axis is None:\n return (tuple(range(ndim(a))),) * 2\n elif not isinstance(axis, (np.ndarray, tuple, list)):\n axis = (axis,)\n canon_axis = tuple(_canonicalize_axis_allow_named(x, ndim(a))\n for x in axis)\n if len(canon_axis) != len(set(canon_axis)):\n raise ValueError(f\"duplicate value in 'axis': {axis}\")\n canon_pos_axis = tuple(x for x in canon_axis if isinstance(x, int))\n if len(canon_pos_axis) != len(canon_axis):\n return canon_pos_axis, canon_axis\n else:\n return canon_axis, canon_axis\n\ndef _reduction_init_val(a, init_val):\n # This function uses np.* functions because lax pattern matches against the\n # specific concrete values of the reduction inputs.\n a_dtype = dtypes.canonicalize_dtype(_dtype(a))\n if a_dtype == 'bool':\n return np.array(init_val > 0, dtype=a_dtype)\n try:\n return np.array(init_val, dtype=a_dtype)\n except OverflowError:\n assert issubdtype(a_dtype, integer)\n sign, info = np.sign(init_val), iinfo(a_dtype)\n return np.array(info.min if sign < 0 else info.max, dtype=a_dtype)\n\ndef _cast_to_bool(operand):\n with warnings.catch_warnings():\n warnings.filterwarnings(\"ignore\", category=np.ComplexWarning)\n return lax.convert_element_type(operand, bool_)\n\n\ndef _ensure_optional_axes(x):\n def force(x):\n if x is None:\n return None\n try:\n return operator.index(x)\n except TypeError:\n return tuple(i if isinstance(i, str) else operator.index(i) for i in x)\n return core.concrete_or_error(\n force, x, \"The axis argument must be known statically.\")\n\n\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'), inline=True)\ndef _reduce_sum(a, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None, keepdims=None, initial=None, where=None):\n return _reduction(a, \"sum\", np.sum, lax.add, 0,\n bool_op=lax.bitwise_or, upcast_f16_for_computation=True,\n axis=axis, dtype=dtype, out=out, keepdims=keepdims,\n initial=initial, where_=where, parallel_reduce=lax.psum)\n\n@_wraps(np.sum, skip_params=['out'])\ndef sum(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None, initial=None, where=None):\n return _reduce_sum(a, axis=_ensure_optional_axes(axis), dtype=dtype, out=out,\n keepdims=keepdims, initial=initial, where=where)\n\n\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'), inline=True)\ndef _reduce_prod(a, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None, keepdims=None, initial=None, where=None):\n return _reduction(a, \"prod\", np.prod, lax.mul, 1,\n bool_op=lax.bitwise_and, upcast_f16_for_computation=True,\n axis=axis, dtype=dtype, out=out, keepdims=keepdims,\n initial=initial, where_=where)\n\n@_wraps(np.prod, skip_params=['out'])\ndef prod(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None, initial=None, where=None):\n return _reduce_prod(a, axis=_ensure_optional_axes(axis), dtype=dtype,\n out=out, keepdims=keepdims, initial=initial, where=where)\n\n\n@partial(jit, static_argnames=('axis', 'keepdims'), inline=True)\ndef _reduce_max(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None):\n return _reduction(a, \"max\", np.max, lax.max, -np.inf, has_identity=False,\n axis=axis, out=out, keepdims=keepdims,\n initial=initial, where_=where, parallel_reduce=lax.pmax)\n\n@_wraps(np.max, skip_params=['out'])\ndef max(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None):\n return _reduce_max(a, axis=_ensure_optional_axes(axis), out=out,\n keepdims=keepdims, initial=initial, where=where)\n\n@partial(jit, static_argnames=('axis', 'keepdims'), inline=True)\ndef _reduce_min(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None):\n return _reduction(a, \"min\", np.min, lax.min, np.inf, has_identity=False,\n axis=axis, out=out, keepdims=keepdims,\n initial=initial, where_=where, parallel_reduce=lax.pmin)\n\n@_wraps(np.min, skip_params=['out'])\ndef min(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, initial=None, where=None):\n return _reduce_min(a, axis=_ensure_optional_axes(axis), out=out,\n keepdims=keepdims, initial=initial, where=where)\n\n@partial(jit, static_argnames=('axis', 'keepdims'), inline=True)\ndef _reduce_all(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, *, where=None):\n return _reduction(a, \"all\", np.all, lax.bitwise_and, True, preproc=_cast_to_bool,\n axis=axis, out=out, keepdims=keepdims, where_=where)\n\n@_wraps(np.all, skip_params=['out'])\ndef all(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, *, where=None):\n return _reduce_all(a, axis=_ensure_optional_axes(axis), out=out,\n keepdims=keepdims, where=where)\n\n@partial(jit, static_argnames=('axis', 'keepdims'), inline=True)\ndef _reduce_any(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, *, where=None):\n return _reduction(a, \"any\", np.any, lax.bitwise_or, False, preproc=_cast_to_bool,\n axis=axis, out=out, keepdims=keepdims, where_=where)\n\n@_wraps(np.any, skip_params=['out'])\ndef any(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None, *, where=None):\n return _reduce_any(a, axis=_ensure_optional_axes(axis), out=out,\n keepdims=keepdims, where=where)\n\nproduct = prod\namin = min\namax = max\nalltrue = all\nsometrue = any\n\ndef _axis_size(a, axis):\n if not isinstance(axis, (tuple, list)):\n axis = (axis,)\n size = 1\n a_shape = shape(a)\n for a in axis:\n size *= maybe_named_axis(a, lambda i: a_shape[i], lambda name: lax.psum(1, name))\n return size\n\n@_wraps(np.mean, skip_params=['out'])\ndef mean(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=False, *, where=None):\n return _mean(a, _ensure_optional_axes(axis), dtype, out, keepdims,\n where=where)\n\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'), inline=True)\ndef _mean(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=False, *, where=None):\n _check_arraylike(\"mean\", a)\n lax._check_user_dtype_supported(dtype, \"mean\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.mean is not supported.\")\n\n if where is None:\n if axis is None:\n normalizer = core.dimension_as_value(size(a))\n else:\n normalizer = core.dimension_as_value(_axis_size(a, axis))\n else:\n normalizer = sum(broadcast_to(where, shape(a)), axis, dtype=dtype, keepdims=keepdims)\n\n if dtype is None:\n if issubdtype(_dtype(a), bool_) or issubdtype(_dtype(a), integer):\n dtype = float_\n else:\n dtype = _dtype(a)\n dtype = dtypes.canonicalize_dtype(dtype)\n\n return lax.div(\n sum(a, axis, dtype=dtype, keepdims=keepdims, where=where),\n lax.convert_element_type(normalizer, dtype))\n\n@_wraps(np.average)\ndef average(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, weights=None,\n returned=False):\n return _average(a, _ensure_optional_axes(axis), weights, returned)\n\n@partial(jit, static_argnames=('axis', 'returned'), inline=True)\ndef _average(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, weights=None,\n returned=False):\n a = asarray(a)\n\n if weights is None: # Treat all weights as 1\n avg = mean(a, axis=axis)\n if axis is None:\n weights_sum = full((), core.dimension_as_value(size(a)), dtype=avg.dtype)\n else:\n weights_sum = full_like(avg, core.dimension_as_value(a.shape[axis]), dtype=avg.dtype)\n else:\n weights = asarray(weights)\n\n if issubdtype(a.dtype, inexact):\n out_dtype = result_type(a.dtype, weights.dtype)\n else:\n out_dtype = result_type(a.dtype, weights.dtype, float_)\n out_dtype = dtypes.canonicalize_dtype(out_dtype)\n\n a_shape = shape(a)\n a_ndim = len(a_shape)\n weights_shape = shape(weights)\n axis = None if axis is None else _canonicalize_axis(axis, a_ndim)\n\n if a_shape != weights_shape:\n # Make sure the dimensions work out\n if axis is None:\n raise ValueError(\"Axis must be specified when shapes of a and \"\n \"weights differ.\")\n if len(weights_shape) != 1:\n raise ValueError(\"1D weights expected when shapes of a and \"\n \"weights differ.\")\n if not core.symbolic_equal_dim(weights_shape[0], a_shape[axis]):\n raise ValueError(\"Length of weights not \"\n \"compatible with specified axis.\")\n\n weights = broadcast_to(weights, (a_ndim - 1) * (1,) + weights_shape)\n weights = moveaxis(weights, -1, axis)\n\n weights_sum = sum(weights, axis=axis, dtype=out_dtype)\n avg = sum(multiply(a, weights), axis=axis, dtype=out_dtype) / weights_sum\n\n if returned:\n if avg.shape != weights_sum.shape:\n weights_sum = broadcast_to(weights_sum, avg.shape)\n return avg, weights_sum\n return avg\n\n\n@_wraps(np.var, skip_params=['out'])\ndef var(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False, *, where=None):\n return _var(a, _ensure_optional_axes(axis), dtype, out, ddof, keepdims,\n where=where)\n\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef _var(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False, *, where=None):\n _check_arraylike(\"var\", a)\n lax._check_user_dtype_supported(dtype, \"var\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.var is not supported.\")\n\n a_dtype, dtype = _var_promote_types(_dtype(a), dtype)\n a_mean = mean(a, axis, dtype=a_dtype, keepdims=True, where=where)\n centered = a - a_mean\n if issubdtype(centered.dtype, complexfloating):\n centered = lax.real(lax.mul(centered, lax.conj(centered)))\n else:\n centered = lax.square(centered)\n\n if where is None:\n if axis is None:\n normalizer = core.dimension_as_value(size(a))\n else:\n normalizer = core.dimension_as_value(_axis_size(a, axis))\n else:\n normalizer = sum(broadcast_to(where, shape(a)), axis, dtype=dtype, keepdims=keepdims)\n normalizer = normalizer - ddof\n\n result = sum(centered, axis, keepdims=keepdims, where=where)\n out = lax.div(result, lax.convert_element_type(normalizer, result.dtype))\n return lax.convert_element_type(out, dtype)\n\n\ndef _var_promote_types(a_dtype, dtype):\n if dtype:\n if (not issubdtype(dtype, complexfloating) and\n issubdtype(a_dtype, complexfloating)):\n msg = (\"jax.numpy.var does not yet support real dtype parameters when \"\n \"computing the variance of an array of complex values. The \"\n \"semantics of numpy.var seem unclear in this case. Please comment \"\n \"on https://github.com/google/jax/issues/2283 if this behavior is \"\n \"important to you.\")\n raise ValueError(msg)\n a_dtype = promote_types(a_dtype, dtype)\n else:\n if not issubdtype(a_dtype, inexact):\n dtype = a_dtype = dtypes.canonicalize_dtype(float_)\n else:\n dtype = _complex_elem_type(a_dtype)\n a_dtype = promote_types(a_dtype, float32)\n return a_dtype, dtype\n\n\n@_wraps(np.std, skip_params=['out'])\ndef std(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False, *, where=None):\n return _std(a, _ensure_optional_axes(axis), dtype, out, ddof, keepdims,\n where=where)\n\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef _std(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False, *, where=None):\n _check_arraylike(\"std\", a)\n lax._check_user_dtype_supported(dtype, \"std\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.std is not supported.\")\n return sqrt(var(a, axis=axis, dtype=dtype, ddof=ddof, keepdims=keepdims, where=where))\n\n\n@_wraps(np.ptp, skip_params=['out'])\ndef ptp(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=False):\n return _ptp(a, _ensure_optional_axes(axis), out, keepdims)\n\n@partial(jit, static_argnames=('axis', 'keepdims'))\ndef _ptp(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=False):\n _check_arraylike(\"ptp\", a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.ptp is not supported.\")\n x = amax(a, axis=axis, keepdims=keepdims)\n y = amin(a, axis=axis, keepdims=keepdims)\n return lax.sub(x, y)\n\n\n@_wraps(np.allclose)\n@partial(jit, static_argnames=('equal_nan',))\ndef allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):\n _check_arraylike(\"allclose\", a, b)\n return all(isclose(a, b, rtol, atol, equal_nan))\n\n\n@_wraps(np.count_nonzero)\n@partial(jit, static_argnames=('axis', 'keepdims'))\ndef count_nonzero(a, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n keepdims=False):\n _check_arraylike(\"count_nonzero\", a)\n return sum(lax.ne(a, _constant_like(a, 0)), axis=axis,\n dtype=dtypes.canonicalize_dtype(np.int_), keepdims=keepdims)\n\n\n_NONZERO_DOC = \"\"\"\\\nBecause the size of the output of ``nonzero`` is data-dependent, the function is not\ntypically compatible with JIT. The JAX version adds the optional `size` argument which\nspecifies the size of the output arrays: it must be specified statically for ``jnp.nonzero``\nto be compiled with non-static operands. If specified, the first `size` nonzero elements\nwill be returned; if there are fewer nonzero elements than `size` indicates, the result\nwill be padded with ``fill_value``, which defaults to zero. ``fill_value`` may be a scalar,\nor a tuple specifying the fill value in each dimension.\n\"\"\"\n\n@_wraps(np.nonzero, lax_description=_NONZERO_DOC)\ndef nonzero(a, *, size=None, fill_value=None):\n a = atleast_1d(a)\n mask = a != 0\n if size is None:\n size = mask.sum()\n size = core.concrete_or_error(int, size,\n \"The size argument of jnp.nonzero must be statically specified \"\n \"to use jnp.nonzero within JAX transformations.\")\n if a.size == 0 or size == 0:\n return tuple(zeros(size, int) for dim in a.shape)\n flat_indices = cumsum(bincount(cumsum(mask), length=size))\n strides = (np.cumprod(a.shape[::-1])[::-1] // a.shape).astype(int_)\n out = tuple((flat_indices // stride) % size for stride, size in zip(strides, a.shape))\n if size is not None and fill_value is not None:\n if not isinstance(fill_value, tuple):\n fill_value = a.ndim * (fill_value,)\n if _shape(fill_value) != (a.ndim,):\n raise ValueError(f\"fill_value must be a scalar or a tuple of length {a.ndim}; got {fill_value}\")\n fill_mask = arange(size) >= mask.sum()\n out = tuple(where(fill_mask, fval, entry) for fval, entry in safe_zip(fill_value, out))\n return out\n\n@_wraps(np.flatnonzero, lax_description=_NONZERO_DOC)\ndef flatnonzero(a, *, size=None, fill_value=None):\n return nonzero(ravel(a), size=size, fill_value=fill_value)[0]\n\n\ndef _nan_reduction(a, name, jnp_reduction, init_val, nan_if_all_nan,\n axis=None, keepdims=None, **kwargs):\n _check_arraylike(name, a)\n if not issubdtype(_dtype(a), inexact):\n return jnp_reduction(a, axis=axis, keepdims=keepdims, **kwargs)\n\n out = jnp_reduction(where(isnan(a), _reduction_init_val(a, init_val), a),\n axis=axis, keepdims=keepdims, **kwargs)\n if nan_if_all_nan:\n return where(all(isnan(a), axis=axis, keepdims=keepdims),\n _constant_like(a, nan), out)\n else:\n return out\n\n@_wraps(np.nanmin, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'keepdims'))\ndef nanmin(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None):\n return _nan_reduction(a, 'nanmin', min, inf, nan_if_all_nan=True,\n axis=axis, out=out, keepdims=keepdims)\n\n@_wraps(np.nanmax, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'keepdims'))\ndef nanmax(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n keepdims=None):\n return _nan_reduction(a, 'nanmax', max, -inf, nan_if_all_nan=True,\n axis=axis, out=out, keepdims=keepdims)\n\n@_wraps(np.nansum, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef nansum(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None):\n lax._check_user_dtype_supported(dtype, \"nanprod\")\n return _nan_reduction(a, 'nansum', sum, 0, nan_if_all_nan=False,\n axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n@_wraps(np.nanprod, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef nanprod(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=None):\n lax._check_user_dtype_supported(dtype, \"nanprod\")\n return _nan_reduction(a, 'nanprod', prod, 1, nan_if_all_nan=False,\n axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n@_wraps(np.nanmean, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef nanmean(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, keepdims=False):\n _check_arraylike(\"nanmean\", a)\n lax._check_user_dtype_supported(dtype, \"nanmean\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.nanmean is not supported.\")\n if issubdtype(_dtype(a), bool_) or issubdtype(_dtype(a), integer):\n return mean(a, axis, dtype, out, keepdims)\n if dtype is None:\n dtype = _dtype(a)\n nan_mask = logical_not(isnan(a))\n normalizer = sum(nan_mask, axis=axis, dtype=int32, keepdims=keepdims)\n normalizer = lax.convert_element_type(normalizer, dtype)\n td = lax.div(nansum(a, axis, dtype=dtype, keepdims=keepdims), normalizer)\n return td\n\n\n@_wraps(np.nanvar, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef nanvar(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False):\n _check_arraylike(\"nanvar\", a)\n lax._check_user_dtype_supported(dtype, \"nanvar\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.nanvar is not supported.\")\n\n a_dtype, dtype = _var_promote_types(_dtype(a), dtype)\n a_mean = nanmean(a, axis, dtype=a_dtype, keepdims=True)\n\n centered = where(isnan(a), 0, a - a_mean) # double-where trick for gradients.\n if issubdtype(centered.dtype, complexfloating):\n centered = lax.real(lax.mul(centered, lax.conj(centered)))\n else:\n centered = lax.square(centered)\n\n normalizer = sum(logical_not(isnan(a)), axis=axis, keepdims=keepdims)\n normalizer = normalizer - ddof\n normalizer_mask = lax.le(normalizer, 0)\n result = sum(centered, axis, keepdims=keepdims)\n result = where(normalizer_mask, nan, result)\n divisor = where(normalizer_mask, 1, normalizer)\n out = lax.div(result, lax.convert_element_type(divisor, result.dtype))\n return lax.convert_element_type(out, dtype)\n\n\n@_wraps(np.nanstd, skip_params=['out'])\n@partial(jit, static_argnames=('axis', 'dtype', 'keepdims'))\ndef nanstd(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype=None,\n out=None, ddof=0, keepdims=False):\n _check_arraylike(\"nanstd\", a)\n lax._check_user_dtype_supported(dtype, \"nanstd\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.nanstd is not supported.\")\n return sqrt(nanvar(a, axis=axis, dtype=dtype, ddof=ddof, keepdims=keepdims))\n\n\ndef _make_cumulative_reduction(np_reduction, reduction, fill_nan=False, fill_value=0):\n @_wraps(np_reduction, skip_params=['out'])\n def cumulative_reduction(a,\n axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None):\n return _cumulative_reduction(a, _ensure_optional_axes(axis), dtype, out)\n\n @partial(jit, static_argnames=('axis', 'dtype'))\n def _cumulative_reduction(a,\n axis: Optional[Union[int, Tuple[int, ...]]] = None,\n dtype=None, out=None):\n _check_arraylike(np_reduction.__name__, a)\n if out is not None:\n raise NotImplementedError(f\"The 'out' argument to jnp.{np_reduction.__name__} \"\n f\"is not supported.\")\n lax._check_user_dtype_supported(dtype, np_reduction.__name__)\n\n if axis is None or isscalar(a):\n a = ravel(a)\n axis = 0\n\n a_shape = list(shape(a))\n num_dims = len(a_shape)\n axis = _canonicalize_axis(axis, num_dims)\n\n if fill_nan:\n a = where(isnan(a), _constant_like(a, fill_value), a)\n\n if not dtype and _dtype(a) == bool_:\n dtype = int_\n if dtype:\n a = lax.convert_element_type(a, dtype)\n\n return reduction(a, axis)\n\n return cumulative_reduction\n\n\ncumsum = _make_cumulative_reduction(np.cumsum, lax.cumsum, fill_nan=False)\ncumprod = _make_cumulative_reduction(np.cumprod, lax.cumprod, fill_nan=False)\ncumproduct = cumprod\nnancumsum = _make_cumulative_reduction(np.nancumsum, lax.cumsum,\n fill_nan=True, fill_value=0)\nnancumprod = _make_cumulative_reduction(np.nancumprod, lax.cumprod,\n fill_nan=True, fill_value=1)\n\n\n@_wraps(np.unwrap)\n@partial(jit, static_argnames=('axis',))\ndef unwrap(p, discont=pi, axis: int = -1):\n _check_arraylike(\"unwrap\", p)\n dd = diff(p, axis=axis)\n ddmod = mod(dd + pi, 2 * pi) - pi\n ddmod = where((ddmod == -pi) & (dd > 0), pi, ddmod)\n\n ph_correct = where(abs(dd) < discont, 0, ddmod - dd)\n\n up = concatenate((\n lax.slice_in_dim(p, 0, 1, axis=axis),\n lax.slice_in_dim(p, 1, None, axis=axis) + cumsum(ph_correct, axis=axis)\n ), axis=axis)\n\n return up\n\n\n### Array-creation functions\n\ndef _check_no_padding(axis_padding, mode):\n if (axis_padding[0] > 0 or axis_padding[1] > 0):\n msg = \"Cannot apply '{}' padding to empty axis\"\n raise ValueError(msg.format(mode))\n\n\ndef _pad_constant(array, pad_width, constant_values):\n nd = ndim(array)\n constant_values = broadcast_to(asarray(constant_values), (nd, 2))\n constant_values = lax._convert_element_type(constant_values, array.dtype, dtypes.is_weakly_typed(array))\n for i in range(nd):\n widths = [(0, 0, 0)] * nd\n widths[i] = (pad_width[i, 0], 0, 0)\n array = lax.pad(array, constant_values[i, 0], widths)\n widths[i] = (0, pad_width[i, 1], 0)\n array = lax.pad(array, constant_values[i, 1], widths)\n return array\n\n\ndef _pad_wrap(array, pad_width):\n for i in range(ndim(array)):\n if array.shape[i] == 0:\n _check_no_padding(pad_width[i], \"wrap\")\n continue\n size = array.shape[i]\n repeats, (left_remainder, right_remainder) = _divmod(pad_width[i], size)\n total_repeats = repeats.sum() + 1\n parts = []\n if left_remainder:\n parts += [lax.slice_in_dim(array, size - left_remainder, size, axis=i)]\n parts += total_repeats * [array]\n if right_remainder:\n parts += [lax.slice_in_dim(array, 0, right_remainder, axis=i)]\n array = lax.concatenate(parts, dimension=i)\n return array\n\n\ndef _pad_symmetric_or_reflect(array, pad_width, mode, reflect_type):\n assert mode in (\"symmetric\", \"reflect\")\n assert reflect_type in (\"even\", \"odd\")\n\n for i in range(ndim(array)):\n if array.shape[i] == 0:\n _check_no_padding(pad_width[i], mode)\n continue\n\n n = array.shape[i]\n offset = 1 if (mode == \"reflect\" and n > 1) else 0\n\n def build_padding(array, padding, before):\n if before:\n edge = lax.slice_in_dim(array, 0, 1, axis=i)\n else:\n edge = lax.slice_in_dim(array, -1, None, axis=i)\n\n while padding > 0:\n curr_pad = _min(padding, n - offset)\n padding -= curr_pad\n\n if before:\n start = offset\n stop = offset + curr_pad\n else:\n start = -(curr_pad + offset)\n stop = None if (mode == \"symmetric\" or n == 1) else -1\n\n x = lax.slice_in_dim(array, start, stop, axis=i)\n x = flip(x, axis=i)\n\n if reflect_type == 'odd':\n x = 2 * edge - x\n if n > 1:\n if before:\n edge = lax.slice_in_dim(x, 0, 1, axis=i)\n else:\n edge = lax.slice_in_dim(x, -1, None, axis=i)\n\n if before:\n array = lax.concatenate([x, array], dimension=i)\n else:\n array = lax.concatenate([array, x], dimension=i)\n return array\n\n array = build_padding(array, pad_width[i, 0], before=True)\n array = build_padding(array, pad_width[i, 1], before=False)\n return array\n\n\ndef _pad_edge(array, pad_width):\n nd = ndim(array)\n for i in range(nd):\n if array.shape[i] == 0:\n _check_no_padding(pad_width[i], \"edge\")\n continue\n\n n = array.shape[i]\n npad_before, npad_after = pad_width[i]\n\n edge_before = lax.slice_in_dim(array, 0, 1, axis=i)\n pad_before = repeat(edge_before, npad_before, axis=i)\n\n edge_after = lax.slice_in_dim(array, n-1, n, axis=i)\n pad_after = repeat(edge_after, npad_after, axis=i)\n\n array = lax.concatenate([pad_before, array, pad_after], dimension=i)\n return array\n\n\ndef _pad_linear_ramp(array, pad_width, end_values):\n for axis in range(ndim(array)):\n edge_before = lax.slice_in_dim(array, 0, 1, axis=axis)\n edge_after = lax.slice_in_dim(array, -1, None, axis=axis)\n ramp_before = linspace(\n start=end_values[axis][0],\n stop=edge_before.squeeze(axis), # Dimension is replaced by linspace\n num=pad_width[axis][0],\n endpoint=False,\n dtype=array.dtype,\n axis=axis\n )\n ramp_before = lax._convert_element_type(ramp_before, weak_type=dtypes.is_weakly_typed(array))\n ramp_after = linspace(\n start=end_values[axis][1],\n stop=edge_after.squeeze(axis), # Dimension is replaced by linspace\n num=pad_width[axis][1],\n endpoint=False,\n dtype=array.dtype,\n axis=axis\n )\n ramp_after = lax._convert_element_type(ramp_after, weak_type=dtypes.is_weakly_typed(array))\n\n # Reverse linear space in appropriate dimension\n ramp_after = flip(ramp_after, axis)\n\n array = lax.concatenate([ramp_before, array, ramp_after], dimension=axis)\n return array\n\n\ndef _pad_stats(array, pad_width, stat_length, stat_func):\n nd = ndim(array)\n for i in range(nd):\n if stat_length is None:\n stat_before = stat_func(array, axis=i, keepdims=True)\n stat_after = stat_before\n else:\n array_length = array.shape[i]\n length_before, length_after = stat_length[i]\n if length_before == 0 or length_after == 0:\n raise ValueError(\"stat_length of 0 yields no value for padding\")\n\n # Limit stat_length to length of array.\n length_before = _min(length_before, array_length)\n length_after = _min(length_after, array_length)\n\n slice_before = lax.slice_in_dim(array, 0, length_before, axis=i)\n slice_after = lax.slice_in_dim(array, -length_after, None, axis=i)\n stat_before = stat_func(slice_before, axis=i, keepdims=True)\n stat_after = stat_func(slice_after, axis=i, keepdims=True)\n\n if np.issubdtype(array.dtype, np.integer):\n stat_before = round(stat_before)\n stat_after = round(stat_after)\n\n stat_before = lax._convert_element_type(stat_before, array.dtype, dtypes.is_weakly_typed(array))\n stat_after = lax._convert_element_type(stat_after, array.dtype, dtypes.is_weakly_typed(array))\n\n npad_before, npad_after = pad_width[i]\n pad_before = repeat(stat_before, npad_before, axis=i)\n pad_after = repeat(stat_after, npad_after, axis=i)\n\n array = lax.concatenate([pad_before, array, pad_after], dimension=i)\n return array\n\n\ndef _pad_empty(array, pad_width):\n # Note: jax.numpy.empty = jax.numpy.zeros\n for i in range(ndim(array)):\n shape_before = array.shape[:i] + (pad_width[i][0],) + array.shape[i + 1:]\n pad_before = empty_like(array, shape=shape_before)\n\n shape_after = array.shape[:i] + (pad_width[i][1],) + array.shape[i + 1:]\n pad_after = empty_like(array, shape=shape_after)\n array = lax.concatenate([pad_before, array, pad_after], dimension=i)\n return array\n\n\ndef _pad_func(array, pad_width, func, **kwargs):\n pad_width = _broadcast_to_pairs(pad_width, ndim(array), \"pad_width\")\n padded = _pad_constant(array, np.array(pad_width), 0)\n for axis in range(ndim(padded)):\n padded = apply_along_axis(func, axis, padded, pad_width[axis], axis, kwargs)\n return padded\n\n\ndef _broadcast_to_pairs(nvals, nd, name):\n nvals = np.asarray(tree_map(\n lambda x: core.concrete_or_error(np.array, x, context=f\"{name} argument of jnp.pad\"),\n nvals))\n if nvals.dtype.kind == 'O':\n raise TypeError(f'`{name}` entries must be the same shape.')\n\n if nvals.shape == (nd, 2):\n # ((before_1, after_1), ..., (before_N, after_N))\n return tuple(tuple(nval) for nval in nvals)\n elif nvals.shape == (1, 2):\n # ((before, after),)\n return tuple(tuple(nvals[0]) for i in range(nd))\n elif nvals.shape == (2,):\n # (before, after) (not in the numpy docstring but works anyway)\n return tuple(tuple(nvals) for i in range(nd))\n elif nvals.shape == (1,):\n # (pad,)\n return tuple((nvals[0], nvals[0]) for i in range(nd))\n elif nvals.shape == ():\n # pad\n return tuple((nvals.flat[0], nvals.flat[0]) for i in range(nd))\n else:\n raise ValueError(f\"jnp.pad: {name} with nd={nd} has unsupported shape {nvals.shape}. \"\n f\"Valid shapes are ({nd}, 2), (1, 2), (2,), (1,), or ().\")\n\n\n@partial(jit, static_argnums=(1, 2, 4, 5, 6))\ndef _pad(array, pad_width, mode, constant_values, stat_length, end_values, reflect_type):\n array = asarray(array)\n nd = ndim(array)\n\n if nd == 0:\n return array\n\n stat_funcs = {\"maximum\": amax, \"minimum\": amin,\n \"mean\": mean, \"median\": median}\n\n pad_width = _broadcast_to_pairs(pad_width, nd, \"pad_width\")\n pad_width = np.array(pad_width)\n assert pad_width.shape == (nd, 2), pad_width\n\n if np.any(pad_width < 0):\n raise ValueError(\"index can't contain negative values\")\n\n if mode == \"constant\":\n return _pad_constant(array, pad_width, constant_values)\n\n elif mode == \"wrap\":\n return _pad_wrap(array, pad_width)\n\n elif mode in (\"symmetric\", \"reflect\"):\n return _pad_symmetric_or_reflect(array, pad_width, mode, reflect_type)\n\n elif mode == \"edge\":\n return _pad_edge(array, pad_width)\n\n elif mode == \"linear_ramp\":\n end_values = _broadcast_to_pairs(end_values, nd, \"end_values\")\n return _pad_linear_ramp(array, pad_width, end_values)\n\n elif mode in stat_funcs:\n if stat_length is not None:\n stat_length = _broadcast_to_pairs(stat_length, nd, \"stat_length\")\n return _pad_stats(array, pad_width, stat_length, stat_funcs[mode])\n\n elif mode == \"empty\":\n return _pad_empty(array, pad_width)\n\n else:\n assert False, (\"Should not be reached since pad already handled unsupported and\"\n \"not implemented modes\")\n\n\n@_wraps(np.pad, lax_description=\"\"\"\\\nUnlike numpy, JAX \"function\" mode's argument (which is another function) should return\nthe modified array. This is because Jax arrays are immutable.\n(In numpy, \"function\" mode's argument should modify a rank 1 array in-place.)\n\"\"\")\ndef pad(array, pad_width, mode=\"constant\", **kwargs):\n _check_arraylike(\"pad\", array)\n pad_width = _broadcast_to_pairs(pad_width, ndim(array), \"pad_width\")\n if pad_width and np.array(pad_width).dtype.kind != 'i':\n raise TypeError('`pad_width` must be of integral type.')\n\n if callable(mode):\n return _pad_func(array, pad_width, mode, **kwargs)\n\n allowed_kwargs = {\n 'empty': [], 'edge': [], 'wrap': [],\n 'constant': ['constant_values'],\n 'linear_ramp': ['end_values'],\n 'maximum': ['stat_length'],\n 'mean': ['stat_length'],\n 'median': ['stat_length'],\n 'minimum': ['stat_length'],\n 'reflect': ['reflect_type'],\n 'symmetric': ['reflect_type'],\n }\n try:\n unsupported_kwargs = set(kwargs) - set(allowed_kwargs[mode])\n except KeyError:\n msg = \"Unimplemented padding mode '{}' for np.pad.\"\n raise NotImplementedError(msg.format(mode))\n if unsupported_kwargs:\n raise ValueError(\"unsupported keyword arguments for mode '{}': {}\"\n .format(mode, unsupported_kwargs))\n # Set default value if not given.\n constant_values = kwargs.get('constant_values', 0)\n stat_length = kwargs.get('stat_length', None)\n end_values = kwargs.get('end_values', 0)\n reflect_type = kwargs.get('reflect_type', \"even\")\n\n return _pad(array, pad_width, mode, constant_values, stat_length, end_values, reflect_type)\n\n\n@_wraps(np.stack, skip_params=['out'])\ndef stack(arrays, axis: int = 0, out=None):\n if not len(arrays):\n raise ValueError(\"Need at least one array to stack.\")\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.stack is not supported.\")\n if isinstance(arrays, (np.ndarray, ndarray)):\n axis = _canonicalize_axis(axis, arrays.ndim)\n return concatenate(expand_dims(arrays, axis + 1), axis=axis)\n else:\n _check_arraylike(\"stack\", *arrays)\n shape0 = shape(arrays[0])\n axis = _canonicalize_axis(axis, len(shape0) + 1)\n new_arrays = []\n for a in arrays:\n if shape(a) != shape0:\n raise ValueError(\"All input arrays must have the same shape.\")\n new_arrays.append(expand_dims(a, axis))\n return concatenate(new_arrays, axis=axis)\n\n@_wraps(np.tile)\ndef tile(A, reps):\n _stackable(A) or _check_arraylike(\"tile\", A)\n try:\n iter(reps)\n except TypeError:\n reps = (reps,)\n reps = tuple(operator.index(rep) if core.is_constant_dim(rep) else rep\n for rep in reps)\n A_shape = (1,) * (len(reps) - ndim(A)) + shape(A)\n reps = (1,) * (len(A_shape) - len(reps)) + reps\n result = broadcast_to(reshape(A, [j for i in A_shape for j in [1, i]]),\n [k for pair in zip(reps, A_shape) for k in pair])\n return reshape(result, tuple(np.multiply(A_shape, reps)))\n\ndef _concatenate_array(arr, axis: int):\n # Fast path for concatenation when the input is an ndarray rather than a list.\n arr = asarray(arr)\n if arr.ndim == 0 or arr.shape[0] == 0:\n raise ValueError(\"Need at least one array to concatenate.\")\n if axis is None:\n return lax.reshape(arr, (arr.size,))\n if arr.ndim == 1:\n raise ValueError(\"Zero-dimensional arrays cannot be concatenated.\")\n axis = _canonicalize_axis(axis, arr.ndim - 1)\n shape = arr.shape[1:axis + 1] + (arr.shape[0] * arr.shape[axis + 1],) + arr.shape[axis + 2:]\n dimensions = [*range(1, axis + 1), 0, *range(axis + 1, arr.ndim)]\n return lax.reshape(arr, shape, dimensions)\n\n@_wraps(np.concatenate)\ndef concatenate(arrays, axis: int = 0):\n if isinstance(arrays, (np.ndarray, ndarray)):\n return _concatenate_array(arrays, axis)\n _stackable(*arrays) or _check_arraylike(\"concatenate\", *arrays)\n if not len(arrays):\n raise ValueError(\"Need at least one array to concatenate.\")\n if ndim(arrays[0]) == 0:\n raise ValueError(\"Zero-dimensional arrays cannot be concatenated.\")\n if axis is None:\n return concatenate([ravel(a) for a in arrays], axis=0)\n if hasattr(arrays[0], \"concatenate\"):\n return arrays[0].concatenate(arrays[1:], axis)\n axis = _canonicalize_axis(axis, ndim(arrays[0]))\n arrays = _promote_dtypes(*arrays)\n # lax.concatenate can be slow to compile for wide concatenations, so form a\n # tree of concatenations as a workaround especially for op-by-op mode.\n # (https://github.com/google/jax/issues/653).\n k = 16\n if len(arrays) == 1:\n return asarray(arrays[0])\n else:\n while len(arrays) > 1:\n arrays = [lax.concatenate(arrays[i:i+k], axis)\n for i in range(0, len(arrays), k)]\n return arrays[0]\n\n\n@_wraps(np.vstack)\ndef vstack(tup):\n if isinstance(tup, (np.ndarray, ndarray)):\n arrs = jax.vmap(atleast_2d)(tup)\n else:\n arrs = [atleast_2d(m) for m in tup]\n return concatenate(arrs, axis=0)\nrow_stack = vstack\n\n\n@_wraps(np.hstack)\ndef hstack(tup):\n if isinstance(tup, (np.ndarray, ndarray)):\n arrs = jax.vmap(atleast_1d)(tup)\n arr0_ndim = arrs.ndim - 1\n else:\n arrs = [atleast_1d(m) for m in tup]\n arr0_ndim = arrs[0].ndim\n return concatenate(arrs, axis=0 if arr0_ndim == 1 else 1)\n\n\n@_wraps(np.dstack)\ndef dstack(tup):\n if isinstance(tup, (np.ndarray, ndarray)):\n arrs = jax.vmap(atleast_3d)(tup)\n else:\n arrs = [atleast_3d(m) for m in tup]\n return concatenate(arrs, axis=2)\n\n\n@_wraps(np.column_stack)\ndef column_stack(tup):\n if isinstance(tup, (np.ndarray, ndarray)):\n arrs = jax.vmap(lambda x: atleast_2d(x).T)(tup) if tup.ndim < 3 else tup\n else:\n arrs = [atleast_2d(arr).T if arr.ndim < 2 else arr for arr in map(asarray, tup)]\n return concatenate(arrs, 1)\n\n\n@_wraps(np.choose, skip_params=['out'])\ndef choose(a, choices, out=None, mode='raise'):\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.choose is not supported.\")\n _check_arraylike('choose', a, *choices)\n if not issubdtype(_dtype(a), integer):\n raise ValueError(\"`a` array must be integer typed\")\n N = len(choices)\n\n if mode == 'raise':\n a = core.concrete_or_error(asarray, a,\n \"The error occurred because jnp.choose was jit-compiled\"\n \" with mode='raise'. Use mode='wrap' or mode='clip' instead.\")\n if any((a < 0) | (a >= N)):\n raise ValueError(\"invalid entry in choice array\")\n elif mode == 'wrap':\n a = a % N\n elif mode == 'clip':\n a = clip(a, 0, N - 1)\n else:\n raise ValueError(f\"mode={mode!r} not understood. Must be 'raise', 'wrap', or 'clip'\")\n\n a, *choices = broadcast_arrays(a, *choices)\n return array(choices)[(a,) + indices(a.shape, sparse=True)]\n\n\ndef _atleast_nd(x, n):\n m = ndim(x)\n return lax.broadcast(x, (1,) * (n - m)) if m < n else x\n\ndef _block(xs):\n if isinstance(xs, tuple):\n raise ValueError(\"jax.numpy.block does not allow tuples, got {}\"\n .format(xs))\n elif isinstance(xs, list):\n if len(xs) == 0:\n raise ValueError(\"jax.numpy.block does not allow empty list arguments\")\n xs, depths = unzip2([_block(x) for x in xs])\n if _any(d != depths[0] for d in depths[1:]):\n raise ValueError(\"Mismatched list depths in jax.numpy.block\")\n rank = _max(depths[0], _max(ndim(x) for x in xs))\n xs = [_atleast_nd(x, rank) for x in xs]\n return concatenate(xs, axis=-depths[0]), depths[0] + 1\n else:\n return asarray(xs), 1\n\n@_wraps(np.block)\n@jit\ndef block(arrays):\n out, _ = _block(arrays)\n return out\n\n@_wraps(np.atleast_1d, update_doc=False, lax_description=_ARRAY_VIEW_DOC)\n@jit\ndef atleast_1d(*arys):\n if len(arys) == 1:\n arr = asarray(arys[0])\n return arr if ndim(arr) >= 1 else reshape(arr, -1)\n else:\n return [atleast_1d(arr) for arr in arys]\n\n\n@_wraps(np.atleast_2d, update_doc=False, lax_description=_ARRAY_VIEW_DOC)\n@jit\ndef atleast_2d(*arys):\n if len(arys) == 1:\n arr = asarray(arys[0])\n if ndim(arr) >= 2:\n return arr\n elif ndim(arr) == 1:\n return expand_dims(arr, axis=0)\n else:\n return expand_dims(arr, axis=(0, 1))\n else:\n return [atleast_2d(arr) for arr in arys]\n\n\n@_wraps(np.atleast_3d, update_doc=False, lax_description=_ARRAY_VIEW_DOC)\n@jit\ndef atleast_3d(*arys):\n if len(arys) == 1:\n arr = asarray(arys[0])\n if ndim(arr) == 0:\n arr = expand_dims(arr, axis=(0, 1, 2))\n elif ndim(arr) == 1:\n arr = expand_dims(arr, axis=(0, 2))\n elif ndim(arr) == 2:\n arr = expand_dims(arr, axis=2)\n return arr\n else:\n return [atleast_3d(arr) for arr in arys]\n\n\n_ARRAY_DOC = \"\"\"\nThis function will create arrays on JAX's default device. For control of the\ndevice placement of data, see :func:`jax.device_put`. More information is\navailable in the JAX FAQ at :ref:`faq-data-placement` (full FAQ at\nhttps://jax.readthedocs.io/en/latest/faq.html).\n\"\"\"\n\n@_wraps(np.array, lax_description=_ARRAY_DOC)\ndef array(object, dtype=None, copy=True, order=\"K\", ndmin=0):\n if order is not None and order != \"K\":\n raise NotImplementedError(\"Only implemented for order='K'\")\n\n # check if the given dtype is compatible with JAX\n lax._check_user_dtype_supported(dtype, \"array\")\n\n # Here we make a judgment call: we only return a weakly-typed array when the\n # input object itself is weakly typed. That ensures asarray(x) is a no-op whenever\n # x is weak, but avoids introducing weak types with something like array([1, 2, 3])\n weak_type = dtype is None and dtypes.is_weakly_typed(object)\n\n # For Python scalar literals, call coerce_to_array to catch any overflow errors.\n # We don't use dtypes.is_python_scalar because we don't want this triggering for\n # traced values. We do this here because it matters whether or not dtype is None.\n # We don't assign the result because we want the raw object to be used for type\n # inference below.\n if isinstance(object, (bool, int, float, complex)):\n _ = dtypes.coerce_to_array(object, dtype)\n\n leaves = tree_leaves(object)\n if dtype is None:\n # Use lattice_result_type rather than result_type to avoid canonicalization.\n # Otherwise, weakly-typed inputs would have their dtypes canonicalized.\n try:\n dtype = dtypes._lattice_result_type(*leaves)[0] if leaves else dtypes.float_\n except TypeError:\n # This happens if, e.g. one of the entries is a memoryview object.\n # This is rare, so we only handle it if the normal path fails.\n leaves = [_convert_to_array_if_dtype_fails(leaf) for leaf in leaves]\n dtype = dtypes._lattice_result_type(*leaves)[0]\n\n if not weak_type:\n dtype = dtypes.canonicalize_dtype(dtype)\n\n # We can't use the ndarray class because we need to handle internal buffers\n # (See https://github.com/google/jax/issues/8950)\n ndarray_types = (device_array.DeviceArray, core.Tracer)\n\n if not _any(isinstance(leaf, ndarray_types) for leaf in leaves):\n # TODO(jakevdp): falling back to numpy here fails to overflow for lists containing\n # large integers; see discussion in https://github.com/google/jax/pull/6047.\n # More correct would be to call coerce_to_array on each leaf, but this may have\n # performance implications.\n out = np.array(object, dtype=dtype, ndmin=ndmin, copy=False)\n elif isinstance(object, ndarray_types):\n if object.aval is None:\n # object is a raw buffer; convert to device array on its current device.\n aval = ShapedArray(object.xla_shape().dimensions(), object.dtype,\n weak_type=bool(getattr(object, \"weak_type\", False)))\n object = device_array.make_device_array(aval, object.device(), object)\n out = _array_copy(object) if copy else object\n elif isinstance(object, (list, tuple)):\n if object:\n out = stack([asarray(elt, dtype=dtype) for elt in object])\n else:\n out = np.array([], dtype=dtype)\n else:\n try:\n view = memoryview(object)\n except TypeError:\n pass # `object` does not support the buffer interface.\n else:\n return array(np.asarray(view), dtype, copy, ndmin=ndmin)\n\n raise TypeError(\"Unexpected input type for array: {}\".format(type(object)))\n\n out = lax._convert_element_type(out, dtype, weak_type=weak_type)\n if ndmin > ndim(out):\n out = lax.expand_dims(out, range(ndmin - ndim(out)))\n return out\n\n\ndef _convert_to_array_if_dtype_fails(x):\n try:\n dtypes.dtype(x)\n except TypeError:\n return np.asarray(x)\n else:\n return x\n\n\n@_wraps(np.asarray, lax_description=_ARRAY_DOC)\ndef asarray(a, dtype=None, order=None):\n lax._check_user_dtype_supported(dtype, \"asarray\")\n dtype = dtypes.canonicalize_dtype(dtype) if dtype is not None else dtype\n return array(a, dtype=dtype, copy=False, order=order)\n\n\n@_wraps(np.zeros_like)\ndef zeros_like(a, dtype=None, shape=None):\n _check_arraylike(\"zeros_like\", a)\n lax._check_user_dtype_supported(dtype, \"zeros_like\")\n if np.isscalar(shape):\n shape = (shape,)\n return lax.full_like(a, 0, dtype, shape)\n\n\n@_wraps(np.ones_like)\ndef ones_like(a, dtype=None, shape=None):\n _check_arraylike(\"ones_like\", a)\n lax._check_user_dtype_supported(dtype, \"ones_like\")\n if np.isscalar(shape):\n shape = (shape,)\n return lax.full_like(a, 1, dtype, shape)\n\n\n@_wraps(np.full)\ndef full(shape, fill_value, dtype=None):\n lax._check_user_dtype_supported(dtype, \"full\")\n _check_arraylike(\"full\", fill_value)\n if ndim(fill_value) == 0:\n shape = (shape,) if ndim(shape) == 0 else shape\n return lax.full(shape, fill_value, dtype)\n else:\n return broadcast_to(asarray(fill_value, dtype=dtype), shape)\n\n\n@_wraps(np.full_like)\ndef full_like(a, fill_value, dtype=None, shape=None):\n lax._check_user_dtype_supported(dtype, \"full_like\")\n _check_arraylike(\"full_like\", a, fill_value)\n if shape is not None:\n shape = (shape,) if ndim(shape) == 0 else shape\n if ndim(fill_value) == 0:\n return lax.full_like(a, fill_value, dtype, shape)\n else:\n shape = np.shape(a) if shape is None else shape\n dtype = result_type(a) if dtype is None else dtype\n return broadcast_to(asarray(fill_value, dtype=dtype), shape)\n\n\n@_wraps(np.zeros)\ndef zeros(shape, dtype=None):\n if isinstance(shape, types.GeneratorType):\n raise TypeError(\"expected sequence object with len >= 0 or a single integer\")\n lax._check_user_dtype_supported(dtype, \"zeros\")\n shape = canonicalize_shape((shape,) if ndim(shape) == 0 else shape)\n return lax.full(shape, 0, _jnp_dtype(dtype))\n\n@_wraps(np.ones)\ndef ones(shape, dtype=None):\n if isinstance(shape, types.GeneratorType):\n raise TypeError(\"expected sequence object with len >= 0 or a single integer\")\n lax._check_user_dtype_supported(dtype, \"ones\")\n shape = canonicalize_shape((shape,) if ndim(shape) == 0 else shape)\n return lax.full(shape, 1, _jnp_dtype(dtype))\n\n\n@_wraps(np.array_equal)\ndef array_equal(a1, a2, equal_nan=False):\n try:\n a1, a2 = asarray(a1), asarray(a2)\n except Exception:\n return False\n if shape(a1) != shape(a2):\n return False\n eq = asarray(a1 == a2)\n if equal_nan:\n eq = logical_or(eq, logical_and(isnan(a1), isnan(a2)))\n return all(eq)\n\n\n@_wraps(np.array_equiv)\ndef array_equiv(a1, a2):\n try:\n a1, a2 = asarray(a1), asarray(a2)\n except Exception:\n return False\n try:\n eq = equal(a1, a2)\n except ValueError:\n # shapes are not broadcastable\n return False\n return all(eq)\n\n\n# We can't create uninitialized arrays in XLA; use zeros for empty.\nempty_like = zeros_like\nempty = zeros\n\n\n@_wraps(np.eye)\ndef eye(N, M=None, k=0, dtype=None):\n lax._check_user_dtype_supported(dtype, \"eye\")\n N = core.canonicalize_dim(N, \"'N' argument of jnp.eye()\")\n M = N if M is None else core.canonicalize_dim(M, \"'M' argument of jnp.eye()\")\n if N < 0 or M < 0:\n raise ValueError(f\"negative dimensions are not allowed, got {N} and {M}\")\n k = operator.index(k)\n return lax._eye(_jnp_dtype(dtype), (N, M), k)\n\n\n@_wraps(np.identity)\ndef identity(n, dtype=None):\n lax._check_user_dtype_supported(dtype, \"identity\")\n return eye(n, dtype=dtype)\n\n\n@_wraps(np.arange)\ndef arange(start: core.DimSize, stop: Optional[core.DimSize]=None,\n step: Optional[core.DimSize]=None, dtype=None):\n lax._check_user_dtype_supported(dtype, \"arange\")\n require = partial(core.concrete_or_error, None)\n msg = \"It arose in jax.numpy.arange argument `{}`.\".format\n if _any(core.is_special_dim_size(d) for d in (start, stop, step)):\n if stop is not None or step is not None:\n raise ValueError(\n \"jax.numpy.arange supports non-constant arguments only in single-argument form. \"\n f\"Found jax.numpy.arange(start={start}, stop={stop}, step={step})\")\n return lax.iota(int_, start)\n if dtype is None:\n dtype = result_type(start, *(x for x in [stop, step] if x is not None))\n dtype = _jnp_dtype(dtype)\n if stop is None and step is None:\n start = require(start, msg(\"stop\"))\n start = np.ceil(start).astype(int)\n return lax.iota(dtype, start)\n else:\n start = require(start, msg(\"start\"))\n stop = None if stop is None else require(stop, msg(\"stop\"))\n step = None if step is None else require(step, msg(\"step\"))\n return array(np.arange(start, stop=stop, step=step, dtype=dtype))\n\n\ndef _wrap_numpy_nullary_function(f):\n \"\"\"Adapts `f` to return a DeviceArray instead of an np.ndarray.\n\n `f` cannot have any non-static array arguments.\n \"\"\"\n @_wraps(f, update_doc=False)\n def wrapper(*args, **kwargs):\n args = [core.concrete_or_error(None, arg, f\"the error occured in argument {i} jnp.{f.__name__}()\")\n for i, arg in enumerate(args)]\n kwargs = {key: core.concrete_or_error(None, val, f\"the error occured in argument '{key}' jnp.{f.__name__}()\")\n for key, val in kwargs.items()}\n return asarray(f(*args, **kwargs))\n return wrapper\n\n\n@_wraps(np.linspace)\ndef linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,\n axis: int = 0):\n num = core.concrete_or_error(operator.index, num, \"'num' argument of jnp.linspace\")\n axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.linspace\")\n return _linspace(start, stop, int(num), endpoint, retstep, dtype,\n operator.index(axis))\n\n@partial(jit, static_argnames=('num', 'endpoint', 'retstep', 'dtype', 'axis'))\ndef _linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,\n axis: int = 0):\n \"\"\"Implementation of linspace differentiable in start and stop args.\"\"\"\n lax._check_user_dtype_supported(dtype, \"linspace\")\n if num < 0:\n raise ValueError(f\"Number of samples, {num}, must be non-negative.\")\n _check_arraylike(\"linspace\", start, stop)\n\n if dtype is None:\n dtype = result_type(start, stop, dtypes.canonicalize_dtype(float_))\n dtype = _jnp_dtype(dtype)\n computation_dtype = promote_types(dtype, dtypes.canonicalize_dtype(float_))\n start = asarray(start, dtype=computation_dtype)\n stop = asarray(stop, dtype=computation_dtype)\n\n bounds_shape = list(lax.broadcast_shapes(shape(start), shape(stop)))\n broadcast_start = broadcast_to(start, bounds_shape)\n broadcast_stop = broadcast_to(stop, bounds_shape)\n axis = len(bounds_shape) + axis + 1 if axis < 0 else axis\n bounds_shape.insert(axis, 1)\n div = (num - 1) if endpoint else num\n if num > 1:\n delta = lax.convert_element_type(stop - start, computation_dtype) / div\n iota_shape = [1,] * len(bounds_shape)\n iota_shape[axis] = div\n # This approach recovers the endpoints with float32 arithmetic,\n # but can lead to rounding errors for integer outputs.\n real_dtype = finfo(computation_dtype).dtype\n step = reshape(lax.iota(real_dtype, div), iota_shape) / div\n out = (reshape(broadcast_start, bounds_shape) * (1 - step) +\n reshape(broadcast_stop, bounds_shape) * step)\n\n if endpoint:\n out = lax.concatenate([out, lax.expand_dims(broadcast_stop, (axis,))],\n _canonicalize_axis(axis, out.ndim))\n\n elif num == 1:\n delta = nan if endpoint else stop - start\n out = reshape(broadcast_start, bounds_shape)\n else: # num == 0 degenerate case, match numpy behavior\n empty_shape = list(lax.broadcast_shapes(shape(start), shape(stop)))\n empty_shape.insert(axis, 0)\n delta = nan\n out = reshape(array([], dtype=dtype), empty_shape)\n\n if issubdtype(dtype, integer) and not issubdtype(out.dtype, integer):\n out = lax.floor(out)\n\n if retstep:\n return lax.convert_element_type(out, dtype), delta\n else:\n return lax.convert_element_type(out, dtype)\n\n\n@_wraps(np.logspace)\ndef logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None,\n axis: int = 0):\n num = core.concrete_or_error(operator.index, num, \"'num' argument of jnp.logspace\")\n axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.logspace\")\n return _logspace(start, stop, int(num), endpoint, base, dtype,\n operator.index(axis))\n\n@partial(jit, static_argnames=('num', 'endpoint', 'dtype', 'axis'))\ndef _logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None,\n axis: int = 0):\n \"\"\"Implementation of logspace differentiable in start and stop args.\"\"\"\n lax._check_user_dtype_supported(dtype, \"logspace\")\n if dtype is None:\n dtype = result_type(start, stop, dtypes.canonicalize_dtype(float_))\n dtype = _jnp_dtype(dtype)\n computation_dtype = promote_types(dtype, dtypes.canonicalize_dtype(float_))\n _check_arraylike(\"logspace\", start, stop)\n start = asarray(start, dtype=computation_dtype)\n stop = asarray(stop, dtype=computation_dtype)\n lin = linspace(start, stop, num,\n endpoint=endpoint, retstep=False, dtype=None, axis=axis)\n return lax.convert_element_type(power(base, lin), dtype)\n\n\n@_wraps(np.geomspace)\ndef geomspace(start, stop, num=50, endpoint=True, dtype=None, axis: int = 0):\n num = core.concrete_or_error(operator.index, num, \"'num' argument of jnp.geomspace\")\n axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.geomspace\")\n return _geomspace(start, stop, int(num), endpoint, dtype,\n operator.index(axis))\n\n@partial(jit, static_argnames=('num', 'endpoint', 'dtype', 'axis'))\ndef _geomspace(start, stop, num=50, endpoint=True, dtype=None, axis: int = 0):\n \"\"\"Implementation of geomspace differentiable in start and stop args.\"\"\"\n lax._check_user_dtype_supported(dtype, \"geomspace\")\n if dtype is None:\n dtype = result_type(start, stop, dtypes.canonicalize_dtype(float_))\n dtype = _jnp_dtype(dtype)\n computation_dtype = promote_types(dtype, dtypes.canonicalize_dtype(float_))\n _check_arraylike(\"geomspace\", start, stop)\n start = asarray(start, dtype=computation_dtype)\n stop = asarray(stop, dtype=computation_dtype)\n # follow the numpy geomspace convention for negative and complex endpoints\n signflip = 1 - (1 - sign(real(start))) * (1 - sign(real(stop))) // 2\n res = signflip * logspace(log10(signflip * start),\n log10(signflip * stop), num,\n endpoint=endpoint, base=10.0,\n dtype=computation_dtype, axis=0)\n if axis != 0:\n res = moveaxis(res, 0, axis)\n return lax.convert_element_type(res, dtype)\n\n\n@_wraps(np.meshgrid, lax_description=_ARRAY_VIEW_DOC)\ndef meshgrid(*xi, copy=True, sparse=False, indexing='xy'):\n _check_arraylike(\"meshgrid\", *xi)\n args = [asarray(x) for x in xi]\n if not copy:\n raise ValueError(\"jax.numpy.meshgrid only supports copy=True\")\n if indexing not in [\"xy\", \"ij\"]:\n raise ValueError(f\"Valid values for indexing are 'xy' and 'ij', got {indexing}\")\n if _any(a.ndim != 1 for a in args):\n raise ValueError(\"Arguments to jax.numpy.meshgrid must be 1D, got shapes \"\n f\"{[a.shape for a in args]}\")\n if indexing == \"xy\" and len(args) >= 2:\n args[0], args[1] = args[1], args[0]\n shape = [1 if sparse else a.shape[0] for a in args]\n _a_shape = lambda i, a: [*shape[:i], a.shape[0], *shape[i + 1:]] if sparse else shape\n output = [lax.broadcast_in_dim(a, _a_shape(i, a), (i,)) for i, a, in enumerate(args)]\n if indexing == \"xy\" and len(args) >= 2:\n output[0], output[1] = output[1], output[0]\n return output\n\n\ndef _make_1d_grid_from_slice(s: slice, op_name: str):\n start = core.concrete_or_error(None, s.start,\n f\"slice start of jnp.{op_name}\") or 0\n stop = core.concrete_or_error(None, s.stop,\n f\"slice stop of jnp.{op_name}\")\n step = core.concrete_or_error(None, s.step,\n f\"slice step of jnp.{op_name}\") or 1\n if np.iscomplex(step):\n newobj = linspace(start, stop, int(_abs(step)))\n else:\n newobj = arange(start, stop, step)\n\n return newobj\n\n\nclass _IndexGrid:\n def __getitem__(self, key):\n single_slice = isinstance(key, slice)\n if single_slice:\n key = (key,)\n output = []\n for k in key:\n output.append(_make_1d_grid_from_slice(k, op_name=self.op_name))\n if single_slice:\n return output[0]\n output = meshgrid(*output, indexing='ij', sparse=self.sparse)\n return output if self.sparse else stack(output, 0)\n\n\nclass _Mgrid(_IndexGrid):\n \"\"\"Return dense multi-dimensional \"meshgrid\".\n\n LAX-backend implementation of :obj:`numpy.mgrid`. This is a convenience wrapper for\n functionality provided by :func:`jax.numpy.meshgrid` with ``sparse=False``.\n\n See Also:\n jnp.ogrid: open/sparse version of jnp.mgrid\n\n Examples:\n Pass ``[start:stop:step]`` to generate values similar to :func:`jax.numpy.arange`:\n\n >>> jnp.mgrid[0:4:1]\n DeviceArray([0, 1, 2, 3], dtype=int32)\n\n Passing an imaginary step generates values similar to :func:`jax.numpy.linspace`:\n\n >>> jnp.mgrid[0:1:4j]\n DeviceArray([0. , 0.33333334, 0.6666667 , 1. ], dtype=float32)\n\n Multiple slices can be used to create broadcasted grids of indices:\n\n >>> jnp.mgrid[:2, :3]\n DeviceArray([[[0, 0, 0],\n [1, 1, 1]],\n [[0, 1, 2],\n [0, 1, 2]]], dtype=int32)\n \"\"\"\n sparse = False\n op_name = \"mgrid\"\n\nmgrid = _Mgrid()\n\n\nclass _Ogrid(_IndexGrid):\n \"\"\"Return open multi-dimensional \"meshgrid\".\n\n LAX-backend implementation of :obj:`numpy.ogrid`. This is a convenience wrapper for\n functionality provided by :func:`jax.numpy.meshgrid` with ``sparse=True``.\n\n See Also:\n jnp.mgrid: dense version of jnp.ogrid\n\n Examples:\n Pass ``[start:stop:step]`` to generate values similar to :func:`jax.numpy.arange`:\n\n >>> jnp.ogrid[0:4:1]\n DeviceArray([0, 1, 2, 3], dtype=int32)\n\n Passing an imaginary step generates values similar to :func:`jax.numpy.linspace`:\n\n >>> jnp.ogrid[0:1:4j]\n DeviceArray([0. , 0.33333334, 0.6666667 , 1. ], dtype=float32)\n\n Multiple slices can be used to create sparse grids of indices:\n\n >>> jnp.ogrid[:2, :3]\n [DeviceArray([[0],\n [1]], dtype=int32),\n DeviceArray([[0, 1, 2]], dtype=int32)]\n \"\"\"\n sparse = True\n op_name = \"ogrid\"\n\n\nogrid = _Ogrid()\n\n\nclass _AxisConcat:\n \"\"\"Concatenates slices, scalars and array-like objects along a given axis.\"\"\"\n def __getitem__(self, key):\n if not isinstance(key, tuple):\n key = (key,)\n\n params = [self.axis, self.ndmin, self.trans1d, -1]\n\n if isinstance(key[0], str):\n # split off the directive\n directive, *key = key\n # check two special cases: matrix directives\n if directive == \"r\":\n params[-1] = 0\n elif directive == \"c\":\n params[-1] = 1\n else:\n vec = directive.split(\",\")\n k = len(vec)\n if k < 4:\n vec += params[k:]\n else:\n # ignore everything after the first three comma-separated ints\n vec = vec[:3] + params[-1]\n try:\n params = list(map(int, vec))\n except ValueError as err:\n raise ValueError(\n \"could not understand directive {!r}\".format(directive)\n ) from err\n\n axis, ndmin, trans1d, matrix = params\n\n output = []\n for item in key:\n if isinstance(item, slice):\n newobj = _make_1d_grid_from_slice(item, op_name=self.op_name)\n elif isinstance(item, str):\n raise ValueError(\"string directive must be placed at the beginning\")\n else:\n newobj = item\n\n newobj = array(newobj, copy=False, ndmin=ndmin)\n\n if trans1d != -1 and ndmin - ndim(item) > 0:\n shape_obj = list(range(ndmin))\n # Calculate number of left shifts, with overflow protection by mod\n num_lshifts = ndmin - _abs(ndmin + trans1d + 1) % ndmin\n shape_obj = tuple(shape_obj[num_lshifts:] + shape_obj[:num_lshifts])\n\n newobj = transpose(newobj, shape_obj)\n\n output.append(newobj)\n\n res = concatenate(tuple(output), axis=axis)\n\n if matrix != -1 and res.ndim == 1:\n # insert 2nd dim at axis 0 or 1\n res = expand_dims(res, matrix)\n\n return res\n\n def __len__(self):\n return 0\n\n\nclass RClass(_AxisConcat):\n \"\"\"Concatenate slices, scalars and array-like objects along the first axis.\n\n LAX-backend implementation of :obj:`numpy.r_`.\n\n See Also:\n ``jnp.c_``: Concatenates slices, scalars and array-like objects along the last axis.\n\n Examples:\n Passing slices in the form ``[start:stop:step]`` generates ``jnp.arange`` objects:\n\n >>> jnp.r_[-1:5:1, 0, 0, jnp.array([1,2,3])]\n DeviceArray([-1, 0, 1, 2, 3, 4, 0, 0, 1, 2, 3], dtype=int32)\n\n An imaginary value for ``step`` will create a ``jnp.linspace`` object instead,\n which includes the right endpoint:\n\n >>> jnp.r_[-1:1:6j, 0, jnp.array([1,2,3])]\n DeviceArray([-1. , -0.6 , -0.20000002, 0.20000005,\n 0.6 , 1. , 0. , 1. ,\n 2. , 3. ], dtype=float32)\n\n Use a string directive of the form ``\"axis,dims,trans1d\"`` as the first argument to\n specify concatenation axis, minimum number of dimensions, and the position of the\n upgraded array's original dimensions in the resulting array's shape tuple:\n\n >>> jnp.r_['0,2', [1,2,3], [4,5,6]] # concatenate along first axis, 2D output\n DeviceArray([[1, 2, 3],\n [4, 5, 6]], dtype=int32)\n\n >>> jnp.r_['0,2,0', [1,2,3], [4,5,6]] # push last input axis to the front\n DeviceArray([[1],\n [2],\n [3],\n [4],\n [5],\n [6]], dtype=int32)\n\n Negative values for ``trans1d`` offset the last axis towards the start\n of the shape tuple:\n\n >>> jnp.r_['0,2,-2', [1,2,3], [4,5,6]]\n DeviceArray([[1],\n [2],\n [3],\n [4],\n [5],\n [6]], dtype=int32)\n\n Use the special directives ``\"r\"`` or ``\"c\"`` as the first argument on flat inputs\n to create an array with an extra row or column axis, respectively:\n\n >>> jnp.r_['r',[1,2,3], [4,5,6]]\n DeviceArray([[1, 2, 3, 4, 5, 6]], dtype=int32)\n\n >>> jnp.r_['c',[1,2,3], [4,5,6]]\n DeviceArray([[1],\n [2],\n [3],\n [4],\n [5],\n [6]], dtype=int32)\n\n For higher-dimensional inputs (``dim >= 2``), both directives ``\"r\"`` and ``\"c\"``\n give the same result.\n \"\"\"\n axis = 0\n ndmin = 1\n trans1d = -1\n op_name = \"r_\"\n\n\nr_ = RClass()\n\n\nclass CClass(_AxisConcat):\n \"\"\"Concatenate slices, scalars and array-like objects along the last axis.\n\n LAX-backend implementation of :obj:`numpy.c_`.\n\n See Also:\n ``jnp.r_``: Concatenates slices, scalars and array-like objects along the first axis.\n\n Examples:\n\n >>> a = jnp.arange(6).reshape((2,3))\n >>> jnp.c_[a,a]\n DeviceArray([[0, 1, 2, 0, 1, 2],\n [3, 4, 5, 3, 4, 5]], dtype=int32)\n\n Use a string directive of the form ``\"axis:dims:trans1d\"`` as the first argument to specify\n concatenation axis, minimum number of dimensions, and the position of the upgraded array's\n original dimensions in the resulting array's shape tuple:\n\n >>> jnp.c_['0,2', [1,2,3], [4,5,6]]\n DeviceArray([[1],\n [2],\n [3],\n [4],\n [5],\n [6]], dtype=int32)\n\n >>> jnp.c_['0,2,-1', [1,2,3], [4,5,6]]\n DeviceArray([[1, 2, 3],\n [4, 5, 6]], dtype=int32)\n\n Use the special directives ``\"r\"`` or ``\"c\"`` as the first argument on flat inputs\n to create an array with inputs stacked along the last axis:\n\n >>> jnp.c_['r',[1,2,3], [4,5,6]]\n DeviceArray([[1, 4],\n [2, 5],\n [3, 6]], dtype=int32)\n \"\"\"\n axis = -1\n ndmin = 2\n trans1d = 0\n op_name = \"c_\"\n\nc_ = CClass()\n\ns_ = np.s_\n\nindex_exp = np.index_exp\n\n@_wraps(np.i0)\n@jit\ndef i0(x):\n x_orig = x\n x, = _promote_args_inexact(\"i0\", x)\n if not issubdtype(x.dtype, np.floating):\n raise ValueError(f\"Unsupported input type to jax.numpy.i0: {_dtype(x_orig)}\")\n x = lax.abs(x)\n return lax.mul(lax.exp(x), lax.bessel_i0e(x))\n\n\n@_wraps(np.ix_)\ndef ix_(*args):\n _check_arraylike(\"ix\", *args)\n n = len(args)\n output = []\n for i, a in enumerate(args):\n a = asarray(a)\n if len(a.shape) != 1:\n msg = \"Arguments to jax.numpy.ix_ must be 1-dimensional, got shape {}\"\n raise ValueError(msg.format(a.shape))\n if _dtype(a) == bool_:\n raise NotImplementedError(\n \"Boolean arguments to jax.numpy.ix_ are not implemented\")\n shape = [1] * n\n shape[i] = a.shape[0]\n if a.size == 0:\n # Numpy uses an integer index type for empty arrays.\n output.append(lax.full(shape, np.zeros((), np.intp)))\n else:\n output.append(lax.broadcast_in_dim(a, shape, (i,)))\n return tuple(output)\n\n\n@_wraps(np.indices)\ndef indices(dimensions, dtype=int32, sparse=False):\n dimensions = tuple(\n core.concrete_or_error(int, d, \"dimensions argument of jnp.indices\")\n for d in dimensions)\n N = len(dimensions)\n output = []\n s = dimensions\n for i, dim in enumerate(dimensions):\n idx = lax.iota(dtype, dim)\n if sparse:\n s = (1,)*i + (dim,) + (1,)*(N - i - 1)\n output.append(lax.broadcast_in_dim(idx, s, (i,)))\n if sparse:\n return tuple(output)\n return stack(output, 0) if output else array([], dtype=dtype)\n\n\n_TOTAL_REPEAT_LENGTH_DOC = \"\"\"\\\nJax adds the optional `total_repeat_length` parameter which specifies the total\nnumber of repeat, and defaults to sum(repeats). It must be specified for repeat\nto be compilable. If `sum(repeats)` is larger than the specified\n`total_repeat_length` the remaining values will be discarded. In the case of\n`sum(repeats)` being smaller than the specified target length, the final value\nwill be repeated.\n\"\"\"\n\n\n@_wraps(np.repeat, lax_description=_TOTAL_REPEAT_LENGTH_DOC)\ndef repeat(a, repeats, axis: Optional[int] = None, *, total_repeat_length=None):\n _check_arraylike(\"repeat\", a, repeats)\n\n if axis is None:\n a = ravel(a)\n axis = 0\n\n axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.repeat()\")\n assert isinstance(axis, int) # to appease mypy\n\n # If total_repeat_length is not given, can't compile, use a default.\n if total_repeat_length is None:\n repeats = core.concrete_or_error(np.array, repeats,\n \"When jit-compiling jnp.repeat, the total number of repeats must be static. \"\n \"To fix this, either specify a static value for `repeats`, or pass a static \"\n \"value to `total_repeat_length`.\")\n\n # Fast path for when repeats is a scalar.\n if np.ndim(repeats) == 0 and ndim(a) != 0:\n input_shape = a.shape\n aux_axis = axis if axis < 0 else axis + 1\n a = expand_dims(a, aux_axis)\n reps = [1] * len(a.shape)\n reps[aux_axis] = repeats\n a = tile(a, reps)\n result_shape = list(input_shape)\n result_shape[axis] *= repeats\n return reshape(a, result_shape)\n\n repeats = np.ravel(repeats)\n if ndim(a) != 0:\n repeats = np.broadcast_to(repeats, [a.shape[axis]])\n total_repeat_length = np.sum(repeats)\n else:\n repeats = ravel(repeats)\n if ndim(a) != 0:\n repeats = broadcast_to(repeats, [a.shape[axis]])\n\n # Special case when a is a scalar.\n if ndim(a) == 0:\n if repeats.shape == (1,):\n return full([total_repeat_length], a)\n else:\n raise ValueError('`repeat` with a scalar parameter `a` is only '\n 'implemented for scalar values of the parameter `repeats`.')\n\n # Special case if total_repeat_length is zero.\n if total_repeat_length == 0:\n result_shape = list(a.shape)\n result_shape[axis] = 0\n return reshape(array([], dtype=a.dtype), result_shape)\n\n # If repeats is on a zero sized axis, then return the array.\n if a.shape[axis] == 0:\n return a\n\n # This implementation of repeat avoid having to instantiate a large.\n # intermediate tensor.\n\n # Modify repeats from e.g. [1,2,0,5] -> [0,1,2,0] for exclusive repeat.\n exclusive_repeats = roll(repeats, shift=1).at[0].set(0)\n # Cumsum to get indices of new number in repeated tensor, e.g. [0, 1, 3, 3]\n scatter_indices = cumsum(exclusive_repeats)\n # Scatter these onto a zero buffer, e.g. [1,1,0,2,0,0,0,0]\n block_split_indicators = zeros([total_repeat_length], dtype=int32)\n block_split_indicators = block_split_indicators.at[scatter_indices].add(1)\n # Cumsum again to get scatter indices for repeat, e.g. [0,1,1,3,3,3,3,3]\n gather_indices = cumsum(block_split_indicators) - 1\n return take(a, gather_indices, axis=axis)\n\n\n@_wraps(np.tri)\ndef tri(N, M=None, k=0, dtype=None):\n lax._check_user_dtype_supported(dtype, \"tri\")\n M = M if M is not None else N\n dtype = dtype or float32\n return lax._tri(dtype, (N, M), k)\n\n\n@_wraps(np.tril)\n@partial(jit, static_argnames=('k',))\ndef tril(m, k=0):\n _check_arraylike(\"tril\", m)\n m_shape = shape(m)\n if len(m_shape) < 2:\n raise ValueError(\"Argument to jax.numpy.tril must be at least 2D\")\n mask = tri(*m_shape[-2:], k=k, dtype=bool)\n return lax.select(lax.broadcast(mask, m_shape[:-2]), m, zeros_like(m))\n\n\n@_wraps(np.triu, update_doc=False)\n@partial(jit, static_argnames=('k',))\ndef triu(m, k=0):\n _check_arraylike(\"triu\", m)\n m_shape = shape(m)\n if len(m_shape) < 2:\n raise ValueError(\"Argument to jax.numpy.triu must be at least 2D\")\n mask = tri(*m_shape[-2:], k=k - 1, dtype=bool)\n return lax.select(lax.broadcast(mask, m_shape[:-2]), zeros_like(m), m)\n\n\n@_wraps(np.trace, skip_params=['out'])\n@partial(jit, static_argnames=('offset', 'axis1', 'axis2', 'dtype'))\ndef trace(a, offset=0, axis1: int = 0, axis2: int = 1, dtype=None, out=None):\n _check_arraylike(\"trace\", a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.trace is not supported.\")\n lax._check_user_dtype_supported(dtype, \"trace\")\n\n axis1 = _canonicalize_axis(axis1, ndim(a))\n axis2 = _canonicalize_axis(axis2, ndim(a))\n\n a_shape = shape(a)\n if dtype is None:\n dtype = _dtype(a)\n if issubdtype(dtype, integer):\n default_int = dtypes.canonicalize_dtype(np.int_)\n if iinfo(dtype).bits < iinfo(default_int).bits:\n dtype = default_int\n\n # Move the axis? dimensions to the end.\n perm = [i for i in range(len(a_shape)) if i != axis1 and i != axis2]\n perm = perm + [axis1, axis2]\n a = lax.transpose(a, perm)\n\n # Mask out the diagonal and reduce.\n a = where(eye(a_shape[axis1], a_shape[axis2], k=offset, dtype=bool),\n a, zeros_like(a))\n return sum(a, axis=(-2, -1), dtype=dtype)\n\n\ndef _wrap_indices_function(f):\n @_wraps(f, update_doc=False)\n def wrapper(*args, **kwargs):\n args = [core.concrete_or_error(\n None, arg, f\"argument {i} of jnp.{f.__name__}()\")\n for i, arg in enumerate(args)]\n kwargs = {key: core.concrete_or_error(\n None, val, f\"argument '{key}' of jnp.{f.__name__}()\")\n for key, val in kwargs.items()}\n return tuple(asarray(x) for x in f(*args, **kwargs))\n return wrapper\n\ntril_indices = _wrap_indices_function(np.tril_indices)\ntriu_indices = _wrap_indices_function(np.triu_indices)\nmask_indices = _wrap_indices_function(np.mask_indices)\n\n\n@_wraps(np.triu_indices_from)\ndef triu_indices_from(arr, k=0):\n return triu_indices(arr.shape[-2], k=k, m=arr.shape[-1])\n\n\n@_wraps(np.tril_indices_from)\ndef tril_indices_from(arr, k=0):\n return tril_indices(arr.shape[-2], k=k, m=arr.shape[-1])\n\n\n@_wraps(np.diag_indices)\ndef diag_indices(n, ndim=2):\n n = core.concrete_or_error(operator.index, n, \"'n' argument of jnp.diag_indices()\")\n ndim = core.concrete_or_error(operator.index, ndim, \"'ndim' argument of jnp.diag_indices()\")\n if n < 0:\n raise ValueError(\"n argument to diag_indices must be nonnegative, got {}\"\n .format(n))\n if ndim < 0:\n raise ValueError(\"ndim argument to diag_indices must be nonnegative, got {}\"\n .format(ndim))\n return (lax.iota(int_, n),) * ndim\n\n@_wraps(np.diag_indices_from)\ndef diag_indices_from(arr):\n _check_arraylike(\"diag_indices_from\", arr)\n if not arr.ndim >= 2:\n raise ValueError(\"input array must be at least 2-d\")\n\n if len(set(arr.shape)) != 1:\n raise ValueError(\"All dimensions of input must be of equal length\")\n\n return diag_indices(arr.shape[0], ndim=arr.ndim)\n\n@_wraps(np.diagonal, lax_description=_ARRAY_VIEW_DOC)\n@partial(jit, static_argnames=('offset', 'axis1', 'axis2'))\ndef diagonal(a, offset=0, axis1: int = 0, axis2: int = 1):\n _check_arraylike(\"diagonal\", a)\n a_shape = shape(a)\n a_ndims = len(a_shape)\n offset = core.concrete_or_error(operator.index, offset, \"'offset' argument of jnp.diagonal()\")\n\n # Move the two dimensions to the end.\n axis1 = _canonicalize_axis(axis1, a_ndims)\n axis2 = _canonicalize_axis(axis2, a_ndims)\n perm = [i for i in range(a_ndims) if i != axis1 and i != axis2]\n perm = perm + [axis1, axis2]\n a = lax.transpose(a, perm)\n\n # Mask out the diagonal and reduce over one of the axes\n a = where(eye(a_shape[axis1], a_shape[axis2], k=offset, dtype=bool),\n a, zeros_like(a))\n reduce_axis = -2 if offset < 0 else -1\n d = sum(a, axis=reduce_axis, dtype=_dtype(a))\n\n # Slice out the correct diagonal size.\n diag_size = _max(0, _min(a_shape[axis1] + _min(offset, 0),\n a_shape[axis2] - _max(offset, 0)))\n return lax.slice_in_dim(d, 0, diag_size, axis=-1)\n\n\n@_wraps(np.diag, lax_description=_ARRAY_VIEW_DOC)\ndef diag(v, k=0):\n return _diag(v, int(k))\n\n@partial(jit, static_argnames=('k',))\ndef _diag(v, k):\n _check_arraylike(\"diag\", v)\n v_shape = shape(v)\n if len(v_shape) == 1:\n zero = lambda x: lax.full_like(x, shape=(), fill_value=0)\n n = v_shape[0] + _abs(k)\n v = lax.pad(v, zero(v), ((_max(0, k), _max(0, -k), 0),))\n return where(eye(n, k=k, dtype=bool), v, zeros_like(v))\n elif len(v_shape) == 2:\n return diagonal(v, offset=k)\n else:\n raise ValueError(\"diag input must be 1d or 2d\")\n\n_SCALAR_VALUE_DOC = \"\"\"\\\nThis differs from np.diagflat for some scalar values of v,\njax always returns a two-dimensional array, whereas numpy may\nreturn a scalar depending on the type of v.\n\"\"\"\n\n@_wraps(np.diagflat, lax_description=_SCALAR_VALUE_DOC)\ndef diagflat(v, k=0):\n _check_arraylike(\"diagflat\", v)\n v = ravel(v)\n v_length = len(v)\n adj_length = v_length + _abs(k)\n res = zeros(adj_length*adj_length, dtype=v.dtype)\n i = arange(0, adj_length-_abs(k))\n if (k >= 0):\n fi = i+k+i*adj_length\n else:\n fi = i+(i-k)*adj_length\n res = res.at[fi].set(v)\n res = res.reshape(adj_length, adj_length)\n return res\n\n_POLY_DOC = \"\"\"\\\nThis differs from np.poly when an integer array is given.\nnp.poly returns a result with dtype float64 in this case.\njax returns a result with an inexact type, but not necessarily\nfloat64.\n\nThis also differs from np.poly when the input array strictly\ncontains pairs of complex conjugates, e.g. [1j, -1j, 1-1j, 1+1j].\nnp.poly returns an array with a real dtype in such cases.\njax returns an array with a complex dtype in such cases.\n\"\"\"\n\n@_wraps(np.poly, lax_description=_POLY_DOC)\n@jit\ndef poly(seq_of_zeros):\n _check_arraylike('poly', seq_of_zeros)\n seq_of_zeros, = _promote_dtypes_inexact(seq_of_zeros)\n seq_of_zeros = atleast_1d(seq_of_zeros)\n\n sh = seq_of_zeros.shape\n if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0:\n # import at runtime to avoid circular import\n from jax._src.numpy import linalg\n seq_of_zeros = linalg.eigvals(seq_of_zeros)\n\n if seq_of_zeros.ndim != 1:\n raise ValueError(\"input must be 1d or non-empty square 2d array.\")\n\n dt = seq_of_zeros.dtype\n if len(seq_of_zeros) == 0:\n return ones((), dtype=dt)\n\n a = ones((1,), dtype=dt)\n for k in range(len(seq_of_zeros)):\n a = convolve(a, array([1, -seq_of_zeros[k]], dtype=dt), mode='full')\n\n return a\n\n\n@_wraps(np.polyval, lax_description=\"\"\"\\\nThe ``unroll`` parameter is JAX specific. It does not effect correctness but can\nhave a major impact on performance for evaluating high-order polynomials. The\nparameter controls the number of unrolled steps with ``lax.scan`` inside the\n``polyval`` implementation. Consider setting ``unroll=128`` (or even higher) to\nimprove runtime performance on accelerators, at the cost of increased\ncompilation time.\n\"\"\")\n@partial(jax.jit, static_argnames=['unroll'])\ndef polyval(p, x, *, unroll=16):\n _check_arraylike(\"polyval\", p, x)\n p, x = _promote_dtypes_inexact(p, x)\n shape = lax.broadcast_shapes(p.shape[1:], x.shape)\n y = lax.full_like(x, 0, shape=shape, dtype=x.dtype)\n y, _ = lax.scan(lambda y, p: (y * x + p, None), y, p, unroll=unroll)\n return y\n\n@_wraps(np.polyadd)\n@jit\ndef polyadd(a1, a2):\n _check_arraylike(\"polyadd\", a1, a2)\n a1, a2 = _promote_dtypes(a1, a2)\n if a2.shape[0] <= a1.shape[0]:\n return a1.at[-a2.shape[0]:].add(a2)\n else:\n return a2.at[-a1.shape[0]:].add(a1)\n\n\n@_wraps(np.polyint)\n@partial(jit, static_argnames=('m',))\ndef polyint(p, m=1, k=None):\n m = core.concrete_or_error(operator.index, m, \"'m' argument of jnp.polyint\")\n k = 0 if k is None else k\n _check_arraylike(\"polyint\", p, k)\n p, k = _promote_dtypes_inexact(p, k)\n if m < 0:\n raise ValueError(\"Order of integral must be positive (see polyder)\")\n k = atleast_1d(k)\n if len(k) == 1:\n k = full((m,), k[0])\n if k.shape != (m,):\n raise ValueError(\"k must be a scalar or a rank-1 array of length 1 or m.\")\n if m == 0:\n return p\n else:\n coeff = maximum(1, arange(len(p) + m, 0, -1)[newaxis, :] - 1 - arange(m)[:, newaxis]).prod(0)\n return true_divide(concatenate((p, k)), coeff)\n\n\n@_wraps(np.polyder)\n@partial(jit, static_argnames=('m',))\ndef polyder(p, m=1):\n _check_arraylike(\"polyder\", p)\n m = core.concrete_or_error(operator.index, m, \"'m' argument of jnp.polyder\")\n p, = _promote_dtypes_inexact(p)\n if m < 0:\n raise ValueError(\"Order of derivative must be positive\")\n if m == 0:\n return p\n coeff = (arange(len(p), m, -1)[newaxis, :] - 1 - arange(m)[:, newaxis]).prod(0)\n return p[:-m] * coeff\n\n\n@_wraps(np.trim_zeros)\ndef trim_zeros(filt, trim='fb'):\n filt = core.concrete_or_error(asarray, filt,\n \"Error arose in the `filt` argument of trim_zeros()\")\n nz = (filt == 0)\n if all(nz):\n return empty(0, _dtype(filt))\n start = argmin(nz) if 'f' in trim.lower() else 0\n end = argmin(nz[::-1]) if 'b' in trim.lower() else 0\n return filt[start:len(filt) - end]\n\n\n_LEADING_ZEROS_DOC = \"\"\"\\\nSetting trim_leading_zeros=True makes the output match that of numpy.\nBut prevents the function from being able to be used in compiled code.\n\"\"\"\n\n@_wraps(np.polymul, lax_description=_LEADING_ZEROS_DOC)\ndef polymul(a1, a2, *, trim_leading_zeros=False):\n _check_arraylike(\"polymul\", a1, a2)\n a1, a2 = _promote_dtypes_inexact(a1, a2)\n if trim_leading_zeros and (len(a1) > 1 or len(a2) > 1):\n a1, a2 = trim_zeros(a1, trim='f'), trim_zeros(a2, trim='f')\n if len(a1) == 0:\n a1 = asarray([0.])\n if len(a2) == 0:\n a2 = asarray([0.])\n val = convolve(a1, a2, mode='full')\n return val\n\n\n@_wraps(np.polysub)\n@jit\ndef polysub(a1, a2):\n _check_arraylike(\"polysub\", a1, a2)\n a1, a2 = _promote_dtypes(a1, a2)\n return polyadd(a1, -a2)\n\n\n@_wraps(np.append)\n@partial(jit, static_argnames=('axis',))\ndef append(arr, values, axis: Optional[int] = None):\n if axis is None:\n return concatenate([ravel(arr), ravel(values)], 0)\n else:\n return concatenate([arr, values], axis=axis)\n\n\n@_wraps(np.delete)\ndef delete(arr, obj, axis=None):\n _check_arraylike(\"delete\", arr)\n if axis is None:\n arr = ravel(arr)\n axis = 0\n axis = _canonicalize_axis(axis, arr.ndim)\n\n # Case 1: obj is a static integer.\n try:\n obj = operator.index(obj)\n obj = _canonicalize_axis(obj, arr.shape[axis])\n except TypeError:\n pass\n else:\n idx = tuple(slice(None) for i in range(axis))\n return concatenate([arr[idx + (slice(0, obj),)], arr[idx + (slice(obj + 1, None),)]], axis=axis)\n\n # Case 2: obj is a static slice.\n if isinstance(obj, slice):\n # TODO(jakevdp): we should be able to do this dynamically with care.\n indices = np.delete(np.arange(arr.shape[axis]), obj)\n return take(arr, indices, axis=axis)\n\n # Case 3: obj is an array\n # NB: pass both arrays to check for appropriate error message.\n _check_arraylike(\"delete\", arr, obj)\n obj = core.concrete_or_error(np.asarray, obj, \"'obj' array argument of jnp.delete()\")\n\n if issubdtype(obj.dtype, integer):\n # TODO(jakevdp): in theory this could be done dynamically if obj has no duplicates,\n # but this would require the complement of lax.gather.\n mask = np.ones(arr.shape[axis], dtype=bool)\n mask[obj] = False\n elif obj.dtype == bool:\n if obj.shape != (arr.shape[axis],):\n raise ValueError(\"np.delete(arr, obj): for boolean indices, obj must be one-dimensional \"\n \"with length matching specified axis.\")\n mask = ~obj\n else:\n raise ValueError(f\"np.delete(arr, obj): got obj.dtype={obj.dtype}; must be integer or bool.\")\n return arr[tuple(slice(None) for i in range(axis)) + (mask,)]\n\n@_wraps(np.insert)\ndef insert(arr, obj, values, axis=None):\n _check_arraylike(\"insert\", arr, 0 if isinstance(obj, slice) else obj, values)\n arr = asarray(arr)\n values = asarray(values)\n\n if axis is None:\n arr = ravel(arr)\n axis = 0\n axis = core.concrete_or_error(None, axis, \"axis argument of jnp.insert()\")\n axis = _canonicalize_axis(axis, arr.ndim)\n if isinstance(obj, slice):\n indices = arange(*obj.indices(arr.shape[axis]))\n else:\n indices = asarray(obj)\n\n if indices.ndim > 1:\n raise ValueError(\"jnp.insert(): obj must be a slice, a one-dimensional \"\n f\"array, or a scalar; got {obj}\")\n if not np.issubdtype(indices.dtype, np.integer):\n if indices.size == 0 and not isinstance(obj, ndarray):\n indices = indices.astype(int)\n else:\n # Note: np.insert allows boolean inputs but the behavior is deprecated.\n raise ValueError(\"jnp.insert(): index array must be \"\n f\"integer typed; got {obj}\")\n values = array(values, ndmin=arr.ndim, dtype=arr.dtype, copy=False)\n\n if indices.size == 1:\n index = ravel(indices)[0]\n if indices.ndim == 0:\n values = moveaxis(values, 0, axis)\n indices = full(values.shape[axis], index)\n n_input = arr.shape[axis]\n n_insert = broadcast_shapes(indices.shape, values.shape[axis])[0]\n out_shape = list(arr.shape)\n out_shape[axis] += n_insert\n out = zeros_like(arr, shape=tuple(out_shape))\n\n indices = where(indices < 0, indices + n_input, indices)\n indices = clip(indices, 0, n_input)\n\n values_ind = indices.at[argsort(indices)].add(arange(n_insert))\n arr_mask = ones(n_input + n_insert, dtype=bool).at[values_ind].set(False)\n arr_ind = where(arr_mask, size=n_input)[0]\n\n out = out.at[(slice(None),) * axis + (values_ind,)].set(values)\n out = out.at[(slice(None),) * axis + (arr_ind,)].set(arr)\n\n return out\n\n\n@_wraps(np.apply_along_axis)\ndef apply_along_axis(func1d, axis: int, arr, *args, **kwargs):\n num_dims = ndim(arr)\n axis = _canonicalize_axis(axis, num_dims)\n func = lambda arr: func1d(arr, *args, **kwargs)\n for i in range(1, num_dims - axis):\n func = jax.vmap(func, in_axes=i, out_axes=-1)\n for i in range(axis):\n func = jax.vmap(func, in_axes=0, out_axes=0)\n return func(arr)\n\n\n@_wraps(np.apply_over_axes)\ndef apply_over_axes(func, a, axes):\n for axis in axes:\n b = func(a, axis=axis)\n if b.ndim == a.ndim:\n a = b\n elif b.ndim == a.ndim - 1:\n a = expand_dims(b, axis)\n else:\n raise ValueError(\"function is not returning an array of the correct shape\")\n return a\n\n\n### Tensor contraction operations\n\n\n@_wraps(np.dot, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('precision',), inline=True)\ndef dot(a, b, *, precision=None): # pylint: disable=missing-docstring\n _check_arraylike(\"dot\", a, b)\n a, b = _promote_dtypes(a, b)\n a_ndim, b_ndim = ndim(a), ndim(b)\n if a_ndim == 0 or b_ndim == 0:\n return lax.mul(a, b)\n if _max(a_ndim, b_ndim) <= 2:\n return lax.dot(a, b, precision=precision)\n\n if b_ndim == 1:\n contract_dims = ((a_ndim - 1,), (0,))\n else:\n contract_dims = ((a_ndim - 1,), (b_ndim - 2,))\n batch_dims = ((), ())\n return lax.dot_general(a, b, (contract_dims, batch_dims), precision)\n\n\n@_wraps(np.matmul, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('precision',), inline=True)\ndef matmul(a, b, *, precision=None): # pylint: disable=missing-docstring\n _check_arraylike(\"matmul\", a, b)\n for i, x in enumerate((a, b)):\n if ndim(x) < 1:\n msg = (f\"matmul input operand {i} must have ndim at least 1, \"\n f\"but it has ndim {ndim(x)}\")\n raise ValueError(msg)\n\n a, b = _promote_dtypes(a, b)\n\n a_is_mat, b_is_mat = (ndim(a) > 1), (ndim(b) > 1)\n a_batch_dims = shape(a)[:-2] if a_is_mat else ()\n b_batch_dims = shape(b)[:-2] if b_is_mat else ()\n num_batch_dims = _max(len(a_batch_dims), len(b_batch_dims))\n a_batch_dims = (None,) * (num_batch_dims - len(a_batch_dims)) + a_batch_dims\n b_batch_dims = (None,) * (num_batch_dims - len(b_batch_dims)) + b_batch_dims\n\n # Dimensions to squeeze from the inputs.\n a_squeeze = []\n b_squeeze = []\n\n # Positions of batch dimensions in squeezed inputs.\n a_batch = []\n b_batch = []\n\n # Desired index in final output of each kind of dimension, in the order that\n # lax.dot_general will emit them.\n idx_batch = []\n idx_a_other = [] # other = non-batch, non-contracting.\n idx_b_other = []\n for i, (ba, bb) in enumerate(zip(a_batch_dims, b_batch_dims)):\n if ba is None:\n idx_b_other.append(i)\n elif bb is None:\n idx_a_other.append(i)\n elif core.symbolic_equal_dim(ba, 1):\n idx_b_other.append(i)\n a_squeeze.append(len(idx_batch) + len(idx_a_other) + len(a_squeeze))\n elif core.symbolic_equal_dim(bb, 1):\n idx_a_other.append(i)\n b_squeeze.append(len(idx_batch) + len(idx_b_other) + len(b_squeeze))\n elif core.symbolic_equal_dim(ba, bb):\n a_batch.append(len(idx_batch) + len(idx_a_other))\n b_batch.append(len(idx_batch) + len(idx_b_other))\n idx_batch.append(i)\n else:\n raise ValueError(\"Incompatible shapes for matmul arguments: {} and {}\"\n .format(shape(a), shape(b)))\n\n if a_is_mat: idx_a_other.append(num_batch_dims)\n if b_is_mat: idx_b_other.append(num_batch_dims + a_is_mat)\n perm = np.argsort(np.concatenate([idx_batch, idx_a_other, idx_b_other]))\n\n a = lax.squeeze(a, tuple(a_squeeze))\n b = lax.squeeze(b, tuple(b_squeeze))\n out = lax.dot_general(\n a, b, (((ndim(a) - 1,), (ndim(b) - 1 - b_is_mat,)), (a_batch, b_batch)),\n precision=precision)\n return lax.transpose(out, perm)\n\n\n@_wraps(np.vdot, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('precision',), inline=True)\ndef vdot(a, b, *, precision=None):\n _check_arraylike(\"vdot\", a, b)\n if issubdtype(_dtype(a), complexfloating):\n a = conj(a)\n return dot(a.ravel(), b.ravel(), precision=precision)\n\n\n@_wraps(np.tensordot, lax_description=_PRECISION_DOC)\ndef tensordot(a, b, axes=2, *, precision=None):\n _check_arraylike(\"tensordot\", a, b)\n a_ndim = ndim(a)\n b_ndim = ndim(b)\n\n a, b = _promote_dtypes(a, b)\n if type(axes) is int:\n if axes > _min(a_ndim, b_ndim):\n msg = \"Number of tensordot axes (axes {}) exceeds input ranks ({} and {})\"\n raise TypeError(msg.format(axes, a.shape, b.shape))\n contracting_dims = tuple(range(a_ndim - axes, a_ndim)), tuple(range(axes))\n elif type(axes) in (list, tuple) and len(axes) == 2:\n ax1, ax2 = axes\n if type(ax1) == type(ax2) == int:\n contracting_dims = ((_canonicalize_axis(ax1, a_ndim),),\n (_canonicalize_axis(ax2, b_ndim),))\n elif type(ax1) in (list, tuple) and type(ax2) in (list, tuple):\n if len(ax1) != len(ax2):\n msg = \"tensordot requires axes lists to have equal length, got {} and {}.\"\n raise TypeError(msg.format(ax1, ax2))\n contracting_dims = (tuple(_canonicalize_axis(i, a_ndim) for i in ax1),\n tuple(_canonicalize_axis(i, b_ndim) for i in ax2))\n else:\n msg = (\"tensordot requires both axes lists to be either ints, tuples or \"\n \"lists, got {} and {}\")\n raise TypeError(msg.format(ax1, ax2))\n else:\n msg = (\"tensordot axes argument must be an int, a pair of ints, or a pair \"\n \"of lists/tuples of ints.\")\n raise TypeError(msg)\n return lax.dot_general(a, b, (contracting_dims, ((), ())),\n precision=precision)\n\n\n@_wraps(np.einsum, lax_description=_PRECISION_DOC, skip_params=['out'])\ndef einsum(*operands, out=None, optimize='optimal', precision=None,\n _use_xeinsum=False):\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.einsum is not supported.\")\n\n if (_use_xeinsum or isinstance(operands[0], str) and '{' in operands[0] and\n len(operands[1:]) == 2):\n return lax.xeinsum(*operands)\n\n optimize = 'optimal' if optimize is True else optimize\n # using einsum_call=True here is an internal api for opt_einsum\n\n # Allow handling of shape polymorphism\n non_constant_dim_types = {\n type(d) for op in operands if not isinstance(op, str)\n for d in np.shape(op) if not core.is_constant_dim(d)\n }\n if not non_constant_dim_types:\n einsum_contract_path_fn = opt_einsum.contract_path\n else:\n einsum_contract_path_fn = _polymorphic_einsum_contract_path_handlers[next(iter(non_constant_dim_types))]\n operands, contractions = einsum_contract_path_fn(\n *operands, einsum_call=True, use_blas=True, optimize=optimize)\n\n contractions = tuple((a, frozenset(b), c) for a, b, c, *_ in contractions)\n return _einsum(operands, contractions, precision)\n\n# Enable other modules to override einsum_contact_path.\n# Indexed by the type of the non constant dimension\n_polymorphic_einsum_contract_path_handlers = {} # type: ignore\n\n@_wraps(np.einsum_path)\ndef einsum_path(subscripts, *operands, optimize='greedy'):\n # using einsum_call=True here is an internal api for opt_einsum\n return opt_einsum.contract_path(subscripts, *operands, optimize=optimize)\n\ndef _removechars(s, chars):\n return s.translate(str.maketrans(dict.fromkeys(chars)))\n\n@partial(jit, static_argnums=(1, 2))\ndef _einsum(operands: Sequence,\n contractions: Sequence[Tuple[Tuple[int, ...], FrozenSet[str], str]],\n precision):\n operands = list(_promote_dtypes(*operands))\n def sum(x, axes):\n return lax.reduce(x, np.array(0, x.dtype),\n lax.add if x.dtype != bool_ else lax.bitwise_or, axes)\n\n def sum_uniques(operand, names, uniques):\n if uniques:\n axes = [names.index(name) for name in uniques]\n operand = sum(operand, axes)\n names = _removechars(names, uniques)\n return operand, names\n\n def sum_repeats(operand, names, counts, keep_names):\n for name, count in counts.items():\n if count > 1:\n axes = [i for i, n in enumerate(names) if n == name]\n eye = lax._delta(operand.dtype, operand.shape, axes)\n if name not in keep_names:\n operand = sum(operand * eye, axes)\n names = names.replace(name, '')\n else:\n operand = sum(operand * eye, axes[:-1])\n names = names.replace(name, '', count - 1)\n return operand, names\n\n def filter_singleton_dims(operand, names, other_shape, other_names):\n s = shape(operand)\n new_shape = []\n new_names = []\n for i, d in enumerate(names):\n other_i = other_names.find(d)\n if not core.symbolic_equal_dim(s[i], 1) or other_i == -1 or core.symbolic_equal_dim(other_shape[other_i], 1):\n new_shape.append(s[i])\n new_names.append(d)\n return reshape(operand, tuple(new_shape)), \"\".join(new_names)\n\n for operand_indices, contracted_names_set, einstr in contractions:\n contracted_names = sorted(contracted_names_set)\n input_str, result_names = einstr.split('->')\n input_names = input_str.split(',')\n\n # switch on the number of operands to be processed in this loop iteration.\n # every case here sets 'operand' and 'names'.\n if len(operand_indices) == 1:\n operand = operands.pop(operand_indices[0])\n names, = input_names\n counts = collections.Counter(names)\n\n # sum out unique contracted indices with a single reduce-sum\n uniques = [name for name in contracted_names if counts[name] == 1]\n operand, names = sum_uniques(operand, names, uniques)\n\n # for every repeated index, do a contraction against an identity matrix\n operand, names = sum_repeats(operand, names, counts, result_names)\n\n elif len(operand_indices) == 2:\n lhs, rhs = map(operands.pop, operand_indices)\n lhs_names, rhs_names = input_names\n\n # handle cases where one side of a contracting or batch dimension is 1\n # but its counterpart is not.\n lhs, lhs_names = filter_singleton_dims(lhs, lhs_names, shape(rhs),\n rhs_names)\n rhs, rhs_names = filter_singleton_dims(rhs, rhs_names, shape(lhs),\n lhs_names)\n\n lhs_counts = collections.Counter(lhs_names)\n rhs_counts = collections.Counter(rhs_names)\n\n # sum out unique contracted indices in lhs and rhs\n lhs_uniques = [name for name in contracted_names\n if lhs_counts[name] == 1 and rhs_counts[name] == 0]\n lhs, lhs_names = sum_uniques(lhs, lhs_names, lhs_uniques)\n\n rhs_uniques = [name for name in contracted_names\n if rhs_counts[name] == 1 and lhs_counts[name] == 0]\n rhs, rhs_names = sum_uniques(rhs, rhs_names, rhs_uniques)\n\n # for every repeated index, contract against an identity matrix\n lhs, lhs_names = sum_repeats(lhs, lhs_names, lhs_counts,\n result_names + rhs_names)\n rhs, rhs_names = sum_repeats(rhs, rhs_names, rhs_counts,\n result_names + lhs_names)\n\n lhs_or_rhs_names = set(lhs_names) | set(rhs_names)\n contracted_names = [x for x in contracted_names if x in lhs_or_rhs_names]\n lhs_and_rhs_names = set(lhs_names) & set(rhs_names)\n batch_names = [x for x in result_names if x in lhs_and_rhs_names]\n\n lhs_batch, rhs_batch = unzip2((lhs_names.find(n), rhs_names.find(n))\n for n in batch_names)\n\n # NOTE(mattjj): this can fail non-deterministically in python3, maybe\n # due to opt_einsum\n assert _all(\n name in lhs_names and name in rhs_names and\n lhs.shape[lhs_names.index(name)] == rhs.shape[rhs_names.index(name)]\n for name in contracted_names)\n\n # contract using lax.dot_general\n batch_names_str = ''.join(batch_names)\n lhs_cont, rhs_cont = unzip2((lhs_names.index(n), rhs_names.index(n))\n for n in contracted_names)\n deleted_names = batch_names_str + ''.join(contracted_names)\n remaining_lhs_names = _removechars(lhs_names, deleted_names)\n remaining_rhs_names = _removechars(rhs_names, deleted_names)\n # Try both orders of lhs and rhs, in the hope that one of them means we\n # don't need an explicit transpose. opt_einsum likes to contract from\n # right to left, so we expect (rhs,lhs) to have the best chance of not\n # needing a transpose.\n names = batch_names_str + remaining_rhs_names + remaining_lhs_names\n if names == result_names:\n dimension_numbers = ((rhs_cont, lhs_cont), (rhs_batch, lhs_batch))\n operand = lax.dot_general(rhs, lhs, dimension_numbers, precision)\n else:\n names = batch_names_str + remaining_lhs_names + remaining_rhs_names\n dimension_numbers = ((lhs_cont, rhs_cont), (lhs_batch, rhs_batch))\n operand = lax.dot_general(lhs, rhs, dimension_numbers, precision)\n else:\n raise NotImplementedError # if this is actually reachable, open an issue!\n\n # the resulting 'operand' with axis labels 'names' should be a permutation\n # of the desired result\n assert len(names) == len(result_names) == len(set(names))\n assert set(names) == set(result_names)\n if names != result_names:\n perm = tuple([names.index(name) for name in result_names])\n operand = lax.transpose(operand, perm)\n operands.append(operand) # used in next iteration\n\n return operands[0]\n\n\ndef _movechars(s, src, dst):\n \"\"\"Helper for einsum string munging, like moveaxis on identifier strings.\"\"\"\n chars = [c for i, c in enumerate(s) if i not in src]\n for i, j in sorted(zip(dst, src)):\n chars.insert(i, s[j])\n return ''.join(chars)\n\n\n@_wraps(np.inner, lax_description=_PRECISION_DOC)\n@partial(jit, static_argnames=('precision',), inline=True)\ndef inner(a, b, *, precision=None):\n if ndim(a) == 0 or ndim(b) == 0:\n return a * b\n return tensordot(a, b, (-1, -1), precision=precision)\n\n\n@_wraps(np.outer, skip_params=['out'])\n@partial(jit, inline=True)\ndef outer(a, b, out=None):\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.outer is not supported.\")\n a, b = _promote_dtypes(a, b)\n return ravel(a)[:, None] * ravel(b)[None, :]\n\n@_wraps(np.cross)\n@partial(jit, static_argnames=('axisa', 'axisb', 'axisc', 'axis'))\ndef cross(a, b, axisa: int = -1, axisb: int = -1, axisc: int = -1,\n axis: Optional[int] = None):\n if axis is not None:\n axisa = axis\n axisb = axis\n axisc = axis\n a = moveaxis(a, axisa, -1)\n b = moveaxis(b, axisb, -1)\n\n if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3):\n raise ValueError(\"Dimension must be either 2 or 3 for cross product\")\n\n if a.shape[-1] == 2 and b.shape[-1] == 2:\n return a[..., 0] * b[..., 1] - a[..., 1] * b[..., 0]\n\n a0 = a[..., 0]\n a1 = a[..., 1]\n a2 = a[..., 2] if a.shape[-1] == 3 else zeros_like(a0)\n b0 = b[..., 0]\n b1 = b[..., 1]\n b2 = b[..., 2] if b.shape[-1] == 3 else zeros_like(b0)\n c = array([a1 * b2 - a2 * b1, a2 * b0 - a0 * b2, a0 * b1 - a1 * b0])\n return moveaxis(c, 0, axisc)\n\n\n@_wraps(np.kron)\n@jit\ndef kron(a, b):\n a, b = _promote_dtypes(a, b)\n if ndim(a) < ndim(b):\n a = expand_dims(a, range(ndim(b) - ndim(a)))\n elif ndim(b) < ndim(a):\n b = expand_dims(b, range(ndim(a) - ndim(b)))\n a_reshaped = expand_dims(a, range(1, 2 * ndim(a), 2))\n b_reshaped = expand_dims(b, range(0, 2 * ndim(b), 2))\n out_shape = tuple(np.multiply(shape(a), shape(b)))\n return reshape(lax.mul(a_reshaped, b_reshaped), out_shape)\n\n\n@_wraps(np.vander)\n@partial(jit, static_argnames=('N', 'increasing'))\ndef vander(x, N=None, increasing=False):\n _check_arraylike(\"vander\", x)\n x = asarray(x)\n if x.ndim != 1:\n raise ValueError(\"x must be a one-dimensional array\")\n N = x.shape[0] if N is None else core.concrete_or_error(\n operator.index, N, \"'N' argument of jnp.vander()\")\n if N < 0:\n raise ValueError(\"N must be nonnegative\")\n\n iota = lax.iota(x.dtype, N)\n if not increasing:\n iota = lax.sub(lax._const(iota, N - 1), iota)\n\n return power(x[..., None], expand_dims(iota, tuple(range(x.ndim))))\n\n\n### Misc\n\n_ARGWHERE_DOC = \"\"\"\\\nBecause the size of the output of ``argwhere`` is data-dependent, the function is not\ntypically compatible with JIT. The JAX version adds the optional ``size`` argument, which\nspecifies the size of the leading dimension of the output - it must be specified statically\nfor ``jnp.argwhere`` to be compiled with non-static operands. If ``size`` is specified,\nthe indices of the first ``size`` True elements will be returned; if there are fewer\nnonzero elements than `size` indicates, the index arrays will be zero-padded.\n\"\"\"\n\n@_wraps(np.argwhere, lax_description=_ARGWHERE_DOC)\ndef argwhere(a, *, size=None, fill_value=None):\n result = transpose(vstack(nonzero(a, size=size, fill_value=fill_value)))\n if ndim(a) == 0:\n return result[:0].reshape(result.shape[0], 0)\n return result.reshape(result.shape[0], ndim(a))\n\n\n@_wraps(np.argmax, skip_params=['out'])\ndef argmax(a, axis: Optional[int] = None, out=None):\n return _argmax(a, None if axis is None else operator.index(axis))\n\n@partial(jit, static_argnames=('axis',), inline=True)\ndef _argmax(a, axis: Optional[int] = None, out=None):\n _check_arraylike(\"argmax\", a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.argmax is not supported.\")\n if axis is None:\n a = ravel(a)\n axis = 0\n if a.shape[axis] == 0:\n raise ValueError(\"attempt to get argmax of an empty sequence\")\n return lax.argmax(a, _canonicalize_axis(axis, a.ndim), dtypes.canonicalize_dtype(int_))\n\n@_wraps(np.argmin, skip_params=['out'])\ndef argmin(a, axis: Optional[int] = None, out=None):\n return _argmin(a, None if axis is None else operator.index(axis))\n\n@partial(jit, static_argnames=('axis',), inline=True)\ndef _argmin(a, axis: Optional[int] = None, out=None):\n _check_arraylike(\"argmin\", a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.argmin is not supported.\")\n if axis is None:\n a = ravel(a)\n axis = 0\n if a.shape[axis] == 0:\n raise ValueError(\"attempt to get argmin of an empty sequence\")\n return lax.argmin(a, _canonicalize_axis(axis, a.ndim), dtypes.canonicalize_dtype(int_))\n\n\n_NANARG_DOC = \"\"\"\\\nWarning: jax.numpy.arg{} returns -1 for all-NaN slices and does not raise\nan error.\n\"\"\"\n\n@_wraps(np.nanargmax, lax_description=_NANARG_DOC.format(\"max\"))\ndef nanargmax(a, axis: Optional[int] = None):\n return _nanargmax(a, None if axis is None else operator.index(axis))\n\n@partial(jit, static_argnames=('axis',))\ndef _nanargmax(a, axis: Optional[int] = None):\n _check_arraylike(\"nanargmax\", a)\n if not issubdtype(_dtype(a), inexact):\n return argmax(a, axis=axis)\n nan_mask = isnan(a)\n a = where(nan_mask, -inf, a)\n res = argmax(a, axis=axis)\n return where(all(nan_mask, axis=axis), -1, res)\n\n@_wraps(np.nanargmin, lax_description=_NANARG_DOC.format(\"min\"))\ndef nanargmin(a, axis: Optional[int] = None):\n return _nanargmin(a, None if axis is None else operator.index(axis))\n\n@partial(jit, static_argnames=('axis',))\ndef _nanargmin(a, axis: Optional[int] = None):\n _check_arraylike(\"nanargmin\", a)\n if not issubdtype(_dtype(a), inexact):\n return argmin(a, axis=axis)\n nan_mask = isnan(a)\n a = where(nan_mask, inf, a)\n res = argmin(a, axis=axis)\n return where(all(nan_mask, axis=axis), -1, res)\n\n\n@_wraps(np.sort)\n@partial(jit, static_argnames=('axis', 'kind', 'order'))\ndef sort(a, axis: Optional[int] = -1, kind='quicksort', order=None):\n _check_arraylike(\"sort\", a)\n if kind != 'quicksort':\n warnings.warn(\"'kind' argument to sort is ignored.\")\n if order is not None:\n raise ValueError(\"'order' argument to sort is not supported.\")\n\n if axis is None:\n return lax.sort(a.ravel(), dimension=0)\n else:\n return lax.sort(a, dimension=_canonicalize_axis(axis, ndim(a)))\n\n@_wraps(np.sort_complex)\n@jit\ndef sort_complex(a):\n _check_arraylike(\"sort_complex\", a)\n a = lax.sort(a, dimension=0)\n return lax.convert_element_type(a, result_type(a, dtypes.canonicalize_dtype(complex_)))\n\n@_wraps(np.lexsort)\n@partial(jit, static_argnames=('axis',))\ndef lexsort(keys, axis=-1):\n keys = tuple(keys)\n if len(keys) == 0:\n raise TypeError(\"need sequence of keys with len > 0 in lexsort\")\n if len({shape(key) for key in keys}) > 1:\n raise ValueError(\"all keys need to be the same shape\")\n if ndim(keys[0]) == 0:\n return array(0, dtype=dtypes.canonicalize_dtype(int_))\n axis = _canonicalize_axis(axis, ndim(keys[0]))\n use_64bit_index = keys[0].shape[axis] >= (1 << 31)\n iota = lax.broadcasted_iota(int64 if use_64bit_index else int_, shape(keys[0]), axis)\n return lax.sort((*keys[::-1], iota), dimension=axis, num_keys=len(keys))[-1]\n\n\n_ARGSORT_DOC = \"\"\"\nOnly :code:`kind='stable'` is supported. Other :code:`kind` values will produce\na warning and be treated as if they were :code:`'stable'`.\n\"\"\"\n\n@_wraps(np.argsort, lax_description=_ARGSORT_DOC)\n@partial(jit, static_argnames=('axis', 'kind', 'order'))\ndef argsort(a, axis: Optional[int] = -1, kind='stable', order=None):\n _check_arraylike(\"argsort\", a)\n if kind != 'stable':\n warnings.warn(\"'kind' argument to argsort is ignored; only 'stable' sorts \"\n \"are supported.\")\n if order is not None:\n raise ValueError(\"'order' argument to argsort is not supported.\")\n\n if axis is None:\n return argsort(a.ravel(), 0)\n else:\n axis_num = _canonicalize_axis(axis, ndim(a))\n use_64bit_index = a.shape[axis_num] >= (1 << 31)\n iota = lax.broadcasted_iota(int64 if use_64bit_index else int_, shape(a), axis_num)\n _, perm = lax.sort_key_val(a, iota, dimension=axis_num)\n return perm\n\n\n@_wraps(np.msort)\ndef msort(a):\n return sort(a, axis=0)\n\n\n@partial(jit, static_argnums=(2,))\ndef _roll(a, shift, axis):\n a_shape = shape(a)\n if axis is None:\n return lax.reshape(_roll(ravel(a), shift, axis=0), a_shape)\n shift = asarray(shift)\n a_ndim = len(a_shape)\n axis = np.asarray(axis)\n b_shape = lax.broadcast_shapes(shift.shape, axis.shape, (1,))\n if len(b_shape) != 1:\n msg = \"'shift' and 'axis' arguments to roll must be scalars or 1D arrays\"\n raise ValueError(msg)\n\n for x, i in zip(broadcast_to(shift, b_shape),\n np.broadcast_to(axis, b_shape)):\n i = _canonicalize_axis(i, a_ndim)\n x = remainder(x, (a_shape[i] or 1))\n a = lax.concatenate((a, a), i)\n a = lax.dynamic_slice_in_dim(a, a_shape[i] - x, a_shape[i], axis=i)\n return a\n\n\n@_wraps(np.roll)\ndef roll(a, shift, axis: Optional[Union[int, Sequence[int]]] = None):\n _check_arraylike(\"roll\", a,)\n if isinstance(axis, list):\n axis = tuple(axis)\n return _roll(a, shift, axis)\n\n\n@_wraps(np.rollaxis, lax_description=_ARRAY_VIEW_DOC)\n@partial(jit, static_argnames=('axis', 'start'))\ndef rollaxis(a, axis: int, start=0):\n _check_arraylike(\"rollaxis\", a)\n start = core.concrete_or_error(operator.index, start, \"'start' argument of jnp.rollaxis()\")\n a_ndim = ndim(a)\n axis = _canonicalize_axis(axis, a_ndim)\n if not (-a_ndim <= start <= a_ndim):\n raise ValueError(f\"start={start} must satisfy {-a_ndim}<=start<={a_ndim}\")\n if start < 0:\n start += a_ndim\n if start > axis:\n start -= 1\n return moveaxis(a, axis, start)\n\n\n@_wraps(np.packbits)\n@partial(jit, static_argnames=('axis', 'bitorder'))\ndef packbits(a, axis: Optional[int] = None, bitorder='big'):\n _check_arraylike(\"packbits\", a)\n if not (issubdtype(_dtype(a), integer) or issubdtype(_dtype(a), bool_)):\n raise TypeError('Expected an input array of integer or boolean data type')\n if bitorder not in ['little', 'big']:\n raise ValueError(\"'order' must be either 'little' or 'big'\")\n a = greater(a, 0).astype('uint8')\n bits = arange(8, dtype='uint8')\n if bitorder == 'big':\n bits = bits[::-1]\n if axis is None:\n a = ravel(a)\n axis = 0\n a = swapaxes(a, axis, -1)\n\n remainder = a.shape[-1] % 8\n if remainder:\n a = lax.pad(a, np.uint8(0),\n (a.ndim - 1) * [(0, 0, 0)] + [(0, 8 - remainder, 0)])\n\n a = a.reshape(a.shape[:-1] + (a.shape[-1] // 8, 8))\n bits = expand_dims(bits, tuple(range(a.ndim - 1)))\n packed = (a << bits).sum(-1).astype('uint8')\n return swapaxes(packed, axis, -1)\n\n\n@_wraps(np.unpackbits)\n@partial(jit, static_argnames=('axis', 'count', 'bitorder'))\ndef unpackbits(a, axis: Optional[int] = None, count=None, bitorder='big'):\n _check_arraylike(\"unpackbits\", a)\n if _dtype(a) != uint8:\n raise TypeError(\"Expected an input array of unsigned byte data type\")\n if bitorder not in ['little', 'big']:\n raise ValueError(\"'order' must be either 'little' or 'big'\")\n bits = asarray(1) << arange(8, dtype='uint8')\n if bitorder == 'big':\n bits = bits[::-1]\n if axis is None:\n a = ravel(a)\n axis = 0\n a = swapaxes(a, axis, -1)\n unpacked = ((a[..., None] & expand_dims(bits, tuple(range(a.ndim)))) > 0).astype('uint8')\n unpacked = unpacked.reshape(unpacked.shape[:-2] + (-1,))[..., :count]\n return swapaxes(unpacked, axis, -1)\n\n\n@_wraps(np.take, skip_params=['out'])\ndef take(a, indices, axis: Optional[int] = None, out=None, mode=None):\n return _take(a, indices, None if axis is None else operator.index(axis), out,\n mode)\n\n@partial(jit, static_argnames=('axis', 'mode'))\ndef _take(a, indices, axis: Optional[int] = None, out=None, mode=None):\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.take is not supported.\")\n _check_arraylike(\"take\", a, indices)\n a = asarray(a)\n indices = asarray(indices)\n\n if axis is None:\n a = ravel(a)\n axis_idx = 0\n else:\n axis_idx = _canonicalize_axis(axis, ndim(a))\n\n if mode is None:\n # TODO(phawkins): change default mode to \"fill\" and delete this case.\n # lax.gather() does not support negative indices, so we wrap them here\n indices = where(indices < 0, indices + a.shape[axis_idx], indices)\n gather_mode = lax.GatherScatterMode.CLIP\n elif mode == \"raise\":\n # TODO(phawkins): we have no way to report out of bounds errors yet.\n raise NotImplementedError(\"The 'raise' mode to jnp.take is not supported.\")\n elif mode == \"wrap\":\n indices = mod(indices, _constant_like(indices, a.shape[axis_idx]))\n gather_mode = lax.GatherScatterMode.PROMISE_IN_BOUNDS\n elif mode == \"fill\":\n # Undocumented non-standard mode corresponding to the fill_or_drop mode on\n # lax.gather()\n gather_mode = lax.GatherScatterMode.FILL_OR_DROP\n # lax.gather() does not support negative indices, so we wrap them here\n indices = where(indices < 0, indices + a.shape[axis_idx], indices)\n elif mode == \"clip\":\n gather_mode = lax.GatherScatterMode.CLIP\n else:\n raise ValueError(\"Invalid mode '{}' for np.take\".format(mode))\n\n index_dims = len(shape(indices))\n slice_sizes = list(shape(a))\n if slice_sizes[axis_idx] == 0:\n if indices.size != 0:\n raise IndexError(\"Cannot do a non-empty jnp.take() from an empty axis.\")\n return a\n\n if indices.size == 0:\n out_shape = (slice_sizes[:axis_idx] + list(indices.shape) +\n slice_sizes[axis_idx + 1:])\n return full_like(a, 0, shape=out_shape)\n\n slice_sizes[axis_idx] = 1\n dnums = lax.GatherDimensionNumbers(\n offset_dims=tuple(\n list(range(axis_idx)) +\n list(range(axis_idx + index_dims, len(a.shape) + index_dims - 1))),\n collapsed_slice_dims=(axis_idx,),\n start_index_map=(axis_idx,))\n return lax.gather(a, indices[..., None], dimension_numbers=dnums,\n slice_sizes=tuple(slice_sizes),\n mode=gather_mode)\n\n\ndef _normalize_index(index, axis_size):\n \"\"\"Normalizes an index value in the range [-N, N) to the range [0, N).\"\"\"\n if core.is_constant_dim(axis_size):\n axis_size_val = _constant_like(index, axis_size)\n else:\n axis_size_val = lax.convert_element_type(core.dimension_as_value(axis_size),\n _dtype(index))\n return lax.select(\n lax.lt(index, _constant_like(index, 0)),\n lax.add(index, axis_size_val),\n index)\n\n@_wraps(np.take_along_axis, update_doc=False)\n@partial(jit, static_argnames=('axis',))\ndef take_along_axis(arr, indices, axis: Optional[int]):\n _check_arraylike(\"take_along_axis\", arr, indices)\n if axis is None:\n if ndim(indices) != 1:\n msg = \"take_along_axis indices must be 1D if axis=None, got shape {}\"\n raise ValueError(msg.format(indices.shape))\n return take_along_axis(arr.ravel(), indices, 0)\n rank = ndim(arr)\n if rank != ndim(indices):\n msg = \"indices and arr must have the same number of dimensions; {} vs. {}\"\n raise ValueError(msg.format(ndim(indices), ndim(arr)))\n axis = _canonicalize_axis(axis, rank)\n\n def replace(tup, val):\n lst = list(tup)\n lst[axis] = val\n return tuple(lst)\n\n use_64bit_index = _any([not core.is_constant_dim(d) or d >= (1 << 31) for d in arr.shape])\n index_dtype = int64 if use_64bit_index else int32\n indices = lax.convert_element_type(indices, index_dtype)\n\n bcast_shape = lax.broadcast_shapes(replace(arr.shape, 1), replace(indices.shape, 1))\n indices = broadcast_to(indices, replace(bcast_shape, indices.shape[axis]))\n arr = broadcast_to(arr, replace(bcast_shape, arr.shape[axis]))\n\n axis_size = arr.shape[axis]\n arr_shape = replace(arr.shape, 1)\n idx_shape = indices.shape\n out_shape = lax.broadcast_shapes(idx_shape, arr_shape)\n\n index_dims = [i for i, idx in enumerate(idx_shape) if i == axis or idx != 1]\n\n gather_index_shape = tuple(np.array(out_shape)[index_dims]) + (1,)\n gather_indices = []\n slice_sizes = []\n offset_dims = []\n start_index_map = []\n collapsed_slice_dims = []\n j = 0\n for i in range(rank):\n if i == axis:\n indices = _normalize_index(indices, axis_size)\n gather_indices.append(lax.reshape(indices, gather_index_shape))\n slice_sizes.append(1)\n start_index_map.append(i)\n collapsed_slice_dims.append(i)\n j += 1\n elif idx_shape[i] != 1:\n iota = lax.iota(_dtype(indices), out_shape[i])\n iota = lax.broadcast_in_dim(iota, gather_index_shape, (j,))\n gather_indices.append(iota)\n slice_sizes.append(1)\n start_index_map.append(i)\n collapsed_slice_dims.append(i)\n j += 1\n else:\n # If idx_shape[i] == 1, we can just take the entirety of the arr's axis\n # and avoid forming an iota index.\n offset_dims.append(i)\n slice_sizes.append(arr_shape[i])\n\n gather_indices = lax.concatenate(gather_indices, dimension=j)\n dnums = lax.GatherDimensionNumbers(\n offset_dims=tuple(offset_dims),\n collapsed_slice_dims=tuple(collapsed_slice_dims),\n start_index_map=tuple(start_index_map))\n return lax.gather(arr, gather_indices, dnums, tuple(slice_sizes))\n\n\n### SetOps\n@partial(jit, static_argnums=1)\ndef _unique_sorted_mask(ar, axis):\n aux = moveaxis(ar, axis, 0)\n size, *out_shape = aux.shape\n if _prod(out_shape) == 0:\n size = 1\n perm = zeros(1, dtype=int)\n else:\n perm = lexsort(aux.reshape(size, _prod(out_shape)).T[::-1])\n aux = aux[perm]\n if aux.size:\n mask = ones(size, dtype=bool).at[1:].set(any(aux[1:] != aux[:-1], tuple(range(1, aux.ndim))))\n else:\n mask = zeros(size, dtype=bool)\n return aux, mask, perm\n\ndef _unique(ar, axis, return_index=False, return_inverse=False, return_counts=False,\n size=None, fill_value=None, return_true_size=False):\n \"\"\"\n Find the unique elements of an array along a particular axis.\n \"\"\"\n if ar.shape[axis] == 0 and size and fill_value is None:\n raise ValueError(\n \"jnp.unique: for zero-sized input with nonzero size argument, fill_value must be specified\")\n\n aux, mask, perm = _unique_sorted_mask(ar, axis)\n ind = mask if size is None else nonzero(mask, size=size)[0]\n result = aux[ind] if aux.size else aux\n if fill_value is not None:\n fill_value = asarray(fill_value, dtype=result.dtype)\n if size is not None and fill_value is not None:\n if result.shape[0]:\n valid = lax.expand_dims(arange(size) < mask.sum(), tuple(range(1, result.ndim)))\n result = where(valid, result, fill_value)\n else:\n result = full_like(result, fill_value, shape=(size, *result.shape[1:]))\n result = moveaxis(result, 0, axis)\n\n ret = (result,)\n if return_index:\n if aux.size:\n ret += (perm[ind],)\n else:\n ret += (perm,)\n if return_inverse:\n if aux.size:\n imask = cumsum(mask) - 1\n inv_idx = zeros(mask.shape, dtype=dtypes.canonicalize_dtype(int_))\n inv_idx = inv_idx.at[perm].set(imask)\n else:\n inv_idx = zeros(ar.shape[axis], dtype=int)\n ret += (inv_idx,)\n if return_counts:\n if aux.size:\n if size is None:\n idx = append(nonzero(mask)[0], mask.size)\n else:\n idx = nonzero(mask, size=size + 1)[0]\n idx = idx.at[1:].set(where(idx[1:], idx[1:], mask.size))\n ret += (diff(idx),)\n elif ar.shape[axis]:\n ret += (array([ar.shape[axis]], dtype=int_),)\n else:\n ret += (empty(0, dtype=int),)\n if return_true_size:\n # Useful for internal uses of unique().\n ret += (mask.sum(),)\n return ret[0] if len(ret) == 1 else ret\n\n\n_UNIQUE_DOC = \"\"\"\\\nBecause the size of the output of ``unique`` is data-dependent, the function is not\ntypically compatible with JIT. The JAX version adds the optional `size` argument which\nspecifies the size of the data-dependent output arrays: it must be specified statically\nfor ``jnp.unique`` to be compiled with non-static operands. If specified, the first `size`\nunique elements will be returned; if there are fewer unique elements than `size` indicates,\nthe return value will be padded with `fill_value`, which defaults to the minimum value\nalong the specified axis of the input.\"\"\"\n\n\n@_wraps(np.unique, skip_params=['axis'], lax_description=_UNIQUE_DOC)\ndef unique(ar, return_index=False, return_inverse=False,\n return_counts=False, axis: Optional[int] = None, *, size=None, fill_value=None):\n _check_arraylike(\"unique\", ar)\n if size is None:\n ar = core.concrete_or_error(None, ar, \"The error arose for the first argument of jnp.unique()\")\n else:\n size = core.concrete_or_error(operator.index, size, \"The error arose for the size argument of jnp.unique()\")\n ar = asarray(ar)\n if axis is None:\n axis = 0\n ar = ar.flatten()\n axis = core.concrete_or_error(operator.index, axis, \"axis argument of jnp.unique()\")\n return _unique(ar, axis, return_index, return_inverse, return_counts, size=size, fill_value=fill_value)\n\n### Indexing\n\ndef _rewriting_take(arr, idx, indices_are_sorted=False, unique_indices=False,\n mode=None, fill_value=None):\n # Computes arr[idx].\n # All supported cases of indexing can be implemented as an XLA gather,\n # followed by an optional reverse and broadcast_in_dim.\n arr = asarray(arr)\n treedef, static_idx, dynamic_idx = _split_index_for_jit(idx, arr.shape)\n return _gather(arr, treedef, static_idx, dynamic_idx, indices_are_sorted,\n unique_indices, mode, fill_value)\n\n# TODO(phawkins): re-enable jit after fixing excessive recompilation for\n# slice indexes (e.g., slice(0, 5, None), slice(10, 15, None), etc.).\n# @partial(jit, static_argnums=(1, 2))\ndef _gather(arr, treedef, static_idx, dynamic_idx, indices_are_sorted,\n unique_indices, mode, fill_value):\n idx = _merge_static_and_dynamic_indices(treedef, static_idx, dynamic_idx)\n indexer = _index_to_gather(shape(arr), idx) # shared with _scatter_update\n y = arr\n\n if fill_value is not None:\n core.concrete_or_error(None, fill_value,\n \"fill_value argument to indexed get()\")\n if np.ndim(fill_value) != 0:\n raise ValueError(\"fill_value argument to indexed get() must be a scalar\")\n if isinstance(fill_value, np.ndarray):\n fill_value = fill_value.item()\n\n # Avoid calling gather if the slice shape is empty, both as a fast path and to\n # handle cases like zeros(0)[array([], int32)].\n if core.is_empty_shape(indexer.slice_shape):\n return zeros_like(y, shape=indexer.slice_shape)\n\n # We avoid generating a gather when indexer.gather_indices.size is empty.\n if not core.is_empty_shape(indexer.gather_indices.shape):\n y = lax.gather(\n y, indexer.gather_indices, indexer.dnums, indexer.gather_slice_shape,\n unique_indices=unique_indices or indexer.unique_indices,\n indices_are_sorted=indices_are_sorted or indexer.indices_are_sorted,\n mode=mode, fill_value=fill_value)\n\n # Reverses axes with negative strides.\n if indexer.reversed_y_dims:\n y = lax.rev(y, indexer.reversed_y_dims)\n\n # This adds np.newaxis/None dimensions.\n return expand_dims(y, indexer.newaxis_dims)\n\n_Indexer = collections.namedtuple(\"_Indexer\", [\n # The expected shape of the slice output.\n \"slice_shape\",\n\n # The slice shape to pass to lax.gather().\n \"gather_slice_shape\",\n\n # The gather indices to use.\n \"gather_indices\",\n\n # A GatherDimensionNumbers object describing the gather to perform.\n \"dnums\",\n\n # Are the gather_indices known to be non-overlapping and/or sorted?\n # (In practice, these translate to \"there no advanced indices\", because\n # only advanced indices could lead to index repetition.)\n \"unique_indices\",\n \"indices_are_sorted\",\n\n # Slice dimensions that have negative strides, and so must be reversed after\n # the gather.\n \"reversed_y_dims\",\n\n # Keep track of any axes created by `newaxis`. These must be inserted for\n # gathers and eliminated for scatters.\n \"newaxis_dims\",\n])\n\ndef _split_index_for_jit(idx, shape):\n \"\"\"Splits indices into necessarily-static and dynamic parts.\n\n Used to pass indices into `jit`-ted function.\n \"\"\"\n # Convert list indices to tuples in cases (deprecated by NumPy.)\n idx = _eliminate_deprecated_list_indexing(idx)\n\n # Expand any (concrete) boolean indices. We can then use advanced integer\n # indexing logic to handle them.\n idx = _expand_bool_indices(idx, shape)\n\n leaves, treedef = tree_flatten(idx)\n dynamic = [None] * len(leaves)\n static = [None] * len(leaves)\n for i, x in enumerate(leaves):\n if x is Ellipsis:\n static[i] = x\n elif isinstance(x, slice):\n # slice objects aren't hashable.\n static[i] = (x.start, x.stop, x.step)\n else:\n dynamic[i] = x\n return treedef, tuple(static), dynamic\n\ndef _merge_static_and_dynamic_indices(treedef, static_idx, dynamic_idx):\n \"\"\"Recombines indices that were split by _split_index_for_jit.\"\"\"\n idx = []\n for s, d in zip(static_idx, dynamic_idx):\n if d is not None:\n idx.append(d)\n elif isinstance(s, tuple):\n idx.append(slice(s[0], s[1], s[2]))\n else:\n idx.append(s)\n return treedef.unflatten(idx)\n\ndef _int(aval):\n return not aval.shape and issubdtype(aval.dtype, integer)\n\ndef _index_to_gather(x_shape, idx, normalize_indices=True):\n # Remove ellipses and add trailing slice(None)s.\n idx = _canonicalize_tuple_index(len(x_shape), idx)\n\n # Check for advanced indexing:\n # https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing\n\n # Do the advanced indexing axes appear contiguously? If not, NumPy semantics\n # move the advanced axes to the front.\n advanced_axes_are_contiguous = False\n\n advanced_indexes = None\n\n # The positions of the advanced indexing axes in `idx`.\n idx_advanced_axes = []\n\n # The positions of the advanced indexes in x's shape.\n # collapsed, after None axes have been removed. See below.\n x_advanced_axes = None\n\n if _is_advanced_int_indexer(idx):\n idx_no_nones = [(i, d) for i, d in enumerate(idx) if d is not None]\n advanced_pairs = (\n (asarray(e), i, j) for j, (i, e) in enumerate(idx_no_nones)\n if isscalar(e) or isinstance(e, (Sequence, ndarray, np.ndarray)))\n if normalize_indices:\n advanced_pairs = ((_normalize_index(e, x_shape[j]), i, j)\n for e, i, j in advanced_pairs)\n advanced_indexes, idx_advanced_axes, x_advanced_axes = zip(*advanced_pairs)\n advanced_axes_are_contiguous = np.all(np.diff(idx_advanced_axes) == 1)\n\n x_axis = 0 # Current axis in x.\n y_axis = 0 # Current axis in y, before collapsing. See below.\n collapsed_y_axis = 0 # Current axis in y, after collapsing.\n\n # Scatter dimension numbers.\n offset_dims = []\n collapsed_slice_dims = []\n start_index_map = []\n\n use_64bit_index = _any([not core.is_constant_dim(d) or d >= (1 << 31) for d in x_shape])\n index_dtype = int64 if use_64bit_index else int32\n\n # Gather indices.\n # Pairs of (array, start_dim) values. These will be broadcast into\n # gather_indices_shape, with the array dimensions aligned to start_dim, and\n # then concatenated.\n gather_indices = []\n gather_indices_shape = []\n\n # We perform three transformations to y before the scatter op, in order:\n # First, y is broadcast to slice_shape. In general `y` only need broadcast to\n # the right shape.\n slice_shape = []\n\n # Next, y is squeezed to remove newaxis_dims. This removes np.newaxis/`None`\n # indices, which the scatter cannot remove itself.\n newaxis_dims = []\n\n # Finally, we reverse reversed_y_dims to handle slices with negative strides.\n reversed_y_dims = []\n\n gather_slice_shape = []\n\n for idx_pos, i in enumerate(idx):\n # Handle the advanced indices here if:\n # * the advanced indices were not contiguous and we are the start.\n # * we are at the position of the first advanced index.\n if (advanced_indexes is not None and\n (advanced_axes_are_contiguous and idx_pos == idx_advanced_axes[0] or\n not advanced_axes_are_contiguous and idx_pos == 0)):\n advanced_indexes = broadcast_arrays(*advanced_indexes)\n shape = advanced_indexes[0].shape\n ndim = len(shape)\n\n start_dim = len(gather_indices_shape)\n gather_indices += ((lax.convert_element_type(a, index_dtype), start_dim)\n for a in advanced_indexes)\n gather_indices_shape += shape\n\n start_index_map.extend(x_advanced_axes)\n collapsed_slice_dims.extend(x_advanced_axes)\n slice_shape.extend(shape)\n y_axis += ndim\n collapsed_y_axis += ndim\n\n # Per-index bookkeeping for advanced indexes.\n if idx_pos in idx_advanced_axes:\n x_axis += 1\n gather_slice_shape.append(1)\n continue\n\n try:\n abstract_i = core.get_aval(i)\n except TypeError:\n abstract_i = None\n # Handle basic int indexes.\n if isinstance(abstract_i, (ConcreteArray, ShapedArray)) and _int(abstract_i):\n if core.symbolic_equal_dim(x_shape[x_axis], 0):\n # XLA gives error when indexing into an axis of size 0\n raise IndexError(f\"index is out of bounds for axis {x_axis} with size 0\")\n i = _normalize_index(i, x_shape[x_axis]) if normalize_indices else i\n i = lax.convert_element_type(i, index_dtype)\n gather_indices.append((i, len(gather_indices_shape)))\n collapsed_slice_dims.append(x_axis)\n gather_slice_shape.append(1)\n start_index_map.append(x_axis)\n x_axis += 1\n # Handle np.newaxis (None)\n elif i is None:\n slice_shape.append(1)\n newaxis_dims.append(y_axis)\n y_axis += 1\n\n elif isinstance(i, slice):\n # Normalize the slice to use None when possible\n start, stop, step = i.start, i.stop, i.step\n try:\n if ((step is None or core.symbolic_equal_dim(step, 1)) and\n stop is not None and core.symbolic_equal_dim(stop, x_shape[x_axis])):\n # The following is a useful special case with shape polymorphism\n stop = None\n except TypeError:\n pass\n\n # Handle slice(None)\n if start is None and stop is None and step is None:\n slice_shape.append(x_shape[x_axis])\n gather_slice_shape.append(x_shape[x_axis])\n offset_dims.append(collapsed_y_axis)\n collapsed_y_axis += 1\n y_axis += 1\n x_axis += 1\n # Handle slice index (only static, otherwise an error is raised)\n else:\n if not _all(_is_slice_element_none_or_constant(elt)\n for elt in (start, stop, step)):\n msg = (\"Array slice indices must have static start/stop/step to be used \"\n \"with NumPy indexing syntax. \"\n f\"Found slice({start}, {stop}, {step}). \"\n \"To index a statically sized \"\n \"array at a dynamic position, try lax.dynamic_slice/\"\n \"dynamic_update_slice (JAX does not support dynamically sized \"\n \"arrays within JIT compiled functions).\")\n raise IndexError(msg)\n if not core.is_constant_dim(x_shape[x_axis]):\n msg = (\"Cannot use NumPy slice indexing on an array dimension whose \"\n f\"size is not statically known ({x_shape[x_axis]}). \"\n \"Try using lax.dynamic_slice/dynamic_update_slice\")\n raise IndexError(msg)\n start, limit, stride, needs_rev = _static_idx(slice(start, stop, step),\n x_shape[x_axis])\n if needs_rev:\n reversed_y_dims.append(collapsed_y_axis)\n if stride == 1:\n i = lax.convert_element_type(start, index_dtype)\n gather_indices.append((i, len(gather_indices_shape)))\n slice_shape.append(limit - start)\n gather_slice_shape.append(limit - start)\n offset_dims.append(collapsed_y_axis)\n start_index_map.append(x_axis)\n else:\n i = arange(start, limit, stride, dtype=index_dtype)\n size = i.shape[0]\n slice_shape.append(size)\n gather_slice_shape.append(1)\n gather_indices.append((i, len(gather_indices_shape)))\n gather_indices_shape.append(size)\n\n start_index_map.append(x_axis)\n collapsed_slice_dims.append(x_axis)\n\n collapsed_y_axis += 1\n y_axis += 1\n x_axis += 1\n else:\n if (abstract_i is not None and\n not (issubdtype(abstract_i.dtype, integer) or issubdtype(abstract_i.dtype, bool_))):\n msg = (\"Indexer must have integer or boolean type, got indexer \"\n \"with type {} at position {}, indexer value {}\")\n raise TypeError(msg.format(abstract_i.dtype.name, idx_pos, i))\n\n msg = \"Indexing mode not yet supported. Open a feature request!\\n{}\"\n raise IndexError(msg.format(idx))\n\n if len(gather_indices) == 0:\n gather_indices_array = np.zeros((0,), dtype=index_dtype)\n elif len(gather_indices) == 1:\n g, _ = gather_indices[0]\n gather_indices_array = lax.expand_dims(g, (g.ndim,))\n else:\n last_dim = len(gather_indices_shape)\n gather_indices_shape.append(1)\n gather_indices_array = lax.concatenate([\n lax.broadcast_in_dim(g, gather_indices_shape, tuple(range(i, i + g.ndim)))\n for g, i in gather_indices],\n last_dim)\n\n dnums = lax.GatherDimensionNumbers(\n offset_dims = tuple(offset_dims),\n collapsed_slice_dims = tuple(sorted(collapsed_slice_dims)),\n start_index_map = tuple(start_index_map)\n )\n return _Indexer(\n slice_shape=slice_shape,\n newaxis_dims=tuple(newaxis_dims),\n gather_slice_shape=gather_slice_shape,\n reversed_y_dims=reversed_y_dims,\n dnums=dnums,\n gather_indices=gather_indices_array,\n unique_indices=advanced_indexes is None,\n indices_are_sorted=advanced_indexes is None)\n\ndef _should_unpack_list_index(x):\n \"\"\"Helper for _eliminate_deprecated_list_indexing.\"\"\"\n return (isinstance(x, (np.ndarray, ndarray)) and np.ndim(x) != 0\n or isinstance(x, (Sequence, slice))\n or x is Ellipsis or x is None)\n\ndef _eliminate_deprecated_list_indexing(idx):\n # \"Basic slicing is initiated if the selection object is a non-array,\n # non-tuple sequence containing slice objects, [Ellipses, or newaxis\n # objects]\". Detects this and raises a TypeError.\n if not isinstance(idx, tuple):\n if isinstance(idx, Sequence) and not isinstance(idx, (ndarray, np.ndarray)):\n # As of numpy 1.16, some non-tuple sequences of indices result in a warning, while\n # others are converted to arrays, based on a set of somewhat convoluted heuristics\n # (See https://github.com/numpy/numpy/blob/v1.19.2/numpy/core/src/multiarray/mapping.c#L179-L343)\n # In JAX, we raise an informative TypeError for *all* non-tuple sequences.\n if _any(_should_unpack_list_index(i) for i in idx):\n msg = (\"Using a non-tuple sequence for multidimensional indexing is not allowed; \"\n \"use `arr[tuple(seq)]` instead of `arr[seq]`. \"\n \"See https://github.com/google/jax/issues/4564 for more information.\")\n else:\n msg = (\"Using a non-tuple sequence for multidimensional indexing is not allowed; \"\n \"use `arr[array(seq)]` instead of `arr[seq]`. \"\n \"See https://github.com/google/jax/issues/4564 for more information.\")\n raise TypeError(msg)\n else:\n idx = (idx,)\n return idx\n\ndef _is_boolean_index(i):\n try:\n abstract_i = core.get_aval(i)\n except TypeError:\n abstract_i = None\n return (isinstance(abstract_i, ShapedArray) and issubdtype(abstract_i.dtype, bool_)\n or isinstance(i, list) and i and _all(_is_scalar(e)\n and issubdtype(_dtype(e), np.bool_) for e in i))\n\ndef _expand_bool_indices(idx, shape):\n \"\"\"Converts concrete bool indexes into advanced integer indexes.\"\"\"\n out = []\n total_dims = len(shape)\n num_ellipsis = _sum(e is Ellipsis for e in idx)\n if num_ellipsis > 1:\n raise IndexError(\"an index can only have a single ellipsis ('...')\")\n elif num_ellipsis == 1:\n total_dims = _sum(_ndim(e) if _is_boolean_index(e) else 1 for e in idx\n if e is not None and e is not Ellipsis)\n ellipsis_offset = 0\n for dim_number, i in enumerate(idx):\n try:\n abstract_i = core.get_aval(i)\n except TypeError:\n abstract_i = None\n if _is_boolean_index(i):\n if isinstance(i, list):\n i = array(i)\n abstract_i = core.get_aval(i)\n\n if not type(abstract_i) is ConcreteArray:\n # TODO(mattjj): improve this error by tracking _why_ the indices are not concrete\n raise errors.NonConcreteBooleanIndexError(abstract_i)\n elif _ndim(i) == 0:\n raise TypeError(\"JAX arrays do not support boolean scalar indices\")\n else:\n i_shape = _shape(i)\n start = len(out) + ellipsis_offset\n expected_shape = shape[start: start + _ndim(i)]\n if i_shape != expected_shape:\n raise IndexError(\"boolean index did not match shape of indexed array in index \"\n f\"{dim_number}: got {i_shape}, expected {expected_shape}\")\n out.extend(np.where(i))\n else:\n out.append(i)\n if i is Ellipsis:\n ellipsis_offset = len(shape) - total_dims - 1\n return tuple(out)\n\n\ndef _is_slice_element_none_or_constant(elt):\n \"\"\"Return True if elt is a constant or None.\"\"\"\n if elt is None: return True\n try:\n return type(core.get_aval(elt)) is ConcreteArray\n except TypeError:\n return False\n\n# TODO(mattjj): clean up this logic\ndef _is_advanced_int_indexer(idx):\n \"\"\"Returns True if idx should trigger int array indexing, False otherwise.\"\"\"\n # https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing\n assert isinstance(idx, tuple)\n if _all(e is None or e is Ellipsis or isinstance(e, slice)\n or _is_scalar(e) and issubdtype(_dtype(e), np.integer) for e in idx):\n return False\n return _all(e is None or e is Ellipsis or isinstance(e, slice)\n or _is_int_arraylike(e) for e in idx)\n\ndef _is_int_arraylike(x):\n \"\"\"Returns True if x is array-like with integer dtype, False otherwise.\"\"\"\n return (isinstance(x, int) and not isinstance(x, bool)\n or issubdtype(getattr(x, \"dtype\", None), np.integer)\n or isinstance(x, (list, tuple)) and _all(_is_int_arraylike(e) for e in x))\n\ndef _is_scalar(x):\n \"\"\"Checks if a Python or NumPy scalar.\"\"\"\n return np.isscalar(x) or (isinstance(x, (np.ndarray, ndarray))\n and np.ndim(x) == 0)\n\ndef _canonicalize_tuple_index(arr_ndim, idx, array_name='array'):\n \"\"\"Helper to remove Ellipsis and add in the implicit trailing slice(None).\"\"\"\n len_without_none = _sum(1 for e in idx if e is not None and e is not Ellipsis)\n if len_without_none > arr_ndim:\n raise IndexError(\n f\"Too many indices for {array_name}: {len_without_none} \"\n f\"non-None/Ellipsis indices for dim {arr_ndim}.\")\n ellipses = (i for i, elt in enumerate(idx) if elt is Ellipsis)\n ellipsis_index = next(ellipses, None)\n if ellipsis_index is not None:\n if next(ellipses, None) is not None:\n raise IndexError(\n f\"Multiple ellipses (...) not supported: {list(map(type, idx))}.\")\n colons = (slice(None),) * (arr_ndim - len_without_none)\n idx = idx[:ellipsis_index] + colons + idx[ellipsis_index + 1:]\n elif len_without_none < arr_ndim:\n colons = (slice(None),) * (arr_ndim - len_without_none)\n idx = tuple(idx) + colons\n return idx\n\ndef _static_idx(idx: slice, size: core.DimSize):\n \"\"\"Helper function to compute the static slice start/limit/stride values.\"\"\"\n if isinstance(size, int):\n start, stop, step = idx.indices(size)\n else:\n raise TypeError(size)\n\n if (step < 0 and stop >= start) or (step > 0 and start >= stop):\n return 0, 0, 1, False # sliced to size zero\n\n if step > 0:\n return start, stop, step, False\n else:\n k = (start - stop - 1) % (-step)\n return stop + k + 1, start + 1, -step, True\n\n\nblackman = _wrap_numpy_nullary_function(np.blackman)\nbartlett = _wrap_numpy_nullary_function(np.bartlett)\nhamming = _wrap_numpy_nullary_function(np.hamming)\nhanning = _wrap_numpy_nullary_function(np.hanning)\n# TODO: lower `kaiser` via lax to allow non-constant beta values.\nkaiser = _wrap_numpy_nullary_function(np.kaiser)\n\ndef _gcd_cond_fn(xs):\n x1, x2 = xs\n return any(x2 != 0)\n\ndef _gcd_body_fn(xs):\n x1, x2 = xs\n x1, x2 = (where(x2 != 0, x2, x1),\n where(x2 != 0, lax.rem(x1, x2), lax._const(x2, 0)))\n return (where(x1 < x2, x2, x1), where(x1 < x2, x1, x2))\n\n@_wraps(np.gcd)\n@jit\ndef gcd(x1, x2):\n _check_arraylike(\"gcd\", x1, x2)\n if (not issubdtype(_dtype(x1), integer) or\n not issubdtype(_dtype(x2), integer)):\n raise ValueError(\"Arguments to jax.numpy.gcd must be integers.\")\n x1, x2 = _promote_dtypes(x1, x2)\n x1, x2 = broadcast_arrays(x1, x2)\n gcd, _ = lax.while_loop(_gcd_cond_fn, _gcd_body_fn, (abs(x1), abs(x2)))\n return gcd\n\n\n@_wraps(np.lcm)\n@jit\ndef lcm(x1, x2):\n _check_arraylike(\"lcm\", x1, x2)\n x1, x2 = _promote_dtypes(x1, x2)\n d = gcd(x1, x2)\n return where(d == 0, lax._const(d, 0),\n abs(multiply(x1, floor_divide(x2, d))))\n\n\n@_wraps(np.extract)\ndef extract(condition, arr):\n return compress(ravel(condition), ravel(arr))\n\n\n@_wraps(np.compress, skip_params=['out'])\ndef compress(condition, a, axis: Optional[int] = None, out=None):\n _check_arraylike(\"compress\", condition, a)\n if out is not None:\n raise NotImplementedError(\"The 'out' argument to jnp.compress is not supported.\")\n if ndim(condition) != 1:\n raise ValueError(\"condition must be a 1D array\")\n condition = asarray(condition).astype(bool)\n if axis is None:\n axis = 0\n a = ravel(a)\n else:\n a = moveaxis(a, axis, 0)\n condition, extra = condition[:a.shape[0]], condition[a.shape[0]:]\n if any(extra):\n raise ValueError(\"condition contains entries that are out of bounds\")\n a = a[:condition.shape[0]]\n return moveaxis(a[condition], 0, axis)\n\n\n@_wraps(np.cov)\n@partial(jit, static_argnames=('rowvar', 'bias', 'ddof'))\ndef cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,\n aweights=None):\n if y is not None:\n m, y = _promote_args_inexact(\"cov\", m, y)\n if y.ndim > 2:\n raise ValueError(\"y has more than 2 dimensions\")\n else:\n m, = _promote_args_inexact(\"cov\", m)\n\n if m.ndim > 2:\n raise ValueError(\"m has more than 2 dimensions\") # same as numpy error\n\n X = atleast_2d(m)\n if not rowvar and X.shape[0] != 1:\n X = X.T\n if X.shape[0] == 0:\n return array([]).reshape(0, 0)\n\n if y is not None:\n y = atleast_2d(y)\n if not rowvar and y.shape[0] != 1:\n y = y.T\n X = concatenate((X, y), axis=0)\n if ddof is None:\n ddof = 1 if bias == 0 else 0\n\n w = None\n if fweights is not None:\n _check_arraylike(\"cov\", fweights)\n if ndim(fweights) > 1:\n raise RuntimeError(\"cannot handle multidimensional fweights\")\n if shape(fweights)[0] != X.shape[1]:\n raise RuntimeError(\"incompatible numbers of samples and fweights\")\n if not issubdtype(_dtype(fweights), integer):\n raise TypeError(\"fweights must be integer.\")\n # Ensure positive fweights; note that numpy raises an error on negative fweights.\n w = asarray(abs(fweights))\n if aweights is not None:\n _check_arraylike(\"cov\", aweights)\n if ndim(aweights) > 1:\n raise RuntimeError(\"cannot handle multidimensional aweights\")\n if shape(aweights)[0] != X.shape[1]:\n raise RuntimeError(\"incompatible numbers of samples and aweights\")\n # Ensure positive aweights: note that numpy raises an error for negative aweights.\n aweights = abs(aweights)\n w = aweights if w is None else w * aweights\n\n avg, w_sum = average(X, axis=1, weights=w, returned=True)\n w_sum = w_sum[0]\n\n if w is None:\n f = X.shape[1] - ddof\n elif ddof == 0:\n f = w_sum\n elif aweights is None:\n f = w_sum - ddof\n else:\n f = w_sum - ddof * sum(w * aweights) / w_sum\n\n X = X - avg[:, None]\n X_T = X.T if w is None else (X * lax.broadcast_to_rank(w, X.ndim)).T\n return true_divide(dot(X, X_T.conj()), f).squeeze()\n\n\n@_wraps(np.corrcoef)\n@partial(jit, static_argnames=('rowvar',))\ndef corrcoef(x, y=None, rowvar=True):\n _check_arraylike(\"corrcoef\", x)\n c = cov(x, y, rowvar)\n if len(shape(c)) == 0:\n # scalar - this should yield nan for values (nan/nan, inf/inf, 0/0), 1 otherwise\n return divide(c, c)\n d = diag(c)\n stddev = sqrt(real(d))\n c = divide(c, stddev[:,None])\n c = divide(c, stddev[None,:])\n\n real_part = clip(real(c), -1, 1)\n if iscomplexobj(c):\n complex_part = clip(imag(c), -1, 1)\n c = lax.complex(real_part, complex_part)\n else:\n c = real_part\n return c\n\n\n@_wraps(np.quantile, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'interpolation',\n 'keepdims'))\ndef quantile(a, q, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n overwrite_input=False, interpolation=\"linear\", keepdims=False):\n _check_arraylike(\"quantile\", a, q)\n if overwrite_input or out is not None:\n msg = (\"jax.numpy.quantile does not support overwrite_input=True or \"\n \"out != None\")\n raise ValueError(msg)\n return _quantile(a, q, axis, interpolation, keepdims, False)\n\n@_wraps(np.nanquantile, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'interpolation',\n 'keepdims'))\ndef nanquantile(a, q, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n out=None, overwrite_input=False, interpolation=\"linear\",\n keepdims=False):\n _check_arraylike(\"nanquantile\", a, q)\n if overwrite_input or out is not None:\n msg = (\"jax.numpy.nanquantile does not support overwrite_input=True or \"\n \"out != None\")\n raise ValueError(msg)\n return _quantile(a, q, axis, interpolation, keepdims, True)\n\ndef _quantile(a, q, axis, interpolation, keepdims, squash_nans):\n if interpolation not in [\"linear\", \"lower\", \"higher\", \"midpoint\", \"nearest\"]:\n raise ValueError(\"interpolation can only be 'linear', 'lower', 'higher', \"\n \"'midpoint', or 'nearest'\")\n a, q = _promote_dtypes_inexact(a, q)\n if issubdtype(a.dtype, np.complexfloating):\n raise ValueError(\"quantile does not support complex input, as the operation is poorly defined.\")\n if axis is None:\n a = ravel(a)\n axis = 0\n elif isinstance(axis, tuple):\n raise NotImplementedError(\"Tuple values for axis are not implemented\")\n else:\n axis = _canonicalize_axis(axis, ndim(a))\n\n q_shape = shape(q)\n q_ndim = ndim(q)\n if q_ndim > 1:\n raise ValueError(\"q must be have rank <= 1, got shape {}\".format(shape(q)))\n\n a_shape = shape(a)\n\n if squash_nans:\n a = where(isnan(a), nan, a) # Ensure nans are positive so they sort to the end.\n a = lax.sort(a, dimension=axis)\n counts = sum(logical_not(isnan(a)), axis=axis, dtype=q.dtype,\n keepdims=keepdims)\n shape_after_reduction = counts.shape\n q = lax.expand_dims(\n q, tuple(range(q_ndim, len(shape_after_reduction) + q_ndim)))\n counts = lax.expand_dims(counts, tuple(range(q_ndim)))\n q = lax.mul(q, lax.sub(counts, _constant_like(q, 1)))\n low = lax.floor(q)\n high = lax.ceil(q)\n high_weight = lax.sub(q, low)\n low_weight = lax.sub(_constant_like(high_weight, 1), high_weight)\n\n low = lax.max(_constant_like(low, 0), lax.min(low, counts - 1))\n high = lax.max(_constant_like(high, 0), lax.min(high, counts - 1))\n low = lax.convert_element_type(low, int64)\n high = lax.convert_element_type(high, int64)\n out_shape = q_shape + shape_after_reduction\n index = [lax.broadcasted_iota(int64, out_shape, dim + q_ndim)\n for dim in range(len(shape_after_reduction))]\n if keepdims:\n index[axis] = low\n else:\n index.insert(axis, low)\n low_value = a[tuple(index)]\n index[axis] = high\n high_value = a[tuple(index)]\n else:\n a = where(any(isnan(a), axis=axis, keepdims=True), nan, a)\n a = lax.sort(a, dimension=axis)\n n = a_shape[axis]\n q = lax.mul(q, _constant_like(q, n - 1))\n low = lax.floor(q)\n high = lax.ceil(q)\n high_weight = lax.sub(q, low)\n low_weight = lax.sub(_constant_like(high_weight, 1), high_weight)\n\n low = lax.clamp(_constant_like(low, 0), low, _constant_like(low, n - 1))\n high = lax.clamp(_constant_like(high, 0), high, _constant_like(high, n - 1))\n low = lax.convert_element_type(low, int64)\n high = lax.convert_element_type(high, int64)\n\n slice_sizes = list(a_shape)\n slice_sizes[axis] = 1\n dnums = lax.GatherDimensionNumbers(\n offset_dims=tuple(range(\n q_ndim,\n len(a_shape) + q_ndim if keepdims else len(a_shape) + q_ndim - 1)),\n collapsed_slice_dims=() if keepdims else (axis,),\n start_index_map=(axis,))\n low_value = lax.gather(a, low[..., None], dimension_numbers=dnums,\n slice_sizes=slice_sizes)\n high_value = lax.gather(a, high[..., None], dimension_numbers=dnums,\n slice_sizes=slice_sizes)\n if q_ndim == 1:\n low_weight = lax.broadcast_in_dim(low_weight, low_value.shape,\n broadcast_dimensions=(0,))\n high_weight = lax.broadcast_in_dim(high_weight, high_value.shape,\n broadcast_dimensions=(0,))\n\n if interpolation == \"linear\":\n result = lax.add(lax.mul(low_value.astype(q.dtype), low_weight),\n lax.mul(high_value.astype(q.dtype), high_weight))\n elif interpolation == \"lower\":\n result = low_value\n elif interpolation == \"higher\":\n result = high_value\n elif interpolation == \"nearest\":\n pred = lax.le(high_weight, _constant_like(high_weight, 0.5))\n result = lax.select(pred, low_value, high_value)\n elif interpolation == \"midpoint\":\n result = lax.mul(lax.add(low_value, high_value), _constant_like(low_value, 0.5))\n else:\n raise ValueError(f\"interpolation={interpolation!r} not recognized\")\n\n return lax.convert_element_type(result, a.dtype)\n\n\n@partial(vectorize, excluded={0, 2})\ndef _searchsorted(a, v, side):\n if len(a) == 0:\n return 0\n op = operator.le if side == 'left' else operator.lt\n\n def body_fun(i, state):\n low, high = state\n mid = (low + high) // 2\n go_left = op(v, a[mid])\n return (where(go_left, low, mid), where(go_left, mid, high))\n\n n_levels = int(np.ceil(np.log2(len(a) + 1)))\n return lax.fori_loop(0, n_levels, body_fun, (0, len(a)))[1]\n\n\n@_wraps(np.searchsorted, skip_params=['sorter'])\n@partial(jit, static_argnames=('side', 'sorter'))\ndef searchsorted(a, v, side='left', sorter=None):\n _check_arraylike(\"searchsorted\", a, v)\n if side not in ['left', 'right']:\n raise ValueError(f\"{side!r} is an invalid value for keyword 'side'\")\n if sorter is not None:\n raise NotImplementedError(\"sorter is not implemented\")\n if ndim(a) != 1:\n raise ValueError(\"a should be 1-dimensional\")\n return _searchsorted(a, v, side)\n\n\n@_wraps(np.digitize)\n@partial(jit, static_argnames=('right',))\ndef digitize(x, bins, right=False):\n _check_arraylike(\"digitize\", x, bins)\n right = core.concrete_or_error(bool, right, \"right argument of jnp.digitize()\")\n if ndim(bins) != 1:\n raise ValueError(f\"digitize: bins must be a 1-dimensional array; got bins={bins}\")\n if len(bins) == 0:\n return zeros(x, dtype=dtypes.canonicalize_dtype(int_))\n side = 'right' if not right else 'left'\n return where(\n bins[-1] >= bins[0],\n searchsorted(bins, x, side=side),\n len(bins) - searchsorted(bins[::-1], x, side=side)\n )\n\n_PIECEWISE_DOC = \"\"\"\\\nUnlike `np.piecewise`, :py:func:`jax.numpy.piecewise` requires functions in\n`funclist` to be traceable by JAX, as it is implemented via :func:`jax.lax.switch`.\nSee the :func:`jax.lax.switch` documentation for more information.\n\"\"\"\n\n@_wraps(np.piecewise, lax_description=_PIECEWISE_DOC)\ndef piecewise(x, condlist, funclist, *args, **kw):\n _check_arraylike(\"piecewise\", x)\n condlist = array(condlist, dtype=bool_)\n nc, nf = len(condlist), len(funclist)\n if nf == nc + 1:\n funclist = funclist[-1:] + funclist[:-1]\n elif nf == nc:\n funclist = [0] + list(funclist)\n else:\n raise ValueError(f\"with {nc} condition(s), either {nc} or {nc+1} functions are expected; got {nf}\")\n consts = {i: c for i, c in enumerate(funclist) if not callable(c)}\n funcs = {i: f for i, f in enumerate(funclist) if callable(f)}\n return _piecewise(x, condlist, consts,\n frozenset(funcs.items()), # dict is not hashable.\n *args, **kw)\n\n@partial(jit, static_argnames=['funcs'])\ndef _piecewise(x, condlist, consts, funcs, *args, **kw):\n funcs = dict(funcs)\n funclist = [consts.get(i, funcs.get(i)) for i in range(len(condlist) + 1)]\n indices = argmax(cumsum(concatenate([zeros_like(condlist[:1]), condlist], 0), 0), 0)\n dtype = _dtype(x)\n def _call(f):\n return lambda x: f(x, *args, **kw).astype(dtype)\n def _const(v):\n return lambda x: array(v, dtype=dtype)\n funclist = [_call(f) if callable(f) else _const(f) for f in funclist]\n return vectorize(lax.switch, excluded=(1,))(indices, funclist, x)\n\n\n@_wraps(np.percentile, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'interpolation',\n 'keepdims'))\ndef percentile(a, q, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n out=None, overwrite_input=False, interpolation=\"linear\",\n keepdims=False):\n _check_arraylike(\"percentile\", a, q)\n a, q = _promote_dtypes_inexact(a, q)\n q = true_divide(q, 100.0)\n return quantile(a, q, axis=axis, out=out, overwrite_input=overwrite_input,\n interpolation=interpolation, keepdims=keepdims)\n\n@_wraps(np.nanpercentile, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'interpolation',\n 'keepdims'))\ndef nanpercentile(a, q, axis: Optional[Union[int, Tuple[int, ...]]] = None,\n out=None, overwrite_input=False, interpolation=\"linear\",\n keepdims=False):\n _check_arraylike(\"nanpercentile\", a, q)\n q = true_divide(q, float32(100.0))\n return nanquantile(a, q, axis=axis, out=out, overwrite_input=overwrite_input,\n interpolation=interpolation, keepdims=keepdims)\n\n@_wraps(np.median, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'keepdims'))\ndef median(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n overwrite_input=False, keepdims=False):\n _check_arraylike(\"median\", a)\n return quantile(a, 0.5, axis=axis, out=out, overwrite_input=overwrite_input,\n keepdims=keepdims, interpolation='midpoint')\n\n@_wraps(np.nanmedian, skip_params=['out', 'overwrite_input'])\n@partial(jit, static_argnames=('axis', 'overwrite_input', 'keepdims'))\ndef nanmedian(a, axis: Optional[Union[int, Tuple[int, ...]]] = None, out=None,\n overwrite_input=False, keepdims=False):\n _check_arraylike(\"nanmedian\", a)\n return nanquantile(a, 0.5, axis=axis, out=out,\n overwrite_input=overwrite_input, keepdims=keepdims,\n interpolation='midpoint')\n\n\ndef _astype(arr, dtype):\n if dtype is None:\n dtype = dtypes.canonicalize_dtype(float_)\n lax._check_user_dtype_supported(dtype, \"astype\")\n return lax.convert_element_type(arr, dtype)\n\n\ndef _nbytes(arr):\n return size(arr) * _dtype(arr).itemsize\n\n\ndef _clip(number, min=None, max=None, out=None, *, a_min=None, a_max=None):\n # ndarray.clip has a slightly different API from clip (min -> a_min, max -> a_max)\n # TODO: remove after deprecation window\n if a_min is not None or a_max is not None:\n warnings.warn('`a_min` and `a_max` keyword arguments to ndarray.clip are deprecated '\n 'in favor of `min` and `max` for compatibility with numpy. '\n 'They will be removed in JAX 0.22.2', FutureWarning)\n if min is None and a_min is not None:\n min = a_min\n if max is None and a_max is not None:\n max = a_max\n return clip(number, a_min=min, a_max=max, out=out)\n\n\ndef _view(arr, dtype=None, type=None):\n lax._check_user_dtype_supported(dtype, \"view\")\n if type is not None:\n raise NotImplementedError(\"`type` argument of array.view()\")\n if dtype is None:\n return arr\n arr_dtype = _dtype(arr)\n if arr_dtype == dtype:\n return arr\n # bool is implemented as lax:PRED, which is not compatible with lax.bitcast_convert_type.\n # We work around this by casting bool to uint8.\n if arr_dtype == bool_:\n arr = arr.astype(uint8)\n nbits_in = 8 * arr_dtype.itemsize\n nbits_out = 8 * np.dtype(dtype).itemsize\n if nbits_in == nbits_out:\n if dtype == bool_:\n return lax.bitcast_convert_type(arr, uint8).astype(dtype)\n return lax.bitcast_convert_type(arr, dtype)\n if nbits_out > nbits_in and (shape(arr)[-1] * nbits_in) % nbits_out != 0:\n raise ValueError(\"When changing to a larger dtype, its size must be a divisor \"\n \"of the total size in bytes of the last axis of the array.\")\n byte_dtypes = {8: uint8, 16: uint16, 32: uint32, 64: uint64}\n if nbits_in not in byte_dtypes:\n raise NotImplementedError(f\"arr.view() for arr.dtype={arr_dtype}\")\n if nbits_out not in byte_dtypes:\n raise NotImplementedError(f\"arr.view(dtype) for dtype={dtype}\")\n dt_in = byte_dtypes[nbits_in]\n dt_out = byte_dtypes[nbits_out]\n arr_bytes = lax.bitcast_convert_type(arr, dt_in)\n if nbits_in < nbits_out:\n arr_bytes = arr_bytes.reshape(arr.shape[:-1] + (-1, nbits_out // nbits_in)).astype(dt_out)\n shifts = expand_dims(arange(0, nbits_out, nbits_in, dtype=dt_out), tuple(range(arr_bytes.ndim - 1)))\n arr_bytes = (arr_bytes << shifts).sum(-1).astype(dt_out)\n else:\n shifts = lax.expand_dims(arange(0, nbits_in, nbits_out, dtype=dt_in), tuple(range(arr_bytes.ndim)))\n arr_bytes = ((arr_bytes[..., newaxis] >> shifts) & iinfo(dt_out).max).astype(dt_out)\n arr_bytes = arr_bytes.reshape(arr_bytes.shape[:-2] + (-1,))\n if dtype == bool_:\n return lax.bitcast_convert_type(arr_bytes, uint8).astype(dtype)\n return lax.bitcast_convert_type(arr_bytes, dtype)\n\n### track unimplemented functions\n\n_NOT_IMPLEMENTED_DESC = \"\"\"\n*** This function is not yet implemented by jax.numpy, and will raise NotImplementedError ***\n\"\"\"\n\ndef _not_implemented(fun):\n @_wraps(fun, update_doc=False, lax_description=_NOT_IMPLEMENTED_DESC)\n def wrapped(*args, **kwargs):\n msg = \"Numpy function {} not yet implemented\"\n raise NotImplementedError(msg.format(fun))\n return wrapped\n\n\n### add method and operator overloads to arraylike classes\n\n# We add operator overloads to DeviceArray and ShapedArray. These method and\n# operator overloads mainly just forward calls to the corresponding lax_numpy\n# functions, which can themselves handle instances from any of these classes.\n\n_scalar_types = (int, float, complex, np.generic)\n_accepted_binop_types = (int, float, complex, np.generic, np.ndarray, ndarray)\n\ndef _defer_to_unrecognized_arg(binary_op):\n # Ensure that other array types have the chance to override arithmetic.\n def deferring_binary_op(self, other):\n if not isinstance(other, _accepted_binop_types):\n return NotImplemented\n return binary_op(self, other)\n return deferring_binary_op\n\ndef _swap_args(f):\n return lambda x, y: f(y, x)\n\ndef _unimplemented_setitem(self, i, x):\n msg = (\"'{}' object does not support item assignment. JAX arrays are \"\n \"immutable. Instead of ``x[idx] = y``, use ``x = x.at[idx].set(y)`` \"\n \"or another .at[] method: \"\n \"https://jax.readthedocs.io/en/latest/_autosummary/jax.numpy.ndarray.at.html\")\n raise TypeError(msg.format(type(self)))\n\ndef _operator_round(number, ndigits=None):\n out = round(number, decimals=ndigits or 0)\n # If `ndigits` is None, for a builtin float round(7.5) returns an integer.\n return out.astype(int) if ndigits is None else out\n\n_operators = {\n \"getitem\": _rewriting_take,\n \"setitem\": _unimplemented_setitem,\n \"neg\": negative,\n \"pos\": positive,\n \"eq\": _defer_to_unrecognized_arg(equal),\n \"ne\": _defer_to_unrecognized_arg(not_equal),\n \"lt\": _defer_to_unrecognized_arg(less),\n \"le\": _defer_to_unrecognized_arg(less_equal),\n \"gt\": _defer_to_unrecognized_arg(greater),\n \"ge\": _defer_to_unrecognized_arg(greater_equal),\n \"abs\": abs,\n \"add\": _defer_to_unrecognized_arg(add),\n \"radd\": _defer_to_unrecognized_arg(add),\n \"sub\": _defer_to_unrecognized_arg(subtract),\n \"rsub\": _defer_to_unrecognized_arg(_swap_args(subtract)),\n \"mul\": _defer_to_unrecognized_arg(multiply),\n \"rmul\": _defer_to_unrecognized_arg(multiply),\n \"div\": _defer_to_unrecognized_arg(divide),\n \"rdiv\": _defer_to_unrecognized_arg(_swap_args(divide)),\n \"truediv\": _defer_to_unrecognized_arg(true_divide),\n \"rtruediv\": _defer_to_unrecognized_arg(_swap_args(true_divide)),\n \"floordiv\": _defer_to_unrecognized_arg(floor_divide),\n \"rfloordiv\": _defer_to_unrecognized_arg(_swap_args(floor_divide)),\n \"divmod\": _defer_to_unrecognized_arg(divmod),\n \"rdivmod\": _defer_to_unrecognized_arg(_swap_args(divmod)),\n \"mod\": _defer_to_unrecognized_arg(mod),\n \"rmod\": _defer_to_unrecognized_arg(_swap_args(mod)),\n \"pow\": _defer_to_unrecognized_arg(power),\n \"rpow\": _defer_to_unrecognized_arg(_swap_args(power)),\n \"matmul\": _defer_to_unrecognized_arg(matmul),\n \"rmatmul\": _defer_to_unrecognized_arg(_swap_args(matmul)),\n \"and\": _defer_to_unrecognized_arg(bitwise_and),\n \"rand\": _defer_to_unrecognized_arg(bitwise_and),\n \"or\": _defer_to_unrecognized_arg(bitwise_or),\n \"ror\": _defer_to_unrecognized_arg(bitwise_or),\n \"xor\": _defer_to_unrecognized_arg(bitwise_xor),\n \"rxor\": _defer_to_unrecognized_arg(bitwise_xor),\n \"invert\": bitwise_not,\n \"lshift\": _defer_to_unrecognized_arg(left_shift),\n \"rshift\": _defer_to_unrecognized_arg(right_shift),\n \"rlshift\": _defer_to_unrecognized_arg(_swap_args(left_shift)),\n \"rrshift\": _defer_to_unrecognized_arg(_swap_args(right_shift)),\n \"round\": _operator_round,\n}\n\n# These numpy.ndarray methods are just refs to an equivalent numpy function\n_nondiff_methods = [\"all\", \"any\", \"argmax\", \"argmin\", \"argpartition\", \"argsort\",\n \"nonzero\", \"searchsorted\", \"round\"]\n_diff_methods = [\"choose\", \"conj\", \"conjugate\", \"cumprod\", \"cumsum\",\n \"diagonal\", \"dot\", \"max\", \"mean\", \"min\", \"prod\", \"ptp\",\n \"ravel\", \"repeat\", \"sort\", \"squeeze\", \"std\", \"sum\",\n \"swapaxes\", \"take\", \"tile\", \"trace\", \"var\"]\n\n# These methods are mentioned explicitly by nondiff_methods, so we create\n# _not_implemented implementations of them here rather than in __init__.py.\n# TODO(phawkins): implement these.\nargpartition = _not_implemented(np.argpartition)\n_NOT_IMPLEMENTED = ['argpartition']\n\n\n# Experimental support for NumPy's module dispatch with NEP-37.\n# Currently requires https://github.com/seberg/numpy-dispatch\n_JAX_ARRAY_TYPES = (device_array.DeviceArray, core.Tracer)\n_HANDLED_ARRAY_TYPES = _JAX_ARRAY_TYPES + (np.ndarray,)\n\ndef __array_module__(self, types):\n if builtins.all(issubclass(t, _HANDLED_ARRAY_TYPES) for t in types):\n return jax.numpy\n else:\n return NotImplemented\n\n\ndef _compress_method(a, condition, axis=None, out=None):\n return compress(condition, a, axis, out)\n\n\n@partial(jit, static_argnums=(1,2,3))\ndef _multi_slice(arr,\n start_indices: Tuple[Tuple[int, ...]],\n limit_indices: Tuple[Tuple[int, ...]],\n removed_dims: Tuple[Tuple[int, ...]]):\n \"\"\"Extracts multiple slices from `arr`.\n\n This is used to shard DeviceArray arguments to pmap. It's implemented as a\n DeviceArray method here to avoid circular imports.\n \"\"\"\n results = []\n for starts, limits, removed in safe_zip(start_indices, limit_indices, removed_dims):\n sliced = lax.slice(arr, starts, limits)\n if removed:\n sliced = lax.squeeze(sliced, removed)\n results.append(sliced)\n return results\n\n# The next two functions are related to iter(device_array), implemented here to\n# avoid circular imports.\n@jit\ndef _unstack(x):\n return [lax.index_in_dim(x, i, keepdims=False) for i in range(x.shape[0])]\nsetattr(device_array.DeviceArray, \"_unstack\", _unstack)\ndef _chunk_iter(x, size):\n if size > x.shape[0]:\n yield x\n else:\n num_chunks, tail = divmod(x.shape[0], size)\n for i in range(num_chunks):\n yield lax.dynamic_slice_in_dim(x, i * size, size)\n if tail:\n yield lax.dynamic_slice_in_dim(x, num_chunks * size, tail)\nsetattr(device_array.DeviceArray, \"_chunk_iter\", _chunk_iter)\n\n# Syntactic sugar for scatter operations.\nclass _IndexUpdateHelper:\n # Note: this docstring will appear as the docstring for the `at` property.\n \"\"\"Helper property for index update functionality.\n\n The ``at`` property provides a functionally pure equivalent of in-place\n array modificatons.\n\n In particular:\n\n ============================== ================================\n Alternate syntax Equivalent In-place expression\n ============================== ================================\n ``x = x.at[idx].set(y)`` ``x[idx] = y``\n ``x = x.at[idx].add(y)`` ``x[idx] += y``\n ``x = x.at[idx].multiply(y)`` ``x[idx] *= y``\n ``x = x.at[idx].divide(y)`` ``x[idx] /= y``\n ``x = x.at[idx].power(y)`` ``x[idx] **= y``\n ``x = x.at[idx].min(y)`` ``x[idx] = minimum(x[idx], y)``\n ``x = x.at[idx].max(y)`` ``x[idx] = maximum(x[idx], y)``\n ``x = x.at[idx].get()`` ``x = x[idx]``\n ============================== ================================\n\n None of the ``x.at`` expressions modify the original ``x``; instead they return\n a modified copy of ``x``. However, inside a :py:func:`~jax.jit` compiled function,\n expressions like :code:`x = x.at[idx].set(y)` are guaranteed to be applied in-place.\n\n Unlike NumPy in-place operations such as :code:`x[idx] += y`, if multiple\n indices refer to the same location, all updates will be applied (NumPy would\n only apply the last update, rather than applying all updates.) The order\n in which conflicting updates are applied is implementation-defined and may be\n nondeterministic (e.g., due to concurrency on some hardware platforms).\n\n By default, JAX assumes that all indices are in-bounds. There is experimental\n support for giving more precise semantics to out-of-bounds indexed accesses,\n via the ``mode`` parameter (see below).\n\n Arguments\n ---------\n mode : str\n Specify out-of-bound indexing mode. Options are:\n\n - ``\"promise_in_bounds\"``: (default) The user promises that indices are in bounds.\n No additional checking will be performed. In practice, this means that\n out-of-bounds indices in ``get()`` will be clipped, and out-of-bounds indices\n in ``set()``, ``add()``, etc. will be dropped.\n - ``\"clip\"``: clamp out of bounds indices into valid range.\n - ``\"drop\"``: ignore out-of-bound indices.\n - ``\"fill\"``: alias for ``\"drop\"``. For `get()`, the optional ``fill_value``\n argument specifies the value that will be returned.\n\n indices_are_sorted : bool\n If True, the implementation will assume that the indices passed to ``at[]``\n are sorted in ascending order, which can lead to more efficient execution\n on some backends.\n unique_indices : bool\n If True, the implementation will assume that the indices passed to ``at[]``\n are unique, which can result in more efficient execution on some backends.\n fill_value : Any\n Only applies to the ``get()`` method: the fill value to return for out-of-bounds\n slices when `mode` is ``'fill'``. Ignored otherwise. Defaults to ``NaN`` for\n inexact types, the largest negative value for signed types, the largest positive\n value for unsigned types, and ``True`` for booleans.\n\n Examples\n --------\n >>> x = jnp.arange(5.0)\n >>> x\n DeviceArray([0., 1., 2., 3., 4.], dtype=float32)\n >>> x.at[2].add(10)\n DeviceArray([ 0., 1., 12., 3., 4.], dtype=float32)\n >>> x.at[10].add(10) # out-of-bounds indices are ignored\n DeviceArray([0., 1., 2., 3., 4.], dtype=float32)\n >>> x.at[20].add(10, mode='clip')\n DeviceArray([ 0., 1., 2., 3., 14.], dtype=float32)\n >>> x.at[2].get()\n DeviceArray(2., dtype=float32)\n >>> x.at[20].get() # out-of-bounds indices clipped\n DeviceArray(4., dtype=float32)\n >>> x.at[20].get(mode='fill') # out-of-bounds indices filled with NaN\n DeviceArray(nan, dtype=float32)\n >>> x.at[20].get(mode='fill', fill_value=-1) # custom fill value\n DeviceArray(-1., dtype=float32)\n \"\"\"\n __slots__ = (\"array\",)\n\n def __init__(self, array):\n self.array = array\n\n def __getitem__(self, index):\n return _IndexUpdateRef(self.array, index)\n\n def __repr__(self):\n return f\"_IndexUpdateHelper({repr(self.array)})\"\nndarray.at.__doc__ = _IndexUpdateHelper.__doc__\n\n_power_fn = power\n_divide_fn = divide\n\nclass _IndexUpdateRef:\n \"\"\"Helper object to call indexed update functions for an (advanced) index.\n\n This object references a source array and a specific indexer into that array.\n Methods on this object return copies of the source array that have been\n modified at the positions specified by the indexer.\n \"\"\"\n __slots__ = (\"array\", \"index\")\n\n def __init__(self, array, index):\n self.array = array\n self.index = index\n\n def __repr__(self):\n return f\"_IndexUpdateRef({repr(self.array)}, {repr(self.index)})\"\n\n def get(self, indices_are_sorted=False, unique_indices=False,\n mode=None, fill_value=None):\n \"\"\"Equivalent to ``x[idx]``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexing <numpy.doc.indexing>` ``x[idx]``. This function differs from\n the usual array indexing syntax in that it allows additional keyword\n arguments ``indices_are_sorted`` and ``unique_indices`` to be passed.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return _rewriting_take(self.array, self.index,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode,\n fill_value=fill_value)\n\n def set(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] = y``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>` ``x[idx] = y``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return scatter._scatter_update(self.array, self.index, values, lax.scatter,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode)\n\n def add(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] += y``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>` ``x[idx] += y``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return scatter._scatter_update(self.array, self.index, values,\n lax.scatter_add,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode)\n\n def multiply(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] *= y``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>` ``x[idx] *= y``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return scatter._scatter_update(self.array, self.index, values,\n lax.scatter_mul,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices,\n mode=mode)\n mul = multiply\n\n def divide(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] /= y``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>` ``x[idx] /= y``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return _divide_fn(\n self.array,\n scatter._scatter_update(ones_like(self.array), self.index, values,\n lax.scatter_mul,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode))\n\n def power(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] **= y``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>` ``x[idx] **= y``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return _power_fn(\n self.array,\n scatter._scatter_update(ones_like(self.array), self.index, values,\n lax.scatter_mul,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode))\n\n def min(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] = minimum(x[idx], y)``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>`\n ``x[idx] = minimum(x[idx], y)``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return scatter._scatter_update(self.array, self.index, values,\n lax.scatter_min,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode)\n\n def max(self, values, indices_are_sorted=False, unique_indices=False,\n mode=None):\n \"\"\"Pure equivalent of ``x[idx] = maximum(x[idx], y)``.\n\n Returns the value of ``x`` that would result from the NumPy-style\n :mod:indexed assignment <numpy.doc.indexing>`\n ``x[idx] = maximum(x[idx], y)``.\n\n See :mod:`jax.ops` for details.\n \"\"\"\n return scatter._scatter_update(self.array, self.index, values,\n lax.scatter_max,\n indices_are_sorted=indices_are_sorted,\n unique_indices=unique_indices, mode=mode)\n\n\ndef _set_shaped_array_attributes(shaped_array):\n # Set up operator, method, and property forwarding on Tracer instances\n # containing\n # ShapedArray avals by following the forwarding conventions for Tracer.\n # Forward operators using a single-underscore-prefix naming convention:\n for operator_name, function in _operators.items():\n setattr(shaped_array, \"_{}\".format(operator_name), staticmethod(function))\n # Forward methods and properties using core.{aval_method, aval_property}:\n for method_name in _nondiff_methods + _diff_methods:\n setattr(shaped_array, method_name, core.aval_method(globals()[method_name]))\n setattr(shaped_array, \"reshape\", core.aval_method(_reshape))\n setattr(shaped_array, \"transpose\", core.aval_method(_transpose))\n setattr(shaped_array, \"flatten\", core.aval_method(ravel))\n setattr(shaped_array, \"T\", core.aval_property(transpose))\n setattr(shaped_array, \"real\", core.aval_property(real))\n setattr(shaped_array, \"imag\", core.aval_property(imag))\n setattr(shaped_array, \"astype\", core.aval_method(_astype))\n setattr(shaped_array, \"view\", core.aval_method(_view))\n setattr(shaped_array, \"nbytes\", core.aval_property(_nbytes))\n setattr(shaped_array, \"clip\", core.aval_method(_clip))\n\n setattr(shaped_array, \"_array_module\", staticmethod(__array_module__))\n setattr(shaped_array, \"broadcast\", core.aval_method(lax.broadcast))\n setattr(shaped_array, \"broadcast_in_dim\", core.aval_method(lax.broadcast_in_dim))\n setattr(shaped_array, \"split\", core.aval_method(split))\n setattr(shaped_array, \"compress\", _compress_method)\n setattr(shaped_array, \"at\", core.aval_property(_IndexUpdateHelper))\n setattr(shaped_array, \"item\", core.aval_method(device_array.DeviceArray.item))\n\n_set_shaped_array_attributes(ShapedArray)\n\n\ndef _set_device_array_base_attributes(device_array):\n # Forward operators, methods, and properties on DeviceArray to lax_numpy\n # functions (with no Tracers involved; this forwarding is direct)\n for operator_name, function in _operators.items():\n setattr(device_array, \"__{}__\".format(operator_name), function)\n for method_name in _nondiff_methods + _diff_methods:\n setattr(device_array, method_name, globals()[method_name])\n setattr(device_array, \"reshape\", _reshape)\n setattr(device_array, \"transpose\", _transpose)\n setattr(device_array, \"flatten\", ravel)\n setattr(device_array, \"T\", property(transpose))\n setattr(device_array, \"real\", property(real))\n setattr(device_array, \"imag\", property(imag))\n setattr(device_array, \"astype\", _astype)\n setattr(device_array, \"view\", _view)\n setattr(device_array, \"nbytes\", property(_nbytes))\n setattr(device_array, \"clip\", _clip)\n\n_set_device_array_base_attributes(device_array.DeviceArray)\n\n\ndef _set_device_array_attributes(device_array):\n setattr(device_array, \"__array_module__\", __array_module__)\n # Extra methods that are handy\n setattr(device_array, \"broadcast\", lax.broadcast)\n setattr(device_array, \"broadcast_in_dim\", lax.broadcast_in_dim)\n setattr(device_array, \"split\", split)\n setattr(device_array, \"compress\", _compress_method)\n setattr(device_array, \"_multi_slice\", _multi_slice)\n setattr(device_array, \"at\", property(_IndexUpdateHelper))\n\nfor t in device_array.device_array_types:\n _set_device_array_attributes(t)\n_set_device_array_attributes(pxla._ShardedDeviceArray)\n_set_device_array_attributes(pxla.pmap_lib.ShardedDeviceArray)\n" ]
[ [ "numpy.sum", "numpy.ones", "numpy.multiply", "numpy.diff", "numpy.dtype", "numpy.any", "numpy.issubdtype", "numpy.asarray", "numpy.int64", "numpy.log", "numpy.isscalar", "numpy.where", "numpy.uint8", "numpy.load", "numpy.ceil", "numpy.zeros", "numpy.cumprod", "numpy.arange", "numpy.ndim", "numpy.broadcast_to", "numpy.iscomplex", "numpy.sign", "numpy.empty", "numpy.ravel", "numpy.shape", "numpy.array", "numpy.concatenate" ] ]
Zeinab-Haroon/detectron2
[ "6a56c9cadaf392697c4bdef00325e415d07a459f" ]
[ "detectron2/evaluation/cityscapes_evaluation.py" ]
[ "# Copyright (c) Facebook, Inc. and its affiliates.\nimport glob\nimport logging\nimport numpy as np\nimport os\nimport tempfile\nfrom collections import OrderedDict\nimport torch\nfrom PIL import Image\n\nfrom detectron2.data import MetadataCatalog\nfrom detectron2.utils import comm\nfrom detectron2.utils.file_io import PathManager\n\nfrom .evaluator import DatasetEvaluator\n\n\nclass CityscapesEvaluator(DatasetEvaluator):\n \"\"\"\n Base class for evaluation using cityscapes API.\n \"\"\"\n\n def __init__(self, dataset_name):\n \"\"\"\n Args:\n dataset_name (str): the name of the dataset.\n It must have the following metadata associated with it:\n \"thing_classes\", \"gt_dir\".\n \"\"\"\n self._metadata = MetadataCatalog.get(dataset_name)\n self._cpu_device = torch.device(\"cpu\")\n self._logger = logging.getLogger(__name__)\n\n def reset(self):\n self._working_dir = tempfile.TemporaryDirectory(prefix=\"cityscapes_eval_\")\n self._temp_dir = self._working_dir.name\n # All workers will write to the same results directory\n # TODO this does not work in distributed training\n self._temp_dir = comm.all_gather(self._temp_dir)[0]\n if self._temp_dir != self._working_dir.name:\n self._working_dir.cleanup()\n self._logger.info(\n \"Writing cityscapes results to temporary directory {} ...\".format(self._temp_dir)\n )\n\n\nclass CityscapesInstanceEvaluator(CityscapesEvaluator):\n \"\"\"\n Evaluate instance segmentation results on cityscapes dataset using cityscapes API.\n\n Note:\n * It does not work in multi-machine distributed training.\n * It contains a synchronization, therefore has to be used on all ranks.\n * Only the main process runs evaluation.\n \"\"\"\n\n def process(self, inputs, outputs):\n from cityscapesscripts.helpers.labels import name2label\n\n for input, output in zip(inputs, outputs):\n file_name = input[\"file_name\"]\n basename = os.path.splitext(os.path.basename(file_name))[0]\n pred_txt = os.path.join(self._temp_dir, basename + \"_pred.txt\")\n\n if \"instances\" in output:\n output = output[\"instances\"].to(self._cpu_device)\n num_instances = len(output)\n with open(pred_txt, \"w\") as fout:\n for i in range(num_instances):\n pred_class = output.pred_classes[i]\n classes = self._metadata.thing_classes[pred_class]\n class_id = name2label[classes].id\n score = output.scores[i]\n mask = output.pred_masks[i].numpy().astype(\"uint8\")\n png_filename = os.path.join(\n self._temp_dir, basename + \"_{}_{}.png\".format(i, classes)\n )\n\n Image.fromarray(mask * 255).save(png_filename)\n fout.write(\n \"{} {} {}\\n\".format(os.path.basename(png_filename), class_id, score)\n )\n else:\n # Cityscapes requires a prediction file for every ground truth image.\n with open(pred_txt, \"w\") as fout:\n pass\n\n def evaluate(self):\n \"\"\"\n Returns:\n dict: has a key \"segm\", whose value is a dict of \"AP\" and \"AP50\".\n \"\"\"\n comm.synchronize()\n if comm.get_rank() > 0:\n return\n import cityscapesscripts.evaluation.evalInstanceLevelSemanticLabeling as cityscapes_eval\n\n self._logger.info(\"Evaluating results under {} ...\".format(self._temp_dir))\n\n # set some global states in cityscapes evaluation API, before evaluating\n cityscapes_eval.args.predictionPath = os.path.abspath(self._temp_dir)\n cityscapes_eval.args.predictionWalk = None\n cityscapes_eval.args.JSONOutput = False\n cityscapes_eval.args.colorized = False\n cityscapes_eval.args.gtInstancesFile = os.path.join(self._temp_dir, \"gtInstances.json\")\n\n # These lines are adopted from\n # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalInstanceLevelSemanticLabeling.py # noqa\n gt_dir = PathManager.get_local_path(self._metadata.gt_dir)\n \n print('==================== ** ======================')\n # wanted = 'lindau'\n wanted = 'munster'\n # wanted = 'frankfurt'\n # groundTruthImgList = glob.glob(os.path.join(gt_dir, \"*\", \"*_gtFine_instanceIds.png\"))\n groundTruthImgList = glob.glob(os.path.join(gt_dir, wanted, \"*_gtFine_instanceIds.png\"))\n\n assert len(\n groundTruthImgList\n ), \"Cannot find any ground truth images to use for evaluation. Searched for: {}\".format(\n cityscapes_eval.args.groundTruthSearch\n )\n predictionImgList = []\n for gt in groundTruthImgList:\n predictionImgList.append(cityscapes_eval.getPrediction(gt, cityscapes_eval.args))\n results = cityscapes_eval.evaluateImgLists(\n predictionImgList, groundTruthImgList, cityscapes_eval.args\n )[\"averages\"]\n\n ret = OrderedDict()\n print('ret[\"segm\"]:', ret[\"segm\"])\n\n self._working_dir.cleanup()\n return ret\n\n\nclass CityscapesSemSegEvaluator(CityscapesEvaluator):\n \"\"\"\n Evaluate semantic segmentation results on cityscapes dataset using cityscapes API.\n\n Note:\n * It does not work in multi-machine distributed training.\n * It contains a synchronization, therefore has to be used on all ranks.\n * Only the main process runs evaluation.\n \"\"\"\n\n def process(self, inputs, outputs):\n from cityscapesscripts.helpers.labels import trainId2label\n\n for input, output in zip(inputs, outputs):\n file_name = input[\"file_name\"]\n basename = os.path.splitext(os.path.basename(file_name))[0]\n pred_filename = os.path.join(self._temp_dir, basename + \"_pred.png\")\n\n output = output[\"sem_seg\"].argmax(dim=0).to(self._cpu_device).numpy()\n pred = 255 * np.ones(output.shape, dtype=np.uint8)\n for train_id, label in trainId2label.items():\n if label.ignoreInEval:\n continue\n pred[output == train_id] = label.id\n Image.fromarray(pred).save(pred_filename)\n\n def evaluate(self):\n comm.synchronize()\n if comm.get_rank() > 0:\n return\n # Load the Cityscapes eval script *after* setting the required env var,\n # since the script reads CITYSCAPES_DATASET into global variables at load time.\n import cityscapesscripts.evaluation.evalPixelLevelSemanticLabeling as cityscapes_eval\n\n self._logger.info(\"Evaluating results under {} ...\".format(self._temp_dir))\n\n # set some global states in cityscapes evaluation API, before evaluating\n cityscapes_eval.args.predictionPath = os.path.abspath(self._temp_dir)\n cityscapes_eval.args.predictionWalk = None\n cityscapes_eval.args.JSONOutput = False\n cityscapes_eval.args.colorized = False\n\n # These lines are adopted from\n # https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/evaluation/evalPixelLevelSemanticLabeling.py # noqa\n gt_dir = PathManager.get_local_path(self._metadata.gt_dir)\n groundTruthImgList = glob.glob(os.path.join(gt_dir, \"*\", \"*_gtFine_labelIds.png\"))\n assert len(\n groundTruthImgList\n ), \"Cannot find any ground truth images to use for evaluation. Searched for: {}\".format(\n cityscapes_eval.args.groundTruthSearch\n )\n predictionImgList = []\n for gt in groundTruthImgList:\n predictionImgList.append(cityscapes_eval.getPrediction(cityscapes_eval.args, gt))\n results = cityscapes_eval.evaluateImgLists(\n predictionImgList, groundTruthImgList, cityscapes_eval.args\n )\n ret = OrderedDict()\n ret[\"sem_seg\"] = {\n \"IoU\": 100.0 * results[\"averageScoreClasses\"],\n \"iIoU\": 100.0 * results[\"averageScoreInstClasses\"],\n \"IoU_sup\": 100.0 * results[\"averageScoreCategories\"],\n \"iIoU_sup\": 100.0 * results[\"averageScoreInstCategories\"],\n }\n self._working_dir.cleanup()\n return ret\n" ]
[ [ "numpy.ones", "torch.device" ] ]
kalyanvasudev/ptv_temp
[ "85c2282bed9aa4eadc3454bd7e8f2a8e8c4b4ec6" ]
[ "pytorchvideo/models/x3d.py" ]
[ "# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n\nfrom typing import Callable, Tuple\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\nfrom fvcore.nn.squeeze_excitation import SqueezeExcitation\nfrom pytorchvideo.layers.convolutions import Conv2plus1d\nfrom pytorchvideo.layers.swish import Swish\nfrom pytorchvideo.layers.utils import round_repeats, round_width, set_attributes\nfrom pytorchvideo.models.head import ResNetBasicHead\nfrom pytorchvideo.models.net import Net\nfrom pytorchvideo.models.resnet import BottleneckBlock, ResBlock, ResStage\nfrom pytorchvideo.models.stem import ResNetBasicStem\n\n\ndef create_x3d_stem(\n *,\n # Conv configs.\n in_channels: int,\n out_channels: int,\n conv_kernel_size: Tuple[int] = (5, 3, 3),\n conv_stride: Tuple[int] = (1, 2, 2),\n conv_padding: Tuple[int] = (2, 1, 1),\n # BN configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n # Activation configs.\n activation: Callable = nn.ReLU,\n) -> nn.Module:\n \"\"\"\n Creates the stem layer for X3D. It performs spatial Conv, temporal Conv, BN, and Relu.\n\n ::\n\n Conv_xy\n ↓\n Conv_t\n ↓\n Normalization\n ↓\n Activation\n\n Args:\n in_channels (int): input channel size of the convolution.\n out_channels (int): output channel size of the convolution.\n conv_kernel_size (tuple): convolutional kernel size(s).\n conv_stride (tuple): convolutional stride size(s).\n conv_padding (tuple): convolutional padding size(s).\n\n norm (callable): a callable that constructs normalization layer, options\n include nn.BatchNorm3d, None (not performing normalization).\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n\n activation (callable): a callable that constructs activation layer, options\n include: nn.ReLU, nn.Softmax, nn.Sigmoid, and None (not performing\n activation).\n\n Returns:\n (nn.Module): X3D stem layer.\n \"\"\"\n conv_xy_module = nn.Conv3d(\n in_channels=in_channels,\n out_channels=out_channels,\n kernel_size=(1, conv_kernel_size[1], conv_kernel_size[2]),\n stride=(1, conv_stride[1], conv_stride[2]),\n padding=(0, conv_padding[1], conv_padding[2]),\n bias=False,\n )\n conv_t_module = nn.Conv3d(\n in_channels=out_channels,\n out_channels=out_channels,\n kernel_size=(conv_kernel_size[0], 1, 1),\n stride=(conv_stride[0], 1, 1),\n padding=(conv_padding[0], 0, 0),\n bias=False,\n groups=out_channels,\n )\n stacked_conv_module = Conv2plus1d(\n conv_t=conv_xy_module,\n norm=None,\n activation=None,\n conv_xy=conv_t_module,\n )\n\n norm_module = (\n None\n if norm is None\n else norm(num_features=out_channels, eps=norm_eps, momentum=norm_momentum)\n )\n activation_module = None if activation is None else activation()\n\n return ResNetBasicStem(\n conv=stacked_conv_module,\n norm=norm_module,\n activation=activation_module,\n pool=None,\n )\n\n\ndef create_x3d_bottleneck_block(\n *,\n # Convolution configs.\n dim_in: int,\n dim_inner: int,\n dim_out: int,\n conv_kernel_size: Tuple[int] = (3, 3, 3),\n conv_stride: Tuple[int] = (1, 2, 2),\n # Norm configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n se_ratio: float = 0.0625,\n # Activation configs.\n activation: Callable = nn.ReLU,\n inner_act: Callable = Swish,\n) -> nn.Module:\n \"\"\"\n Bottleneck block for X3D: a sequence of Conv, Normalization with optional SE block,\n and Activations repeated in the following order:\n\n ::\n\n Conv3d (conv_a)\n ↓\n Normalization (norm_a)\n ↓\n Activation (act_a)\n ↓\n Conv3d (conv_b)\n ↓\n Normalization (norm_b)\n ↓\n Squeeze-and-Excitation\n ↓\n Activation (act_b)\n ↓\n Conv3d (conv_c)\n ↓\n Normalization (norm_c)\n\n Args:\n dim_in (int): input channel size to the bottleneck block.\n dim_inner (int): intermediate channel size of the bottleneck.\n dim_out (int): output channel size of the bottleneck.\n conv_kernel_size (tuple): convolutional kernel size(s) for conv_b.\n conv_stride (tuple): convolutional stride size(s) for conv_b.\n\n norm (callable): a callable that constructs normalization layer, examples\n include nn.BatchNorm3d, None (not performing normalization).\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n se_ratio (float): if > 0, apply SE to the 3x3x3 conv, with the SE\n channel dimensionality being se_ratio times the 3x3x3 conv dim.\n\n activation (callable): a callable that constructs activation layer, examples\n include: nn.ReLU, nn.Softmax, nn.Sigmoid, and None (not performing\n activation).\n inner_act (callable): whether use Swish activation for act_b or not.\n\n Returns:\n (nn.Module): X3D bottleneck block.\n \"\"\"\n # 1x1x1 Conv\n conv_a = nn.Conv3d(\n in_channels=dim_in, out_channels=dim_inner, kernel_size=(1, 1, 1), bias=False\n )\n norm_a = (\n None\n if norm is None\n else norm(num_features=dim_inner, eps=norm_eps, momentum=norm_momentum)\n )\n act_a = None if activation is None else activation()\n\n # 3x3x3 Conv\n conv_b = nn.Conv3d(\n in_channels=dim_inner,\n out_channels=dim_inner,\n kernel_size=conv_kernel_size,\n stride=conv_stride,\n padding=[size // 2 for size in conv_kernel_size],\n bias=False,\n groups=dim_inner,\n dilation=(1, 1, 1),\n )\n se = (\n SqueezeExcitation(\n num_channels=dim_inner,\n num_channels_reduced=round_width(dim_inner, se_ratio),\n is_3d=True,\n )\n if se_ratio > 0.0\n else nn.Identity()\n )\n norm_b = nn.Sequential(\n (\n nn.Identity()\n if norm is None\n else norm(num_features=dim_inner, eps=norm_eps, momentum=norm_momentum)\n ),\n se,\n )\n act_b = None if inner_act is None else inner_act()\n\n # 1x1x1 Conv\n conv_c = nn.Conv3d(\n in_channels=dim_inner, out_channels=dim_out, kernel_size=(1, 1, 1), bias=False\n )\n norm_c = (\n None\n if norm is None\n else norm(num_features=dim_out, eps=norm_eps, momentum=norm_momentum)\n )\n\n return BottleneckBlock(\n conv_a=conv_a,\n norm_a=norm_a,\n act_a=act_a,\n conv_b=conv_b,\n norm_b=norm_b,\n act_b=act_b,\n conv_c=conv_c,\n norm_c=norm_c,\n )\n\n\ndef create_x3d_res_block(\n *,\n # Bottleneck Block configs.\n dim_in: int,\n dim_inner: int,\n dim_out: int,\n bottleneck: Callable = create_x3d_bottleneck_block,\n use_shortcut: bool = True,\n # Conv configs.\n conv_kernel_size: Tuple[int] = (3, 3, 3),\n conv_stride: Tuple[int] = (1, 2, 2),\n # Norm configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n se_ratio: float = 0.0625,\n # Activation configs.\n activation: Callable = nn.ReLU,\n inner_act: Callable = Swish,\n) -> nn.Module:\n \"\"\"\n Residual block for X3D. Performs a summation between an identity shortcut in branch1 and a\n main block in branch2. When the input and output dimensions are different, a\n convolution followed by a normalization will be performed.\n\n ::\n\n Input\n |-------+\n ↓ |\n Block |\n ↓ |\n Summation ←-+\n ↓\n Activation\n\n Args:\n dim_in (int): input channel size to the bottleneck block.\n dim_inner (int): intermediate channel size of the bottleneck.\n dim_out (int): output channel size of the bottleneck.\n bottleneck (callable): a callable for create_x3d_bottleneck_block.\n\n conv_kernel_size (tuple): convolutional kernel size(s) for conv_b.\n conv_stride (tuple): convolutional stride size(s) for conv_b.\n\n norm (callable): a callable that constructs normalization layer, examples\n include nn.BatchNorm3d, None (not performing normalization).\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n se_ratio (float): if > 0, apply SE to the 3x3x3 conv, with the SE\n channel dimensionality being se_ratio times the 3x3x3 conv dim.\n\n activation (callable): a callable that constructs activation layer, examples\n include: nn.ReLU, nn.Softmax, nn.Sigmoid, and None (not performing\n activation).\n inner_act (callable): whether use Swish activation for act_b or not.\n\n Returns:\n (nn.Module): X3D block layer.\n \"\"\"\n\n norm_model = None\n if norm is not None and dim_in != dim_out:\n norm_model = norm(num_features=dim_out)\n\n return ResBlock(\n branch1_conv=nn.Conv3d(\n dim_in,\n dim_out,\n kernel_size=(1, 1, 1),\n stride=conv_stride,\n bias=False,\n )\n if (dim_in != dim_out or np.prod(conv_stride) > 1) and use_shortcut\n else None,\n branch1_norm=norm_model if dim_in != dim_out and use_shortcut else None,\n branch2=bottleneck(\n dim_in=dim_in,\n dim_inner=dim_inner,\n dim_out=dim_out,\n conv_kernel_size=conv_kernel_size,\n conv_stride=conv_stride,\n norm=norm,\n norm_eps=norm_eps,\n norm_momentum=norm_momentum,\n se_ratio=se_ratio,\n activation=activation,\n inner_act=inner_act,\n ),\n activation=None if activation is None else activation(),\n branch_fusion=lambda x, y: x + y,\n )\n\n\ndef create_x3d_res_stage(\n *,\n # Stage configs.\n depth: int,\n # Bottleneck Block configs.\n dim_in: int,\n dim_inner: int,\n dim_out: int,\n bottleneck: Callable = create_x3d_bottleneck_block,\n # Conv configs.\n conv_kernel_size: Tuple[int] = (3, 3, 3),\n conv_stride: Tuple[int] = (1, 2, 2),\n # Norm configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n se_ratio: float = 0.0625,\n # Activation configs.\n activation: Callable = nn.ReLU,\n inner_act: Callable = Swish,\n) -> nn.Module:\n \"\"\"\n Create Residual Stage, which composes sequential blocks that make up X3D.\n\n ::\n\n Input\n ↓\n ResBlock\n ↓\n .\n .\n .\n ↓\n ResBlock\n\n Args:\n\n depth (init): number of blocks to create.\n\n dim_in (int): input channel size to the bottleneck block.\n dim_inner (int): intermediate channel size of the bottleneck.\n dim_out (int): output channel size of the bottleneck.\n bottleneck (callable): a callable for create_x3d_bottleneck_block.\n\n conv_kernel_size (tuple): convolutional kernel size(s) for conv_b.\n conv_stride (tuple): convolutional stride size(s) for conv_b.\n\n norm (callable): a callable that constructs normalization layer, examples\n include nn.BatchNorm3d, None (not performing normalization).\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n se_ratio (float): if > 0, apply SE to the 3x3x3 conv, with the SE\n channel dimensionality being se_ratio times the 3x3x3 conv dim.\n\n activation (callable): a callable that constructs activation layer, examples\n include: nn.ReLU, nn.Softmax, nn.Sigmoid, and None (not performing\n activation).\n inner_act (callable): whether use Swish activation for act_b or not.\n\n Returns:\n (nn.Module): X3D stage layer.\n \"\"\"\n res_blocks = []\n for idx in range(depth):\n block = create_x3d_res_block(\n dim_in=dim_in if idx == 0 else dim_out,\n dim_inner=dim_inner,\n dim_out=dim_out,\n bottleneck=bottleneck,\n conv_kernel_size=conv_kernel_size,\n conv_stride=conv_stride if idx == 0 else (1, 1, 1),\n norm=norm,\n norm_eps=norm_eps,\n norm_momentum=norm_momentum,\n se_ratio=(se_ratio if (idx + 1) % 2 else 0.0),\n activation=activation,\n inner_act=inner_act,\n )\n res_blocks.append(block)\n\n return ResStage(res_blocks=nn.ModuleList(res_blocks))\n\n\ndef create_x3d_head(\n *,\n # Projection configs.\n dim_in: int,\n dim_inner: int,\n dim_out: int,\n num_classes: int,\n # Pooling configs.\n pool_act: Callable = nn.ReLU,\n pool_kernel_size: Tuple[int] = (13, 5, 5),\n # BN configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n bn_lin5_on=False,\n # Dropout configs.\n dropout_rate: float = 0.5,\n # Activation configs.\n activation: Callable = nn.Softmax,\n # Output configs.\n output_with_global_average: bool = True,\n) -> nn.Module:\n \"\"\"\n Creates X3D head. This layer performs an projected pooling operation followed\n by an dropout, a fully-connected projection, an activation layer and a global\n spatiotemporal averaging.\n\n ::\n\n ProjectedPool\n ↓\n Dropout\n ↓\n Projection\n ↓\n Activation\n ↓\n Averaging\n\n Args:\n dim_in (int): input channel size of the X3D head.\n dim_inner (int): intermediate channel size of the X3D head.\n dim_out (int): output channel size of the X3D head.\n num_classes (int): the number of classes for the video dataset.\n\n pool_act (callable): a callable that constructs resnet pool activation\n layer such as nn.ReLU.\n pool_kernel_size (tuple): pooling kernel size(s) when not using adaptive\n pooling.\n\n norm (callable): a callable that constructs normalization layer, examples\n include nn.BatchNorm3d, None (not performing normalization).\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n bn_lin5_on (bool): if True, perform normalization on the features\n before the classifier.\n\n dropout_rate (float): dropout rate.\n\n activation (callable): a callable that constructs resnet head activation\n layer, examples include: nn.ReLU, nn.Softmax, nn.Sigmoid, and None (not\n applying activation).\n\n output_with_global_average (bool): if True, perform global averaging on temporal\n and spatial dimensions and reshape output to batch_size x out_features.\n\n Returns:\n (nn.Module): X3D head layer.\n \"\"\"\n pre_conv_module = nn.Conv3d(\n in_channels=dim_in, out_channels=dim_inner, kernel_size=(1, 1, 1), bias=False\n )\n\n pre_norm_module = norm(num_features=dim_inner, eps=norm_eps, momentum=norm_momentum)\n pre_act_module = None if pool_act is None else pool_act()\n\n if pool_kernel_size is None:\n pool_module = nn.AdaptiveAvgPool3d((1, 1, 1))\n else:\n pool_module = nn.AvgPool3d(pool_kernel_size, stride=1)\n\n post_conv_module = nn.Conv3d(\n in_channels=dim_inner, out_channels=dim_out, kernel_size=(1, 1, 1), bias=False\n )\n\n if bn_lin5_on:\n post_norm_module = norm(\n num_features=dim_out, eps=norm_eps, momentum=norm_momentum\n )\n else:\n post_norm_module = None\n post_act_module = None if pool_act is None else pool_act()\n\n projected_pool_module = ProjectedPool(\n pre_conv=pre_conv_module,\n pre_norm=pre_norm_module,\n pre_act=pre_act_module,\n pool=pool_module,\n post_conv=post_conv_module,\n post_norm=post_norm_module,\n post_act=post_act_module,\n )\n\n if activation is None:\n activation_module = None\n elif activation == nn.Softmax:\n activation_module = activation(dim=1)\n elif activation == nn.Sigmoid:\n activation_module = activation()\n else:\n raise NotImplementedError(\n \"{} is not supported as an activation\" \"function.\".format(activation)\n )\n\n if output_with_global_average:\n output_pool = nn.AdaptiveAvgPool3d(1)\n else:\n output_pool = None\n\n return ResNetBasicHead(\n proj=nn.Linear(dim_out, num_classes, bias=True),\n activation=activation_module,\n pool=projected_pool_module,\n dropout=nn.Dropout(dropout_rate) if dropout_rate > 0 else None,\n output_pool=output_pool,\n )\n\n\ndef create_x3d(\n *,\n # Input clip configs.\n input_channel: int = 3,\n input_clip_length: int = 13,\n input_crop_size: int = 160,\n # Model configs.\n model_num_class: int = 400,\n dropout_rate: float = 0.5,\n width_factor: float = 2.0,\n depth_factor: float = 2.2,\n # Normalization configs.\n norm: Callable = nn.BatchNorm3d,\n norm_eps: float = 1e-5,\n norm_momentum: float = 0.1,\n # Activation configs.\n activation: Callable = nn.ReLU,\n # Stem configs.\n stem_dim_in: int = 12,\n stem_conv_kernel_size: Tuple[int] = (5, 3, 3),\n stem_conv_stride: Tuple[int] = (1, 2, 2),\n # Stage configs.\n stage_conv_kernel_size: Tuple[Tuple[int]] = (\n (3, 3, 3),\n (3, 3, 3),\n (3, 3, 3),\n (3, 3, 3),\n ),\n stage_spatial_stride: Tuple[int] = (2, 2, 2, 2),\n stage_temporal_stride: Tuple[int] = (1, 1, 1, 1),\n bottleneck: Callable = create_x3d_bottleneck_block,\n bottleneck_factor: float = 2.25,\n se_ratio: float = 0.0625,\n inner_act: Callable = Swish,\n # Head configs.\n head_dim_out: int = 2048,\n head_pool_act: Callable = nn.ReLU,\n head_bn_lin5_on: bool = False,\n head_activation: Callable = nn.Softmax,\n head_output_with_global_average: bool = True,\n) -> nn.Module:\n \"\"\"\n X3D model builder. It builds a X3D network backbone, which is a ResNet.\n\n Christoph Feichtenhofer.\n \"X3D: Expanding Architectures for Efficient Video Recognition.\"\n https://arxiv.org/abs/2004.04730\n\n ::\n\n Input\n ↓\n Stem\n ↓\n Stage 1\n ↓\n .\n .\n .\n ↓\n Stage N\n ↓\n Head\n\n Args:\n input_channel (int): number of channels for the input video clip.\n input_clip_length (int): length of the input video clip. Value for\n different models: X3D-XS: 4; X3D-S: 13; X3D-M: 16; X3D-L: 16.\n input_crop_size (int): spatial resolution of the input video clip.\n Value for different models: X3D-XS: 160; X3D-S: 160; X3D-M: 224;\n X3D-L: 312.\n\n model_num_class (int): the number of classes for the video dataset.\n dropout_rate (float): dropout rate.\n width_factor (float): width expansion factor.\n depth_factor (float): depth expansion factor. Value for different\n models: X3D-XS: 2.2; X3D-S: 2.2; X3D-M: 2.2; X3D-L: 5.0.\n\n norm (callable): a callable that constructs normalization layer.\n norm_eps (float): normalization epsilon.\n norm_momentum (float): normalization momentum.\n\n activation (callable): a callable that constructs activation layer.\n\n stem_dim_in (int): input channel size for stem before expansion.\n stem_conv_kernel_size (tuple): convolutional kernel size(s) of stem.\n stem_conv_stride (tuple): convolutional stride size(s) of stem.\n\n stage_conv_kernel_size (tuple): convolutional kernel size(s) for conv_b.\n stage_spatial_stride (tuple): the spatial stride for each stage.\n stage_temporal_stride (tuple): the temporal stride for each stage.\n bottleneck_factor (float): bottleneck expansion factor for the 3x3x3 conv.\n se_ratio (float): if > 0, apply SE to the 3x3x3 conv, with the SE\n channel dimensionality being se_ratio times the 3x3x3 conv dim.\n inner_act (callable): whether use Swish activation for act_b or not.\n\n head_dim_out (int): output channel size of the X3D head.\n head_pool_act (callable): a callable that constructs resnet pool activation\n layer such as nn.ReLU.\n head_bn_lin5_on (bool): if True, perform normalization on the features\n before the classifier.\n head_activation (callable): a callable that constructs activation layer.\n head_output_with_global_average (bool): if True, perform global averaging on\n the head output.\n\n Returns:\n (nn.Module): the X3D network.\n \"\"\"\n blocks = []\n # Create stem for X3D.\n stem_dim_out = round_width(stem_dim_in, width_factor)\n stem = create_x3d_stem(\n in_channels=input_channel,\n out_channels=stem_dim_out,\n conv_kernel_size=stem_conv_kernel_size,\n conv_stride=stem_conv_stride,\n conv_padding=[size // 2 for size in stem_conv_kernel_size],\n norm=norm,\n norm_eps=norm_eps,\n norm_momentum=norm_momentum,\n activation=activation,\n )\n blocks.append(stem)\n\n # Compute the depth and dimension for each stage\n stage_depths = [1, 2, 5, 3]\n exp_stage = 2.0\n stage_dim1 = stem_dim_in\n stage_dim2 = round_width(stage_dim1, exp_stage, divisor=8)\n stage_dim3 = round_width(stage_dim2, exp_stage, divisor=8)\n stage_dim4 = round_width(stage_dim3, exp_stage, divisor=8)\n stage_dims = [stage_dim1, stage_dim2, stage_dim3, stage_dim4]\n\n dim_in = stem_dim_out\n # Create each stage for X3D.\n for idx in range(len(stage_depths)):\n dim_out = round_width(stage_dims[idx], width_factor)\n dim_inner = int(bottleneck_factor * dim_out)\n depth = round_repeats(stage_depths[idx], depth_factor)\n\n stage_conv_stride = (\n stage_temporal_stride[idx],\n stage_spatial_stride[idx],\n stage_spatial_stride[idx],\n )\n\n stage = create_x3d_res_stage(\n depth=depth,\n dim_in=dim_in,\n dim_inner=dim_inner,\n dim_out=dim_out,\n bottleneck=bottleneck,\n conv_kernel_size=stage_conv_kernel_size[idx],\n conv_stride=stage_conv_stride,\n norm=norm,\n norm_eps=norm_eps,\n norm_momentum=norm_momentum,\n se_ratio=se_ratio,\n activation=activation,\n inner_act=inner_act,\n )\n blocks.append(stage)\n dim_in = dim_out\n\n # Create head for X3D.\n total_spatial_stride = stem_conv_stride[1] * np.prod(stage_spatial_stride)\n total_temporal_stride = stem_conv_stride[0] * np.prod(stage_temporal_stride)\n\n assert (\n input_clip_length >= total_temporal_stride\n ), \"Clip length doesn't match temporal stride!\"\n assert (\n input_crop_size >= total_spatial_stride\n ), \"Crop size doesn't match spatial stride!\"\n\n head_pool_kernel_size = (\n input_clip_length // total_temporal_stride,\n input_crop_size // total_spatial_stride,\n input_crop_size // total_spatial_stride,\n )\n\n head = create_x3d_head(\n dim_in=dim_out,\n dim_inner=dim_inner,\n dim_out=head_dim_out,\n num_classes=model_num_class,\n pool_act=head_pool_act,\n pool_kernel_size=head_pool_kernel_size,\n norm=norm,\n norm_eps=norm_eps,\n norm_momentum=norm_momentum,\n bn_lin5_on=head_bn_lin5_on,\n dropout_rate=dropout_rate,\n activation=head_activation,\n output_with_global_average=head_output_with_global_average,\n )\n blocks.append(head)\n return Net(blocks=nn.ModuleList(blocks))\n\n\nclass ProjectedPool(nn.Module):\n \"\"\"\n A pooling module augmented with Conv, Normalization and Activation both\n before and after pooling for the head layer of X3D.\n\n ::\n\n Conv3d (pre_conv)\n ↓\n Normalization (pre_norm)\n ↓\n Activation (pre_act)\n ↓\n Pool3d\n ↓\n Conv3d (post_conv)\n ↓\n Normalization (post_norm)\n ↓\n Activation (post_act)\n \"\"\"\n\n def __init__(\n self,\n *,\n pre_conv: nn.Module = None,\n pre_norm: nn.Module = None,\n pre_act: nn.Module = None,\n pool: nn.Module = None,\n post_conv: nn.Module = None,\n post_norm: nn.Module = None,\n post_act: nn.Module = None,\n ) -> None:\n \"\"\"\n Args:\n pre_conv (torch.nn.modules): convolutional module.\n pre_norm (torch.nn.modules): normalization module.\n pre_act (torch.nn.modules): activation module.\n pool (torch.nn.modules): pooling module.\n post_conv (torch.nn.modules): convolutional module.\n post_norm (torch.nn.modules): normalization module.\n post_act (torch.nn.modules): activation module.\n \"\"\"\n super().__init__()\n set_attributes(self, locals())\n assert self.pre_conv is not None\n assert self.pool is not None\n assert self.post_conv is not None\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n x = self.pre_conv(x)\n\n if self.pre_norm is not None:\n x = self.pre_norm(x)\n if self.pre_act is not None:\n x = self.pre_act(x)\n\n x = self.pool(x)\n x = self.post_conv(x)\n\n if self.post_norm is not None:\n x = self.post_norm(x)\n if self.post_act is not None:\n x = self.post_act(x)\n return x\n" ]
[ [ "torch.nn.Linear", "torch.nn.Dropout", "torch.nn.AvgPool3d", "torch.nn.ModuleList", "torch.nn.Identity", "numpy.prod", "torch.nn.AdaptiveAvgPool3d", "torch.nn.Conv3d" ] ]
pmehta08/MulensModel
[ "261738c445a8d116d09c90e65f6e847cfc8a7ad8", "261738c445a8d116d09c90e65f6e847cfc8a7ad8" ]
[ "examples/run_time_tests/compare.py", "data/interpolate_elliptic_integral_1_2.py" ]
[ "import os\nimport sys\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom astropy.coordinates import SkyCoord\nfrom astropy import units as u\n\nsys.path.append(\"/usr/custom/pyLIMA-1.0.0\")\nfrom pyLIMA import event, telescopes, microlmodels\nimport MulensModel as mm\n\n\n# Common settings:\nt_0 = 2456900.\nu_0 = 0.1\nt_E = 150.\npi_E_N = 1.\npi_E_E = 2.\nra_deg = 270.\ndec_deg = -30.\ntime = np.linspace(t_0-80., t_0+80., 1000)\nd_time = 2450000.\n\n# MulensModel calculations and plot:\ncoords = SkyCoord(ra_deg, dec_deg, unit=u.deg)\nparams = {'t_0': t_0, 'u_0': u_0, 't_E': t_E,\n 'pi_E_N': pi_E_N, 'pi_E_E': pi_E_E}\nmm_model = mm.Model(params, coords=coords)\nmm_mag = mm_model.magnification(time)\nplt.plot(time-d_time, mm_mag, 'r.', label='MulensModel')\n\n# Read VBBL output and plot it:\nvbbl_data = np.loadtxt(\"fake.out\", unpack=True)\nplt.plot(vbbl_data[0], vbbl_data[1], 'g-.', label='VBBL')\n\n# This are the changes I have to make to make the results as close as possible:\npi_E_E = -pi_E_E\npi_E_N = -pi_E_N\n\n# pyLIMA calculations and plots:\nyour_event = event.Event()\nyour_event.ra = ra_deg\nyour_event.dec = dec_deg\ndata_1 = np.array([time, time*0.+15., time*0.+.01]).T\ntelescope_1 = telescopes.Telescope(light_curve_magnitude=data_1)\nyour_event.telescopes.append(telescope_1)\nyour_event.check_event()\nmodel_1 = microlmodels.create_model(\n 'PSPL', your_event, parallax=['Annual', t_0])\nmodel_1.define_model_parameters()\npyLIMA_parameters = model_1.compute_pyLIMA_parameters(\n [t_0, u_0, t_E, pi_E_N, pi_E_E])\nmodel = model_1.compute_the_microlensing_model(telescope_1, pyLIMA_parameters)\nmag_pyLIMA = model_1.model_magnification(telescope_1, pyLIMA_parameters)\nplt.plot(time-d_time, mag_pyLIMA, 'b.', label='pyLIMA')\n\n# Compare pyLIMA and MM:\nindex = np.argmax(np.abs(mm_mag - mag_pyLIMA))\nprint(\"Largest difference is for: \", index, time[index]-d_time)\nprint(\"pyLIMA:\", mag_pyLIMA[index])\nprint(\"MM:\", mm_mag[index])\n\n# This is the end:\nplt.legend(loc='best')\nplt.xlabel('JD-2450000')\nplt.ylabel('magnification')\nplt.show()\n", "\"\"\"\nCalculates interpolation tables for elliptical integral of\nthe first and second kind.\n\"\"\"\nimport math\nimport numpy as np\nfrom math import sin, cos, sqrt\nfrom scipy import integrate\nfrom scipy.interpolate import interp1d\nfrom scipy.special import ellipk, ellipe\n# These are complete elliptic integrals of the first and the second kind.\n\n\naccuracy = 1.e-6\nn_divide = 10 + 1\nx_start = 2.e-5\nx_stop = 1. - 1.e-12\nn_start = 10\n\n# Settings end here.\n\n\ndef get_ellip(x):\n k = []\n e = []\n for x_ in x:\n if x_ not in get_ellip.k:\n get_ellip.k[x_] = ellipk(x_)\n get_ellip.e[x_] = ellipe(x_)\n k.append(get_ellip.k[x_])\n e.append(get_ellip.e[x_])\n return (np.array(k), np.array(e))\nget_ellip.k = dict()\nget_ellip.e = dict()\n\nx = np.logspace(np.log10(x_start), np.log10(x_stop), n_start)\n\niteration = 0\nadd = [None]\nwhile len(add) > 0:\n iteration += 1\n add = []\n (k, e) = get_ellip(x)\n interp_k = interp1d(np.log10(x), k, kind='cubic')\n interp_e = interp1d(np.log10(x), e, kind='cubic')\n for i in range(len(x)-1):\n x_1 = x[i]\n x_2 = x[i+1]\n check = np.logspace(np.log10(x_1), np.log10(x_2), n_divide)[1:-1]\n (check_true_k, check_true_e) = get_ellip(check)\n check_k = []\n check_e = []\n for c in check:\n check_k.append(interp_k(np.log10(c)))\n check_e.append(interp_e(np.log10(c)))\n check_k = np.array(check_k)\n check_e = np.array(check_e)\n relative_diff_k = np.abs(check_k - check_true_k) / check_true_k\n relative_diff_e = np.abs(check_e - check_true_e) / check_true_e\n relative_diff_max = np.amax(\n np.array([relative_diff_k, relative_diff_e]), axis=0)\n index = np.argsort(relative_diff_max)[-1]\n if relative_diff_max[index] < accuracy:\n continue\n add.append(check[index])\n new_x = np.sort(add + x.tolist())\n x = new_x\n\nfor (x_, k_, e_) in zip(x, k, e):\n print(x_, k_, e_)\n" ]
[ [ "matplotlib.pyplot.legend", "numpy.abs", "matplotlib.pyplot.show", "matplotlib.pyplot.ylabel", "numpy.array", "matplotlib.pyplot.plot", "numpy.linspace", "matplotlib.pyplot.xlabel", "numpy.loadtxt" ], [ "scipy.special.ellipk", "scipy.special.ellipe", "numpy.abs", "numpy.argsort", "numpy.log10", "numpy.array" ] ]
fabriziocosta/EGO
[ "d89e88183cce1ff24dca9333c09fa11597a45c7a" ]
[ "ego/optimization/neighborhood_graph_grammar.py" ]
[ "#!/usr/bin/env python\n\"\"\"Provides scikit interface.\"\"\"\n\nimport numpy as np\nimport networkx as nx\nimport random\nfrom ego.decompose import do_decompose\nfrom ego.decomposition.positive_and_negative import decompose_positive, decompose_negative\nfrom ego.decomposition.union import decompose_all_union\nfrom graphlearn.sample import LocalSubstitutionGraphGrammarSample as lsgg\nfrom graphlearn.lsgg_ego import lsgg_ego\nfrom ego.optimization.part_importance_estimator import PartImportanceEstimator\nimport logging\nlogger = logging.getLogger(__name__)\n\n\nclass NeighborhoodGraphGrammar(object):\n\n def __init__(self,\n core=2,\n context=1,\n count=2,\n filter_max_num_substitutions=None,\n n_neighbors=None,\n perturbation_size=2,\n part_importance_estimator=None,\n objective_func=None):\n self.n_neighbors = n_neighbors\n self.graph_grammar = lsgg(\n radii=list(range(core)),\n thickness=context,\n filter_min_cip=count,\n filter_min_interface=2,\n filter_max_num_substitutions=filter_max_num_substitutions,\n nodelevel_radius_and_thickness=False)\n self.perturbation_size = perturbation_size\n self.part_importance_estimator = part_importance_estimator\n self.objective_func = objective_func\n\n def fit(self, graphs):\n self.graph_grammar.fit(graphs)\n return self\n\n def __repr__(self):\n return str(self.graph_grammar)\n\n def perturb(self, orig_graph):\n graph = nx.Graph(orig_graph)\n for i in range(self.perturbation_size):\n # select a node at random\n ns = [u for u in graph.nodes()]\n root = np.random.choice(ns, size=1)[0]\n # materialize the neighborhood\n graphs_it = self.graph_grammar.root_neighbors(\n graph, [root], n_neighbors=self.n_neighbors)\n graphs = list(graphs_it) + [graph]\n # select a neighbor at random\n ids = list(range(len(graphs)))\n id = np.random.choice(ids, size=1)[0]\n graph = nx.Graph(graphs[id])\n return graph\n\n def _select_nodes_in_prob(self, graph, node_score_dict, edge_score_dict):\n # select root nodes with probability proportional to negative scores\n _eps_ = 1e-4\n s = np.array([node_score_dict[u] + _eps_ for u in graph.nodes()])\n # invert score and take exp\n p = np.exp(-s / np.max(np.absolute(s)))\n # normalize it to make it a prob\n p = p / p.sum()\n ns = [u for u in graph.nodes()]\n # sample up to half of the most negative nodes\n n = int(len(graph) / 2)\n sel = np.random.choice(ns, size=n, p=p, replace=False)\n selected_nodes = list(sel)\n return selected_nodes\n\n def _select_negative_nodes(self, graph, node_score_dict, edge_score_dict):\n selected_nodes = [u for u in graph.nodes() if node_score_dict[u] <= 0]\n return selected_nodes\n\n def _select_best(self, graphs):\n # select graph that maximizes the average objective score\n # (to contrast the tendency to produce large molecules)\n func = lambda g: self.objective_func(g) / float(len(g))\n best_graph = max(graphs, key=func)\n return best_graph\n\n def neighbors(self, graph):\n if self.n_neighbors is None:\n return list(self.graph_grammar.neighbors(graph))\n else:\n return list(self.graph_grammar.neighbors_sample(graph, self.n_neighbors))\n\n def make_neighbors(self, graph, selected_nodes=None, include_original=True):\n if selected_nodes is None or len(selected_nodes) == 0:\n out_graphs = self.make_all_neighbors(graph, include_original)\n else:\n out_graphs = self.make_neighbors_from_selected_nodes(\n graph, selected_nodes, include_original)\n return out_graphs\n\n def make_all_neighbors(self, graph, include_original=True):\n if include_original:\n out_graphs = [graph]\n else:\n out_graphs = []\n out_graphs += self.neighbors(graph)\n return out_graphs\n\n def make_neighbors_from_selected_nodes(self, graph, selected_nodes=None, include_original=True):\n # compute neighborhood, i.e. graphs that are obtained as a single core\n # substitution\n if include_original:\n out_graphs = [graph]\n else:\n out_graphs = []\n for selected_node in selected_nodes:\n graphs_it = self.graph_grammar.root_neighbors(\n graph, [selected_node], n_neighbors=self.n_neighbors)\n out_graphs += list(graphs_it)\n return out_graphs\n\n def make_gradient_neighbors(self, graph):\n if self.part_importance_estimator is None:\n selected_nodes = None\n else:\n res = self.part_importance_estimator.predict(graph)\n node_score_dict, edge_score_dict = res\n\n selected_nodes = self._select_negative_nodes(\n graph, node_score_dict, edge_score_dict)\n\n out_graphs = self.make_neighbors(graph, selected_nodes)\n return out_graphs\n\n def gradient_descent(self, graph):\n out_graphs = self.make_gradient_neighbors(graph)\n best_graph = self._select_best(out_graphs)\n return best_graph\n\n\n# ----------------------------------------------------------\n\nclass NeighborhoodEgoGraphGrammar(object):\n\n def __init__(self,\n decomposition_function=None,\n context=1,\n count=1,\n filter_max_num_substitutions=None,\n n_neighbors=None,\n perturbation_size=0,\n objective_func=None):\n self.n_neighbors = n_neighbors\n self.graph_grammar = lsgg_ego(\n decomposition_function=decomposition_function,\n thickness=context,\n filter_min_cip=count,\n filter_min_interface=2,\n filter_max_num_substitutions=filter_max_num_substitutions,\n nodelevel_radius_and_thickness=False)\n self.perturbation_size = perturbation_size\n self.objective_func = objective_func\n\n def fit(self, graphs):\n self.graph_grammar.fit(graphs)\n return self\n\n def __repr__(self):\n return str(self.graph_grammar)\n\n def neighbors(self, graph, n_neighbors=None):\n if n_neighbors is None:\n n_neighbors = self.n_neighbors\n if n_neighbors is None:\n ns = list(self.graph_grammar.neighbors(graph))\n else:\n ns = list(self.graph_grammar.neighbors_sample(graph, n_neighbors))\n return ns\n\n def perturb(self, orig_graph):\n graph = nx.Graph(orig_graph)\n for i in range(self.perturbation_size):\n # select a graph at random\n out_graphs = self.neighbors(graph)\n if len(out_graphs) > 2:\n graph = np.random.choice(out_graphs, size=1)[0]\n return graph\n\n def _select_best(self, graphs):\n # select graph that maximizes the average objective score\n # (to contrast the tendency to produce large molecules)\n func = lambda g: self.objective_func(g) / float(len(g))\n best_graph = max(graphs, key=func)\n return best_graph\n\n def gradient_descent(self, graph):\n out_graphs = self.neighbors(graph)\n best_graph = self._select_best([graph] + out_graphs)\n return best_graph\n\n\n# ----------------------------------------------------------\n\nclass NeighborhoodPartImportanceGraphGrammar(object):\n\n def __init__(self,\n decomposition_function=None,\n context=1,\n count=1,\n filter_max_num_substitutions=None,\n n_neighbors=None,\n fit_at_each_iteration=False,\n frac_nodes_to_select=.5,\n enforce_connected=True,\n domain_graphs=[]):\n self.domain_graphs = domain_graphs\n self.decomposition_function = decomposition_function\n self.n_neighbors = n_neighbors\n self.fit_at_each_iteration = fit_at_each_iteration\n self.part_importance_estimator = PartImportanceEstimator(\n decompose_func=decomposition_function)\n self.graph_grammar = lsgg_ego(\n decomposition_function=decomposition_function,\n thickness=context,\n filter_min_cip=count,\n filter_min_interface=2,\n filter_max_num_substitutions=filter_max_num_substitutions,\n nodelevel_radius_and_thickness=False)\n self.frac_nodes_to_select = frac_nodes_to_select\n self.enforce_connected = enforce_connected\n\n def fit_grammar(self, graphs):\n self.graph_grammar.fit(graphs+self.domain_graphs)\n return self\n\n def fit_part_importance_estimator(self, graphs, targets):\n self.part_importance_estimator.fit(graphs, targets)\n return self\n\n def fit(self, graphs, targets):\n if self.fit_at_each_iteration:\n self.fit_grammar(graphs)\n print('%30s: %s' % ('Part Importance Graph Grammar', self))\n return self.fit_part_importance_estimator(graphs, targets)\n\n def __repr__(self):\n return str(self.graph_grammar)\n\n def neighbors(self, graph, n_neighbors=None):\n res = self.part_importance_estimator.predict(graph)\n node_score_dict, edge_score_dict = res\n nodes = list(graph.nodes())\n # select the nodes with lowest score\n selected_nodes = sorted(nodes, key=lambda u: node_score_dict[u])\n selected_nodes = selected_nodes[\n :int(len(selected_nodes) * self.frac_nodes_to_select)]\n if n_neighbors is None:\n n_neighbors = self.n_neighbors\n if n_neighbors is None:\n ns = list(self.graph_grammar.neighbors(graph))\n else:\n ns = list(self.graph_grammar.root_neighbors(\n graph, selected_nodes, n_neighbors))\n random.shuffle(ns)\n ns = ns[:n_neighbors]\n if self.enforce_connected:\n ns = [g for g in ns if nx.is_connected(g)]\n return ns\n\n\n# ----------------------------------------------------------\n\nclass NeighborhoodAdaptiveGraphGrammar(object):\n\n def __init__(self,\n base_decomposition_function=None,\n approximate_decomposition_function=None,\n context=1,\n count=1,\n filter_max_num_substitutions=None,\n n_neighbors=None,\n ktop=4,\n enforce_connected=True):\n self.ktop = ktop\n self.base_decomposition_function = base_decomposition_function\n self.approximate_decomposition_function = approximate_decomposition_function\n self.n_neighbors = n_neighbors\n self.part_importance_estimator = PartImportanceEstimator(\n decompose_func=self.base_decomposition_function)\n self.graph_grammar = lsgg_ego(\n decomposition_function=self.base_decomposition_function,\n thickness=context,\n filter_min_cip=count,\n filter_min_interface=2,\n filter_max_num_substitutions=filter_max_num_substitutions,\n nodelevel_radius_and_thickness=False)\n self.enforce_connected = enforce_connected\n\n def fit(self, graphs, targets):\n self.fit_part_importance_estimator(graphs, targets)\n\n pos_dec = do_decompose(decompose_positive(ktop=self.ktop, part_importance_estimator=self.part_importance_estimator),\n compose_function=decompose_all_union)\n neg_dec = do_decompose(decompose_negative(ktop=self.ktop, part_importance_estimator=self.part_importance_estimator),\n compose_function=decompose_all_union)\n frag_dec = do_decompose(pos_dec, neg_dec, compose_function=self.approximate_decomposition_function)\n self.adaptive_decomposition_function = do_decompose(pos_dec, neg_dec, frag_dec)\n self.graph_grammar.set_decomposition(self.adaptive_decomposition_function)\n self.fit_grammar(graphs)\n print('%30s: %s' % ('Adaptive Graph Grammar', self))\n return self\n\n def fit_grammar(self, graphs):\n self.graph_grammar.fit(graphs)\n return self\n\n def fit_part_importance_estimator(self, graphs, targets):\n self.part_importance_estimator.fit(graphs, targets)\n return self\n\n def __repr__(self):\n return str(self.graph_grammar)\n\n def neighbors(self, graph, n_neighbors=None):\n if n_neighbors is None:\n n_neighbors = self.n_neighbors\n if n_neighbors is None:\n ns = list(self.graph_grammar.neighbors(graph))\n else:\n ns = list(self.graph_grammar.neighbors_sample(graph, n_neighbors))\n if self.enforce_connected:\n ns = [g for g in ns if nx.is_connected(g)]\n return ns\n" ]
[ [ "numpy.random.choice", "numpy.absolute" ] ]
OliverT1/gnina_tensorflow
[ "339310c643a85e6df1248d03dbbe4ae78cf59f19" ]
[ "layers/layer_functions.py" ]
[ "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Fri Sep 11 09:12:15 2020\n\n@author: scantleb\n@brief: Functions for generating keras layers.\n\"\"\"\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers\n\n\ndef generate_activation_layers(block_name, activation, append_name_info=True):\n \"\"\"Generate activation layers from strings representing activation layers.\n\n Arguments:\n block_name: name of the block the layer is a part of\n activation: string representing an activation function; can be\n standard keras string to AF names ('relu', 'sigmoid', etc.), or\n one of either 'prelu' (Parameterised ReLU) or 'threlu'\n (Thresholded ReLU)\n append_name_info: add activation function information to name\n\n Returns:\n Generator which produces layers with names containing increasing\n index numbers, with the specified activation function and base name.\n \"\"\"\n name_template = '{0}_{{0}}_{1}'.format(block_name, activation)\n block_name_index = 0\n while True:\n if append_name_info:\n act_name = name_template.format(block_name_index)\n else:\n act_name = block_name\n block_name_index += 1\n if activation == 'prelu':\n yield layers.PReLU(\n name=act_name,\n alpha_initializer=tf.keras.initializers.constant(0.1))\n elif activation == 'threlu':\n yield layers.ThresholdedReLU(theta=1.0, name=act_name)\n else:\n yield layers.Activation(activation, name=act_name)\n" ]
[ [ "tensorflow.keras.layers.ThresholdedReLU", "tensorflow.keras.initializers.constant", "tensorflow.keras.layers.Activation" ] ]
hologerry/magenta
[ "c08c17a548f97a3f5d294a010c28ea2803718d6f" ]
[ "magenta/models/gansynth/lib/data_helpers.py" ]
[ "# Copyright 2020 The Magenta Authors.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Data utility.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom magenta.models.gansynth.lib import datasets\nfrom magenta.models.gansynth.lib import train_util\nfrom magenta.models.gansynth.lib.specgrams_helper import SpecgramsHelper\nimport tensorflow.compat.v1 as tf # noqa\n\n\n\nclass DataHelper(object):\n \"\"\"A class for querying and converting data.\"\"\"\n\n def __init__(self, config):\n self._config = config\n self._dataset_name = config['dataset_name']\n self.dataset = datasets.registry[self._dataset_name](config)\n self.specgrams_helper = self.make_specgrams_helper()\n\n def _map_fn(self):\n \"\"\"Create a mapping function for the dataset.\"\"\"\n raise NotImplementedError\n\n def make_specgrams_helper(self):\n \"\"\"Create a specgrams helper for the dataset.\"\"\"\n raise NotImplementedError\n\n def data_to_waves(self, data):\n \"\"\"Converts data representation to waveforms.\"\"\"\n raise NotImplementedError\n\n def waves_to_data(self, waves):\n \"\"\"Converts data representation to waveforms.\"\"\"\n raise NotImplementedError\n\n def get_pitch_counts(self):\n \"\"\"Returns a dictionary {pitch value (int): count (int)}.\"\"\"\n return self.dataset.get_pitch_counts()\n\n def provide_one_hot_labels(self, batch_size):\n \"\"\"Returns a batch of one-hot labels.\"\"\"\n with tf.name_scope('inputs'):\n with tf.device('/cpu:0'):\n return self.dataset.provide_one_hot_labels(batch_size=batch_size)\n\n def provide_data(self, batch_size):\n \"\"\"Returns a batch of data and one-hot labels.\"\"\"\n with tf.name_scope('inputs'):\n with tf.device('/cpu:0'):\n dataset = self.dataset.provide_dataset()\n dataset = dataset.shuffle(buffer_size=1000)\n dataset = dataset.map(self._map_fn, num_parallel_calls=4)\n dataset = dataset.batch(batch_size)\n dataset = dataset.prefetch(1)\n\n iterator = dataset.make_initializable_iterator()\n tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS,\n iterator.initializer)\n\n data, one_hot_labels = iterator.get_next()\n data.set_shape([batch_size, None, None, None])\n one_hot_labels.set_shape([batch_size, None])\n return data, one_hot_labels\n\n\nclass DataSTFTHelper(DataHelper):\n \"\"\"A data helper for Linear Spectrograms.\"\"\"\n\n def make_specgrams_helper(self):\n final_resolutions = train_util.make_resolution_schedule(\n **self._config).final_resolutions\n return SpecgramsHelper(\n audio_length=self._config['audio_length'],\n spec_shape=final_resolutions,\n overlap=0.75,\n sample_rate=self._config['sample_rate'],\n mel_downscale=1,\n ifreq=True)\n\n def _map_fn(self, wave, one_hot_label):\n waves = wave[tf.newaxis, :, :]\n data = self.waves_to_data(waves)\n return data[0], one_hot_label\n\n def data_to_waves(self, data):\n return self.specgrams_helper.specgrams_to_waves(data)\n\n def waves_to_data(self, waves):\n return self.specgrams_helper.waves_to_specgrams(waves)\n\n\nclass DataWaveHelper(DataSTFTHelper):\n \"\"\"A data helper for raw waveforms.\n\n For compatibility with the spectral network architectues, we add a second\n (redundant) channel and zero-pad along the time axis.\n \"\"\"\n\n def make_specgrams_helper(self):\n return SpecgramsHelper(audio_length=64000,\n spec_shape=(256, 512),\n overlap=0.75,\n sample_rate=self._config['sample_rate'],\n mel_downscale=2)\n\n def data_to_waves(self, data):\n return data[:, 768:-768, 0, :1]\n\n def waves_to_data(self, waves):\n waves = waves[:, :, None, :]\n pad = tf.zeros([tf.shape(waves)[0], 768, 1, 1])\n waves = tf.concat([pad, waves, pad], axis=1)\n return tf.concat([waves, waves], axis=3)\n\n\nclass DataSTFTNoIFreqHelper(DataHelper):\n \"\"\"A data helper for Linear Spectrograms.\"\"\"\n\n def make_specgrams_helper(self):\n final_resolutions = train_util.make_resolution_schedule(\n **self._config).final_resolutions\n return SpecgramsHelper(\n audio_length=self._config['audio_length'],\n spec_shape=final_resolutions,\n overlap=0.75,\n sample_rate=self._config['sample_rate'],\n mel_downscale=1,\n ifreq=False)\n\n def _map_fn(self, wave, one_hot_label):\n waves = wave[tf.newaxis, :, :]\n data = self.waves_to_data(waves)\n return data[0], one_hot_label\n\n def data_to_waves(self, data):\n return self.specgrams_helper.specgrams_to_waves(data)\n\n def waves_to_data(self, waves):\n return self.specgrams_helper.waves_to_specgrams(waves)\n\n\nclass DataMelHelper(DataSTFTHelper):\n \"\"\"A data helper for Mel Spectrograms.\"\"\"\n\n def data_to_waves(self, data):\n return self.specgrams_helper.melspecgrams_to_waves(data)\n\n def waves_to_data(self, waves):\n return self.specgrams_helper.waves_to_melspecgrams(waves)\n\n\nregistry = {\n 'linear': DataSTFTHelper,\n 'phase': DataSTFTNoIFreqHelper,\n 'mel': DataMelHelper,\n 'wave': DataWaveHelper,\n}\n" ]
[ [ "tensorflow.compat.v1.device", "tensorflow.compat.v1.concat", "tensorflow.compat.v1.shape", "tensorflow.compat.v1.add_to_collection", "tensorflow.compat.v1.name_scope" ] ]
BMC-SDNU/Cross-Modal-Hashing-Retrieval
[ "0196e313aad0a93ebf93e1150f024d6a07f8363a" ]
[ "DCHUC/utils/txt_module.py" ]
[ "import torch\nfrom torch import nn\nfrom torch.nn import functional as F\n\nLAYER1_NODE = 10240\n\n\ndef weights_init(m):\n if type(m) == nn.Conv2d:\n nn.init.xavier_uniform(m.weight.data)\n nn.init.constant(m.bias.data, 0.01)\n\n\nclass TxtModule(nn.Module):\n def __init__(self, y_dim, bit):\n \"\"\"\n :param y_dim: dimension of tags\n :param bit: bit number of the final binary code\n \"\"\"\n super(TxtModule, self).__init__()\n self.module_name = \"text_model\"\n\n # full-conv layers\n self.conv1 = nn.Conv2d(1, LAYER1_NODE, kernel_size=(y_dim, 1), stride=(1, 1))\n self.conv2 = nn.Conv2d(LAYER1_NODE, bit, kernel_size=1, stride=(1, 1))\n self.apply(weights_init)\n self.classifier = nn.Sequential(\n self.conv1,\n nn.ReLU(inplace=True),\n nn.Dropout(),\n self.conv2,\n )\n\n def forward(self, x):\n x = self.classifier(x)\n x = x.squeeze()\n tanh = nn.Tanh()\n x = tanh(x)\n return x\n\n" ]
[ [ "torch.nn.init.xavier_uniform", "torch.nn.init.constant", "torch.nn.Tanh", "torch.nn.Conv2d", "torch.nn.ReLU", "torch.nn.Dropout" ] ]
csherstan/DeepRL
[ "fbf8da1f158792a0b9d29728c9d407ae40573070" ]
[ "aux_0.99_10.0.py" ]
[ "import argparse\n\nimport torch\n\nfrom deep_rl import random_seed, set_one_thread, select_device, Config, generate_tag, Task, TDAuxNet, NatureConvBody, \\\n LinearSchedule, AsyncReplay, ImageNormalizer, SignNormalizer, run_steps, mkdir\nfrom deep_rl.agent.TDAux_agent import TDAuxAgent\nimport os\n\ndef td_aux_many(config: Config, **kwargs):\n \"\"\"\n\n :param config:\n :param kwargs: kwargs used to generate the experiment tag name uses for saving.\n :return:\n \"\"\"\n generate_tag(kwargs)\n kwargs.setdefault('log_level', 0)\n config.merge(kwargs)\n\n mkdir(os.path.join(config.data_dir, 'log'))\n mkdir(os.path.join(config.data_dir, 'data'))\n\n config.task_fn = lambda: Task(config.game)\n config.eval_env = config.task_fn()\n # aux_gammas = [0.0, 0.5, 0.9, 0.99]\n aux_gammas = [0.99]\n\n aux_dict = {str(g).replace(\".\", \"_\"): TDAuxNet.AuxCFG(g, loss_weight=10.0) for g in aux_gammas}\n # aux_dict = {}\n\n # config.optimizer_fn = lambda params: torch.optim.RMSprop(\n # params, lr=0.00025, alpha=0.95, eps=0.01, centered=True)\n config.optimizer_fn = lambda params: torch.optim.Adam(params, lr=1e-4)\n # I'm just hard coding the shape of the target\n config.network_fn = lambda: TDAuxNet((84, 84), config.action_dim,\n NatureConvBody(in_channels=config.history_length), aux_dict)\n config.random_action_prob = LinearSchedule(1.0, 0.01, 1e6)\n\n config.replay_fn = lambda: AsyncReplay(memory_size=int(5e5), batch_size=32)\n\n config.batch_size = 32\n config.state_normalizer = ImageNormalizer()\n config.reward_normalizer = SignNormalizer()\n config.discount = 0.99\n config.target_network_update_freq = 10000\n config.exploration_steps = 50000\n config.sgd_update_frequency = 4\n config.gradient_clip = 5\n config.history_length = 4\n # config.double_q = True\n config.double_q = False\n run_steps(TDAuxAgent(config))\n\n\nif __name__ == \"__main__\":\n cf = Config()\n cf.add_argument('--game', required=True)\n cf.add_argument('--run', type=int, required=True)\n cf.add_argument('--data_dir', type=str, required=True)\n cf.add_argument('--save_interval', type=int, default=1000000)\n cf.add_argument('--max_steps', type=int, default=int(2.5e7))\n cf.merge()\n\n set_one_thread()\n select_device(0)\n\n td_aux_many(cf, game=cf.game, run=cf.run, remark=\"aux_0.99_10.0\")\n" ]
[ [ "torch.optim.Adam" ] ]
wqruan/tf-encrypted
[ "50ee4ae3ba76b7c1f70a90e18f875191adea0a07", "50ee4ae3ba76b7c1f70a90e18f875191adea0a07" ]
[ "primitives/tf_encrypted/primitives/paillier/primitives_test.py", "operations/secure_random/test_secure_random.py" ]
[ "# pylint: disable=missing-docstring\nimport unittest\n\nimport numpy as np\nimport tensorflow as tf\nfrom absl.testing import parameterized\n\nfrom tf_encrypted.primitives import paillier\nfrom tf_encrypted.test import tf_execution_context\n\n\nclass EncryptionTest(parameterized.TestCase):\n @parameterized.parameters(\n {\n \"run_eagerly\": run_eagerly,\n \"export_dtype\": export_dtype,\n \"export_expansion\": export_expansion,\n }\n for run_eagerly in [True, False]\n for export_dtype, export_expansion in [(tf.string, ())]\n )\n def test_export(self, run_eagerly, export_dtype, export_expansion):\n x = np.array([[12345, 34342]])\n\n context = tf_execution_context(run_eagerly)\n with context.scope():\n\n ek, dk = paillier.gen_keypair()\n assert isinstance(ek, paillier.EncryptionKey)\n assert isinstance(dk, paillier.DecryptionKey)\n n_exported = ek.export(export_dtype)\n assert isinstance(n_exported, tf.Tensor)\n assert n_exported.dtype == export_dtype\n assert n_exported.shape == (1, 1), n_exported.shape\n p_exported, q_exported = dk.export(export_dtype)\n assert isinstance(p_exported, tf.Tensor)\n assert p_exported.dtype == export_dtype\n assert p_exported.shape == (1, 1), p_exported.shape\n assert isinstance(q_exported, tf.Tensor)\n assert q_exported.dtype == export_dtype\n assert q_exported.shape == (1, 1), q_exported.shape\n\n r = paillier.gen_randomness(ek, shape=x.shape)\n assert isinstance(r, paillier.Randomness)\n r_exported = r.export(export_dtype)\n assert isinstance(r_exported, tf.Tensor)\n assert r_exported.dtype == export_dtype\n assert r_exported.shape == x.shape + export_expansion\n\n c = paillier.encrypt(ek, x, r)\n assert isinstance(c, paillier.Ciphertext)\n c_exported = c.export(export_dtype)\n assert isinstance(c_exported, tf.Tensor)\n assert c_exported.dtype == export_dtype\n assert c_exported.shape == x.shape + export_expansion\n\n @parameterized.parameters(\n {\"run_eagerly\": run_eagerly} for run_eagerly in (True, False)\n )\n def test_correctness(self, run_eagerly):\n\n p = 100000015333\n q = 100000015021\n n = p * q\n nn = n * n\n g = 1 + n\n x = 123456789\n r = 5083216764521909821749\n c = pow(g, x, nn) * pow(r, n, nn) % nn\n\n context = tf_execution_context(run_eagerly)\n with context.scope():\n\n ek = paillier.EncryptionKey(tf.constant([[str(n)]]))\n plaintext = np.array([[x]]).astype(str)\n randomness = paillier.Randomness(tf.constant([[str(r)]]))\n ciphertext = paillier.encrypt(ek, plaintext, randomness)\n\n expected = np.array([[c]]).astype(str)\n actual = ciphertext.export(tf.string)\n\n np.testing.assert_equal(context.evaluate(actual).astype(str), expected)\n\n @parameterized.parameters(\n {\"run_eagerly\": run_eagerly, \"x\": x, \"dtype\": dtype}\n for run_eagerly in [True, False]\n for x, dtype in [\n (np.array([[12345, 34342]]).astype(np.int32), tf.int32),\n (np.array([[\"12345\", \"34342\"]]).astype(str), tf.string),\n (\n np.array(\n [\n [\n \"123456789123456789123456789123456789\",\n \"987654321987654321987654321987654321\",\n ]\n ]\n ).astype(str),\n tf.string,\n ),\n ]\n )\n def test_encrypt_decrypt(self, run_eagerly, x, dtype):\n context = tf_execution_context(run_eagerly)\n with context.scope():\n\n ek, dk = paillier.gen_keypair()\n r = paillier.gen_randomness(ek, shape=x.shape)\n c = paillier.encrypt(ek, x, r)\n y = paillier.decrypt(dk, c, dtype=dtype)\n assert isinstance(y, tf.Tensor)\n assert y.dtype == dtype\n\n np.testing.assert_equal(context.evaluate(y).astype(x.dtype), x)\n\n @parameterized.parameters(\n {\"run_eagerly\": run_eagerly, \"dtype\": dtype, \"x0\": x0, \"x1\": x1}\n for run_eagerly in (True, False)\n for dtype in (tf.int32, tf.string)\n for x0 in (np.array([[12345, 123243]]), np.array([[12345]]))\n for x1 in (np.array([[12656, 434234]]),)\n )\n def test_add(self, run_eagerly, dtype, x0, x1):\n\n expected = x0 + x1\n\n context = tf_execution_context(run_eagerly)\n with context.scope():\n ek, dk = paillier.gen_keypair()\n\n r0 = paillier.gen_randomness(ek, shape=x0.shape)\n c0 = paillier.encrypt(ek, x0, r0)\n\n r1 = paillier.gen_randomness(ek, shape=x1.shape)\n c1 = paillier.encrypt(ek, x1, r1)\n\n c = paillier.add(ek, c0, c1)\n y = paillier.decrypt(dk, c, dtype=dtype)\n\n np.testing.assert_equal(\n context.evaluate(y).astype(np.int32), expected.astype(np.int32)\n )\n\n\nif __name__ == \"__main__\":\n unittest.main()\n", "# pylint: disable=missing-docstring\nimport os\nimport unittest\n\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.python.framework import errors\nfrom tensorflow.python.framework.errors import NotFoundError\n\nimport tf_encrypted as tfe\n\ndirname = os.path.dirname(tfe.__file__)\nso_name = \"{dn}/operations/secure_random/secure_random_module_tf_{tfv}.so\"\nshared_object = so_name.format(dn=dirname, tfv=tf.__version__)\nnotfound_msg = \"secure_random_module not found\"\n\ntry:\n secure_random_module = tf.load_op_library(shared_object)\n seeded_random_uniform = secure_random_module.secure_seeded_random_uniform\n random_uniform = secure_random_module.secure_random_uniform\n seed = secure_random_module.secure_seed\nexcept NotFoundError:\n secure_random_module = None\n\n\[email protected](secure_random_module is None, notfound_msg)\nclass TestSeededRandomUniform(unittest.TestCase):\n def test_int32_return(self):\n expected = [[608, 425, 925], [198, 891, 721]]\n\n with tf.Session():\n output = seeded_random_uniform(\n [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], 0, 1000\n ).eval()\n\n np.testing.assert_array_equal(output, expected)\n\n def test_int64_return(self):\n expected = [[425, 198, 721], [911, 617, 113]]\n\n with tf.Session():\n minval = tf.constant(0, dtype=tf.int64)\n maxval = tf.constant(1000, dtype=tf.int64)\n\n output = seeded_random_uniform(\n [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval\n ).eval()\n\n np.testing.assert_array_equal(output, expected)\n\n def test_min_max_range(self):\n with tf.Session():\n minval = tf.constant(-100000000, dtype=tf.int32)\n maxval = tf.constant(100000000, dtype=tf.int32)\n\n output = seeded_random_uniform(\n [10000], [1, 1, 1, 500, 1, 1, 1, 2], minval, maxval\n ).eval()\n\n for out in output:\n assert -100000000 <= out < 100000000\n\n def test_invalid_max_min(self):\n with tf.Session():\n minval = tf.constant(1000, dtype=tf.int64)\n maxval = tf.constant(-1000, dtype=tf.int64)\n\n with np.testing.assert_raises(errors.InvalidArgumentError):\n seeded_random_uniform(\n [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval\n ).eval()\n\n def test_negative_numbers(self):\n expected = [[-1575, -1802, -1279], [-1089, -1383, -1887]]\n with tf.Session():\n minval = tf.constant(-2000, dtype=tf.int64)\n maxval = tf.constant(-1000, dtype=tf.int64)\n\n output = seeded_random_uniform(\n [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval\n ).eval()\n\n np.testing.assert_array_equal(output, expected)\n\n\[email protected](secure_random_module is None, notfound_msg)\nclass TestRandomUniform(unittest.TestCase):\n def test_min_max_range(self):\n with tf.Session():\n minval = tf.constant(-10000000, dtype=tf.int32)\n maxval = tf.constant(10000000, dtype=tf.int32)\n\n output = random_uniform([1000], minval, maxval).eval()\n\n for out in output:\n assert -10000000 <= out < 10000000\n\n def test_small_range(self):\n with tf.Session():\n minval = tf.constant(-10, dtype=tf.int32)\n maxval = tf.constant(10, dtype=tf.int32)\n\n output = random_uniform([1000], minval, maxval).eval()\n\n for out in output:\n assert -10 <= out < 10\n\n def test_neg_range(self):\n with tf.Session():\n minval = tf.constant(-100, dtype=tf.int32)\n maxval = tf.constant(0, dtype=tf.int32)\n\n output = random_uniform([1000], minval, maxval).eval()\n\n for out in output:\n assert out < 0\n\n\[email protected](secure_random_module is None, notfound_msg)\nclass TestSeed(unittest.TestCase):\n def test_seed(self):\n with tf.Session():\n s = seed()\n\n minval = tf.constant(-2000, dtype=tf.int64)\n maxval = tf.constant(0, dtype=tf.int64)\n\n shape = [2, 3]\n\n output = seeded_random_uniform(shape, s, minval, maxval).eval()\n\n np.testing.assert_array_equal(output.shape, shape)\n\n\nif __name__ == \"__main__\":\n unittest.main()\n" ]
[ [ "numpy.array" ], [ "numpy.testing.assert_raises", "tensorflow.load_op_library", "numpy.testing.assert_array_equal", "tensorflow.Session", "tensorflow.constant" ] ]
guilhermeprokisch/catalyst
[ "21e096b261912d9e905584178d6ee626072c23cb" ]
[ "catalyst/pipeline/mixins.py" ]
[ "\"\"\"\r\nMixins classes for use with Filters and Factors.\r\n\"\"\"\r\nfrom textwrap import dedent\r\n\r\nfrom numpy import (\r\n array,\r\n full,\r\n recarray,\r\n vstack,\r\n)\r\nfrom pandas import NaT as pd_NaT\r\n\r\nfrom catalyst.errors import (\r\n WindowLengthNotPositive,\r\n UnsupportedDataType,\r\n NoFurtherDataError,\r\n)\r\nfrom catalyst.utils.control_flow import nullctx\r\nfrom catalyst.utils.input_validation import expect_types\r\nfrom catalyst.utils.sharedoc import (\r\n format_docstring,\r\n PIPELINE_ALIAS_NAME_DOC,\r\n PIPELINE_DOWNSAMPLING_FREQUENCY_DOC,\r\n)\r\nfrom catalyst.utils.pandas_utils import nearest_unequal_elements\r\n\r\n\r\nfrom .downsample_helpers import (\r\n select_sampling_indices,\r\n expect_downsample_frequency,\r\n)\r\nfrom .sentinels import NotSpecified\r\nfrom .term import Term\r\n\r\n\r\nclass PositiveWindowLengthMixin(object):\r\n \"\"\"\r\n Validation mixin enforcing that a Term gets a positive WindowLength\r\n \"\"\"\r\n def _validate(self):\r\n super(PositiveWindowLengthMixin, self)._validate()\r\n if not self.windowed:\r\n raise WindowLengthNotPositive(window_length=self.window_length)\r\n\r\n\r\nclass SingleInputMixin(object):\r\n \"\"\"\r\n Validation mixin enforcing that a Term gets a length-1 inputs list.\r\n \"\"\"\r\n def _validate(self):\r\n super(SingleInputMixin, self)._validate()\r\n num_inputs = len(self.inputs)\r\n if num_inputs != 1:\r\n raise ValueError(\r\n \"{typename} expects only one input, \"\r\n \"but received {num_inputs} instead.\".format(\r\n typename=type(self).__name__,\r\n num_inputs=num_inputs\r\n )\r\n )\r\n\r\n\r\nclass StandardOutputs(object):\r\n \"\"\"\r\n Validation mixin enforcing that a Term cannot produce non-standard outputs.\r\n \"\"\"\r\n def _validate(self):\r\n super(StandardOutputs, self)._validate()\r\n if self.outputs is not NotSpecified:\r\n raise ValueError(\r\n \"{typename} does not support custom outputs,\"\r\n \" but received custom outputs={outputs}.\".format(\r\n typename=type(self).__name__,\r\n outputs=self.outputs,\r\n )\r\n )\r\n\r\n\r\nclass RestrictedDTypeMixin(object):\r\n \"\"\"\r\n Validation mixin enforcing that a term has a specific dtype.\r\n \"\"\"\r\n ALLOWED_DTYPES = NotSpecified\r\n\r\n def _validate(self):\r\n super(RestrictedDTypeMixin, self)._validate()\r\n assert self.ALLOWED_DTYPES is not NotSpecified, (\r\n \"ALLOWED_DTYPES not supplied on subclass \"\r\n \"of RestrictedDTypeMixin: %s.\" % type(self).__name__\r\n )\r\n\r\n if self.dtype not in self.ALLOWED_DTYPES:\r\n raise UnsupportedDataType(\r\n typename=type(self).__name__,\r\n dtype=self.dtype,\r\n )\r\n\r\n\r\nclass CustomTermMixin(object):\r\n \"\"\"\r\n Mixin for user-defined rolling-window Terms.\r\n\r\n Implements `_compute` in terms of a user-defined `compute` function, which\r\n is mapped over the input windows.\r\n\r\n Used by CustomFactor, CustomFilter, CustomClassifier, etc.\r\n \"\"\"\r\n ctx = nullctx()\r\n\r\n def __new__(cls,\r\n inputs=NotSpecified,\r\n outputs=NotSpecified,\r\n window_length=NotSpecified,\r\n mask=NotSpecified,\r\n dtype=NotSpecified,\r\n missing_value=NotSpecified,\r\n ndim=NotSpecified,\r\n **kwargs):\r\n\r\n unexpected_keys = set(kwargs) - set(cls.params)\r\n if unexpected_keys:\r\n raise TypeError(\r\n \"{termname} received unexpected keyword \"\r\n \"arguments {unexpected}\".format(\r\n termname=cls.__name__,\r\n unexpected={k: kwargs[k] for k in unexpected_keys},\r\n )\r\n )\r\n\r\n return super(CustomTermMixin, cls).__new__(\r\n cls,\r\n inputs=inputs,\r\n outputs=outputs,\r\n window_length=window_length,\r\n mask=mask,\r\n dtype=dtype,\r\n missing_value=missing_value,\r\n ndim=ndim,\r\n **kwargs\r\n )\r\n\r\n def compute(self, today, assets, out, *arrays):\r\n \"\"\"\r\n Override this method with a function that writes a value into `out`.\r\n \"\"\"\r\n raise NotImplementedError()\r\n\r\n def _allocate_output(self, windows, shape):\r\n \"\"\"\r\n Allocate an output array whose rows should be passed to `self.compute`.\r\n\r\n The resulting array must have a shape of ``shape``.\r\n\r\n If we have standard outputs (i.e. self.outputs is NotSpecified), the\r\n default is an empty ndarray whose dtype is ``self.dtype``.\r\n\r\n If we have an outputs tuple, the default is an empty recarray with\r\n ``self.outputs`` as field names. Each field will have dtype\r\n ``self.dtype``.\r\n\r\n This can be overridden to control the kind of array constructed\r\n (e.g. to produce a LabelArray instead of an ndarray).\r\n \"\"\"\r\n missing_value = self.missing_value\r\n outputs = self.outputs\r\n if outputs is not NotSpecified:\r\n out = recarray(\r\n shape,\r\n formats=[self.dtype.str] * len(outputs),\r\n names=outputs,\r\n )\r\n out[:] = missing_value\r\n else:\r\n out = full(shape, missing_value, dtype=self.dtype)\r\n return out\r\n\r\n def _format_inputs(self, windows, column_mask):\r\n inputs = []\r\n for input_ in windows:\r\n window = next(input_)\r\n if window.shape[1] == 1:\r\n # Do not mask single-column inputs.\r\n inputs.append(window)\r\n else:\r\n inputs.append(window[:, column_mask])\r\n return inputs\r\n\r\n def _compute(self, windows, dates, assets, mask):\r\n \"\"\"\r\n Call the user's `compute` function on each window with a pre-built\r\n output array.\r\n \"\"\"\r\n format_inputs = self._format_inputs\r\n compute = self.compute\r\n params = self.params\r\n ndim = self.ndim\r\n\r\n shape = (len(mask), 1) if ndim == 1 else mask.shape\r\n out = self._allocate_output(windows, shape)\r\n\r\n with self.ctx:\r\n for idx, date in enumerate(dates):\r\n # Never apply a mask to 1D outputs.\r\n out_mask = array([True]) if ndim == 1 else mask[idx]\r\n\r\n # Mask our inputs as usual.\r\n inputs_mask = mask[idx]\r\n\r\n masked_assets = assets[inputs_mask]\r\n out_row = out[idx][out_mask]\r\n inputs = format_inputs(windows, inputs_mask)\r\n\r\n compute(date, masked_assets, out_row, *inputs, **params)\r\n out[idx][out_mask] = out_row\r\n return out\r\n\r\n def short_repr(self):\r\n return type(self).__name__ + '(%d)' % self.window_length\r\n\r\n\r\nclass LatestMixin(SingleInputMixin):\r\n \"\"\"\r\n Mixin for behavior shared by Custom{Factor,Filter,Classifier}.\r\n \"\"\"\r\n window_length = 1\r\n\r\n def compute(self, today, assets, out, data):\r\n out[:] = data[-1]\r\n\r\n def _validate(self):\r\n super(LatestMixin, self)._validate()\r\n if self.inputs[0].dtype != self.dtype:\r\n raise TypeError(\r\n \"{name} expected an input of dtype {expected}, \"\r\n \"but got {actual} instead.\".format(\r\n name=type(self).__name__,\r\n expected=self.dtype,\r\n actual=self.inputs[0].dtype,\r\n )\r\n )\r\n\r\n\r\nclass AliasedMixin(SingleInputMixin):\r\n \"\"\"\r\n Mixin for aliased terms.\r\n \"\"\"\r\n def __new__(cls, term, name):\r\n return super(AliasedMixin, cls).__new__(\r\n cls,\r\n inputs=(term,),\r\n outputs=term.outputs,\r\n window_length=0,\r\n name=name,\r\n dtype=term.dtype,\r\n missing_value=term.missing_value,\r\n ndim=term.ndim,\r\n window_safe=term.window_safe,\r\n )\r\n\r\n def _init(self, name, *args, **kwargs):\r\n self.name = name\r\n return super(AliasedMixin, self)._init(*args, **kwargs)\r\n\r\n @classmethod\r\n def _static_identity(cls, name, *args, **kwargs):\r\n return (\r\n super(AliasedMixin, cls)._static_identity(*args, **kwargs),\r\n name,\r\n )\r\n\r\n def _compute(self, inputs, dates, assets, mask):\r\n return inputs[0]\r\n\r\n def __repr__(self):\r\n return '{type}({inner_type}(...), name={name!r})'.format(\r\n type=type(self).__name__,\r\n inner_type=type(self.inputs[0]).__name__,\r\n name=self.name,\r\n )\r\n\r\n def short_repr(self):\r\n return self.name\r\n\r\n @classmethod\r\n def make_aliased_type(cls, other_base):\r\n \"\"\"\r\n Factory for making Aliased{Filter,Factor,Classifier}.\r\n \"\"\"\r\n docstring = dedent(\r\n \"\"\"\r\n A {t} that names another {t}.\r\n\r\n Parameters\r\n ----------\r\n term : {t}\r\n {{name}}\r\n \"\"\"\r\n ).format(t=other_base.__name__)\r\n\r\n doc = format_docstring(\r\n owner_name=other_base.__name__,\r\n docstring=docstring,\r\n formatters={'name': PIPELINE_ALIAS_NAME_DOC},\r\n )\r\n\r\n return type(\r\n 'Aliased' + other_base.__name__,\r\n (cls, other_base),\r\n {'__doc__': doc,\r\n '__module__': other_base.__module__},\r\n )\r\n\r\n\r\nclass DownsampledMixin(StandardOutputs):\r\n \"\"\"\r\n Mixin for behavior shared by Downsampled{Factor,Filter,Classifier}\r\n\r\n A downsampled term is a wrapper around the \"real\" term that performs actual\r\n computation. The downsampler is responsible for calling the real term's\r\n `compute` method at selected intervals and forward-filling the computed\r\n values.\r\n\r\n Downsampling is not currently supported for terms with multiple outputs.\r\n \"\"\"\r\n # There's no reason to take a window of a downsampled term. The whole\r\n # point is that you're re-using the same result multiple times.\r\n window_safe = False\r\n\r\n @expect_types(term=Term)\r\n @expect_downsample_frequency\r\n def __new__(cls, term, frequency):\r\n return super(DownsampledMixin, cls).__new__(\r\n cls,\r\n inputs=term.inputs,\r\n outputs=term.outputs,\r\n window_length=term.window_length,\r\n mask=term.mask,\r\n frequency=frequency,\r\n wrapped_term=term,\r\n dtype=term.dtype,\r\n missing_value=term.missing_value,\r\n ndim=term.ndim,\r\n )\r\n\r\n def _init(self, frequency, wrapped_term, *args, **kwargs):\r\n self._frequency = frequency\r\n self._wrapped_term = wrapped_term\r\n return super(DownsampledMixin, self)._init(*args, **kwargs)\r\n\r\n @classmethod\r\n def _static_identity(cls, frequency, wrapped_term, *args, **kwargs):\r\n return (\r\n super(DownsampledMixin, cls)._static_identity(*args, **kwargs),\r\n frequency,\r\n wrapped_term,\r\n )\r\n\r\n def compute_extra_rows(self,\r\n all_dates,\r\n start_date,\r\n end_date,\r\n min_extra_rows):\r\n \"\"\"\r\n Ensure that min_extra_rows pushes us back to a computation date.\r\n\r\n Parameters\r\n ----------\r\n all_dates : pd.DatetimeIndex\r\n The trading sessions against which ``self`` will be computed.\r\n start_date : pd.Timestamp\r\n The first date for which final output is requested.\r\n end_date : pd.Timestamp\r\n The last date for which final output is requested.\r\n min_extra_rows : int\r\n The minimum number of extra rows required of ``self``, as\r\n determined by other terms that depend on ``self``.\r\n\r\n Returns\r\n -------\r\n extra_rows : int\r\n The number of extra rows to compute. This will be the minimum\r\n number of rows required to make our computed start_date fall on a\r\n recomputation date.\r\n \"\"\"\r\n try:\r\n current_start_pos = all_dates.get_loc(start_date) - min_extra_rows\r\n if current_start_pos < 0:\r\n raise NoFurtherDataError(\r\n initial_message=\"Insufficient data to compute Pipeline:\",\r\n first_date=all_dates[0],\r\n lookback_start=start_date,\r\n lookback_length=min_extra_rows,\r\n )\r\n except KeyError:\r\n before, after = nearest_unequal_elements(all_dates, start_date)\r\n raise ValueError(\r\n \"Pipeline start_date {start_date} is not in calendar.\\n\"\r\n \"Latest date before start_date is {before}.\\n\"\r\n \"Earliest date after start_date is {after}.\".format(\r\n start_date=start_date,\r\n before=before,\r\n after=after,\r\n )\r\n )\r\n\r\n # Our possible target dates are all the dates on or before the current\r\n # starting position.\r\n # TODO: Consider bounding this below by self.window_length\r\n candidates = all_dates[:current_start_pos + 1]\r\n\r\n # Choose the latest date in the candidates that is the start of a new\r\n # period at our frequency.\r\n choices = select_sampling_indices(candidates, self._frequency)\r\n\r\n # If we have choices, the last choice is the first date if the\r\n # period containing current_start_date. Choose it.\r\n new_start_date = candidates[choices[-1]]\r\n\r\n # Add the difference between the new and old start dates to get the\r\n # number of rows for the new start_date.\r\n new_start_pos = all_dates.get_loc(new_start_date)\r\n assert new_start_pos <= current_start_pos, \\\r\n \"Computed negative extra rows!\"\r\n\r\n return min_extra_rows + (current_start_pos - new_start_pos)\r\n\r\n def _compute(self, inputs, dates, assets, mask):\r\n \"\"\"\r\n Compute by delegating to self._wrapped_term._compute on sample dates.\r\n\r\n On non-sample dates, forward-fill from previously-computed samples.\r\n \"\"\"\r\n to_sample = dates[select_sampling_indices(dates, self._frequency)]\r\n assert to_sample[0] == dates[0], \\\r\n \"Misaligned sampling dates in %s.\" % type(self).__name__\r\n\r\n real_compute = self._wrapped_term._compute\r\n\r\n # Inputs will contain different kinds of values depending on whether or\r\n # not we're a windowed computation.\r\n\r\n # If we're windowed, then `inputs` is a list of iterators of ndarrays.\r\n # If we're not windowed, then `inputs` is just a list of ndarrays.\r\n # There are two things we care about doing with the input:\r\n # 1. Preparing an input to be passed to our wrapped term.\r\n # 2. Skipping an input if we're going to use an already-computed row.\r\n # We perform these actions differently based on the expected kind of\r\n # input, and we encapsulate these actions with closures so that we\r\n # don't clutter the code below with lots of branching.\r\n if self.windowed:\r\n # If we're windowed, inputs are stateful AdjustedArrays. We don't\r\n # need to do any preparation before forwarding to real_compute, but\r\n # we need to call `next` on them if we want to skip an iteration.\r\n def prepare_inputs():\r\n return inputs\r\n\r\n def skip_this_input():\r\n for w in inputs:\r\n next(w)\r\n else:\r\n # If we're not windowed, inputs are just ndarrays. We need to\r\n # slice out a single row when forwarding to real_compute, but we\r\n # don't need to do anything to skip an input.\r\n def prepare_inputs():\r\n # i is the loop iteration variable below.\r\n return [a[[i]] for a in inputs]\r\n\r\n def skip_this_input():\r\n pass\r\n\r\n results = []\r\n samples = iter(to_sample)\r\n next_sample = next(samples)\r\n for i, compute_date in enumerate(dates):\r\n if next_sample == compute_date:\r\n results.append(\r\n real_compute(\r\n prepare_inputs(),\r\n dates[i:i + 1],\r\n assets,\r\n mask[i:i + 1],\r\n )\r\n )\r\n try:\r\n next_sample = next(samples)\r\n except StopIteration:\r\n # No more samples to take. Set next_sample to Nat, which\r\n # compares False with any other datetime.\r\n next_sample = pd_NaT\r\n else:\r\n skip_this_input()\r\n # Copy results from previous sample period.\r\n results.append(results[-1])\r\n\r\n # We should have exhausted our sample dates.\r\n try:\r\n next_sample = next(samples)\r\n except StopIteration:\r\n pass\r\n else:\r\n raise AssertionError(\"Unconsumed sample date: %s\" % next_sample)\r\n\r\n # Concatenate stored results.\r\n return vstack(results)\r\n\r\n @classmethod\r\n def make_downsampled_type(cls, other_base):\r\n \"\"\"\r\n Factory for making Downsampled{Filter,Factor,Classifier}.\r\n \"\"\"\r\n docstring = dedent(\r\n \"\"\"\r\n A {t} that defers to another {t} at lower-than-daily frequency.\r\n\r\n Parameters\r\n ----------\r\n term : {t}\r\n {{frequency}}\r\n \"\"\"\r\n ).format(t=other_base.__name__)\r\n\r\n doc = format_docstring(\r\n owner_name=other_base.__name__,\r\n docstring=docstring,\r\n formatters={'frequency': PIPELINE_DOWNSAMPLING_FREQUENCY_DOC},\r\n )\r\n\r\n return type(\r\n 'Downsampled' + other_base.__name__,\r\n (cls, other_base,),\r\n {'__doc__': doc,\r\n '__module__': other_base.__module__},\r\n )\r\n" ]
[ [ "numpy.vstack", "numpy.full", "numpy.array" ] ]
ssbyrne89/DIYInvestmentPrimer
[ "eae81d9e7c67b5d912bc7ed7037432f03ee4792c" ]
[ "web_app/routes/company_routes.py" ]
[ "# web_app/routes/company_routes.py\nimport pandas as pd\nfrom flask import Blueprint, jsonify, request, render_template #, flash, redirect\n\nfrom web_app.models import *\n\ncompany_routes = Blueprint(\"company_routes\", __name__)\n\n@company_routes.route(\"/div_yield\")\ndef seeDivYield():\n return render_template(\"highest_DivYield_charts.html\")\n\n@company_routes.route(\"/highest_increasing_divs\")\ndef seecompanies_w_highest_dividend_increases():\n return render_template(\"companies_w_highest_dividend_increases.html\")\n\n@company_routes.route(\"/most_affordable_div_payers\")\ndef seemost_affordable_div_payers():\n return render_template(\"most_affordable.html\")\n\n\n\n@company_routes.route(\"/companies\")\ndef list_companies_for_humans():\n return render_template(\"All_SP500.html\", message=\"Here's all the companies on the S&P 500\",\n companies=get_AllCompanies())\n\n\n@company_routes.route(\"/test\")\ndef seeTEST():\n return render_template(\"test.html\", message=\"Here's all the companies on the S&P 500\")\n\ndef get_AllCompanies():\n all = Company_Info.query.all()\n names = [record.Company_Name for record in all]\n return names\n\n\ndef createCompanyInfoTable(): # ran once\n SandP500 = pd.read_csv('../DIYInvestmentPrimer/SandP_500_companies.csv')\n\n for x in range(0, len(SandP500)):\n db.create_all()\n company_entry = Company_Info.query.get\n (Company_Info(Company_Name=SandP500['Security'][x],\n Company_Ticker=SandP500['Symbol'][x],\n Sector=SandP500['GICS Sector'][x],\n SubIndustry=SandP500['GICS Sub-Industry'][x],\n HQ_Location=SandP500['Headquarters Location'][x],\n Date_first_added_to_SP500=SandP500['Date first added'][x],\n Founded=SandP500['Founded'][x]))\n\n db.session.add(company_entry)\n db.session.commit()\n\n " ]
[ [ "pandas.read_csv" ] ]
epfl-lasa/crowdbot-evaluation-tools
[ "0e98c76428f6af5a4caa6b83b91ac05b3ed300ce" ]
[ "qolo/viz_traj.py" ]
[ "#!/usr/bin/env python3\n# -*-coding:utf-8 -*-\n# =============================================================================\n\"\"\"\n@Author : Yujie He\n@File : viz_traj.py\n@Date created : 2022/02/25\n@Maintainer : Yujie He\n@Email : [email protected]\n\"\"\"\n# =============================================================================\n\"\"\"\nThe module provides script to visualize the robot trajectory and the longest k\n(default:3) trajectories of the pedestrians around Qolo robot.\nExample: python qolo/viz_traj.py -f 0410_mds --all --overwrite\n\"\"\"\n# =============================================================================\n\"\"\"\nTODO:\n1. plot vx/vy or linear/angular velocity\n\"\"\"\n# =============================================================================\n\nimport os\nimport sys\nimport argparse\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom qolo.core.crowdbot_data import CrowdBotDatabase\nfrom qolo.utils.geo_util import quat2yaw\nfrom qolo.utils.file_io_util import (\n load_json2dict,\n load_pkl2dict,\n)\nfrom qolo.utils.res_plot_util import (\n viz_qolo_ped_traj_full,\n viz_qolo_ped_traj_frame,\n get_nlongest_peds,\n viz_ped_speed,\n viz_ped_speed_vw,\n)\n\ncolor_list = ['navy', 'blue', 'slateblue', 'violet', 'skyblue']\n\n\ndef main():\n parser = argparse.ArgumentParser(\n description=\"visualize trajectories of pedestrians around Qolo\"\n )\n\n parser.add_argument(\n \"-f\",\n \"--folder\",\n default=\"0410_mds\",\n type=str,\n help=\"different subfolder in rosbag/ dir\",\n )\n parser.add_argument(\n \"--seq\",\n default=\"2021-04-10-11-28-10\", # 2021-04-10-10-38-36 2021-04-10-10-41-17\n type=str,\n help=\"specific sequence in the subfolder\",\n )\n parser.add_argument(\n \"--all\",\n dest=\"process_all\",\n action=\"store_true\",\n help=\"Process all sequences and disable single sequences\",\n )\n parser.set_defaults(process_all=False)\n parser.add_argument(\n \"--overwrite\",\n dest=\"overwrite\",\n action=\"store_true\",\n help=\"Whether to overwrite existing output (default: false)\",\n )\n parser.set_defaults(overwrite=True)\n args = parser.parse_args()\n\n cb_data = CrowdBotDatabase(args.folder)\n\n if args.seq is None or args.process_all:\n seqs = [cb_data.seqs[seq_idx] for seq_idx in range(cb_data.nr_seqs())]\n else:\n seqs = [args.seq]\n\n for seq_idx, seq in enumerate(seqs):\n\n sq_idx = cb_data.seqs.index(seq)\n seq_len = cb_data.nr_frames(sq_idx)\n\n print(\"({}/{}): {} with {} frames\".format(seq_idx + 1, len(seqs), seq, seq_len))\n\n # dest: path_img_path\n eval_res_dir = os.path.join(cb_data.metrics_dir)\n\n if not os.path.exists(eval_res_dir):\n print(\"Result images and npy will be saved in {}\".format(eval_res_dir))\n os.makedirs(eval_res_dir, exist_ok=True)\n\n path_img_path = os.path.join(eval_res_dir, seq, seq + \"_traj.png\")\n proc_path_img_path = os.path.join(eval_res_dir, seq, seq + \"_traj_proc.png\")\n\n # path_img_path = os.path.join(eval_res_dir, seq, seq + \"_{}_traj.png\".format(frame_id))\n plot_exist = os.path.exists(path_img_path)\n if plot_exist and not args.overwrite:\n print(\"{} plots already generated!!!\".format(seq))\n print(\"Will not overwrite. If you want to overwrite, use flag --overwrite\")\n continue\n\n # src 1: trajectory data\n traj_dir = os.path.join(cb_data.ped_data_dir, \"traj\")\n if not os.path.exists(traj_dir):\n sys.exit(\"Please use det2traj.py to extract pedestrian trajectories first!\")\n traj_pkl_path = os.path.join(traj_dir, seq + '.pkl')\n # traj_json_path = os.path.join(traj_dir, seq + '.json')\n proc_traj_pkl_path = os.path.join(traj_dir, seq + '_proc.pkl')\n\n # src 2: qolo data\n tf_qolo_dir = os.path.join(cb_data.source_data_dir, \"tf_qolo\")\n pose_stamp_path = os.path.join(tf_qolo_dir, seq + \"_tfqolo_sampled.npy\")\n pose_stamped = np.load(pose_stamp_path, allow_pickle=True).item()\n\n # src 3: velocity path\n vel_dir = os.path.join(cb_data.ped_data_dir, \"vel\")\n vel_pkl_path = os.path.join(vel_dir, seq + '.pkl')\n\n trans_array = pose_stamped[\"position\"]\n qolo_pose = {\n 'x': trans_array[:, 0],\n 'y': trans_array[:, 1],\n 'init_ori': pose_stamped[\"orientation\"],\n }\n\n ped_traj_dict = load_pkl2dict(traj_pkl_path)\n # ped_traj_dict = load_json2dict(traj_json_path)\n ped_vel_dict = load_pkl2dict(vel_pkl_path)\n\n top_ids = get_nlongest_peds(ped_traj_dict, ped_num=5)\n\n viz_qolo_ped_traj_full(\n path_img_path,\n qolo_pose,\n ped_traj_dict,\n viz_ids=top_ids,\n color_list=color_list,\n )\n\n # visualize processed trajectory\n proc_ped_traj_dict = load_pkl2dict(proc_traj_pkl_path)\n viz_qolo_ped_traj_full(\n proc_path_img_path,\n qolo_pose,\n proc_ped_traj_dict,\n viz_ids=top_ids,\n color_list=color_list,\n )\n\n ped_vel_img_path1 = os.path.join(eval_res_dir, seq, seq + \"_ped_vel.png\")\n ped_vel_img_path2 = os.path.join(eval_res_dir, seq, seq + \"_ped_vw.png\")\n\n viz_ped_speed(\n ped_vel_img_path1,\n ped_vel_dict,\n viz_ids=top_ids,\n color_list=color_list,\n )\n\n viz_ped_speed_vw(\n ped_vel_img_path2,\n ped_vel_dict,\n viz_ids=top_ids,\n color_list=color_list,\n )\n\n\nif __name__ == \"__main__\":\n main()\n" ]
[ [ "numpy.load" ] ]
muzudho/collatz
[ "da99c1cf4cc65c42eeb3ea07134c0e25fc35e606" ]
[ "lifegame_lj.py" ]
[ "\"\"\"\n左寄せ表記(Left justified)\n\"\"\"\n\nimport os\nimport numpy as np\n\n# 環境変数\nRADIX = int(os.getenv(\"RADIX\", 2))\n\n# 桁揃えに利用。10進数27 を指定したときの見やすさをデフォルトにするぜ(^~^)\ncount_width = 3\ncount_str = \"\"\ndec_width = 4\ndec_str = \"\"\nradix_str = \"\"\n# 表示した数の個数\ncount = 0\n\ndef update_print_number(dec):\n \"\"\"表示するテキストの更新\"\"\"\n global count_width\n global count_str\n global dec_width\n global dec_str\n global radix_str\n global count\n global RADIX\n\n count_str = f\"{count}\"\n\n # 桁数の更新\n if count_width < len(count_str):\n count_width += 1\n if dec_width < len(str(dec)):\n dec_width += 1\n\n count_str = count_str.rjust(count_width)\n radix_str = str(np.base_repr(dec, RADIX))\n dec_str = str(dec).rjust(dec_width)\n count += 1\n\ndef print_number():\n \"\"\"表示\"\"\"\n global count_str\n global dec_str\n global radix_str\n\n print(f\"[{count_str}] ({dec_str}) {radix_str}\")\n\n# 数を入力してください。\nprint(f\"RADIX={RADIX}\")\nprint(\"Please enter a number.\")\nprint(\"Example 1: 27\")\nprint(\"Example 2: 0b11011\")\n\n# めんどくさいんで、内部的には10進で計算\nnumber_str = input()\n\nif number_str.startswith(\"0b\"):\n radix_str = number_str[2:]\n dec = int(radix_str, 2) # 2進数で入力されたものを10進に変換\nelse:\n dec = int(number_str) # 10進数\n\n# 初回表示\nprint(f\"Start\")\nupdate_print_number(dec)\nprint_number()\n\nwhile True:\n # 奇数になるまで2で割るぜ(^~^)\n while dec % 2 == 0:\n dec = dec // 2\n\n # 表示\n # TODO 偶数なんで省くオプションとか欲しい(^~^)\n update_print_number(dec)\n print_number()\n\n # 1 だったら抜けるぜ(^~^)\n if dec==1:\n break\n\n # 3x+1 するぜ(^~^)\n dec = 3 * dec + 1\n\n # 表示\n update_print_number(dec)\n print_number()\n\nprint(f\"Finished\")\n" ]
[ [ "numpy.base_repr" ] ]
balazssimon/ml-playground
[ "c2eba497bebc53e5a03807bdd8873c55f0ec73e1" ]
[ "udemy/Deep Learning A-Z/Volume 2 - Unsupervised Deep Learning/Part 4 - Self Organizing Maps (SOM)/mega_case_study.py" ]
[ "# Mega Case Study - Make a Hybrid Deep Learning Model\n\n\n\n# Part 1 - Identify the Frauds with the Self-Organizing Map\n\n# Importing the libraries\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\n# Importing the dataset\ndataset = pd.read_csv('Credit_Card_Applications.csv')\nX = dataset.iloc[:, :-1].values\ny = dataset.iloc[:, -1].values\n\n# Feature Scaling\nfrom sklearn.preprocessing import MinMaxScaler\nsc = MinMaxScaler(feature_range = (0, 1))\nX = sc.fit_transform(X)\n\n# Training the SOM\nfrom minisom import MiniSom\nsom = MiniSom(x = 10, y = 10, input_len = 15, sigma = 1.0, learning_rate = 0.5)\nsom.random_weights_init(X)\nsom.train_random(data = X, num_iteration = 100)\n\n# Visualizing the results\nfrom pylab import bone, pcolor, colorbar, plot, show\nbone()\npcolor(som.distance_map().T)\ncolorbar()\nmarkers = ['o', 's']\ncolors = ['r', 'g']\nfor i, x in enumerate(X):\n w = som.winner(x)\n plot(w[0] + 0.5,\n w[1] + 0.5,\n markers[y[i]],\n markeredgecolor = colors[y[i]],\n markerfacecolor = 'None',\n markersize = 10,\n markeredgewidth = 2)\nshow()\n\n# Finding the frauds\nmappings = som.win_map(X)\nfrauds = np.concatenate((mappings[(1,7)], mappings[1,4]), axis = 0)\nfrauds = sc.inverse_transform(frauds)\n\n\n\n# Part 2 - Going from Unsupervised to Supervised Deep Learning\n\n# Creating the matrix of features\ncustomers = dataset.iloc[:, 1:].values\n\n# Creating the dependent variable\nis_fraud = np.zeros(len(dataset))\nfor i in range(len(dataset)):\n if dataset.iloc[i,0] in frauds:\n is_fraud[i] = 1\n\n# Feature Scaling\nfrom sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\ncustomers = sc.fit_transform(customers)\n\n# Part 2 - Now let's make the ANN!\n\n# Importing the Keras libraries and packages\nfrom keras.models import Sequential\nfrom keras.layers import Dense\n\n# Initialising the ANN\nclassifier = Sequential()\n\n# Adding the input layer and the first hidden layer\nclassifier.add(Dense(units = 2, kernel_initializer = 'uniform', activation = 'relu', input_dim = 15))\n\n# Adding the output layer\nclassifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))\n\n# Compiling the ANN\nclassifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])\n\n# Fitting the ANN to the Training set\nclassifier.fit(customers, is_fraud, batch_size = 1, epochs = 2)\n\n# Predicting the probabilities of frauds\ny_pred = classifier.predict(customers)\ny_pred = np.concatenate((dataset.iloc[:, 0:1].values, y_pred), axis = 1)\ny_pred = y_pred[y_pred[:, 1].argsort()]" ]
[ [ "pandas.read_csv", "sklearn.preprocessing.MinMaxScaler", "numpy.concatenate", "sklearn.preprocessing.StandardScaler" ] ]
gatling-nrl/scikit-fem
[ "04730d80d612470b7e802eed4c21dd96b89cef61" ]
[ "skfem/generic_utils.py" ]
[ "import numpy as np\n\nfrom numpy import ndarray\n\n\ndef hash_args(*args):\n \"\"\"Return a tuple of hashes, with numpy support.\"\"\"\n return tuple(hash(arg.tobytes())\n if isinstance(arg, ndarray)\n else hash(arg) for arg in args)\n\n\nclass OrientedBoundary(ndarray):\n \"\"\"An array of facet indices with orientation.\"\"\"\n\n def __new__(cls, indices, ori):\n obj = np.asarray(indices).view(cls)\n obj.ori = np.array(ori, dtype=int)\n assert len(obj) == len(obj.ori)\n return obj\n\n def __array_finalize__(self, obj):\n if obj is None:\n return\n self.ori = getattr(obj, 'ori', None)\n" ]
[ [ "numpy.array", "numpy.asarray" ] ]
asears/stanza
[ "f91ca215e175d4f7b202259fe789374db7829395" ]
[ "stanza/models/parser.py" ]
[ "\"\"\"\nEntry point for training and evaluating a dependency parser.\n\nThis implementation combines a deep biaffine graph-based parser with linearization and distance features.\nFor details please refer to paper: https://nlp.stanford.edu/pubs/qi2018universal.pdf.\n\"\"\"\n\n\"\"\"\nTraining and evaluation for the parser.\n\"\"\"\n\nimport sys\nimport os\nimport shutil\nimport time\nimport argparse\nimport logging\nimport numpy as np\nimport random\nimport torch\nfrom torch import nn, optim\n\nimport stanza.models.depparse.data as data\nfrom stanza.models.depparse.data import DataLoader\nfrom stanza.models.depparse.trainer import Trainer\nfrom stanza.models.depparse import scorer\nfrom stanza.models.common import utils\nfrom stanza.models.common import pretrain\nfrom stanza.models.common.data import augment_punct\nfrom stanza.models.common.doc import *\nfrom stanza.utils.conll import CoNLL\nfrom stanza.models import _training_logging\n\nlogger = logging.getLogger('stanza')\n\ndef parse_args(args=None):\n parser = argparse.ArgumentParser()\n parser.add_argument('--data_dir', type=str, default='data/depparse', help='Root dir for saving models.')\n parser.add_argument('--wordvec_dir', type=str, default='extern_data/word2vec', help='Directory of word vectors.')\n parser.add_argument('--wordvec_file', type=str, default=None, help='Word vectors filename.')\n parser.add_argument('--wordvec_pretrain_file', type=str, default=None, help='Exact name of the pretrain file to read')\n parser.add_argument('--train_file', type=str, default=None, help='Input file for data loader.')\n parser.add_argument('--eval_file', type=str, default=None, help='Input file for data loader.')\n parser.add_argument('--output_file', type=str, default=None, help='Output CoNLL-U file.')\n parser.add_argument('--gold_file', type=str, default=None, help='Output CoNLL-U file.')\n\n parser.add_argument('--mode', default='train', choices=['train', 'predict'])\n parser.add_argument('--lang', type=str, help='Language')\n parser.add_argument('--shorthand', type=str, help=\"Treebank shorthand\")\n\n parser.add_argument('--hidden_dim', type=int, default=400)\n parser.add_argument('--char_hidden_dim', type=int, default=400)\n parser.add_argument('--deep_biaff_hidden_dim', type=int, default=400)\n parser.add_argument('--composite_deep_biaff_hidden_dim', type=int, default=100)\n parser.add_argument('--word_emb_dim', type=int, default=75)\n parser.add_argument('--char_emb_dim', type=int, default=100)\n parser.add_argument('--tag_emb_dim', type=int, default=50)\n parser.add_argument('--transformed_dim', type=int, default=125)\n parser.add_argument('--num_layers', type=int, default=3)\n parser.add_argument('--char_num_layers', type=int, default=1)\n parser.add_argument('--pretrain_max_vocab', type=int, default=250000)\n parser.add_argument('--word_dropout', type=float, default=0.33)\n parser.add_argument('--dropout', type=float, default=0.5)\n parser.add_argument('--rec_dropout', type=float, default=0, help=\"Recurrent dropout\")\n parser.add_argument('--char_rec_dropout', type=float, default=0, help=\"Recurrent dropout\")\n parser.add_argument('--no_char', dest='char', action='store_false', help=\"Turn off character model.\")\n parser.add_argument('--no_pretrain', dest='pretrain', action='store_false', help=\"Turn off pretrained embeddings.\")\n parser.add_argument('--no_linearization', dest='linearization', action='store_false', help=\"Turn off linearization term.\")\n parser.add_argument('--no_distance', dest='distance', action='store_false', help=\"Turn off distance term.\")\n\n parser.add_argument('--sample_train', type=float, default=1.0, help='Subsample training data.')\n parser.add_argument('--optim', type=str, default='adam', help='sgd, adagrad, adam or adamax.')\n parser.add_argument('--lr', type=float, default=3e-3, help='Learning rate')\n parser.add_argument('--beta2', type=float, default=0.95)\n\n parser.add_argument('--max_steps', type=int, default=50000)\n parser.add_argument('--eval_interval', type=int, default=100)\n parser.add_argument('--max_steps_before_stop', type=int, default=3000)\n parser.add_argument('--batch_size', type=int, default=5000)\n parser.add_argument('--max_grad_norm', type=float, default=1.0, help='Gradient clipping.')\n parser.add_argument('--log_step', type=int, default=20, help='Print log every k steps.')\n parser.add_argument('--save_dir', type=str, default='saved_models/depparse', help='Root dir for saving models.')\n parser.add_argument('--save_name', type=str, default=None, help=\"File name to save the model\")\n\n parser.add_argument('--seed', type=int, default=1234)\n parser.add_argument('--cuda', type=bool, default=torch.cuda.is_available())\n parser.add_argument('--cpu', action='store_true', help='Ignore CUDA.')\n\n parser.add_argument('--augment_nopunct', type=float, default=None, help='Augment the training data by copying this fraction of punct-ending sentences as non-punct. Default of None will aim for roughly 10%')\n\n args = parser.parse_args(args=args)\n return args\n\ndef main(args=None):\n args = parse_args(args=args)\n\n if args.cpu:\n args.cuda = False\n utils.set_random_seed(args.seed, args.cuda)\n\n args = vars(args)\n logger.info(\"Running parser in {} mode\".format(args['mode']))\n\n if args['mode'] == 'train':\n train(args)\n else:\n evaluate(args)\n\n# TODO: refactor with tagger\ndef model_file_name(args):\n if args['save_name'] is not None:\n save_name = args['save_name']\n else:\n save_name = args['shorthand'] + \"_parser.pt\"\n\n return os.path.join(args['save_dir'], save_name)\n\n# TODO: refactor with everywhere\ndef load_pretrain(args):\n pt = None\n if args['pretrain']:\n pretrain_file = pretrain.find_pretrain_file(args['wordvec_pretrain_file'], args['save_dir'], args['shorthand'], args['lang'])\n if os.path.exists(pretrain_file):\n vec_file = None\n else:\n vec_file = args['wordvec_file'] if args['wordvec_file'] else utils.get_wordvec_file(args['wordvec_dir'], args['shorthand'])\n pt = pretrain.Pretrain(pretrain_file, vec_file, args['pretrain_max_vocab'])\n return pt\n\ndef train(args):\n model_file = model_file_name(args)\n utils.ensure_dir(os.path.split(model_file)[0])\n\n # load pretrained vectors if needed\n pretrain = load_pretrain(args)\n\n # load data\n logger.info(\"Loading data with batch size {}...\".format(args['batch_size']))\n train_data, _ = CoNLL.conll2dict(input_file=args['train_file'])\n # possibly augment the training data with some amount of fake data\n # based on the options chosen\n logger.info(\"Original data size: {}\".format(len(train_data)))\n train_data.extend(augment_punct(train_data, args['augment_nopunct'],\n keep_original_sentences=False))\n logger.info(\"Augmented data size: {}\".format(len(train_data)))\n train_doc = Document(train_data)\n train_batch = DataLoader(train_doc, args['batch_size'], args, pretrain, evaluation=False)\n vocab = train_batch.vocab\n dev_doc = CoNLL.conll2doc(input_file=args['eval_file'])\n dev_batch = DataLoader(dev_doc, args['batch_size'], args, pretrain, vocab=vocab, evaluation=True, sort_during_eval=True)\n\n # pred and gold path\n system_pred_file = args['output_file']\n gold_file = args['gold_file']\n\n # skip training if the language does not have training or dev data\n if len(train_batch) == 0 or len(dev_batch) == 0:\n logger.info(\"Skip training because no data available...\")\n sys.exit(0)\n\n logger.info(\"Training parser...\")\n trainer = Trainer(args=args, vocab=vocab, pretrain=pretrain, use_cuda=args['cuda'])\n\n global_step = 0\n max_steps = args['max_steps']\n dev_score_history = []\n best_dev_preds = []\n current_lr = args['lr']\n global_start_time = time.time()\n format_str = 'Finished STEP {}/{}, loss = {:.6f} ({:.3f} sec/batch), lr: {:.6f}'\n\n using_amsgrad = False\n last_best_step = 0\n # start training\n train_loss = 0\n while True:\n do_break = False\n for i, batch in enumerate(train_batch):\n start_time = time.time()\n global_step += 1\n loss = trainer.update(batch, eval=False) # update step\n train_loss += loss\n if global_step % args['log_step'] == 0:\n duration = time.time() - start_time\n logger.info(format_str.format(global_step, max_steps, loss, duration, current_lr))\n\n if global_step % args['eval_interval'] == 0:\n # eval on dev\n logger.info(\"Evaluating on dev set...\")\n dev_preds = []\n for batch in dev_batch:\n preds = trainer.predict(batch)\n dev_preds += preds\n dev_preds = utils.unsort(dev_preds, dev_batch.data_orig_idx)\n\n dev_batch.doc.set([HEAD, DEPREL], [y for x in dev_preds for y in x])\n CoNLL.write_doc2conll(dev_batch.doc, system_pred_file)\n _, _, dev_score = scorer.score(system_pred_file, gold_file)\n\n train_loss = train_loss / args['eval_interval'] # avg loss per batch\n logger.info(\"step {}: train_loss = {:.6f}, dev_score = {:.4f}\".format(global_step, train_loss, dev_score))\n train_loss = 0\n\n # save best model\n if len(dev_score_history) == 0 or dev_score > max(dev_score_history):\n last_best_step = global_step\n trainer.save(model_file)\n logger.info(\"new best model saved.\")\n best_dev_preds = dev_preds\n\n dev_score_history += [dev_score]\n\n if global_step - last_best_step >= args['max_steps_before_stop']:\n if not using_amsgrad:\n logger.info(\"Switching to AMSGrad\")\n last_best_step = global_step\n using_amsgrad = True\n trainer.optimizer = optim.Adam(trainer.model.parameters(), amsgrad=True, lr=args['lr'], betas=(.9, args['beta2']), eps=1e-6)\n else:\n do_break = True\n break\n\n if global_step >= args['max_steps']:\n do_break = True\n break\n\n if do_break: break\n\n train_batch.reshuffle()\n\n logger.info(\"Training ended with {} steps.\".format(global_step))\n\n best_f, best_eval = max(dev_score_history)*100, np.argmax(dev_score_history)+1\n logger.info(\"Best dev F1 = {:.2f}, at iteration = {}\".format(best_f, best_eval * args['eval_interval']))\n\ndef evaluate(args):\n # file paths\n system_pred_file = args['output_file']\n gold_file = args['gold_file']\n\n model_file = model_file_name(args)\n # load pretrained vectors if needed\n pretrain = load_pretrain(args)\n\n # load model\n logger.info(\"Loading model from: {}\".format(model_file))\n use_cuda = args['cuda'] and not args['cpu']\n trainer = Trainer(pretrain=pretrain, model_file=model_file, use_cuda=use_cuda)\n loaded_args, vocab = trainer.args, trainer.vocab\n\n # load config\n for k in args:\n if k.endswith('_dir') or k.endswith('_file') or k in ['shorthand'] or k == 'mode':\n loaded_args[k] = args[k]\n\n # load data\n logger.info(\"Loading data with batch size {}...\".format(args['batch_size']))\n doc = CoNLL.conll2doc(input_file=args['eval_file'])\n batch = DataLoader(doc, args['batch_size'], loaded_args, pretrain, vocab=vocab, evaluation=True, sort_during_eval=True)\n\n if len(batch) > 0:\n logger.info(\"Start evaluation...\")\n preds = []\n for i, b in enumerate(batch):\n preds += trainer.predict(b)\n else:\n # skip eval if dev data does not exist\n preds = []\n preds = utils.unsort(preds, batch.data_orig_idx)\n\n # write to file and score\n batch.doc.set([HEAD, DEPREL], [y for x in preds for y in x])\n CoNLL.write_doc2conll(batch.doc, system_pred_file)\n\n if gold_file is not None:\n _, _, score = scorer.score(system_pred_file, gold_file)\n\n logger.info(\"Parser score:\")\n logger.info(\"{} {:.2f}\".format(args['shorthand'], score*100))\n\nif __name__ == '__main__':\n main()\n" ]
[ [ "torch.cuda.is_available", "numpy.argmax" ] ]
st--/jupytext
[ "72aa6c4968da714323fbd7a7c548ee4b1274c946" ]
[ "demo/World population.pct.py" ]
[ "# ---\n# jupyter:\n# jupytext:\n# cell_markers: region,endregion\n# formats: ipynb,.pct.py:percent,.lgt.py:light,.spx.py:sphinx,md,Rmd,.pandoc.md:pandoc\n# text_representation:\n# extension: .py\n# format_name: percent\n# format_version: '1.2'\n# jupytext_version: 1.1.0\n# kernelspec:\n# display_name: Python 3\n# language: python\n# name: python3\n# ---\n\n# %% [markdown]\n# # A quick insight at world population\n#\n# ## Collecting population data\n#\n# In the below we retrieve population data from the\n# [World Bank](http://www.worldbank.org/)\n# using the [wbdata](https://github.com/OliverSherouse/wbdata) python package\n\n# %%\nimport pandas as pd\nimport wbdata as wb\n\npd.options.display.max_rows = 6\npd.options.display.max_columns = 20\n\n# %% [markdown]\n# Corresponding indicator is found using search method - or, directly,\n# the World Bank site.\n\n# %%\nwb.search_indicators('Population, total') # SP.POP.TOTL\n# wb.search_indicators('area')\n# => https://data.worldbank.org/indicator is easier to use\n\n# %% [markdown]\n# Now we download the population data\n\n# %%\nindicators = {'SP.POP.TOTL': 'Population, total',\n 'AG.SRF.TOTL.K2': 'Surface area (sq. km)',\n 'AG.LND.TOTL.K2': 'Land area (sq. km)',\n 'AG.LND.ARBL.ZS': 'Arable land (% of land area)'}\ndata = wb.get_dataframe(indicators, convert_date=True).sort_index()\ndata\n\n# %% [markdown]\n# World is one of the countries\n\n# %%\ndata.loc['World']\n\n# %% [markdown]\n# Can we classify over continents?\n\n# %%\ndata.loc[(slice(None), '2017-01-01'), :]['Population, total'].dropna(\n).sort_values().tail(60).index.get_level_values('country')\n\n# %% [markdown]\n# Extract zones manually (in order of increasing population)\n\n# %%\nzones = ['North America', 'Middle East & North Africa',\n 'Latin America & Caribbean', 'Europe & Central Asia',\n 'Sub-Saharan Africa', 'South Asia',\n 'East Asia & Pacific'][::-1]\n\n# %% [markdown]\n# And extract population information (and check total is right)\n\n# %%\npopulation = data.loc[zones]['Population, total'].swaplevel().unstack()\npopulation = population[zones]\nassert all(data.loc['World']['Population, total'] == population.sum(axis=1))\n\n# %% [markdown]\n# ## Stacked area plot with matplotlib\n\n# %%\nimport matplotlib.pyplot as plt\n\n# %%\nplt.clf()\nplt.figure(figsize=(10, 5), dpi=100)\nplt.stackplot(population.index, population.values.T / 1e9)\nplt.legend(population.columns, loc='upper left')\nplt.ylabel('Population count (B)')\nplt.show()\n\n# %% [markdown]\n# ## Stacked bar plot with plotly\n\n# %%\nimport plotly.offline as offline\nimport plotly.graph_objs as go\n\noffline.init_notebook_mode()\n\n# %%\ndata = [go.Scatter(x=population.index, y=population[zone], name=zone, stackgroup='World')\n for zone in zones]\nfig = go.Figure(data=data,\n layout=go.Layout(title='World population'))\noffline.iplot(fig)\n" ]
[ [ "matplotlib.pyplot.legend", "matplotlib.pyplot.stackplot", "matplotlib.pyplot.figure", "matplotlib.pyplot.clf", "matplotlib.pyplot.show", "matplotlib.pyplot.ylabel" ] ]
Zwysilence/tensorflow
[ "b55001be83da044bb21d539d433dec6231eaec55", "b55001be83da044bb21d539d433dec6231eaec55", "b55001be83da044bb21d539d433dec6231eaec55" ]
[ "tensorflow/contrib/rnn/python/ops/rnn_cell.py", "tensorflow/python/ops/image_ops_test.py", "tensorflow/compiler/tests/momentum_test.py" ]
[ "# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Module for constructing RNN Cells.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport collections\nimport math\n\nfrom tensorflow.contrib.compiler import jit\nfrom tensorflow.contrib.layers.python.layers import layers\nfrom tensorflow.contrib.rnn.python.ops import core_rnn_cell\nfrom tensorflow.python.framework import constant_op\nfrom tensorflow.python.framework import dtypes\nfrom tensorflow.python.framework import op_def_registry\nfrom tensorflow.python.framework import ops\nfrom tensorflow.python.framework import tensor_shape\nfrom tensorflow.python.layers import base as base_layer\nfrom tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import clip_ops\nfrom tensorflow.python.ops import gen_array_ops\nfrom tensorflow.python.ops import init_ops\nfrom tensorflow.python.ops import math_ops\nfrom tensorflow.python.ops import nn_impl # pylint: disable=unused-import\nfrom tensorflow.python.ops import nn_ops\nfrom tensorflow.python.ops import partitioned_variables # pylint: disable=unused-import\nfrom tensorflow.python.ops import random_ops\nfrom tensorflow.python.ops import rnn_cell_impl\nfrom tensorflow.python.ops import variable_scope as vs\nfrom tensorflow.python.platform import tf_logging as logging\nfrom tensorflow.python.util import nest\n\n\ndef _get_concat_variable(name, shape, dtype, num_shards):\n \"\"\"Get a sharded variable concatenated into one tensor.\"\"\"\n sharded_variable = _get_sharded_variable(name, shape, dtype, num_shards)\n if len(sharded_variable) == 1:\n return sharded_variable[0]\n\n concat_name = name + \"/concat\"\n concat_full_name = vs.get_variable_scope().name + \"/\" + concat_name + \":0\"\n for value in ops.get_collection(ops.GraphKeys.CONCATENATED_VARIABLES):\n if value.name == concat_full_name:\n return value\n\n concat_variable = array_ops.concat(sharded_variable, 0, name=concat_name)\n ops.add_to_collection(ops.GraphKeys.CONCATENATED_VARIABLES, concat_variable)\n return concat_variable\n\n\ndef _get_sharded_variable(name, shape, dtype, num_shards):\n \"\"\"Get a list of sharded variables with the given dtype.\"\"\"\n if num_shards > shape[0]:\n raise ValueError(\"Too many shards: shape=%s, num_shards=%d\" % (shape,\n num_shards))\n unit_shard_size = int(math.floor(shape[0] / num_shards))\n remaining_rows = shape[0] - unit_shard_size * num_shards\n\n shards = []\n for i in range(num_shards):\n current_size = unit_shard_size\n if i < remaining_rows:\n current_size += 1\n shards.append(\n vs.get_variable(\n name + \"_%d\" % i, [current_size] + shape[1:], dtype=dtype))\n return shards\n\n\ndef _norm(g, b, inp, scope):\n shape = inp.get_shape()[-1:]\n gamma_init = init_ops.constant_initializer(g)\n beta_init = init_ops.constant_initializer(b)\n with vs.variable_scope(scope):\n # Initialize beta and gamma for use by layer_norm.\n vs.get_variable(\"gamma\", shape=shape, initializer=gamma_init)\n vs.get_variable(\"beta\", shape=shape, initializer=beta_init)\n normalized = layers.layer_norm(inp, reuse=True, scope=scope)\n return normalized\n\n\nclass CoupledInputForgetGateLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Long short-term memory unit (LSTM) recurrent network cell.\n\n The default non-peephole implementation is based on:\n\n http://www.bioinf.jku.at/publications/older/2604.pdf\n\n S. Hochreiter and J. Schmidhuber.\n \"Long Short-Term Memory\". Neural Computation, 9(8):1735-1780, 1997.\n\n The peephole implementation is based on:\n\n https://research.google.com/pubs/archive/43905.pdf\n\n Hasim Sak, Andrew Senior, and Francoise Beaufays.\n \"Long short-term memory recurrent neural network architectures for\n large scale acoustic modeling.\" INTERSPEECH, 2014.\n\n The coupling of input and forget gate is based on:\n\n http://arxiv.org/pdf/1503.04069.pdf\n\n Greff et al. \"LSTM: A Search Space Odyssey\"\n\n The class uses optional peep-hole connections, and an optional projection\n layer.\n Layer normalization implementation is based on:\n\n https://arxiv.org/abs/1607.06450.\n\n \"Layer Normalization\"\n Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton\n\n and is applied before the internal nonlinearities.\n\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n initializer=None,\n num_proj=None,\n proj_clip=None,\n num_unit_shards=1,\n num_proj_shards=1,\n forget_bias=1.0,\n state_is_tuple=True,\n activation=math_ops.tanh,\n reuse=None,\n layer_norm=False,\n norm_gain=1.0,\n norm_shift=0.0):\n \"\"\"Initialize the parameters for an LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n use_peepholes: bool, set True to enable diagonal/peephole connections.\n initializer: (optional) The initializer to use for the weight and\n projection matrices.\n num_proj: (optional) int, The output dimensionality for the projection\n matrices. If None, no projection is performed.\n proj_clip: (optional) A float value. If `num_proj > 0` and `proj_clip` is\n provided, then the projected values are clipped elementwise to within\n `[-proj_clip, proj_clip]`.\n num_unit_shards: How to split the weight matrix. If >1, the weight\n matrix is stored across num_unit_shards.\n num_proj_shards: How to split the projection matrix. If >1, the\n projection matrix is stored across num_proj_shards.\n forget_bias: Biases of the forget gate are initialized by default to 1\n in order to reduce the scale of forgetting at the beginning of\n the training.\n state_is_tuple: If True, accepted and returned states are 2-tuples of\n the `c_state` and `m_state`. By default (False), they are concatenated\n along the column axis. This default behavior will soon be deprecated.\n activation: Activation function of the inner states.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n layer_norm: If `True`, layer normalization will be applied.\n norm_gain: float, The layer normalization gain initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n norm_shift: float, The layer normalization shift initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n \"\"\"\n super(CoupledInputForgetGateLSTMCell, self).__init__(_reuse=reuse)\n if not state_is_tuple:\n logging.warn(\"%s: Using a concatenated state is slower and will soon be \"\n \"deprecated. Use state_is_tuple=True.\", self)\n self._num_units = num_units\n self._use_peepholes = use_peepholes\n self._initializer = initializer\n self._num_proj = num_proj\n self._proj_clip = proj_clip\n self._num_unit_shards = num_unit_shards\n self._num_proj_shards = num_proj_shards\n self._forget_bias = forget_bias\n self._state_is_tuple = state_is_tuple\n self._activation = activation\n self._reuse = reuse\n self._layer_norm = layer_norm\n self._norm_gain = norm_gain\n self._norm_shift = norm_shift\n\n if num_proj:\n self._state_size = (\n rnn_cell_impl.LSTMStateTuple(num_units, num_proj)\n if state_is_tuple else num_units + num_proj)\n self._output_size = num_proj\n else:\n self._state_size = (\n rnn_cell_impl.LSTMStateTuple(num_units, num_units)\n if state_is_tuple else 2 * num_units)\n self._output_size = num_units\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def output_size(self):\n return self._output_size\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, batch x num_units.\n state: if `state_is_tuple` is False, this must be a state Tensor,\n `2-D, batch x state_size`. If `state_is_tuple` is True, this must be a\n tuple of state Tensors, both `2-D`, with column sizes `c_state` and\n `m_state`.\n\n Returns:\n A tuple containing:\n - A `2-D, [batch x output_dim]`, Tensor representing the output of the\n LSTM after reading `inputs` when previous state was `state`.\n Here output_dim is:\n num_proj if num_proj was set,\n num_units otherwise.\n - Tensor(s) representing the new state of LSTM after reading `inputs` when\n the previous state was `state`. Same type and shape(s) as `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference.\n \"\"\"\n sigmoid = math_ops.sigmoid\n\n num_proj = self._num_units if self._num_proj is None else self._num_proj\n\n if self._state_is_tuple:\n (c_prev, m_prev) = state\n else:\n c_prev = array_ops.slice(state, [0, 0], [-1, self._num_units])\n m_prev = array_ops.slice(state, [0, self._num_units], [-1, num_proj])\n\n dtype = inputs.dtype\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n concat_w = _get_concat_variable(\n \"W\", [input_size.value + num_proj, 3 * self._num_units], dtype,\n self._num_unit_shards)\n\n b = vs.get_variable(\n \"B\",\n shape=[3 * self._num_units],\n initializer=init_ops.zeros_initializer(),\n dtype=dtype)\n\n # j = new_input, f = forget_gate, o = output_gate\n cell_inputs = array_ops.concat([inputs, m_prev], 1)\n lstm_matrix = math_ops.matmul(cell_inputs, concat_w)\n\n # If layer nomalization is applied, do not add bias\n if not self._layer_norm:\n lstm_matrix = nn_ops.bias_add(lstm_matrix, b)\n\n j, f, o = array_ops.split(value=lstm_matrix, num_or_size_splits=3, axis=1)\n\n # Apply layer normalization\n if self._layer_norm:\n j = _norm(self._norm_gain, self._norm_shift, j, \"transform\")\n f = _norm(self._norm_gain, self._norm_shift, f, \"forget\")\n o = _norm(self._norm_gain, self._norm_shift, o, \"output\")\n\n # Diagonal connections\n if self._use_peepholes:\n w_f_diag = vs.get_variable(\n \"W_F_diag\", shape=[self._num_units], dtype=dtype)\n w_o_diag = vs.get_variable(\n \"W_O_diag\", shape=[self._num_units], dtype=dtype)\n\n if self._use_peepholes:\n f_act = sigmoid(f + self._forget_bias + w_f_diag * c_prev)\n else:\n f_act = sigmoid(f + self._forget_bias)\n c = (f_act * c_prev + (1 - f_act) * self._activation(j))\n\n # Apply layer normalization\n if self._layer_norm:\n c = _norm(self._norm_gain, self._norm_shift, c, \"state\")\n\n if self._use_peepholes:\n m = sigmoid(o + w_o_diag * c) * self._activation(c)\n else:\n m = sigmoid(o) * self._activation(c)\n\n if self._num_proj is not None:\n concat_w_proj = _get_concat_variable(\"W_P\",\n [self._num_units, self._num_proj],\n dtype, self._num_proj_shards)\n\n m = math_ops.matmul(m, concat_w_proj)\n if self._proj_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n m = clip_ops.clip_by_value(m, -self._proj_clip, self._proj_clip)\n # pylint: enable=invalid-unary-operand-type\n\n new_state = (\n rnn_cell_impl.LSTMStateTuple(c, m)\n if self._state_is_tuple else array_ops.concat([c, m], 1))\n return m, new_state\n\n\nclass TimeFreqLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Time-Frequency Long short-term memory unit (LSTM) recurrent network cell.\n\n This implementation is based on:\n\n Tara N. Sainath and Bo Li\n \"Modeling Time-Frequency Patterns with LSTM vs. Convolutional Architectures\n for LVCSR Tasks.\" submitted to INTERSPEECH, 2016.\n\n It uses peep-hole connections and optional cell clipping.\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n cell_clip=None,\n initializer=None,\n num_unit_shards=1,\n forget_bias=1.0,\n feature_size=None,\n frequency_skip=1,\n reuse=None):\n \"\"\"Initialize the parameters for an LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n use_peepholes: bool, set True to enable diagonal/peephole connections.\n cell_clip: (optional) A float value, if provided the cell state is clipped\n by this value prior to the cell output activation.\n initializer: (optional) The initializer to use for the weight and\n projection matrices.\n num_unit_shards: int, How to split the weight matrix. If >1, the weight\n matrix is stored across num_unit_shards.\n forget_bias: float, Biases of the forget gate are initialized by default\n to 1 in order to reduce the scale of forgetting at the beginning\n of the training.\n feature_size: int, The size of the input feature the LSTM spans over.\n frequency_skip: int, The amount the LSTM filter is shifted by in\n frequency.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(TimeFreqLSTMCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._use_peepholes = use_peepholes\n self._cell_clip = cell_clip\n self._initializer = initializer\n self._num_unit_shards = num_unit_shards\n self._forget_bias = forget_bias\n self._feature_size = feature_size\n self._frequency_skip = frequency_skip\n self._state_size = 2 * num_units\n self._output_size = num_units\n self._reuse = reuse\n\n @property\n def output_size(self):\n return self._output_size\n\n @property\n def state_size(self):\n return self._state_size\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, batch x num_units.\n state: state Tensor, 2D, batch x state_size.\n\n Returns:\n A tuple containing:\n - A 2D, batch x output_dim, Tensor representing the output of the LSTM\n after reading \"inputs\" when previous state was \"state\".\n Here output_dim is num_units.\n - A 2D, batch x state_size, Tensor representing the new state of LSTM\n after reading \"inputs\" when previous state was \"state\".\n Raises:\n ValueError: if an input_size was specified and the provided inputs have\n a different dimension.\n \"\"\"\n sigmoid = math_ops.sigmoid\n tanh = math_ops.tanh\n\n freq_inputs = self._make_tf_features(inputs)\n dtype = inputs.dtype\n actual_input_size = freq_inputs[0].get_shape().as_list()[1]\n\n concat_w = _get_concat_variable(\n \"W\", [actual_input_size + 2 * self._num_units, 4 * self._num_units],\n dtype, self._num_unit_shards)\n\n b = vs.get_variable(\n \"B\",\n shape=[4 * self._num_units],\n initializer=init_ops.zeros_initializer(),\n dtype=dtype)\n\n # Diagonal connections\n if self._use_peepholes:\n w_f_diag = vs.get_variable(\n \"W_F_diag\", shape=[self._num_units], dtype=dtype)\n w_i_diag = vs.get_variable(\n \"W_I_diag\", shape=[self._num_units], dtype=dtype)\n w_o_diag = vs.get_variable(\n \"W_O_diag\", shape=[self._num_units], dtype=dtype)\n\n # initialize the first freq state to be zero\n m_prev_freq = array_ops.zeros(\n [inputs.shape[0].value or inputs.get_shape()[0], self._num_units],\n dtype)\n for fq in range(len(freq_inputs)):\n c_prev = array_ops.slice(state, [0, 2 * fq * self._num_units],\n [-1, self._num_units])\n m_prev = array_ops.slice(state, [0, (2 * fq + 1) * self._num_units],\n [-1, self._num_units])\n # i = input_gate, j = new_input, f = forget_gate, o = output_gate\n cell_inputs = array_ops.concat([freq_inputs[fq], m_prev, m_prev_freq], 1)\n lstm_matrix = nn_ops.bias_add(math_ops.matmul(cell_inputs, concat_w), b)\n i, j, f, o = array_ops.split(\n value=lstm_matrix, num_or_size_splits=4, axis=1)\n\n if self._use_peepholes:\n c = (\n sigmoid(f + self._forget_bias + w_f_diag * c_prev) * c_prev +\n sigmoid(i + w_i_diag * c_prev) * tanh(j))\n else:\n c = (sigmoid(f + self._forget_bias) * c_prev + sigmoid(i) * tanh(j))\n\n if self._cell_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n c = clip_ops.clip_by_value(c, -self._cell_clip, self._cell_clip)\n # pylint: enable=invalid-unary-operand-type\n\n if self._use_peepholes:\n m = sigmoid(o + w_o_diag * c) * tanh(c)\n else:\n m = sigmoid(o) * tanh(c)\n m_prev_freq = m\n if fq == 0:\n state_out = array_ops.concat([c, m], 1)\n m_out = m\n else:\n state_out = array_ops.concat([state_out, c, m], 1)\n m_out = array_ops.concat([m_out, m], 1)\n return m_out, state_out\n\n def _make_tf_features(self, input_feat):\n \"\"\"Make the frequency features.\n\n Args:\n input_feat: input Tensor, 2D, batch x num_units.\n\n Returns:\n A list of frequency features, with each element containing:\n - A 2D, batch x output_dim, Tensor representing the time-frequency feature\n for that frequency index. Here output_dim is feature_size.\n Raises:\n ValueError: if input_size cannot be inferred from static shape inference.\n \"\"\"\n input_size = input_feat.get_shape().with_rank(2)[-1].value\n if input_size is None:\n raise ValueError(\"Cannot infer input_size from static shape inference.\")\n num_feats = int(\n (input_size - self._feature_size) / (self._frequency_skip)) + 1\n freq_inputs = []\n for f in range(num_feats):\n cur_input = array_ops.slice(input_feat, [0, f * self._frequency_skip],\n [-1, self._feature_size])\n freq_inputs.append(cur_input)\n return freq_inputs\n\n\nclass GridLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Grid Long short-term memory unit (LSTM) recurrent network cell.\n\n The default is based on:\n Nal Kalchbrenner, Ivo Danihelka and Alex Graves\n \"Grid Long Short-Term Memory,\" Proc. ICLR 2016.\n http://arxiv.org/abs/1507.01526\n\n When peephole connections are used, the implementation is based on:\n Tara N. Sainath and Bo Li\n \"Modeling Time-Frequency Patterns with LSTM vs. Convolutional Architectures\n for LVCSR Tasks.\" submitted to INTERSPEECH, 2016.\n\n The code uses optional peephole connections, shared_weights and cell clipping.\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n share_time_frequency_weights=False,\n cell_clip=None,\n initializer=None,\n num_unit_shards=1,\n forget_bias=1.0,\n feature_size=None,\n frequency_skip=None,\n num_frequency_blocks=None,\n start_freqindex_list=None,\n end_freqindex_list=None,\n couple_input_forget_gates=False,\n state_is_tuple=True,\n reuse=None):\n \"\"\"Initialize the parameters for an LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n use_peepholes: (optional) bool, default False. Set True to enable\n diagonal/peephole connections.\n share_time_frequency_weights: (optional) bool, default False. Set True to\n enable shared cell weights between time and frequency LSTMs.\n cell_clip: (optional) A float value, default None, if provided the cell\n state is clipped by this value prior to the cell output activation.\n initializer: (optional) The initializer to use for the weight and\n projection matrices, default None.\n num_unit_shards: (optional) int, default 1, How to split the weight\n matrix. If > 1, the weight matrix is stored across num_unit_shards.\n forget_bias: (optional) float, default 1.0, The initial bias of the\n forget gates, used to reduce the scale of forgetting at the beginning\n of the training.\n feature_size: (optional) int, default None, The size of the input feature\n the LSTM spans over.\n frequency_skip: (optional) int, default None, The amount the LSTM filter\n is shifted by in frequency.\n num_frequency_blocks: [required] A list of frequency blocks needed to\n cover the whole input feature splitting defined by start_freqindex_list\n and end_freqindex_list.\n start_freqindex_list: [optional], list of ints, default None, The\n starting frequency index for each frequency block.\n end_freqindex_list: [optional], list of ints, default None. The ending\n frequency index for each frequency block.\n couple_input_forget_gates: (optional) bool, default False, Whether to\n couple the input and forget gates, i.e. f_gate = 1.0 - i_gate, to reduce\n model parameters and computation cost.\n state_is_tuple: If True, accepted and returned states are 2-tuples of\n the `c_state` and `m_state`. By default (False), they are concatenated\n along the column axis. This default behavior will soon be deprecated.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n Raises:\n ValueError: if the num_frequency_blocks list is not specified\n \"\"\"\n super(GridLSTMCell, self).__init__(_reuse=reuse)\n if not state_is_tuple:\n logging.warn(\"%s: Using a concatenated state is slower and will soon be \"\n \"deprecated. Use state_is_tuple=True.\", self)\n self._num_units = num_units\n self._use_peepholes = use_peepholes\n self._share_time_frequency_weights = share_time_frequency_weights\n self._couple_input_forget_gates = couple_input_forget_gates\n self._state_is_tuple = state_is_tuple\n self._cell_clip = cell_clip\n self._initializer = initializer\n self._num_unit_shards = num_unit_shards\n self._forget_bias = forget_bias\n self._feature_size = feature_size\n self._frequency_skip = frequency_skip\n self._start_freqindex_list = start_freqindex_list\n self._end_freqindex_list = end_freqindex_list\n self._num_frequency_blocks = num_frequency_blocks\n self._total_blocks = 0\n self._reuse = reuse\n if self._num_frequency_blocks is None:\n raise ValueError(\"Must specify num_frequency_blocks\")\n\n for block_index in range(len(self._num_frequency_blocks)):\n self._total_blocks += int(self._num_frequency_blocks[block_index])\n if state_is_tuple:\n state_names = \"\"\n for block_index in range(len(self._num_frequency_blocks)):\n for freq_index in range(self._num_frequency_blocks[block_index]):\n name_prefix = \"state_f%02d_b%02d\" % (freq_index, block_index)\n state_names += (\"%s_c, %s_m,\" % (name_prefix, name_prefix))\n self._state_tuple_type = collections.namedtuple(\"GridLSTMStateTuple\",\n state_names.strip(\",\"))\n self._state_size = self._state_tuple_type(*(\n [num_units, num_units] * self._total_blocks))\n else:\n self._state_tuple_type = None\n self._state_size = num_units * self._total_blocks * 2\n self._output_size = num_units * self._total_blocks * 2\n\n @property\n def output_size(self):\n return self._output_size\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def state_tuple_type(self):\n return self._state_tuple_type\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, [batch, feature_size].\n state: Tensor or tuple of Tensors, 2D, [batch, state_size], depends on the\n flag self._state_is_tuple.\n\n Returns:\n A tuple containing:\n - A 2D, [batch, output_dim], Tensor representing the output of the LSTM\n after reading \"inputs\" when previous state was \"state\".\n Here output_dim is num_units.\n - A 2D, [batch, state_size], Tensor representing the new state of LSTM\n after reading \"inputs\" when previous state was \"state\".\n Raises:\n ValueError: if an input_size was specified and the provided inputs have\n a different dimension.\n \"\"\"\n batch_size = inputs.shape[0].value or array_ops.shape(inputs)[0]\n freq_inputs = self._make_tf_features(inputs)\n m_out_lst = []\n state_out_lst = []\n for block in range(len(freq_inputs)):\n m_out_lst_current, state_out_lst_current = self._compute(\n freq_inputs[block],\n block,\n state,\n batch_size,\n state_is_tuple=self._state_is_tuple)\n m_out_lst.extend(m_out_lst_current)\n state_out_lst.extend(state_out_lst_current)\n if self._state_is_tuple:\n state_out = self._state_tuple_type(*state_out_lst)\n else:\n state_out = array_ops.concat(state_out_lst, 1)\n m_out = array_ops.concat(m_out_lst, 1)\n return m_out, state_out\n\n def _compute(self,\n freq_inputs,\n block,\n state,\n batch_size,\n state_prefix=\"state\",\n state_is_tuple=True):\n \"\"\"Run the actual computation of one step LSTM.\n\n Args:\n freq_inputs: list of Tensors, 2D, [batch, feature_size].\n block: int, current frequency block index to process.\n state: Tensor or tuple of Tensors, 2D, [batch, state_size], it depends on\n the flag state_is_tuple.\n batch_size: int32, batch size.\n state_prefix: (optional) string, name prefix for states, defaults to\n \"state\".\n state_is_tuple: boolean, indicates whether the state is a tuple or Tensor.\n\n Returns:\n A tuple, containing:\n - A list of [batch, output_dim] Tensors, representing the output of the\n LSTM given the inputs and state.\n - A list of [batch, state_size] Tensors, representing the LSTM state\n values given the inputs and previous state.\n \"\"\"\n sigmoid = math_ops.sigmoid\n tanh = math_ops.tanh\n num_gates = 3 if self._couple_input_forget_gates else 4\n dtype = freq_inputs[0].dtype\n actual_input_size = freq_inputs[0].get_shape().as_list()[1]\n\n concat_w_f = _get_concat_variable(\n \"W_f_%d\" % block,\n [actual_input_size + 2 * self._num_units, num_gates * self._num_units],\n dtype, self._num_unit_shards)\n b_f = vs.get_variable(\n \"B_f_%d\" % block,\n shape=[num_gates * self._num_units],\n initializer=init_ops.zeros_initializer(),\n dtype=dtype)\n if not self._share_time_frequency_weights:\n concat_w_t = _get_concat_variable(\"W_t_%d\" % block, [\n actual_input_size + 2 * self._num_units, num_gates * self._num_units\n ], dtype, self._num_unit_shards)\n b_t = vs.get_variable(\n \"B_t_%d\" % block,\n shape=[num_gates * self._num_units],\n initializer=init_ops.zeros_initializer(),\n dtype=dtype)\n\n if self._use_peepholes:\n # Diagonal connections\n if not self._couple_input_forget_gates:\n w_f_diag_freqf = vs.get_variable(\n \"W_F_diag_freqf_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_f_diag_freqt = vs.get_variable(\n \"W_F_diag_freqt_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_i_diag_freqf = vs.get_variable(\n \"W_I_diag_freqf_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_i_diag_freqt = vs.get_variable(\n \"W_I_diag_freqt_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_o_diag_freqf = vs.get_variable(\n \"W_O_diag_freqf_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_o_diag_freqt = vs.get_variable(\n \"W_O_diag_freqt_%d\" % block, shape=[self._num_units], dtype=dtype)\n if not self._share_time_frequency_weights:\n if not self._couple_input_forget_gates:\n w_f_diag_timef = vs.get_variable(\n \"W_F_diag_timef_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_f_diag_timet = vs.get_variable(\n \"W_F_diag_timet_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_i_diag_timef = vs.get_variable(\n \"W_I_diag_timef_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_i_diag_timet = vs.get_variable(\n \"W_I_diag_timet_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_o_diag_timef = vs.get_variable(\n \"W_O_diag_timef_%d\" % block, shape=[self._num_units], dtype=dtype)\n w_o_diag_timet = vs.get_variable(\n \"W_O_diag_timet_%d\" % block, shape=[self._num_units], dtype=dtype)\n\n # initialize the first freq state to be zero\n m_prev_freq = array_ops.zeros([batch_size, self._num_units], dtype)\n c_prev_freq = array_ops.zeros([batch_size, self._num_units], dtype)\n for freq_index in range(len(freq_inputs)):\n if state_is_tuple:\n name_prefix = \"%s_f%02d_b%02d\" % (state_prefix, freq_index, block)\n c_prev_time = getattr(state, name_prefix + \"_c\")\n m_prev_time = getattr(state, name_prefix + \"_m\")\n else:\n c_prev_time = array_ops.slice(\n state, [0, 2 * freq_index * self._num_units], [-1, self._num_units])\n m_prev_time = array_ops.slice(\n state, [0, (2 * freq_index + 1) * self._num_units],\n [-1, self._num_units])\n\n # i = input_gate, j = new_input, f = forget_gate, o = output_gate\n cell_inputs = array_ops.concat(\n [freq_inputs[freq_index], m_prev_time, m_prev_freq], 1)\n\n # F-LSTM\n lstm_matrix_freq = nn_ops.bias_add(\n math_ops.matmul(cell_inputs, concat_w_f), b_f)\n if self._couple_input_forget_gates:\n i_freq, j_freq, o_freq = array_ops.split(\n value=lstm_matrix_freq, num_or_size_splits=num_gates, axis=1)\n f_freq = None\n else:\n i_freq, j_freq, f_freq, o_freq = array_ops.split(\n value=lstm_matrix_freq, num_or_size_splits=num_gates, axis=1)\n # T-LSTM\n if self._share_time_frequency_weights:\n i_time = i_freq\n j_time = j_freq\n f_time = f_freq\n o_time = o_freq\n else:\n lstm_matrix_time = nn_ops.bias_add(\n math_ops.matmul(cell_inputs, concat_w_t), b_t)\n if self._couple_input_forget_gates:\n i_time, j_time, o_time = array_ops.split(\n value=lstm_matrix_time, num_or_size_splits=num_gates, axis=1)\n f_time = None\n else:\n i_time, j_time, f_time, o_time = array_ops.split(\n value=lstm_matrix_time, num_or_size_splits=num_gates, axis=1)\n\n # F-LSTM c_freq\n # input gate activations\n if self._use_peepholes:\n i_freq_g = sigmoid(i_freq + w_i_diag_freqf * c_prev_freq +\n w_i_diag_freqt * c_prev_time)\n else:\n i_freq_g = sigmoid(i_freq)\n # forget gate activations\n if self._couple_input_forget_gates:\n f_freq_g = 1.0 - i_freq_g\n else:\n if self._use_peepholes:\n f_freq_g = sigmoid(f_freq + self._forget_bias + w_f_diag_freqf *\n c_prev_freq + w_f_diag_freqt * c_prev_time)\n else:\n f_freq_g = sigmoid(f_freq + self._forget_bias)\n # cell state\n c_freq = f_freq_g * c_prev_freq + i_freq_g * tanh(j_freq)\n if self._cell_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n c_freq = clip_ops.clip_by_value(c_freq, -self._cell_clip,\n self._cell_clip)\n # pylint: enable=invalid-unary-operand-type\n\n # T-LSTM c_freq\n # input gate activations\n if self._use_peepholes:\n if self._share_time_frequency_weights:\n i_time_g = sigmoid(i_time + w_i_diag_freqf * c_prev_freq +\n w_i_diag_freqt * c_prev_time)\n else:\n i_time_g = sigmoid(i_time + w_i_diag_timef * c_prev_freq +\n w_i_diag_timet * c_prev_time)\n else:\n i_time_g = sigmoid(i_time)\n # forget gate activations\n if self._couple_input_forget_gates:\n f_time_g = 1.0 - i_time_g\n else:\n if self._use_peepholes:\n if self._share_time_frequency_weights:\n f_time_g = sigmoid(f_time + self._forget_bias + w_f_diag_freqf *\n c_prev_freq + w_f_diag_freqt * c_prev_time)\n else:\n f_time_g = sigmoid(f_time + self._forget_bias + w_f_diag_timef *\n c_prev_freq + w_f_diag_timet * c_prev_time)\n else:\n f_time_g = sigmoid(f_time + self._forget_bias)\n # cell state\n c_time = f_time_g * c_prev_time + i_time_g * tanh(j_time)\n if self._cell_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n c_time = clip_ops.clip_by_value(c_time, -self._cell_clip,\n self._cell_clip)\n # pylint: enable=invalid-unary-operand-type\n\n # F-LSTM m_freq\n if self._use_peepholes:\n m_freq = sigmoid(o_freq + w_o_diag_freqf * c_freq +\n w_o_diag_freqt * c_time) * tanh(c_freq)\n else:\n m_freq = sigmoid(o_freq) * tanh(c_freq)\n\n # T-LSTM m_time\n if self._use_peepholes:\n if self._share_time_frequency_weights:\n m_time = sigmoid(o_time + w_o_diag_freqf * c_freq +\n w_o_diag_freqt * c_time) * tanh(c_time)\n else:\n m_time = sigmoid(o_time + w_o_diag_timef * c_freq +\n w_o_diag_timet * c_time) * tanh(c_time)\n else:\n m_time = sigmoid(o_time) * tanh(c_time)\n\n m_prev_freq = m_freq\n c_prev_freq = c_freq\n # Concatenate the outputs for T-LSTM and F-LSTM for each shift\n if freq_index == 0:\n state_out_lst = [c_time, m_time]\n m_out_lst = [m_time, m_freq]\n else:\n state_out_lst.extend([c_time, m_time])\n m_out_lst.extend([m_time, m_freq])\n\n return m_out_lst, state_out_lst\n\n def _make_tf_features(self, input_feat, slice_offset=0):\n \"\"\"Make the frequency features.\n\n Args:\n input_feat: input Tensor, 2D, [batch, num_units].\n slice_offset: (optional) Python int, default 0, the slicing offset is only\n used for the backward processing in the BidirectionalGridLSTMCell. It\n specifies a different starting point instead of always 0 to enable the\n forward and backward processing look at different frequency blocks.\n\n Returns:\n A list of frequency features, with each element containing:\n - A 2D, [batch, output_dim], Tensor representing the time-frequency\n feature for that frequency index. Here output_dim is feature_size.\n Raises:\n ValueError: if input_size cannot be inferred from static shape inference.\n \"\"\"\n input_size = input_feat.get_shape().with_rank(2)[-1].value\n if input_size is None:\n raise ValueError(\"Cannot infer input_size from static shape inference.\")\n if slice_offset > 0:\n # Padding to the end\n inputs = array_ops.pad(input_feat,\n array_ops.constant(\n [0, 0, 0, slice_offset],\n shape=[2, 2],\n dtype=dtypes.int32), \"CONSTANT\")\n elif slice_offset < 0:\n # Padding to the front\n inputs = array_ops.pad(input_feat,\n array_ops.constant(\n [0, 0, -slice_offset, 0],\n shape=[2, 2],\n dtype=dtypes.int32), \"CONSTANT\")\n slice_offset = 0\n else:\n inputs = input_feat\n freq_inputs = []\n if not self._start_freqindex_list:\n if len(self._num_frequency_blocks) != 1:\n raise ValueError(\"Length of num_frequency_blocks\"\n \" is not 1, but instead is %d\",\n len(self._num_frequency_blocks))\n num_feats = int(\n (input_size - self._feature_size) / (self._frequency_skip)) + 1\n if num_feats != self._num_frequency_blocks[0]:\n raise ValueError(\n \"Invalid num_frequency_blocks, requires %d but gets %d, please\"\n \" check the input size and filter config are correct.\" %\n (self._num_frequency_blocks[0], num_feats))\n block_inputs = []\n for f in range(num_feats):\n cur_input = array_ops.slice(\n inputs, [0, slice_offset + f * self._frequency_skip],\n [-1, self._feature_size])\n block_inputs.append(cur_input)\n freq_inputs.append(block_inputs)\n else:\n if len(self._start_freqindex_list) != len(self._end_freqindex_list):\n raise ValueError(\"Length of start and end freqindex_list\"\n \" does not match %d %d\",\n len(self._start_freqindex_list),\n len(self._end_freqindex_list))\n if len(self._num_frequency_blocks) != len(self._start_freqindex_list):\n raise ValueError(\"Length of num_frequency_blocks\"\n \" is not equal to start_freqindex_list %d %d\",\n len(self._num_frequency_blocks),\n len(self._start_freqindex_list))\n for b in range(len(self._start_freqindex_list)):\n start_index = self._start_freqindex_list[b]\n end_index = self._end_freqindex_list[b]\n cur_size = end_index - start_index\n block_feats = int(\n (cur_size - self._feature_size) / (self._frequency_skip)) + 1\n if block_feats != self._num_frequency_blocks[b]:\n raise ValueError(\n \"Invalid num_frequency_blocks, requires %d but gets %d, please\"\n \" check the input size and filter config are correct.\" %\n (self._num_frequency_blocks[b], block_feats))\n block_inputs = []\n for f in range(block_feats):\n cur_input = array_ops.slice(\n inputs,\n [0, start_index + slice_offset + f * self._frequency_skip],\n [-1, self._feature_size])\n block_inputs.append(cur_input)\n freq_inputs.append(block_inputs)\n return freq_inputs\n\n\nclass BidirectionalGridLSTMCell(GridLSTMCell):\n \"\"\"Bidirectional GridLstm cell.\n\n The bidirection connection is only used in the frequency direction, which\n hence doesn't affect the time direction's real-time processing that is\n required for online recognition systems.\n The current implementation uses different weights for the two directions.\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n share_time_frequency_weights=False,\n cell_clip=None,\n initializer=None,\n num_unit_shards=1,\n forget_bias=1.0,\n feature_size=None,\n frequency_skip=None,\n num_frequency_blocks=None,\n start_freqindex_list=None,\n end_freqindex_list=None,\n couple_input_forget_gates=False,\n backward_slice_offset=0,\n reuse=None):\n \"\"\"Initialize the parameters for an LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n use_peepholes: (optional) bool, default False. Set True to enable\n diagonal/peephole connections.\n share_time_frequency_weights: (optional) bool, default False. Set True to\n enable shared cell weights between time and frequency LSTMs.\n cell_clip: (optional) A float value, default None, if provided the cell\n state is clipped by this value prior to the cell output activation.\n initializer: (optional) The initializer to use for the weight and\n projection matrices, default None.\n num_unit_shards: (optional) int, default 1, How to split the weight\n matrix. If > 1, the weight matrix is stored across num_unit_shards.\n forget_bias: (optional) float, default 1.0, The initial bias of the\n forget gates, used to reduce the scale of forgetting at the beginning\n of the training.\n feature_size: (optional) int, default None, The size of the input feature\n the LSTM spans over.\n frequency_skip: (optional) int, default None, The amount the LSTM filter\n is shifted by in frequency.\n num_frequency_blocks: [required] A list of frequency blocks needed to\n cover the whole input feature splitting defined by start_freqindex_list\n and end_freqindex_list.\n start_freqindex_list: [optional], list of ints, default None, The\n starting frequency index for each frequency block.\n end_freqindex_list: [optional], list of ints, default None. The ending\n frequency index for each frequency block.\n couple_input_forget_gates: (optional) bool, default False, Whether to\n couple the input and forget gates, i.e. f_gate = 1.0 - i_gate, to reduce\n model parameters and computation cost.\n backward_slice_offset: (optional) int32, default 0, the starting offset to\n slice the feature for backward processing.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(BidirectionalGridLSTMCell, self).__init__(\n num_units, use_peepholes, share_time_frequency_weights, cell_clip,\n initializer, num_unit_shards, forget_bias, feature_size, frequency_skip,\n num_frequency_blocks, start_freqindex_list, end_freqindex_list,\n couple_input_forget_gates, True, reuse)\n self._backward_slice_offset = int(backward_slice_offset)\n state_names = \"\"\n for direction in [\"fwd\", \"bwd\"]:\n for block_index in range(len(self._num_frequency_blocks)):\n for freq_index in range(self._num_frequency_blocks[block_index]):\n name_prefix = \"%s_state_f%02d_b%02d\" % (direction, freq_index,\n block_index)\n state_names += (\"%s_c, %s_m,\" % (name_prefix, name_prefix))\n self._state_tuple_type = collections.namedtuple(\n \"BidirectionalGridLSTMStateTuple\", state_names.strip(\",\"))\n self._state_size = self._state_tuple_type(*(\n [num_units, num_units] * self._total_blocks * 2))\n self._output_size = 2 * num_units * self._total_blocks * 2\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, [batch, num_units].\n state: tuple of Tensors, 2D, [batch, state_size].\n\n Returns:\n A tuple containing:\n - A 2D, [batch, output_dim], Tensor representing the output of the LSTM\n after reading \"inputs\" when previous state was \"state\".\n Here output_dim is num_units.\n - A 2D, [batch, state_size], Tensor representing the new state of LSTM\n after reading \"inputs\" when previous state was \"state\".\n Raises:\n ValueError: if an input_size was specified and the provided inputs have\n a different dimension.\n \"\"\"\n batch_size = inputs.shape[0].value or array_ops.shape(inputs)[0]\n fwd_inputs = self._make_tf_features(inputs)\n if self._backward_slice_offset:\n bwd_inputs = self._make_tf_features(inputs, self._backward_slice_offset)\n else:\n bwd_inputs = fwd_inputs\n\n # Forward processing\n with vs.variable_scope(\"fwd\"):\n fwd_m_out_lst = []\n fwd_state_out_lst = []\n for block in range(len(fwd_inputs)):\n fwd_m_out_lst_current, fwd_state_out_lst_current = self._compute(\n fwd_inputs[block],\n block,\n state,\n batch_size,\n state_prefix=\"fwd_state\",\n state_is_tuple=True)\n fwd_m_out_lst.extend(fwd_m_out_lst_current)\n fwd_state_out_lst.extend(fwd_state_out_lst_current)\n # Backward processing\n bwd_m_out_lst = []\n bwd_state_out_lst = []\n with vs.variable_scope(\"bwd\"):\n for block in range(len(bwd_inputs)):\n # Reverse the blocks\n bwd_inputs_reverse = bwd_inputs[block][::-1]\n bwd_m_out_lst_current, bwd_state_out_lst_current = self._compute(\n bwd_inputs_reverse,\n block,\n state,\n batch_size,\n state_prefix=\"bwd_state\",\n state_is_tuple=True)\n bwd_m_out_lst.extend(bwd_m_out_lst_current)\n bwd_state_out_lst.extend(bwd_state_out_lst_current)\n state_out = self._state_tuple_type(*(fwd_state_out_lst + bwd_state_out_lst))\n # Outputs are always concated as it is never used separately.\n m_out = array_ops.concat(fwd_m_out_lst + bwd_m_out_lst, 1)\n return m_out, state_out\n\n\n# pylint: disable=protected-access\n_Linear = core_rnn_cell._Linear # pylint: disable=invalid-name\n\n# pylint: enable=protected-access\n\n\nclass AttentionCellWrapper(rnn_cell_impl.RNNCell):\n \"\"\"Basic attention cell wrapper.\n\n Implementation based on https://arxiv.org/abs/1409.0473.\n \"\"\"\n\n def __init__(self,\n cell,\n attn_length,\n attn_size=None,\n attn_vec_size=None,\n input_size=None,\n state_is_tuple=True,\n reuse=None):\n \"\"\"Create a cell with attention.\n\n Args:\n cell: an RNNCell, an attention is added to it.\n attn_length: integer, the size of an attention window.\n attn_size: integer, the size of an attention vector. Equal to\n cell.output_size by default.\n attn_vec_size: integer, the number of convolutional features calculated\n on attention state and a size of the hidden layer built from\n base cell state. Equal attn_size to by default.\n input_size: integer, the size of a hidden linear layer,\n built from inputs and attention. Derived from the input tensor\n by default.\n state_is_tuple: If True, accepted and returned states are n-tuples, where\n `n = len(cells)`. By default (False), the states are all\n concatenated along the column axis.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n\n Raises:\n TypeError: if cell is not an RNNCell.\n ValueError: if cell returns a state tuple but the flag\n `state_is_tuple` is `False` or if attn_length is zero or less.\n \"\"\"\n super(AttentionCellWrapper, self).__init__(_reuse=reuse)\n rnn_cell_impl.assert_like_rnncell(\"cell\", cell)\n if nest.is_sequence(cell.state_size) and not state_is_tuple:\n raise ValueError(\n \"Cell returns tuple of states, but the flag \"\n \"state_is_tuple is not set. State size is: %s\" % str(cell.state_size))\n if attn_length <= 0:\n raise ValueError(\n \"attn_length should be greater than zero, got %s\" % str(attn_length))\n if not state_is_tuple:\n logging.warn(\"%s: Using a concatenated state is slower and will soon be \"\n \"deprecated. Use state_is_tuple=True.\", self)\n if attn_size is None:\n attn_size = cell.output_size\n if attn_vec_size is None:\n attn_vec_size = attn_size\n self._state_is_tuple = state_is_tuple\n self._cell = cell\n self._attn_vec_size = attn_vec_size\n self._input_size = input_size\n self._attn_size = attn_size\n self._attn_length = attn_length\n self._reuse = reuse\n self._linear1 = None\n self._linear2 = None\n self._linear3 = None\n\n @property\n def state_size(self):\n size = (self._cell.state_size, self._attn_size,\n self._attn_size * self._attn_length)\n if self._state_is_tuple:\n return size\n else:\n return sum(list(size))\n\n @property\n def output_size(self):\n return self._attn_size\n\n def call(self, inputs, state):\n \"\"\"Long short-term memory cell with attention (LSTMA).\"\"\"\n if self._state_is_tuple:\n state, attns, attn_states = state\n else:\n states = state\n state = array_ops.slice(states, [0, 0], [-1, self._cell.state_size])\n attns = array_ops.slice(states, [0, self._cell.state_size],\n [-1, self._attn_size])\n attn_states = array_ops.slice(\n states, [0, self._cell.state_size + self._attn_size],\n [-1, self._attn_size * self._attn_length])\n attn_states = array_ops.reshape(attn_states,\n [-1, self._attn_length, self._attn_size])\n input_size = self._input_size\n if input_size is None:\n input_size = inputs.get_shape().as_list()[1]\n if self._linear1 is None:\n self._linear1 = _Linear([inputs, attns], input_size, True)\n inputs = self._linear1([inputs, attns])\n cell_output, new_state = self._cell(inputs, state)\n if self._state_is_tuple:\n new_state_cat = array_ops.concat(nest.flatten(new_state), 1)\n else:\n new_state_cat = new_state\n new_attns, new_attn_states = self._attention(new_state_cat, attn_states)\n with vs.variable_scope(\"attn_output_projection\"):\n if self._linear2 is None:\n self._linear2 = _Linear([cell_output, new_attns], self._attn_size, True)\n output = self._linear2([cell_output, new_attns])\n new_attn_states = array_ops.concat(\n [new_attn_states, array_ops.expand_dims(output, 1)], 1)\n new_attn_states = array_ops.reshape(\n new_attn_states, [-1, self._attn_length * self._attn_size])\n new_state = (new_state, new_attns, new_attn_states)\n if not self._state_is_tuple:\n new_state = array_ops.concat(list(new_state), 1)\n return output, new_state\n\n def _attention(self, query, attn_states):\n conv2d = nn_ops.conv2d\n reduce_sum = math_ops.reduce_sum\n softmax = nn_ops.softmax\n tanh = math_ops.tanh\n\n with vs.variable_scope(\"attention\"):\n k = vs.get_variable(\"attn_w\",\n [1, 1, self._attn_size, self._attn_vec_size])\n v = vs.get_variable(\"attn_v\", [self._attn_vec_size])\n hidden = array_ops.reshape(attn_states,\n [-1, self._attn_length, 1, self._attn_size])\n hidden_features = conv2d(hidden, k, [1, 1, 1, 1], \"SAME\")\n if self._linear3 is None:\n self._linear3 = _Linear(query, self._attn_vec_size, True)\n y = self._linear3(query)\n y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size])\n s = reduce_sum(v * tanh(hidden_features + y), [2, 3])\n a = softmax(s)\n d = reduce_sum(\n array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2])\n new_attns = array_ops.reshape(d, [-1, self._attn_size])\n new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1])\n return new_attns, new_attn_states\n\n\nclass HighwayWrapper(rnn_cell_impl.RNNCell):\n \"\"\"RNNCell wrapper that adds highway connection on cell input and output.\n\n Based on:\n R. K. Srivastava, K. Greff, and J. Schmidhuber, \"Highway networks\",\n arXiv preprint arXiv:1505.00387, 2015.\n https://arxiv.org/abs/1505.00387\n \"\"\"\n\n def __init__(self,\n cell,\n couple_carry_transform_gates=True,\n carry_bias_init=1.0):\n \"\"\"Constructs a `HighwayWrapper` for `cell`.\n\n Args:\n cell: An instance of `RNNCell`.\n couple_carry_transform_gates: boolean, should the Carry and Transform gate\n be coupled.\n carry_bias_init: float, carry gates bias initialization.\n \"\"\"\n self._cell = cell\n self._couple_carry_transform_gates = couple_carry_transform_gates\n self._carry_bias_init = carry_bias_init\n\n @property\n def state_size(self):\n return self._cell.state_size\n\n @property\n def output_size(self):\n return self._cell.output_size\n\n def zero_state(self, batch_size, dtype):\n with ops.name_scope(type(self).__name__ + \"ZeroState\", values=[batch_size]):\n return self._cell.zero_state(batch_size, dtype)\n\n def _highway(self, inp, out):\n input_size = inp.get_shape().with_rank(2)[1].value\n carry_weight = vs.get_variable(\"carry_w\", [input_size, input_size])\n carry_bias = vs.get_variable(\n \"carry_b\", [input_size],\n initializer=init_ops.constant_initializer(self._carry_bias_init))\n carry = math_ops.sigmoid(nn_ops.xw_plus_b(inp, carry_weight, carry_bias))\n if self._couple_carry_transform_gates:\n transform = 1 - carry\n else:\n transform_weight = vs.get_variable(\"transform_w\",\n [input_size, input_size])\n transform_bias = vs.get_variable(\n \"transform_b\", [input_size],\n initializer=init_ops.constant_initializer(-self._carry_bias_init))\n transform = math_ops.sigmoid(\n nn_ops.xw_plus_b(inp, transform_weight, transform_bias))\n return inp * carry + out * transform\n\n def __call__(self, inputs, state, scope=None):\n \"\"\"Run the cell and add its inputs to its outputs.\n\n Args:\n inputs: cell inputs.\n state: cell state.\n scope: optional cell scope.\n\n Returns:\n Tuple of cell outputs and new state.\n\n Raises:\n TypeError: If cell inputs and outputs have different structure (type).\n ValueError: If cell inputs and outputs have different structure (value).\n \"\"\"\n outputs, new_state = self._cell(inputs, state, scope=scope)\n nest.assert_same_structure(inputs, outputs)\n\n # Ensure shapes match\n def assert_shape_match(inp, out):\n inp.get_shape().assert_is_compatible_with(out.get_shape())\n\n nest.map_structure(assert_shape_match, inputs, outputs)\n res_outputs = nest.map_structure(self._highway, inputs, outputs)\n return (res_outputs, new_state)\n\n\nclass LayerNormBasicLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"LSTM unit with layer normalization and recurrent dropout.\n\n This class adds layer normalization and recurrent dropout to a\n basic LSTM unit. Layer normalization implementation is based on:\n\n https://arxiv.org/abs/1607.06450.\n\n \"Layer Normalization\"\n Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton\n\n and is applied before the internal nonlinearities.\n Recurrent dropout is base on:\n\n https://arxiv.org/abs/1603.05118\n\n \"Recurrent Dropout without Memory Loss\"\n Stanislau Semeniuta, Aliaksei Severyn, Erhardt Barth.\n \"\"\"\n\n def __init__(self,\n num_units,\n forget_bias=1.0,\n input_size=None,\n activation=math_ops.tanh,\n layer_norm=True,\n norm_gain=1.0,\n norm_shift=0.0,\n dropout_keep_prob=1.0,\n dropout_prob_seed=None,\n reuse=None):\n \"\"\"Initializes the basic LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell.\n forget_bias: float, The bias added to forget gates (see above).\n input_size: Deprecated and unused.\n activation: Activation function of the inner states.\n layer_norm: If `True`, layer normalization will be applied.\n norm_gain: float, The layer normalization gain initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n norm_shift: float, The layer normalization shift initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n dropout_keep_prob: unit Tensor or float between 0 and 1 representing the\n recurrent dropout probability value. If float and 1.0, no dropout will\n be applied.\n dropout_prob_seed: (optional) integer, the randomness seed.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(LayerNormBasicLSTMCell, self).__init__(_reuse=reuse)\n\n if input_size is not None:\n logging.warn(\"%s: The input_size parameter is deprecated.\", self)\n\n self._num_units = num_units\n self._activation = activation\n self._forget_bias = forget_bias\n self._keep_prob = dropout_keep_prob\n self._seed = dropout_prob_seed\n self._layer_norm = layer_norm\n self._norm_gain = norm_gain\n self._norm_shift = norm_shift\n self._reuse = reuse\n\n @property\n def state_size(self):\n return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)\n\n @property\n def output_size(self):\n return self._num_units\n\n def _norm(self, inp, scope, dtype=dtypes.float32):\n shape = inp.get_shape()[-1:]\n gamma_init = init_ops.constant_initializer(self._norm_gain)\n beta_init = init_ops.constant_initializer(self._norm_shift)\n with vs.variable_scope(scope):\n # Initialize beta and gamma for use by layer_norm.\n vs.get_variable(\"gamma\", shape=shape, initializer=gamma_init, dtype=dtype)\n vs.get_variable(\"beta\", shape=shape, initializer=beta_init, dtype=dtype)\n normalized = layers.layer_norm(inp, reuse=True, scope=scope)\n return normalized\n\n def _linear(self, args):\n out_size = 4 * self._num_units\n proj_size = args.get_shape()[-1]\n dtype = args.dtype\n weights = vs.get_variable(\"kernel\", [proj_size, out_size], dtype=dtype)\n out = math_ops.matmul(args, weights)\n if not self._layer_norm:\n bias = vs.get_variable(\"bias\", [out_size], dtype=dtype)\n out = nn_ops.bias_add(out, bias)\n return out\n\n def call(self, inputs, state):\n \"\"\"LSTM cell with layer normalization and recurrent dropout.\"\"\"\n c, h = state\n args = array_ops.concat([inputs, h], 1)\n concat = self._linear(args)\n dtype = args.dtype\n\n i, j, f, o = array_ops.split(value=concat, num_or_size_splits=4, axis=1)\n if self._layer_norm:\n i = self._norm(i, \"input\", dtype=dtype)\n j = self._norm(j, \"transform\", dtype=dtype)\n f = self._norm(f, \"forget\", dtype=dtype)\n o = self._norm(o, \"output\", dtype=dtype)\n\n g = self._activation(j)\n if (not isinstance(self._keep_prob, float)) or self._keep_prob < 1:\n g = nn_ops.dropout(g, self._keep_prob, seed=self._seed)\n\n new_c = (\n c * math_ops.sigmoid(f + self._forget_bias) + math_ops.sigmoid(i) * g)\n if self._layer_norm:\n new_c = self._norm(new_c, \"state\", dtype=dtype)\n new_h = self._activation(new_c) * math_ops.sigmoid(o)\n\n new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h)\n return new_h, new_state\n\n\nclass NASCell(rnn_cell_impl.RNNCell):\n \"\"\"Neural Architecture Search (NAS) recurrent network cell.\n\n This implements the recurrent cell from the paper:\n\n https://arxiv.org/abs/1611.01578\n\n Barret Zoph and Quoc V. Le.\n \"Neural Architecture Search with Reinforcement Learning\" Proc. ICLR 2017.\n\n The class uses an optional projection layer.\n \"\"\"\n\n def __init__(self, num_units, num_proj=None, use_biases=False, reuse=None):\n \"\"\"Initialize the parameters for a NAS cell.\n\n Args:\n num_units: int, The number of units in the NAS cell\n num_proj: (optional) int, The output dimensionality for the projection\n matrices. If None, no projection is performed.\n use_biases: (optional) bool, If True then use biases within the cell. This\n is False by default.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(NASCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._num_proj = num_proj\n self._use_biases = use_biases\n self._reuse = reuse\n\n if num_proj is not None:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj)\n self._output_size = num_proj\n else:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units)\n self._output_size = num_units\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def output_size(self):\n return self._output_size\n\n def call(self, inputs, state):\n \"\"\"Run one step of NAS Cell.\n\n Args:\n inputs: input Tensor, 2D, batch x num_units.\n state: This must be a tuple of state Tensors, both `2-D`, with column\n sizes `c_state` and `m_state`.\n\n Returns:\n A tuple containing:\n - A `2-D, [batch x output_dim]`, Tensor representing the output of the\n NAS Cell after reading `inputs` when previous state was `state`.\n Here output_dim is:\n num_proj if num_proj was set,\n num_units otherwise.\n - Tensor(s) representing the new state of NAS Cell after reading `inputs`\n when the previous state was `state`. Same type and shape(s) as `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference.\n \"\"\"\n sigmoid = math_ops.sigmoid\n tanh = math_ops.tanh\n relu = nn_ops.relu\n\n num_proj = self._num_units if self._num_proj is None else self._num_proj\n\n (c_prev, m_prev) = state\n\n dtype = inputs.dtype\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n # Variables for the NAS cell. W_m is all matrices multiplying the\n # hiddenstate and W_inputs is all matrices multiplying the inputs.\n concat_w_m = vs.get_variable(\"recurrent_kernel\",\n [num_proj, 8 * self._num_units], dtype)\n concat_w_inputs = vs.get_variable(\n \"kernel\", [input_size.value, 8 * self._num_units], dtype)\n\n m_matrix = math_ops.matmul(m_prev, concat_w_m)\n inputs_matrix = math_ops.matmul(inputs, concat_w_inputs)\n\n if self._use_biases:\n b = vs.get_variable(\n \"bias\",\n shape=[8 * self._num_units],\n initializer=init_ops.zeros_initializer(),\n dtype=dtype)\n m_matrix = nn_ops.bias_add(m_matrix, b)\n\n # The NAS cell branches into 8 different splits for both the hiddenstate\n # and the input\n m_matrix_splits = array_ops.split(\n axis=1, num_or_size_splits=8, value=m_matrix)\n inputs_matrix_splits = array_ops.split(\n axis=1, num_or_size_splits=8, value=inputs_matrix)\n\n # First layer\n layer1_0 = sigmoid(inputs_matrix_splits[0] + m_matrix_splits[0])\n layer1_1 = relu(inputs_matrix_splits[1] + m_matrix_splits[1])\n layer1_2 = sigmoid(inputs_matrix_splits[2] + m_matrix_splits[2])\n layer1_3 = relu(inputs_matrix_splits[3] * m_matrix_splits[3])\n layer1_4 = tanh(inputs_matrix_splits[4] + m_matrix_splits[4])\n layer1_5 = sigmoid(inputs_matrix_splits[5] + m_matrix_splits[5])\n layer1_6 = tanh(inputs_matrix_splits[6] + m_matrix_splits[6])\n layer1_7 = sigmoid(inputs_matrix_splits[7] + m_matrix_splits[7])\n\n # Second layer\n l2_0 = tanh(layer1_0 * layer1_1)\n l2_1 = tanh(layer1_2 + layer1_3)\n l2_2 = tanh(layer1_4 * layer1_5)\n l2_3 = sigmoid(layer1_6 + layer1_7)\n\n # Inject the cell\n l2_0 = tanh(l2_0 + c_prev)\n\n # Third layer\n l3_0_pre = l2_0 * l2_1\n new_c = l3_0_pre # create new cell\n l3_0 = l3_0_pre\n l3_1 = tanh(l2_2 + l2_3)\n\n # Final layer\n new_m = tanh(l3_0 * l3_1)\n\n # Projection layer if specified\n if self._num_proj is not None:\n concat_w_proj = vs.get_variable(\"projection_weights\",\n [self._num_units, self._num_proj], dtype)\n new_m = math_ops.matmul(new_m, concat_w_proj)\n\n new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_m)\n return new_m, new_state\n\n\nclass UGRNNCell(rnn_cell_impl.RNNCell):\n \"\"\"Update Gate Recurrent Neural Network (UGRNN) cell.\n\n Compromise between a LSTM/GRU and a vanilla RNN. There is only one\n gate, and that is to determine whether the unit should be\n integrating or computing instantaneously. This is the recurrent\n idea of the feedforward Highway Network.\n\n This implements the recurrent cell from the paper:\n\n https://arxiv.org/abs/1611.09913\n\n Jasmine Collins, Jascha Sohl-Dickstein, and David Sussillo.\n \"Capacity and Trainability in Recurrent Neural Networks\" Proc. ICLR 2017.\n \"\"\"\n\n def __init__(self,\n num_units,\n initializer=None,\n forget_bias=1.0,\n activation=math_ops.tanh,\n reuse=None):\n \"\"\"Initialize the parameters for an UGRNN cell.\n\n Args:\n num_units: int, The number of units in the UGRNN cell\n initializer: (optional) The initializer to use for the weight matrices.\n forget_bias: (optional) float, default 1.0, The initial bias of the\n forget gate, used to reduce the scale of forgetting at the beginning\n of the training.\n activation: (optional) Activation function of the inner states.\n Default is `tf.tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(UGRNNCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._initializer = initializer\n self._forget_bias = forget_bias\n self._activation = activation\n self._reuse = reuse\n self._linear = None\n\n @property\n def state_size(self):\n return self._num_units\n\n @property\n def output_size(self):\n return self._num_units\n\n def call(self, inputs, state):\n \"\"\"Run one step of UGRNN.\n\n Args:\n inputs: input Tensor, 2D, batch x input size.\n state: state Tensor, 2D, batch x num units.\n\n Returns:\n new_output: batch x num units, Tensor representing the output of the UGRNN\n after reading `inputs` when previous state was `state`. Identical to\n `new_state`.\n new_state: batch x num units, Tensor representing the state of the UGRNN\n after reading `inputs` when previous state was `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference.\n \"\"\"\n sigmoid = math_ops.sigmoid\n\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n\n with vs.variable_scope(\n vs.get_variable_scope(), initializer=self._initializer):\n cell_inputs = array_ops.concat([inputs, state], 1)\n if self._linear is None:\n self._linear = _Linear(cell_inputs, 2 * self._num_units, True)\n rnn_matrix = self._linear(cell_inputs)\n\n [g_act, c_act] = array_ops.split(\n axis=1, num_or_size_splits=2, value=rnn_matrix)\n\n c = self._activation(c_act)\n g = sigmoid(g_act + self._forget_bias)\n new_state = g * state + (1.0 - g) * c\n new_output = new_state\n\n return new_output, new_state\n\n\nclass IntersectionRNNCell(rnn_cell_impl.RNNCell):\n \"\"\"Intersection Recurrent Neural Network (+RNN) cell.\n\n Architecture with coupled recurrent gate as well as coupled depth\n gate, designed to improve information flow through stacked RNNs. As the\n architecture uses depth gating, the dimensionality of the depth\n output (y) also should not change through depth (input size == output size).\n To achieve this, the first layer of a stacked Intersection RNN projects\n the inputs to N (num units) dimensions. Therefore when initializing an\n IntersectionRNNCell, one should set `num_in_proj = N` for the first layer\n and use default settings for subsequent layers.\n\n This implements the recurrent cell from the paper:\n\n https://arxiv.org/abs/1611.09913\n\n Jasmine Collins, Jascha Sohl-Dickstein, and David Sussillo.\n \"Capacity and Trainability in Recurrent Neural Networks\" Proc. ICLR 2017.\n\n The Intersection RNN is built for use in deeply stacked\n RNNs so it may not achieve best performance with depth 1.\n \"\"\"\n\n def __init__(self,\n num_units,\n num_in_proj=None,\n initializer=None,\n forget_bias=1.0,\n y_activation=nn_ops.relu,\n reuse=None):\n \"\"\"Initialize the parameters for an +RNN cell.\n\n Args:\n num_units: int, The number of units in the +RNN cell\n num_in_proj: (optional) int, The input dimensionality for the RNN.\n If creating the first layer of an +RNN, this should be set to\n `num_units`. Otherwise, this should be set to `None` (default).\n If `None`, dimensionality of `inputs` should be equal to `num_units`,\n otherwise ValueError is thrown.\n initializer: (optional) The initializer to use for the weight matrices.\n forget_bias: (optional) float, default 1.0, The initial bias of the\n forget gates, used to reduce the scale of forgetting at the beginning\n of the training.\n y_activation: (optional) Activation function of the states passed\n through depth. Default is 'tf.nn.relu`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(IntersectionRNNCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._initializer = initializer\n self._forget_bias = forget_bias\n self._num_input_proj = num_in_proj\n self._y_activation = y_activation\n self._reuse = reuse\n self._linear1 = None\n self._linear2 = None\n\n @property\n def state_size(self):\n return self._num_units\n\n @property\n def output_size(self):\n return self._num_units\n\n def call(self, inputs, state):\n \"\"\"Run one step of the Intersection RNN.\n\n Args:\n inputs: input Tensor, 2D, batch x input size.\n state: state Tensor, 2D, batch x num units.\n\n Returns:\n new_y: batch x num units, Tensor representing the output of the +RNN\n after reading `inputs` when previous state was `state`.\n new_state: batch x num units, Tensor representing the state of the +RNN\n after reading `inputs` when previous state was `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from `inputs` via\n static shape inference.\n ValueError: If input size != output size (these must be equal when\n using the Intersection RNN).\n \"\"\"\n sigmoid = math_ops.sigmoid\n tanh = math_ops.tanh\n\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n\n with vs.variable_scope(\n vs.get_variable_scope(), initializer=self._initializer):\n # read-in projections (should be used for first layer in deep +RNN\n # to transform size of inputs from I --> N)\n if input_size.value != self._num_units:\n if self._num_input_proj:\n with vs.variable_scope(\"in_projection\"):\n if self._linear1 is None:\n self._linear1 = _Linear(inputs, self._num_units, True)\n inputs = self._linear1(inputs)\n else:\n raise ValueError(\"Must have input size == output size for \"\n \"Intersection RNN. To fix, num_in_proj should \"\n \"be set to num_units at cell init.\")\n\n n_dim = i_dim = self._num_units\n cell_inputs = array_ops.concat([inputs, state], 1)\n if self._linear2 is None:\n self._linear2 = _Linear(cell_inputs, 2 * n_dim + 2 * i_dim, True)\n rnn_matrix = self._linear2(cell_inputs)\n\n gh_act = rnn_matrix[:, :n_dim] # b x n\n h_act = rnn_matrix[:, n_dim:2 * n_dim] # b x n\n gy_act = rnn_matrix[:, 2 * n_dim:2 * n_dim + i_dim] # b x i\n y_act = rnn_matrix[:, 2 * n_dim + i_dim:2 * n_dim + 2 * i_dim] # b x i\n\n h = tanh(h_act)\n y = self._y_activation(y_act)\n gh = sigmoid(gh_act + self._forget_bias)\n gy = sigmoid(gy_act + self._forget_bias)\n\n new_state = gh * state + (1.0 - gh) * h # passed thru time\n new_y = gy * inputs + (1.0 - gy) * y # passed thru depth\n\n return new_y, new_state\n\n\n_REGISTERED_OPS = None\n\n\nclass CompiledWrapper(rnn_cell_impl.RNNCell):\n \"\"\"Wraps step execution in an XLA JIT scope.\"\"\"\n\n def __init__(self, cell, compile_stateful=False):\n \"\"\"Create CompiledWrapper cell.\n\n Args:\n cell: Instance of `RNNCell`.\n compile_stateful: Whether to compile stateful ops like initializers\n and random number generators (default: False).\n \"\"\"\n self._cell = cell\n self._compile_stateful = compile_stateful\n\n @property\n def state_size(self):\n return self._cell.state_size\n\n @property\n def output_size(self):\n return self._cell.output_size\n\n def zero_state(self, batch_size, dtype):\n with ops.name_scope(type(self).__name__ + \"ZeroState\", values=[batch_size]):\n return self._cell.zero_state(batch_size, dtype)\n\n def __call__(self, inputs, state, scope=None):\n if self._compile_stateful:\n compile_ops = True\n else:\n\n def compile_ops(node_def):\n global _REGISTERED_OPS\n if _REGISTERED_OPS is None:\n _REGISTERED_OPS = op_def_registry.get_registered_ops()\n return not _REGISTERED_OPS[node_def.op].is_stateful\n\n with jit.experimental_jit_scope(compile_ops=compile_ops):\n return self._cell(inputs, state, scope=scope)\n\n\ndef _random_exp_initializer(minval, maxval, seed=None, dtype=dtypes.float32):\n \"\"\"Returns an exponential distribution initializer.\n\n Args:\n minval: float or a scalar float Tensor. With value > 0. Lower bound of the\n range of random values to generate.\n maxval: float or a scalar float Tensor. With value > minval. Upper bound of\n the range of random values to generate.\n seed: An integer. Used to create random seeds.\n dtype: The data type.\n\n Returns:\n An initializer that generates tensors with an exponential distribution.\n \"\"\"\n\n def _initializer(shape, dtype=dtype, partition_info=None):\n del partition_info # Unused.\n return math_ops.exp(\n random_ops.random_uniform(\n shape, math_ops.log(minval), math_ops.log(maxval), dtype,\n seed=seed))\n\n return _initializer\n\n\nclass PhasedLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Phased LSTM recurrent network cell.\n\n https://arxiv.org/pdf/1610.09513v1.pdf\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n leak=0.001,\n ratio_on=0.1,\n trainable_ratio_on=True,\n period_init_min=1.0,\n period_init_max=1000.0,\n reuse=None):\n \"\"\"Initialize the Phased LSTM cell.\n\n Args:\n num_units: int, The number of units in the Phased LSTM cell.\n use_peepholes: bool, set True to enable peephole connections.\n leak: float or scalar float Tensor with value in [0, 1]. Leak applied\n during training.\n ratio_on: float or scalar float Tensor with value in [0, 1]. Ratio of the\n period during which the gates are open.\n trainable_ratio_on: bool, weather ratio_on is trainable.\n period_init_min: float or scalar float Tensor. With value > 0.\n Minimum value of the initialized period.\n The period values are initialized by drawing from the distribution:\n e^U(log(period_init_min), log(period_init_max))\n Where U(.,.) is the uniform distribution.\n period_init_max: float or scalar float Tensor.\n With value > period_init_min. Maximum value of the initialized period.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(PhasedLSTMCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._use_peepholes = use_peepholes\n self._leak = leak\n self._ratio_on = ratio_on\n self._trainable_ratio_on = trainable_ratio_on\n self._period_init_min = period_init_min\n self._period_init_max = period_init_max\n self._reuse = reuse\n self._linear1 = None\n self._linear2 = None\n self._linear3 = None\n\n @property\n def state_size(self):\n return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)\n\n @property\n def output_size(self):\n return self._num_units\n\n def _mod(self, x, y):\n \"\"\"Modulo function that propagates x gradients.\"\"\"\n return array_ops.stop_gradient(math_ops.mod(x, y) - x) + x\n\n def _get_cycle_ratio(self, time, phase, period):\n \"\"\"Compute the cycle ratio in the dtype of the time.\"\"\"\n phase_casted = math_ops.cast(phase, dtype=time.dtype)\n period_casted = math_ops.cast(period, dtype=time.dtype)\n shifted_time = time - phase_casted\n cycle_ratio = self._mod(shifted_time, period_casted) / period_casted\n return math_ops.cast(cycle_ratio, dtype=dtypes.float32)\n\n def call(self, inputs, state):\n \"\"\"Phased LSTM Cell.\n\n Args:\n inputs: A tuple of 2 Tensor.\n The first Tensor has shape [batch, 1], and type float32 or float64.\n It stores the time.\n The second Tensor has shape [batch, features_size], and type float32.\n It stores the features.\n state: rnn_cell_impl.LSTMStateTuple, state from previous timestep.\n\n Returns:\n A tuple containing:\n - A Tensor of float32, and shape [batch_size, num_units], representing the\n output of the cell.\n - A rnn_cell_impl.LSTMStateTuple, containing 2 Tensors of float32, shape\n [batch_size, num_units], representing the new state and the output.\n \"\"\"\n (c_prev, h_prev) = state\n (time, x) = inputs\n\n in_mask_gates = [x, h_prev]\n if self._use_peepholes:\n in_mask_gates.append(c_prev)\n\n with vs.variable_scope(\"mask_gates\"):\n if self._linear1 is None:\n self._linear1 = _Linear(in_mask_gates, 2 * self._num_units, True)\n\n mask_gates = math_ops.sigmoid(self._linear1(in_mask_gates))\n [input_gate, forget_gate] = array_ops.split(\n axis=1, num_or_size_splits=2, value=mask_gates)\n\n with vs.variable_scope(\"new_input\"):\n if self._linear2 is None:\n self._linear2 = _Linear([x, h_prev], self._num_units, True)\n new_input = math_ops.tanh(self._linear2([x, h_prev]))\n\n new_c = (c_prev * forget_gate + input_gate * new_input)\n\n in_out_gate = [x, h_prev]\n if self._use_peepholes:\n in_out_gate.append(new_c)\n\n with vs.variable_scope(\"output_gate\"):\n if self._linear3 is None:\n self._linear3 = _Linear(in_out_gate, self._num_units, True)\n output_gate = math_ops.sigmoid(self._linear3(in_out_gate))\n\n new_h = math_ops.tanh(new_c) * output_gate\n\n period = vs.get_variable(\n \"period\", [self._num_units],\n initializer=_random_exp_initializer(self._period_init_min,\n self._period_init_max))\n phase = vs.get_variable(\n \"phase\", [self._num_units],\n initializer=init_ops.random_uniform_initializer(0.,\n period.initial_value))\n ratio_on = vs.get_variable(\n \"ratio_on\", [self._num_units],\n initializer=init_ops.constant_initializer(self._ratio_on),\n trainable=self._trainable_ratio_on)\n\n cycle_ratio = self._get_cycle_ratio(time, phase, period)\n\n k_up = 2 * cycle_ratio / ratio_on\n k_down = 2 - k_up\n k_closed = self._leak * cycle_ratio\n\n k = array_ops.where(cycle_ratio < ratio_on, k_down, k_closed)\n k = array_ops.where(cycle_ratio < 0.5 * ratio_on, k_up, k)\n\n new_c = k * new_c + (1 - k) * c_prev\n new_h = k * new_h + (1 - k) * h_prev\n\n new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h)\n\n return new_h, new_state\n\n\nclass ConvLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Convolutional LSTM recurrent network cell.\n\n https://arxiv.org/pdf/1506.04214v1.pdf\n \"\"\"\n\n def __init__(self,\n conv_ndims,\n input_shape,\n output_channels,\n kernel_shape,\n use_bias=True,\n skip_connection=False,\n forget_bias=1.0,\n initializers=None,\n name=\"conv_lstm_cell\"):\n \"\"\"Construct ConvLSTMCell.\n\n Args:\n conv_ndims: Convolution dimensionality (1, 2 or 3).\n input_shape: Shape of the input as int tuple, excluding the batch size.\n output_channels: int, number of output channels of the conv LSTM.\n kernel_shape: Shape of kernel as in tuple (of size 1,2 or 3).\n use_bias: (bool) Use bias in convolutions.\n skip_connection: If set to `True`, concatenate the input to the\n output of the conv LSTM. Default: `False`.\n forget_bias: Forget bias.\n initializers: Unused.\n name: Name of the module.\n\n Raises:\n ValueError: If `skip_connection` is `True` and stride is different from 1\n or if `input_shape` is incompatible with `conv_ndims`.\n \"\"\"\n super(ConvLSTMCell, self).__init__(name=name)\n\n if conv_ndims != len(input_shape) - 1:\n raise ValueError(\"Invalid input_shape {} for conv_ndims={}.\".format(\n input_shape, conv_ndims))\n\n self._conv_ndims = conv_ndims\n self._input_shape = input_shape\n self._output_channels = output_channels\n self._kernel_shape = kernel_shape\n self._use_bias = use_bias\n self._forget_bias = forget_bias\n self._skip_connection = skip_connection\n\n self._total_output_channels = output_channels\n if self._skip_connection:\n self._total_output_channels += self._input_shape[-1]\n\n state_size = tensor_shape.TensorShape(\n self._input_shape[:-1] + [self._output_channels])\n self._state_size = rnn_cell_impl.LSTMStateTuple(state_size, state_size)\n self._output_size = tensor_shape.TensorShape(\n self._input_shape[:-1] + [self._total_output_channels])\n\n @property\n def output_size(self):\n return self._output_size\n\n @property\n def state_size(self):\n return self._state_size\n\n def call(self, inputs, state, scope=None):\n cell, hidden = state\n new_hidden = _conv([inputs, hidden], self._kernel_shape,\n 4 * self._output_channels, self._use_bias)\n gates = array_ops.split(\n value=new_hidden, num_or_size_splits=4, axis=self._conv_ndims + 1)\n\n input_gate, new_input, forget_gate, output_gate = gates\n new_cell = math_ops.sigmoid(forget_gate + self._forget_bias) * cell\n new_cell += math_ops.sigmoid(input_gate) * math_ops.tanh(new_input)\n output = math_ops.tanh(new_cell) * math_ops.sigmoid(output_gate)\n\n if self._skip_connection:\n output = array_ops.concat([output, inputs], axis=-1)\n new_state = rnn_cell_impl.LSTMStateTuple(new_cell, output)\n return output, new_state\n\n\nclass Conv1DLSTMCell(ConvLSTMCell):\n \"\"\"1D Convolutional LSTM recurrent network cell.\n\n https://arxiv.org/pdf/1506.04214v1.pdf\n \"\"\"\n\n def __init__(self, name=\"conv_1d_lstm_cell\", **kwargs):\n \"\"\"Construct Conv1DLSTM. See `ConvLSTMCell` for more details.\"\"\"\n super(Conv1DLSTMCell, self).__init__(conv_ndims=1, name=name, **kwargs)\n\n\nclass Conv2DLSTMCell(ConvLSTMCell):\n \"\"\"2D Convolutional LSTM recurrent network cell.\n\n https://arxiv.org/pdf/1506.04214v1.pdf\n \"\"\"\n\n def __init__(self, name=\"conv_2d_lstm_cell\", **kwargs):\n \"\"\"Construct Conv2DLSTM. See `ConvLSTMCell` for more details.\"\"\"\n super(Conv2DLSTMCell, self).__init__(conv_ndims=2, name=name, **kwargs)\n\n\nclass Conv3DLSTMCell(ConvLSTMCell):\n \"\"\"3D Convolutional LSTM recurrent network cell.\n\n https://arxiv.org/pdf/1506.04214v1.pdf\n \"\"\"\n\n def __init__(self, name=\"conv_3d_lstm_cell\", **kwargs):\n \"\"\"Construct Conv3DLSTM. See `ConvLSTMCell` for more details.\"\"\"\n super(Conv3DLSTMCell, self).__init__(conv_ndims=3, name=name, **kwargs)\n\n\ndef _conv(args, filter_size, num_features, bias, bias_start=0.0):\n \"\"\"Convolution.\n\n Args:\n args: a Tensor or a list of Tensors of dimension 3D, 4D or 5D,\n batch x n, Tensors.\n filter_size: int tuple of filter height and width.\n num_features: int, number of features.\n bias: Whether to use biases in the convolution layer.\n bias_start: starting value to initialize the bias; 0 by default.\n\n Returns:\n A 3D, 4D, or 5D Tensor with shape [batch ... num_features]\n\n Raises:\n ValueError: if some of the arguments has unspecified or wrong shape.\n \"\"\"\n\n # Calculate the total size of arguments on dimension 1.\n total_arg_size_depth = 0\n shapes = [a.get_shape().as_list() for a in args]\n shape_length = len(shapes[0])\n for shape in shapes:\n if len(shape) not in [3, 4, 5]:\n raise ValueError(\"Conv Linear expects 3D, 4D \"\n \"or 5D arguments: %s\" % str(shapes))\n if len(shape) != len(shapes[0]):\n raise ValueError(\"Conv Linear expects all args \"\n \"to be of same Dimension: %s\" % str(shapes))\n else:\n total_arg_size_depth += shape[-1]\n dtype = [a.dtype for a in args][0]\n\n # determine correct conv operation\n if shape_length == 3:\n conv_op = nn_ops.conv1d\n strides = 1\n elif shape_length == 4:\n conv_op = nn_ops.conv2d\n strides = shape_length * [1]\n elif shape_length == 5:\n conv_op = nn_ops.conv3d\n strides = shape_length * [1]\n\n # Now the computation.\n kernel = vs.get_variable(\n \"kernel\", filter_size + [total_arg_size_depth, num_features], dtype=dtype)\n if len(args) == 1:\n res = conv_op(args[0], kernel, strides, padding=\"SAME\")\n else:\n res = conv_op(\n array_ops.concat(axis=shape_length - 1, values=args),\n kernel,\n strides,\n padding=\"SAME\")\n if not bias:\n return res\n bias_term = vs.get_variable(\n \"biases\", [num_features],\n dtype=dtype,\n initializer=init_ops.constant_initializer(bias_start, dtype=dtype))\n return res + bias_term\n\n\nclass GLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Group LSTM cell (G-LSTM).\n\n The implementation is based on:\n\n https://arxiv.org/abs/1703.10722\n\n O. Kuchaiev and B. Ginsburg\n \"Factorization Tricks for LSTM Networks\", ICLR 2017 workshop.\n\n In brief, a G-LSTM cell consists of one LSTM sub-cell per group, where each\n sub-cell operates on an evenly-sized sub-vector of the input and produces an\n evenly-sized sub-vector of the output. For example, a G-LSTM cell with 128\n units and 4 groups consists of 4 LSTMs sub-cells with 32 units each. If that\n G-LSTM cell is fed a 200-dim input, then each sub-cell receives a 50-dim part\n of the input and produces a 32-dim part of the output.\n \"\"\"\n\n def __init__(self,\n num_units,\n initializer=None,\n num_proj=None,\n number_of_groups=1,\n forget_bias=1.0,\n activation=math_ops.tanh,\n reuse=None):\n \"\"\"Initialize the parameters of G-LSTM cell.\n\n Args:\n num_units: int, The number of units in the G-LSTM cell\n initializer: (optional) The initializer to use for the weight and\n projection matrices.\n num_proj: (optional) int, The output dimensionality for the projection\n matrices. If None, no projection is performed.\n number_of_groups: (optional) int, number of groups to use.\n If `number_of_groups` is 1, then it should be equivalent to LSTM cell\n forget_bias: Biases of the forget gate are initialized by default to 1\n in order to reduce the scale of forgetting at the beginning of\n the training.\n activation: Activation function of the inner states.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already\n has the given variables, an error is raised.\n\n Raises:\n ValueError: If `num_units` or `num_proj` is not divisible by\n `number_of_groups`.\n \"\"\"\n super(GLSTMCell, self).__init__(_reuse=reuse)\n self._num_units = num_units\n self._initializer = initializer\n self._num_proj = num_proj\n self._forget_bias = forget_bias\n self._activation = activation\n self._number_of_groups = number_of_groups\n\n if self._num_units % self._number_of_groups != 0:\n raise ValueError(\"num_units must be divisible by number_of_groups\")\n if self._num_proj:\n if self._num_proj % self._number_of_groups != 0:\n raise ValueError(\"num_proj must be divisible by number_of_groups\")\n self._group_shape = [\n int(self._num_proj / self._number_of_groups),\n int(self._num_units / self._number_of_groups)\n ]\n else:\n self._group_shape = [\n int(self._num_units / self._number_of_groups),\n int(self._num_units / self._number_of_groups)\n ]\n\n if num_proj:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj)\n self._output_size = num_proj\n else:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units)\n self._output_size = num_units\n self._linear1 = [None] * number_of_groups\n self._linear2 = None\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def output_size(self):\n return self._output_size\n\n def _get_input_for_group(self, inputs, group_id, group_size):\n \"\"\"Slices inputs into groups to prepare for processing by cell's groups.\n\n Args:\n inputs: cell input or it's previous state,\n a Tensor, 2D, [batch x num_units]\n group_id: group id, a Scalar, for which to prepare input\n group_size: size of the group\n\n Returns:\n subset of inputs corresponding to group \"group_id\",\n a Tensor, 2D, [batch x num_units/number_of_groups]\n \"\"\"\n return array_ops.slice(\n input_=inputs,\n begin=[0, group_id * group_size],\n size=[self._batch_size, group_size],\n name=(\"GLSTM_group%d_input_generation\" % group_id))\n\n def call(self, inputs, state):\n \"\"\"Run one step of G-LSTM.\n\n Args:\n inputs: input Tensor, 2D, [batch x num_inputs]. num_inputs must be\n statically-known and evenly divisible into groups. The innermost\n vectors of the inputs are split into evenly-sized sub-vectors and fed\n into the per-group LSTM sub-cells.\n state: this must be a tuple of state Tensors, both `2-D`, with column\n sizes `c_state` and `m_state`.\n\n Returns:\n A tuple containing:\n\n - A `2-D, [batch x output_dim]`, Tensor representing the output of the\n G-LSTM after reading `inputs` when previous state was `state`.\n Here output_dim is:\n num_proj if num_proj was set,\n num_units otherwise.\n - LSTMStateTuple representing the new state of G-LSTM cell\n after reading `inputs` when the previous state was `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference, or if the input shape is incompatible\n with the number of groups.\n \"\"\"\n (c_prev, m_prev) = state\n\n self._batch_size = inputs.shape[0].value or array_ops.shape(inputs)[0]\n\n # If the input size is statically-known, calculate and validate its group\n # size. Otherwise, use the output group size.\n input_size = inputs.shape[1].value\n if input_size is None:\n raise ValueError(\"input size must be statically known\")\n if input_size % self._number_of_groups != 0:\n raise ValueError(\n \"input size (%d) must be divisible by number_of_groups (%d)\" %\n (input_size, self._number_of_groups))\n input_group_size = int(input_size / self._number_of_groups)\n\n dtype = inputs.dtype\n scope = vs.get_variable_scope()\n with vs.variable_scope(scope, initializer=self._initializer):\n i_parts = []\n j_parts = []\n f_parts = []\n o_parts = []\n\n for group_id in range(self._number_of_groups):\n with vs.variable_scope(\"group%d\" % group_id):\n x_g_id = array_ops.concat(\n [\n self._get_input_for_group(inputs, group_id, input_group_size),\n self._get_input_for_group(m_prev, group_id,\n self._group_shape[0])\n ],\n axis=1)\n linear = self._linear1[group_id]\n if linear is None:\n linear = _Linear(x_g_id, 4 * self._group_shape[1], False)\n self._linear1[group_id] = linear\n R_k = linear(x_g_id) # pylint: disable=invalid-name\n i_k, j_k, f_k, o_k = array_ops.split(R_k, 4, 1)\n\n i_parts.append(i_k)\n j_parts.append(j_k)\n f_parts.append(f_k)\n o_parts.append(o_k)\n\n bi = vs.get_variable(\n name=\"bias_i\",\n shape=[self._num_units],\n dtype=dtype,\n initializer=init_ops.constant_initializer(0.0, dtype=dtype))\n bj = vs.get_variable(\n name=\"bias_j\",\n shape=[self._num_units],\n dtype=dtype,\n initializer=init_ops.constant_initializer(0.0, dtype=dtype))\n bf = vs.get_variable(\n name=\"bias_f\",\n shape=[self._num_units],\n dtype=dtype,\n initializer=init_ops.constant_initializer(0.0, dtype=dtype))\n bo = vs.get_variable(\n name=\"bias_o\",\n shape=[self._num_units],\n dtype=dtype,\n initializer=init_ops.constant_initializer(0.0, dtype=dtype))\n\n i = nn_ops.bias_add(array_ops.concat(i_parts, axis=1), bi)\n j = nn_ops.bias_add(array_ops.concat(j_parts, axis=1), bj)\n f = nn_ops.bias_add(array_ops.concat(f_parts, axis=1), bf)\n o = nn_ops.bias_add(array_ops.concat(o_parts, axis=1), bo)\n\n c = (\n math_ops.sigmoid(f + self._forget_bias) * c_prev +\n math_ops.sigmoid(i) * math_ops.tanh(j))\n m = math_ops.sigmoid(o) * self._activation(c)\n\n if self._num_proj is not None:\n with vs.variable_scope(\"projection\"):\n if self._linear2 is None:\n self._linear2 = _Linear(m, self._num_proj, False)\n m = self._linear2(m)\n\n new_state = rnn_cell_impl.LSTMStateTuple(c, m)\n return m, new_state\n\n\nclass LayerNormLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Long short-term memory unit (LSTM) recurrent network cell.\n\n The default non-peephole implementation is based on:\n\n http://www.bioinf.jku.at/publications/older/2604.pdf\n\n S. Hochreiter and J. Schmidhuber.\n \"Long Short-Term Memory\". Neural Computation, 9(8):1735-1780, 1997.\n\n The peephole implementation is based on:\n\n https://research.google.com/pubs/archive/43905.pdf\n\n Hasim Sak, Andrew Senior, and Francoise Beaufays.\n \"Long short-term memory recurrent neural network architectures for\n large scale acoustic modeling.\" INTERSPEECH, 2014.\n\n The class uses optional peep-hole connections, optional cell clipping, and\n an optional projection layer.\n\n Layer normalization implementation is based on:\n\n https://arxiv.org/abs/1607.06450.\n\n \"Layer Normalization\"\n Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton\n\n and is applied before the internal nonlinearities.\n\n \"\"\"\n\n def __init__(self,\n num_units,\n use_peepholes=False,\n cell_clip=None,\n initializer=None,\n num_proj=None,\n proj_clip=None,\n forget_bias=1.0,\n activation=None,\n layer_norm=False,\n norm_gain=1.0,\n norm_shift=0.0,\n reuse=None):\n \"\"\"Initialize the parameters for an LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n use_peepholes: bool, set True to enable diagonal/peephole connections.\n cell_clip: (optional) A float value, if provided the cell state is clipped\n by this value prior to the cell output activation.\n initializer: (optional) The initializer to use for the weight and\n projection matrices.\n num_proj: (optional) int, The output dimensionality for the projection\n matrices. If None, no projection is performed.\n proj_clip: (optional) A float value. If `num_proj > 0` and `proj_clip` is\n provided, then the projected values are clipped elementwise to within\n `[-proj_clip, proj_clip]`.\n forget_bias: Biases of the forget gate are initialized by default to 1\n in order to reduce the scale of forgetting at the beginning of\n the training. Must set it manually to `0.0` when restoring from\n CudnnLSTM trained checkpoints.\n activation: Activation function of the inner states. Default: `tanh`.\n layer_norm: If `True`, layer normalization will be applied.\n norm_gain: float, The layer normalization gain initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n norm_shift: float, The layer normalization shift initial value. If\n `layer_norm` has been set to `False`, this argument will be ignored.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n\n When restoring from CudnnLSTM-trained checkpoints, must use\n CudnnCompatibleLSTMCell instead.\n \"\"\"\n super(LayerNormLSTMCell, self).__init__(_reuse=reuse)\n\n self._num_units = num_units\n self._use_peepholes = use_peepholes\n self._cell_clip = cell_clip\n self._initializer = initializer\n self._num_proj = num_proj\n self._proj_clip = proj_clip\n self._forget_bias = forget_bias\n self._activation = activation or math_ops.tanh\n self._layer_norm = layer_norm\n self._norm_gain = norm_gain\n self._norm_shift = norm_shift\n\n if num_proj:\n self._state_size = (rnn_cell_impl.LSTMStateTuple(num_units, num_proj))\n self._output_size = num_proj\n else:\n self._state_size = (rnn_cell_impl.LSTMStateTuple(num_units, num_units))\n self._output_size = num_units\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def output_size(self):\n return self._output_size\n\n def _linear(self,\n args,\n output_size,\n bias,\n bias_initializer=None,\n kernel_initializer=None,\n layer_norm=False):\n \"\"\"Linear map: sum_i(args[i] * W[i]), where W[i] is a Variable.\n\n Args:\n args: a 2D Tensor or a list of 2D, batch x n, Tensors.\n output_size: int, second dimension of W[i].\n bias: boolean, whether to add a bias term or not.\n bias_initializer: starting value to initialize the bias\n (default is all zeros).\n kernel_initializer: starting value to initialize the weight.\n layer_norm: boolean, whether to apply layer normalization.\n\n\n Returns:\n A 2D Tensor with shape [batch x output_size] taking value\n sum_i(args[i] * W[i]), where each W[i] is a newly created Variable.\n\n Raises:\n ValueError: if some of the arguments has unspecified or wrong shape.\n \"\"\"\n if args is None or (nest.is_sequence(args) and not args):\n raise ValueError(\"`args` must be specified\")\n if not nest.is_sequence(args):\n args = [args]\n\n # Calculate the total size of arguments on dimension 1.\n total_arg_size = 0\n shapes = [a.get_shape() for a in args]\n for shape in shapes:\n if shape.ndims != 2:\n raise ValueError(\"linear is expecting 2D arguments: %s\" % shapes)\n if shape[1].value is None:\n raise ValueError(\"linear expects shape[1] to be provided for shape %s, \"\n \"but saw %s\" % (shape, shape[1]))\n else:\n total_arg_size += shape[1].value\n\n dtype = [a.dtype for a in args][0]\n\n # Now the computation.\n scope = vs.get_variable_scope()\n with vs.variable_scope(scope) as outer_scope:\n weights = vs.get_variable(\n \"kernel\", [total_arg_size, output_size],\n dtype=dtype,\n initializer=kernel_initializer)\n if len(args) == 1:\n res = math_ops.matmul(args[0], weights)\n else:\n res = math_ops.matmul(array_ops.concat(args, 1), weights)\n if not bias:\n return res\n with vs.variable_scope(outer_scope) as inner_scope:\n inner_scope.set_partitioner(None)\n if bias_initializer is None:\n bias_initializer = init_ops.constant_initializer(0.0, dtype=dtype)\n biases = vs.get_variable(\n \"bias\", [output_size], dtype=dtype, initializer=bias_initializer)\n\n if not layer_norm:\n res = nn_ops.bias_add(res, biases)\n\n return res\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, batch x num_units.\n state: this must be a tuple of state Tensors,\n both `2-D`, with column sizes `c_state` and\n `m_state`.\n\n Returns:\n A tuple containing:\n\n - A `2-D, [batch x output_dim]`, Tensor representing the output of the\n LSTM after reading `inputs` when previous state was `state`.\n Here output_dim is:\n num_proj if num_proj was set,\n num_units otherwise.\n - Tensor(s) representing the new state of LSTM after reading `inputs` when\n the previous state was `state`. Same type and shape(s) as `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference.\n \"\"\"\n sigmoid = math_ops.sigmoid\n\n (c_prev, m_prev) = state\n\n dtype = inputs.dtype\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n scope = vs.get_variable_scope()\n with vs.variable_scope(scope, initializer=self._initializer) as unit_scope:\n\n # i = input_gate, j = new_input, f = forget_gate, o = output_gate\n lstm_matrix = self._linear(\n [inputs, m_prev],\n 4 * self._num_units,\n bias=True,\n bias_initializer=None,\n layer_norm=self._layer_norm)\n i, j, f, o = array_ops.split(\n value=lstm_matrix, num_or_size_splits=4, axis=1)\n\n if self._layer_norm:\n i = _norm(self._norm_gain, self._norm_shift, i, \"input\")\n j = _norm(self._norm_gain, self._norm_shift, j, \"transform\")\n f = _norm(self._norm_gain, self._norm_shift, f, \"forget\")\n o = _norm(self._norm_gain, self._norm_shift, o, \"output\")\n\n # Diagonal connections\n if self._use_peepholes:\n with vs.variable_scope(unit_scope):\n w_f_diag = vs.get_variable(\n \"w_f_diag\", shape=[self._num_units], dtype=dtype)\n w_i_diag = vs.get_variable(\n \"w_i_diag\", shape=[self._num_units], dtype=dtype)\n w_o_diag = vs.get_variable(\n \"w_o_diag\", shape=[self._num_units], dtype=dtype)\n\n if self._use_peepholes:\n c = (\n sigmoid(f + self._forget_bias + w_f_diag * c_prev) * c_prev +\n sigmoid(i + w_i_diag * c_prev) * self._activation(j))\n else:\n c = (\n sigmoid(f + self._forget_bias) * c_prev +\n sigmoid(i) * self._activation(j))\n\n if self._layer_norm:\n c = _norm(self._norm_gain, self._norm_shift, c, \"state\")\n\n if self._cell_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n c = clip_ops.clip_by_value(c, -self._cell_clip, self._cell_clip)\n # pylint: enable=invalid-unary-operand-type\n if self._use_peepholes:\n m = sigmoid(o + w_o_diag * c) * self._activation(c)\n else:\n m = sigmoid(o) * self._activation(c)\n\n if self._num_proj is not None:\n with vs.variable_scope(\"projection\"):\n m = self._linear(m, self._num_proj, bias=False)\n\n if self._proj_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n m = clip_ops.clip_by_value(m, -self._proj_clip, self._proj_clip)\n # pylint: enable=invalid-unary-operand-type\n\n new_state = (rnn_cell_impl.LSTMStateTuple(c, m))\n return m, new_state\n\n\nclass SRUCell(rnn_cell_impl.LayerRNNCell):\n \"\"\"SRU, Simple Recurrent Unit.\n\n Implementation based on\n Training RNNs as Fast as CNNs (cf. https://arxiv.org/abs/1709.02755).\n\n This variation of RNN cell is characterized by the simplified data\n dependence\n between hidden states of two consecutive time steps. Traditionally, hidden\n states from a cell at time step t-1 needs to be multiplied with a matrix\n W_hh before being fed into the ensuing cell at time step t.\n This flavor of RNN replaces the matrix multiplication between h_{t-1}\n and W_hh with a pointwise multiplication, resulting in performance\n gain.\n\n Args:\n num_units: int, The number of units in the SRU cell.\n activation: Nonlinearity to use. Default: `tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n name: (optional) String, the name of the layer. Layers with the same name\n will share weights, but to avoid mistakes we require reuse=True in such\n cases.\n \"\"\"\n\n def __init__(self, num_units, activation=None, reuse=None, name=None):\n super(SRUCell, self).__init__(_reuse=reuse, name=name)\n self._num_units = num_units\n self._activation = activation or math_ops.tanh\n\n # Restrict inputs to be 2-dimensional matrices\n self.input_spec = base_layer.InputSpec(ndim=2)\n\n @property\n def state_size(self):\n return self._num_units\n\n @property\n def output_size(self):\n return self._num_units\n\n def build(self, inputs_shape):\n if inputs_shape[1].value is None:\n raise ValueError(\n \"Expected inputs.shape[-1] to be known, saw shape: %s\" % inputs_shape)\n\n input_depth = inputs_shape[1].value\n\n # pylint: disable=protected-access\n self._kernel = self.add_variable(\n rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[input_depth, 4 * self._num_units])\n # pylint: enable=protected-access\n self._bias = self.add_variable(\n rnn_cell_impl._BIAS_VARIABLE_NAME, # pylint: disable=protected-access\n shape=[2 * self._num_units],\n initializer=init_ops.constant_initializer(0.0, dtype=self.dtype))\n\n self._built = True\n\n def call(self, inputs, state):\n \"\"\"Simple recurrent unit (SRU) with num_units cells.\"\"\"\n\n U = math_ops.matmul(inputs, self._kernel) # pylint: disable=invalid-name\n x_bar, f_intermediate, r_intermediate, x_tx = array_ops.split(\n value=U, num_or_size_splits=4, axis=1)\n\n f_r = math_ops.sigmoid(\n nn_ops.bias_add(\n array_ops.concat([f_intermediate, r_intermediate], 1), self._bias))\n f, r = array_ops.split(value=f_r, num_or_size_splits=2, axis=1)\n\n c = f * state + (1.0 - f) * x_bar\n h = r * self._activation(c) + (1.0 - r) * x_tx\n\n return h, c\n\n\nclass WeightNormLSTMCell(rnn_cell_impl.RNNCell):\n \"\"\"Weight normalized LSTM Cell. Adapted from `rnn_cell_impl.LSTMCell`.\n\n The weight-norm implementation is based on:\n https://arxiv.org/abs/1602.07868\n Tim Salimans, Diederik P. Kingma.\n Weight Normalization: A Simple Reparameterization to Accelerate\n Training of Deep Neural Networks\n\n The default LSTM implementation based on:\n http://www.bioinf.jku.at/publications/older/2604.pdf\n S. Hochreiter and J. Schmidhuber.\n \"Long Short-Term Memory\". Neural Computation, 9(8):1735-1780, 1997.\n\n The class uses optional peephole connections, optional cell clipping\n and an optional projection layer.\n\n The optional peephole implementation is based on:\n https://research.google.com/pubs/archive/43905.pdf\n Hasim Sak, Andrew Senior, and Francoise Beaufays.\n \"Long short-term memory recurrent neural network architectures for\n large scale acoustic modeling.\" INTERSPEECH, 2014.\n \"\"\"\n\n def __init__(self,\n num_units,\n norm=True,\n use_peepholes=False,\n cell_clip=None,\n initializer=None,\n num_proj=None,\n proj_clip=None,\n forget_bias=1,\n activation=None,\n reuse=None):\n \"\"\"Initialize the parameters of a weight-normalized LSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell\n norm: If `True`, apply normalization to the weight matrices. If False,\n the result is identical to that obtained from `rnn_cell_impl.LSTMCell`\n use_peepholes: bool, set `True` to enable diagonal/peephole connections.\n cell_clip: (optional) A float value, if provided the cell state is clipped\n by this value prior to the cell output activation.\n initializer: (optional) The initializer to use for the weight matrices.\n num_proj: (optional) int, The output dimensionality for the projection\n matrices. If None, no projection is performed.\n proj_clip: (optional) A float value. If `num_proj > 0` and `proj_clip` is\n provided, then the projected values are clipped elementwise to within\n `[-proj_clip, proj_clip]`.\n forget_bias: Biases of the forget gate are initialized by default to 1\n in order to reduce the scale of forgetting at the beginning of\n the training.\n activation: Activation function of the inner states. Default: `tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n \"\"\"\n super(WeightNormLSTMCell, self).__init__(_reuse=reuse)\n\n self._scope = \"wn_lstm_cell\"\n self._num_units = num_units\n self._norm = norm\n self._initializer = initializer\n self._use_peepholes = use_peepholes\n self._cell_clip = cell_clip\n self._num_proj = num_proj\n self._proj_clip = proj_clip\n self._activation = activation or math_ops.tanh\n self._forget_bias = forget_bias\n\n self._weights_variable_name = \"kernel\"\n self._bias_variable_name = \"bias\"\n\n if num_proj:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj)\n self._output_size = num_proj\n else:\n self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units)\n self._output_size = num_units\n\n @property\n def state_size(self):\n return self._state_size\n\n @property\n def output_size(self):\n return self._output_size\n\n def _normalize(self, weight, name):\n \"\"\"Apply weight normalization.\n\n Args:\n weight: a 2D tensor with known number of columns.\n name: string, variable name for the normalizer.\n Returns:\n A tensor with the same shape as `weight`.\n \"\"\"\n\n output_size = weight.get_shape().as_list()[1]\n g = vs.get_variable(name, [output_size], dtype=weight.dtype)\n return nn_impl.l2_normalize(weight, axis=0) * g\n\n def _linear(self,\n args,\n output_size,\n norm,\n bias,\n bias_initializer=None,\n kernel_initializer=None):\n \"\"\"Linear map: sum_i(args[i] * W[i]), where W[i] is a variable.\n\n Args:\n args: a 2D Tensor or a list of 2D, batch x n, Tensors.\n output_size: int, second dimension of W[i].\n norm: bool, whether to normalize the weights.\n bias: boolean, whether to add a bias term or not.\n bias_initializer: starting value to initialize the bias\n (default is all zeros).\n kernel_initializer: starting value to initialize the weight.\n\n Returns:\n A 2D Tensor with shape [batch x output_size] equal to\n sum_i(args[i] * W[i]), where W[i]s are newly created matrices.\n\n Raises:\n ValueError: if some of the arguments has unspecified or wrong shape.\n \"\"\"\n if args is None or (nest.is_sequence(args) and not args):\n raise ValueError(\"`args` must be specified\")\n if not nest.is_sequence(args):\n args = [args]\n\n # Calculate the total size of arguments on dimension 1.\n total_arg_size = 0\n shapes = [a.get_shape() for a in args]\n for shape in shapes:\n if shape.ndims != 2:\n raise ValueError(\"linear is expecting 2D arguments: %s\" % shapes)\n if shape[1].value is None:\n raise ValueError(\"linear expects shape[1] to be provided for shape %s, \"\n \"but saw %s\" % (shape, shape[1]))\n else:\n total_arg_size += shape[1].value\n\n dtype = [a.dtype for a in args][0]\n\n # Now the computation.\n scope = vs.get_variable_scope()\n with vs.variable_scope(scope) as outer_scope:\n weights = vs.get_variable(\n self._weights_variable_name, [total_arg_size, output_size],\n dtype=dtype,\n initializer=kernel_initializer)\n if norm:\n wn = []\n st = 0\n with ops.control_dependencies(None):\n for i in range(len(args)):\n en = st + shapes[i][1].value\n wn.append(\n self._normalize(weights[st:en, :], name=\"norm_{}\".format(i)))\n st = en\n\n weights = array_ops.concat(wn, axis=0)\n\n if len(args) == 1:\n res = math_ops.matmul(args[0], weights)\n else:\n res = math_ops.matmul(array_ops.concat(args, 1), weights)\n if not bias:\n return res\n\n with vs.variable_scope(outer_scope) as inner_scope:\n inner_scope.set_partitioner(None)\n if bias_initializer is None:\n bias_initializer = init_ops.constant_initializer(0.0, dtype=dtype)\n\n biases = vs.get_variable(\n self._bias_variable_name, [output_size],\n dtype=dtype,\n initializer=bias_initializer)\n\n return nn_ops.bias_add(res, biases)\n\n def call(self, inputs, state):\n \"\"\"Run one step of LSTM.\n\n Args:\n inputs: input Tensor, 2D, batch x num_units.\n state: A tuple of state Tensors, both `2-D`, with column sizes\n `c_state` and `m_state`.\n\n Returns:\n A tuple containing:\n\n - A `2-D, [batch x output_dim]`, Tensor representing the output of the\n LSTM after reading `inputs` when previous state was `state`.\n Here output_dim is:\n num_proj if num_proj was set,\n num_units otherwise.\n - Tensor(s) representing the new state of LSTM after reading `inputs` when\n the previous state was `state`. Same type and shape(s) as `state`.\n\n Raises:\n ValueError: If input size cannot be inferred from inputs via\n static shape inference.\n \"\"\"\n dtype = inputs.dtype\n num_units = self._num_units\n sigmoid = math_ops.sigmoid\n c, h = state\n\n input_size = inputs.get_shape().with_rank(2)[1]\n if input_size.value is None:\n raise ValueError(\"Could not infer input size from inputs.get_shape()[-1]\")\n\n with vs.variable_scope(self._scope, initializer=self._initializer):\n\n concat = self._linear(\n [inputs, h], 4 * num_units, norm=self._norm, bias=True)\n\n # i = input_gate, j = new_input, f = forget_gate, o = output_gate\n i, j, f, o = array_ops.split(value=concat, num_or_size_splits=4, axis=1)\n\n if self._use_peepholes:\n w_f_diag = vs.get_variable(\"w_f_diag\", shape=[num_units], dtype=dtype)\n w_i_diag = vs.get_variable(\"w_i_diag\", shape=[num_units], dtype=dtype)\n w_o_diag = vs.get_variable(\"w_o_diag\", shape=[num_units], dtype=dtype)\n\n new_c = (\n c * sigmoid(f + self._forget_bias + w_f_diag * c) +\n sigmoid(i + w_i_diag * c) * self._activation(j))\n else:\n new_c = (\n c * sigmoid(f + self._forget_bias) +\n sigmoid(i) * self._activation(j))\n\n if self._cell_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n new_c = clip_ops.clip_by_value(new_c, -self._cell_clip, self._cell_clip)\n # pylint: enable=invalid-unary-operand-type\n if self._use_peepholes:\n new_h = sigmoid(o + w_o_diag * new_c) * self._activation(new_c)\n else:\n new_h = sigmoid(o) * self._activation(new_c)\n\n if self._num_proj is not None:\n with vs.variable_scope(\"projection\"):\n new_h = self._linear(\n new_h, self._num_proj, norm=self._norm, bias=False)\n\n if self._proj_clip is not None:\n # pylint: disable=invalid-unary-operand-type\n new_h = clip_ops.clip_by_value(new_h, -self._proj_clip,\n self._proj_clip)\n # pylint: enable=invalid-unary-operand-type\n\n new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h)\n return new_h, new_state\n\n\nclass IndRNNCell(rnn_cell_impl.LayerRNNCell):\n \"\"\"Independently Recurrent Neural Network (IndRNN) cell\n (cf. https://arxiv.org/abs/1803.04831).\n\n Args:\n num_units: int, The number of units in the RNN cell.\n activation: Nonlinearity to use. Default: `tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n name: String, the name of the layer. Layers with the same name will\n share weights, but to avoid mistakes we require reuse=True in such\n cases.\n dtype: Default dtype of the layer (default of `None` means use the type\n of the first input). Required when `build` is called before `call`.\n \"\"\"\n\n def __init__(self,\n num_units,\n activation=None,\n reuse=None,\n name=None,\n dtype=None):\n super(IndRNNCell, self).__init__(_reuse=reuse, name=name, dtype=dtype)\n\n # Inputs must be 2-dimensional.\n self.input_spec = base_layer.InputSpec(ndim=2)\n\n self._num_units = num_units\n self._activation = activation or math_ops.tanh\n\n @property\n def state_size(self):\n return self._num_units\n\n @property\n def output_size(self):\n return self._num_units\n\n def build(self, inputs_shape):\n if inputs_shape[1].value is None:\n raise ValueError(\n \"Expected inputs.shape[-1] to be known, saw shape: %s\" % inputs_shape)\n\n input_depth = inputs_shape[1].value\n # pylint: disable=protected-access\n self._kernel_w = self.add_variable(\n \"%s_w\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[input_depth, self._num_units])\n self._kernel_u = self.add_variable(\n \"%s_u\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[1, self._num_units],\n initializer=init_ops.random_uniform_initializer(\n minval=-1, maxval=1, dtype=self.dtype))\n self._bias = self.add_variable(\n rnn_cell_impl._BIAS_VARIABLE_NAME,\n shape=[self._num_units],\n initializer=init_ops.zeros_initializer(dtype=self.dtype))\n # pylint: enable=protected-access\n\n self.built = True\n\n def call(self, inputs, state):\n \"\"\"IndRNN: output = new_state = act(W * input + u * state + B).\"\"\"\n\n gate_inputs = math_ops.matmul(inputs, self._kernel_w) + (\n state * self._kernel_u)\n gate_inputs = nn_ops.bias_add(gate_inputs, self._bias)\n output = self._activation(gate_inputs)\n return output, output\n\n\nclass IndyGRUCell(rnn_cell_impl.LayerRNNCell):\n r\"\"\"Independently Gated Recurrent Unit cell.\n\n Based on IndRNNs (https://arxiv.org/abs/1803.04831) and similar to GRUCell,\n yet with the \\(U_r\\), \\(U_z\\), and \\(U\\) matrices in equations 5, 6, and\n 8 of http://arxiv.org/abs/1406.1078 respectively replaced by diagonal\n matrices, i.e. a Hadamard product with a single vector:\n\n $$r_j = \\sigma\\left([\\mathbf W_r\\mathbf x]_j +\n [\\mathbf u_r\\circ \\mathbf h_{(t-1)}]_j\\right)$$\n $$z_j = \\sigma\\left([\\mathbf W_z\\mathbf x]_j +\n [\\mathbf u_z\\circ \\mathbf h_{(t-1)}]_j\\right)$$\n $$\\tilde{h}^{(t)}_j = \\phi\\left([\\mathbf W \\mathbf x]_j +\n [\\mathbf u \\circ \\mathbf r \\circ \\mathbf h_{(t-1)}]_j\\right)$$\n\n where \\(\\circ\\) denotes the Hadamard operator. This means that each IndyGRU\n node sees only its own state, as opposed to seeing all states in the same\n layer.\n\n TODO(gonnet): Write a paper describing this and add a reference here.\n\n Args:\n num_units: int, The number of units in the GRU cell.\n activation: Nonlinearity to use. Default: `tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n kernel_initializer: (optional) The initializer to use for the weight\n matrices applied to the input.\n bias_initializer: (optional) The initializer to use for the bias.\n name: String, the name of the layer. Layers with the same name will\n share weights, but to avoid mistakes we require reuse=True in such\n cases.\n dtype: Default dtype of the layer (default of `None` means use the type\n of the first input). Required when `build` is called before `call`.\n \"\"\"\n\n def __init__(self,\n num_units,\n activation=None,\n reuse=None,\n kernel_initializer=None,\n bias_initializer=None,\n name=None,\n dtype=None):\n super(IndyGRUCell, self).__init__(_reuse=reuse, name=name, dtype=dtype)\n\n # Inputs must be 2-dimensional.\n self.input_spec = base_layer.InputSpec(ndim=2)\n\n self._num_units = num_units\n self._activation = activation or math_ops.tanh\n self._kernel_initializer = kernel_initializer\n self._bias_initializer = bias_initializer\n\n @property\n def state_size(self):\n return self._num_units\n\n @property\n def output_size(self):\n return self._num_units\n\n def build(self, inputs_shape):\n if inputs_shape[1].value is None:\n raise ValueError(\n \"Expected inputs.shape[-1] to be known, saw shape: %s\" % inputs_shape)\n\n input_depth = inputs_shape[1].value\n # pylint: disable=protected-access\n self._gate_kernel_w = self.add_variable(\n \"gates/%s_w\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[input_depth, 2 * self._num_units],\n initializer=self._kernel_initializer)\n self._gate_kernel_u = self.add_variable(\n \"gates/%s_u\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[1, 2 * self._num_units],\n initializer=init_ops.random_uniform_initializer(\n minval=-1, maxval=1, dtype=self.dtype))\n self._gate_bias = self.add_variable(\n \"gates/%s\" % rnn_cell_impl._BIAS_VARIABLE_NAME,\n shape=[2 * self._num_units],\n initializer=(self._bias_initializer\n if self._bias_initializer is not None else\n init_ops.constant_initializer(1.0, dtype=self.dtype)))\n self._candidate_kernel_w = self.add_variable(\n \"candidate/%s\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[input_depth, self._num_units],\n initializer=self._kernel_initializer)\n self._candidate_kernel_u = self.add_variable(\n \"candidate/%s_u\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[1, self._num_units],\n initializer=init_ops.random_uniform_initializer(\n minval=-1, maxval=1, dtype=self.dtype))\n self._candidate_bias = self.add_variable(\n \"candidate/%s\" % rnn_cell_impl._BIAS_VARIABLE_NAME,\n shape=[self._num_units],\n initializer=(self._bias_initializer\n if self._bias_initializer is not None else\n init_ops.zeros_initializer(dtype=self.dtype)))\n # pylint: enable=protected-access\n\n self.built = True\n\n def call(self, inputs, state):\n \"\"\"Gated recurrent unit (GRU) with nunits cells.\"\"\"\n\n gate_inputs = math_ops.matmul(inputs, self._gate_kernel_w) + (\n gen_array_ops.tile(state, [1, 2]) * self._gate_kernel_u)\n gate_inputs = nn_ops.bias_add(gate_inputs, self._gate_bias)\n\n value = math_ops.sigmoid(gate_inputs)\n r, u = array_ops.split(value=value, num_or_size_splits=2, axis=1)\n\n r_state = r * state\n\n candidate = math_ops.matmul(inputs, self._candidate_kernel_w) + (\n r_state * self._candidate_kernel_u)\n candidate = nn_ops.bias_add(candidate, self._candidate_bias)\n\n c = self._activation(candidate)\n new_h = u * state + (1 - u) * c\n return new_h, new_h\n\n\nclass IndyLSTMCell(rnn_cell_impl.LayerRNNCell):\n r\"\"\"Basic IndyLSTM recurrent network cell.\n\n Based on IndRNNs (https://arxiv.org/abs/1803.04831) and similar to\n BasicLSTMCell, yet with the \\(U_f\\), \\(U_i\\), \\(U_o\\) and \\(U_c\\)\n matrices in\n https://en.wikipedia.org/wiki/Long_short-term_memory#LSTM_with_a_forget_gate\n replaced by diagonal matrices, i.e. a Hadamard product with a single vector:\n\n $$f_t = \\sigma_g\\left(W_f x_t + u_f \\circ h_{t-1} + b_f\\right)$$\n $$i_t = \\sigma_g\\left(W_i x_t + u_i \\circ h_{t-1} + b_i\\right)$$\n $$o_t = \\sigma_g\\left(W_o x_t + u_o \\circ h_{t-1} + b_o\\right)$$\n $$c_t = f_t \\circ c_{t-1} +\n i_t \\circ \\sigma_c\\left(W_c x_t + u_c \\circ h_{t-1} + b_c\\right)$$\n\n where \\(\\circ\\) denotes the Hadamard operator. This means that each IndyLSTM\n node sees only its own state \\(h\\) and \\(c\\), as opposed to seeing all\n states in the same layer.\n\n We add forget_bias (default: 1) to the biases of the forget gate in order to\n reduce the scale of forgetting in the beginning of the training.\n\n It does not allow cell clipping, a projection layer, and does not\n use peep-hole connections: it is the basic baseline.\n\n For advanced models, please use the full `tf.nn.rnn_cell.LSTMCell`\n that follows.\n\n TODO(gonnet): Write a paper describing this and add a reference here.\n \"\"\"\n\n def __init__(self,\n num_units,\n forget_bias=1.0,\n activation=None,\n reuse=None,\n kernel_initializer=None,\n bias_initializer=None,\n name=None,\n dtype=None):\n \"\"\"Initialize the IndyLSTM cell.\n\n Args:\n num_units: int, The number of units in the LSTM cell.\n forget_bias: float, The bias added to forget gates (see above).\n Must set to `0.0` manually when restoring from CudnnLSTM-trained\n checkpoints.\n activation: Activation function of the inner states. Default: `tanh`.\n reuse: (optional) Python boolean describing whether to reuse variables\n in an existing scope. If not `True`, and the existing scope already has\n the given variables, an error is raised.\n kernel_initializer: (optional) The initializer to use for the weight\n matrix applied to the inputs.\n bias_initializer: (optional) The initializer to use for the bias.\n name: String, the name of the layer. Layers with the same name will\n share weights, but to avoid mistakes we require reuse=True in such\n cases.\n dtype: Default dtype of the layer (default of `None` means use the type\n of the first input). Required when `build` is called before `call`.\n \"\"\"\n super(IndyLSTMCell, self).__init__(_reuse=reuse, name=name, dtype=dtype)\n\n # Inputs must be 2-dimensional.\n self.input_spec = base_layer.InputSpec(ndim=2)\n\n self._num_units = num_units\n self._forget_bias = forget_bias\n self._activation = activation or math_ops.tanh\n self._kernel_initializer = kernel_initializer\n self._bias_initializer = bias_initializer\n\n @property\n def state_size(self):\n return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)\n\n @property\n def output_size(self):\n return self._num_units\n\n def build(self, inputs_shape):\n if inputs_shape[1].value is None:\n raise ValueError(\n \"Expected inputs.shape[-1] to be known, saw shape: %s\" % inputs_shape)\n\n input_depth = inputs_shape[1].value\n # pylint: disable=protected-access\n self._kernel_w = self.add_variable(\n \"%s_w\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[input_depth, 4 * self._num_units],\n initializer=self._kernel_initializer)\n self._kernel_u = self.add_variable(\n \"%s_u\" % rnn_cell_impl._WEIGHTS_VARIABLE_NAME,\n shape=[1, 4 * self._num_units],\n initializer=init_ops.random_uniform_initializer(\n minval=-1, maxval=1, dtype=self.dtype))\n self._bias = self.add_variable(\n rnn_cell_impl._BIAS_VARIABLE_NAME,\n shape=[4 * self._num_units],\n initializer=(self._bias_initializer\n if self._bias_initializer is not None else\n init_ops.zeros_initializer(dtype=self.dtype)))\n # pylint: enable=protected-access\n\n self.built = True\n\n def call(self, inputs, state):\n \"\"\"Independent Long short-term memory cell (IndyLSTM).\n\n Args:\n inputs: `2-D` tensor with shape `[batch_size, input_size]`.\n state: An `LSTMStateTuple` of state tensors, each shaped\n `[batch_size, num_units]`.\n\n Returns:\n A pair containing the new hidden state, and the new state (a\n `LSTMStateTuple`).\n \"\"\"\n sigmoid = math_ops.sigmoid\n one = constant_op.constant(1, dtype=dtypes.int32)\n c, h = state\n\n gate_inputs = math_ops.matmul(inputs, self._kernel_w)\n gate_inputs += gen_array_ops.tile(h, [1, 4]) * self._kernel_u\n gate_inputs = nn_ops.bias_add(gate_inputs, self._bias)\n\n # i = input_gate, j = new_input, f = forget_gate, o = output_gate\n i, j, f, o = array_ops.split(\n value=gate_inputs, num_or_size_splits=4, axis=one)\n\n forget_bias_tensor = constant_op.constant(self._forget_bias, dtype=f.dtype)\n # Note that using `add` and `multiply` instead of `+` and `*` gives a\n # performance improvement. So using those at the cost of readability.\n add = math_ops.add\n multiply = math_ops.multiply\n new_c = add(\n multiply(c, sigmoid(add(f, forget_bias_tensor))),\n multiply(sigmoid(i), self._activation(j)))\n new_h = multiply(self._activation(new_c), sigmoid(o))\n\n new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h)\n return new_h, new_state\n", "# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Tests for tensorflow.ops.image_ops.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport colorsys\nimport functools\nimport itertools\nimport math\nimport os\nimport time\n\nimport numpy as np\nfrom six.moves import xrange # pylint: disable=redefined-builtin\n\nfrom tensorflow.core.protobuf import config_pb2\nfrom tensorflow.python.client import session\nfrom tensorflow.python.framework import constant_op\nfrom tensorflow.python.framework import dtypes\nfrom tensorflow.python.framework import errors\nfrom tensorflow.python.framework import ops\nfrom tensorflow.python.framework import test_util\nfrom tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import control_flow_ops\nfrom tensorflow.python.ops import gen_image_ops\nfrom tensorflow.python.ops import gradients\nfrom tensorflow.python.ops import image_ops\nfrom tensorflow.python.ops import image_ops_impl\nfrom tensorflow.python.ops import io_ops\nfrom tensorflow.python.ops import math_ops\nfrom tensorflow.python.ops import random_ops\nfrom tensorflow.python.ops import variables\nfrom tensorflow.python.platform import googletest\nfrom tensorflow.python.platform import test\n\n\nclass RGBToHSVTest(test_util.TensorFlowTestCase):\n\n def testBatch(self):\n # Build an arbitrary RGB image\n np.random.seed(7)\n batch_size = 5\n shape = (batch_size, 2, 7, 3)\n\n for nptype in [np.float32, np.float64]:\n inp = np.random.rand(*shape).astype(nptype)\n\n # Convert to HSV and back, as a batch and individually\n with self.test_session(use_gpu=True) as sess:\n batch0 = constant_op.constant(inp)\n batch1 = image_ops.rgb_to_hsv(batch0)\n batch2 = image_ops.hsv_to_rgb(batch1)\n split0 = array_ops.unstack(batch0)\n split1 = list(map(image_ops.rgb_to_hsv, split0))\n split2 = list(map(image_ops.hsv_to_rgb, split1))\n join1 = array_ops.stack(split1)\n join2 = array_ops.stack(split2)\n batch1, batch2, join1, join2 = sess.run([batch1, batch2, join1, join2])\n\n # Verify that processing batch elements together is the same as separate\n self.assertAllClose(batch1, join1)\n self.assertAllClose(batch2, join2)\n self.assertAllClose(batch2, inp)\n\n def testRGBToHSVRoundTrip(self):\n data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n for nptype in [np.float32, np.float64]:\n rgb_np = np.array(data, dtype=nptype).reshape([2, 2, 3]) / 255.\n with self.test_session(use_gpu=True):\n hsv = image_ops.rgb_to_hsv(rgb_np)\n rgb = image_ops.hsv_to_rgb(hsv)\n rgb_tf = rgb.eval()\n self.assertAllClose(rgb_tf, rgb_np)\n\n\nclass RGBToYIQTest(test_util.TensorFlowTestCase):\n\n def testBatch(self):\n # Build an arbitrary RGB image\n np.random.seed(7)\n batch_size = 5\n shape = (batch_size, 2, 7, 3)\n\n for nptype in [np.float32, np.float64]:\n inp = np.random.rand(*shape).astype(nptype)\n\n # Convert to YIQ and back, as a batch and individually\n with self.test_session(use_gpu=True) as sess:\n batch0 = constant_op.constant(inp)\n batch1 = image_ops.rgb_to_yiq(batch0)\n batch2 = image_ops.yiq_to_rgb(batch1)\n split0 = array_ops.unstack(batch0)\n split1 = list(map(image_ops.rgb_to_yiq, split0))\n split2 = list(map(image_ops.yiq_to_rgb, split1))\n join1 = array_ops.stack(split1)\n join2 = array_ops.stack(split2)\n batch1, batch2, join1, join2 = sess.run([batch1, batch2, join1, join2])\n\n # Verify that processing batch elements together is the same as separate\n self.assertAllClose(batch1, join1, rtol=1e-4, atol=1e-4)\n self.assertAllClose(batch2, join2, rtol=1e-4, atol=1e-4)\n self.assertAllClose(batch2, inp, rtol=1e-4, atol=1e-4)\n\n\nclass RGBToYUVTest(test_util.TensorFlowTestCase):\n\n def testBatch(self):\n # Build an arbitrary RGB image\n np.random.seed(7)\n batch_size = 5\n shape = (batch_size, 2, 7, 3)\n\n for nptype in [np.float32, np.float64]:\n inp = np.random.rand(*shape).astype(nptype)\n\n # Convert to YUV and back, as a batch and individually\n with self.test_session(use_gpu=True) as sess:\n batch0 = constant_op.constant(inp)\n batch1 = image_ops.rgb_to_yuv(batch0)\n batch2 = image_ops.yuv_to_rgb(batch1)\n split0 = array_ops.unstack(batch0)\n split1 = list(map(image_ops.rgb_to_yuv, split0))\n split2 = list(map(image_ops.yuv_to_rgb, split1))\n join1 = array_ops.stack(split1)\n join2 = array_ops.stack(split2)\n batch1, batch2, join1, join2 = sess.run([batch1, batch2, join1, join2])\n\n # Verify that processing batch elements together is the same as separate\n self.assertAllClose(batch1, join1, rtol=1e-4, atol=1e-4)\n self.assertAllClose(batch2, join2, rtol=1e-4, atol=1e-4)\n self.assertAllClose(batch2, inp, rtol=1e-4, atol=1e-4)\n\n\nclass GrayscaleToRGBTest(test_util.TensorFlowTestCase):\n\n def _RGBToGrayscale(self, images):\n is_batch = True\n if len(images.shape) == 3:\n is_batch = False\n images = np.expand_dims(images, axis=0)\n out_shape = images.shape[0:3] + (1,)\n out = np.zeros(shape=out_shape, dtype=np.uint8)\n for batch in xrange(images.shape[0]):\n for y in xrange(images.shape[1]):\n for x in xrange(images.shape[2]):\n red = images[batch, y, x, 0]\n green = images[batch, y, x, 1]\n blue = images[batch, y, x, 2]\n gray = 0.2989 * red + 0.5870 * green + 0.1140 * blue\n out[batch, y, x, 0] = int(gray)\n if not is_batch:\n out = np.squeeze(out, axis=0)\n return out\n\n def _TestRGBToGrayscale(self, x_np):\n y_np = self._RGBToGrayscale(x_np)\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.rgb_to_grayscale(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testBasicRGBToGrayscale(self):\n # 4-D input with batch dimension.\n x_np = np.array(\n [[1, 2, 3], [4, 10, 1]], dtype=np.uint8).reshape([1, 1, 2, 3])\n self._TestRGBToGrayscale(x_np)\n\n # 3-D input with no batch dimension.\n x_np = np.array([[1, 2, 3], [4, 10, 1]], dtype=np.uint8).reshape([1, 2, 3])\n self._TestRGBToGrayscale(x_np)\n\n def testBasicGrayscaleToRGB(self):\n # 4-D input with batch dimension.\n x_np = np.array([[1, 2]], dtype=np.uint8).reshape([1, 1, 2, 1])\n y_np = np.array(\n [[1, 1, 1], [2, 2, 2]], dtype=np.uint8).reshape([1, 1, 2, 3])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.grayscale_to_rgb(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n # 3-D input with no batch dimension.\n x_np = np.array([[1, 2]], dtype=np.uint8).reshape([1, 2, 1])\n y_np = np.array([[1, 1, 1], [2, 2, 2]], dtype=np.uint8).reshape([1, 2, 3])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.grayscale_to_rgb(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testShapeInference(self):\n # Shape inference works and produces expected output where possible\n rgb_shape = [7, None, 19, 3]\n gray_shape = rgb_shape[:-1] + [1]\n with self.test_session(use_gpu=True):\n rgb_tf = array_ops.placeholder(dtypes.uint8, shape=rgb_shape)\n gray = image_ops.rgb_to_grayscale(rgb_tf)\n self.assertEqual(gray_shape, gray.get_shape().as_list())\n\n with self.test_session(use_gpu=True):\n gray_tf = array_ops.placeholder(dtypes.uint8, shape=gray_shape)\n rgb = image_ops.grayscale_to_rgb(gray_tf)\n self.assertEqual(rgb_shape, rgb.get_shape().as_list())\n\n # Shape inference does not break for unknown shapes\n with self.test_session(use_gpu=True):\n rgb_tf_unknown = array_ops.placeholder(dtypes.uint8)\n gray_unknown = image_ops.rgb_to_grayscale(rgb_tf_unknown)\n self.assertFalse(gray_unknown.get_shape())\n\n with self.test_session(use_gpu=True):\n gray_tf_unknown = array_ops.placeholder(dtypes.uint8)\n rgb_unknown = image_ops.grayscale_to_rgb(gray_tf_unknown)\n self.assertFalse(rgb_unknown.get_shape())\n\n\nclass AdjustGamma(test_util.TensorFlowTestCase):\n\n def test_adjust_gamma_one(self):\n \"\"\"Same image should be returned for gamma equal to one\"\"\"\n with self.cached_session():\n x_data = np.random.uniform(0, 255, (8, 8))\n x_np = np.array(x_data, dtype=np.float32)\n\n x = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.adjust_gamma(x, gamma=1)\n\n y_tf = y.eval()\n y_np = x_np\n\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n def test_adjust_gamma_less_zero(self):\n \"\"\"White image should be returned for gamma equal to zero\"\"\"\n with self.cached_session():\n x_data = np.random.uniform(0, 255, (8, 8))\n x_np = np.array(x_data, dtype=np.float32)\n\n x = constant_op.constant(x_np, shape=x_np.shape)\n\n err_msg = \"Gamma should be a non-negative real number.\"\n\n try:\n image_ops.adjust_gamma(x, gamma=-1)\n except Exception as e:\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def test_adjust_gamma_less_zero_tensor(self):\n \"\"\"White image should be returned for gamma equal to zero\"\"\"\n with self.cached_session():\n x_data = np.random.uniform(0, 255, (8, 8))\n x_np = np.array(x_data, dtype=np.float32)\n\n x = constant_op.constant(x_np, shape=x_np.shape)\n y = constant_op.constant(-1.0, dtype=dtypes.float32)\n\n image = image_ops.adjust_gamma(x, gamma=y)\n\n err_msg = \"Gamma should be a non-negative real number.\"\n try:\n image.eval()\n except Exception as e:\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def test_adjust_gamma_zero(self):\n \"\"\"White image should be returned for gamma equal to zero\"\"\"\n with self.cached_session():\n x_data = np.random.uniform(0, 255, (8, 8))\n x_np = np.array(x_data, dtype=np.float32)\n\n x = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.adjust_gamma(x, gamma=0)\n\n y_tf = y.eval()\n\n dtype = x.dtype.as_numpy_dtype\n y_np = np.array([dtypes.dtype_range[dtype][1]] * x_np.size)\n y_np = y_np.reshape((8, 8))\n\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n def test_adjust_gamma_less_one(self):\n \"\"\"Verifying the output with expected results for gamma\n correction with gamma equal to half\"\"\"\n with self.cached_session():\n x_np = np.arange(0, 255, 4, np.uint8).reshape(8, 8)\n y = image_ops.adjust_gamma(x_np, gamma=0.5)\n y_tf = np.trunc(y.eval())\n\n y_np = np.array(\n [[0, 31, 45, 55, 63, 71, 78, 84], [\n 90, 95, 100, 105, 110, 115, 119, 123\n ], [127, 131, 135, 139, 142, 146, 149, 153], [\n 156, 159, 162, 165, 168, 171, 174, 177\n ], [180, 183, 186, 188, 191, 194, 196, 199], [\n 201, 204, 206, 209, 211, 214, 216, 218\n ], [221, 223, 225, 228, 230, 232, 234, 236],\n [238, 241, 243, 245, 247, 249, 251, 253]],\n dtype=np.float32)\n\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n def test_adjust_gamma_greater_one(self):\n \"\"\"Verifying the output with expected results for gamma\n correction with gamma equal to two\"\"\"\n with self.cached_session():\n x_np = np.arange(0, 255, 4, np.uint8).reshape(8, 8)\n y = image_ops.adjust_gamma(x_np, gamma=2)\n y_tf = np.trunc(y.eval())\n\n y_np = np.array(\n [[0, 0, 0, 0, 1, 1, 2, 3], [4, 5, 6, 7, 9, 10, 12, 14], [\n 16, 18, 20, 22, 25, 27, 30, 33\n ], [36, 39, 42, 45, 49, 52, 56, 60], [64, 68, 72, 76, 81, 85, 90, 95],\n [100, 105, 110, 116, 121, 127, 132, 138], [\n 144, 150, 156, 163, 169, 176, 182, 189\n ], [196, 203, 211, 218, 225, 233, 241, 249]],\n dtype=np.float32)\n\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n\nclass AdjustHueTest(test_util.TensorFlowTestCase):\n\n def testAdjustNegativeHue(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n delta = -0.25\n y_data = [0, 13, 1, 54, 226, 59, 8, 234, 150, 255, 39, 1]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_hue(x, delta)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testAdjustPositiveHue(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n delta = 0.25\n y_data = [13, 0, 11, 226, 54, 221, 234, 8, 92, 1, 217, 255]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_hue(x, delta)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testBatchAdjustHue(self):\n x_shape = [2, 1, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n delta = 0.25\n y_data = [13, 0, 11, 226, 54, 221, 234, 8, 92, 1, 217, 255]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_hue(x, delta)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def _adjustHueNp(self, x_np, delta_h):\n self.assertEqual(x_np.shape[-1], 3)\n x_v = x_np.reshape([-1, 3])\n y_v = np.ndarray(x_v.shape, dtype=x_v.dtype)\n channel_count = x_v.shape[0]\n for i in xrange(channel_count):\n r = x_v[i][0]\n g = x_v[i][1]\n b = x_v[i][2]\n h, s, v = colorsys.rgb_to_hsv(r, g, b)\n h += delta_h\n h = math.fmod(h + 10.0, 1.0)\n r, g, b = colorsys.hsv_to_rgb(h, s, v)\n y_v[i][0] = r\n y_v[i][1] = g\n y_v[i][2] = b\n return y_v.reshape(x_np.shape)\n\n def _adjustHueTf(self, x_np, delta_h):\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np)\n y = image_ops.adjust_hue(x, delta_h)\n y_tf = y.eval()\n return y_tf\n\n def testAdjustRandomHue(self):\n x_shapes = [\n [2, 2, 3],\n [4, 2, 3],\n [2, 4, 3],\n [2, 5, 3],\n [1000, 1, 3],\n ]\n test_styles = [\n \"all_random\",\n \"rg_same\",\n \"rb_same\",\n \"gb_same\",\n \"rgb_same\",\n ]\n for x_shape in x_shapes:\n for test_style in test_styles:\n x_np = np.random.rand(*x_shape) * 255.\n delta_h = np.random.rand() * 2.0 - 1.0\n if test_style == \"all_random\":\n pass\n elif test_style == \"rg_same\":\n x_np[..., 1] = x_np[..., 0]\n elif test_style == \"rb_same\":\n x_np[..., 2] = x_np[..., 0]\n elif test_style == \"gb_same\":\n x_np[..., 2] = x_np[..., 1]\n elif test_style == \"rgb_same\":\n x_np[..., 1] = x_np[..., 0]\n x_np[..., 2] = x_np[..., 0]\n else:\n raise AssertionError(\"Invalid test style: %s\" % (test_style))\n y_np = self._adjustHueNp(x_np, delta_h)\n y_tf = self._adjustHueTf(x_np, delta_h)\n self.assertAllClose(y_tf, y_np, rtol=2e-5, atol=1e-5)\n\n def testInvalidShapes(self):\n fused = False\n if not fused:\n # The tests are known to pass with the fused adjust_hue. We will enable\n # them when the fused implementation is the default.\n return\n x_np = np.random.rand(2, 3) * 255.\n delta_h = np.random.rand() * 2.0 - 1.0\n fused = False\n with self.assertRaisesRegexp(ValueError, \"Shape must be at least rank 3\"):\n self._adjustHueTf(x_np, delta_h)\n x_np = np.random.rand(4, 2, 4) * 255.\n delta_h = np.random.rand() * 2.0 - 1.0\n with self.assertRaisesOpError(\"input must have 3 channels\"):\n self._adjustHueTf(x_np, delta_h)\n\n\nclass FlipImageBenchmark(test.Benchmark):\n\n def _benchmarkFlipLeftRight(self, device, cpu_count):\n image_shape = [299, 299, 3]\n warmup_rounds = 100\n benchmark_rounds = 1000\n config = config_pb2.ConfigProto()\n if cpu_count is not None:\n config.inter_op_parallelism_threads = 1\n config.intra_op_parallelism_threads = cpu_count\n with session.Session(\"\", graph=ops.Graph(), config=config) as sess:\n with ops.device(device):\n inputs = variables.Variable(\n random_ops.random_uniform(image_shape, dtype=dtypes.float32) * 255,\n trainable=False,\n dtype=dtypes.float32)\n run_op = image_ops.flip_left_right(inputs)\n sess.run(variables.global_variables_initializer())\n for i in xrange(warmup_rounds + benchmark_rounds):\n if i == warmup_rounds:\n start = time.time()\n sess.run(run_op)\n end = time.time()\n step_time = (end - start) / benchmark_rounds\n tag = device + \"_%s\" % (cpu_count if cpu_count is not None else \"_all\")\n print(\"benchmarkFlipLeftRight_299_299_3_%s step_time: %.2f us\" %\n (tag, step_time * 1e6))\n self.report_benchmark(\n name=\"benchmarkFlipLeftRight_299_299_3_%s\" % (tag),\n iters=benchmark_rounds,\n wall_time=step_time)\n\n def _benchmarkRandomFlipLeftRight(self, device, cpu_count):\n image_shape = [299, 299, 3]\n warmup_rounds = 100\n benchmark_rounds = 1000\n config = config_pb2.ConfigProto()\n if cpu_count is not None:\n config.inter_op_parallelism_threads = 1\n config.intra_op_parallelism_threads = cpu_count\n with session.Session(\"\", graph=ops.Graph(), config=config) as sess:\n with ops.device(device):\n inputs = variables.Variable(\n random_ops.random_uniform(image_shape, dtype=dtypes.float32) * 255,\n trainable=False,\n dtype=dtypes.float32)\n run_op = image_ops.random_flip_left_right(inputs)\n sess.run(variables.global_variables_initializer())\n for i in xrange(warmup_rounds + benchmark_rounds):\n if i == warmup_rounds:\n start = time.time()\n sess.run(run_op)\n end = time.time()\n step_time = (end - start) / benchmark_rounds\n tag = device + \"_%s\" % (cpu_count if cpu_count is not None else \"_all\")\n print(\"benchmarkRandomFlipLeftRight_299_299_3_%s step_time: %.2f us\" %\n (tag, step_time * 1e6))\n self.report_benchmark(\n name=\"benchmarkRandomFlipLeftRight_299_299_3_%s\" % (tag),\n iters=benchmark_rounds,\n wall_time=step_time)\n\n def _benchmarkBatchedRandomFlipLeftRight(self, device, cpu_count):\n image_shape = [16, 299, 299, 3]\n warmup_rounds = 100\n benchmark_rounds = 1000\n config = config_pb2.ConfigProto()\n if cpu_count is not None:\n config.inter_op_parallelism_threads = 1\n config.intra_op_parallelism_threads = cpu_count\n with session.Session(\"\", graph=ops.Graph(), config=config) as sess:\n with ops.device(device):\n inputs = variables.Variable(\n random_ops.random_uniform(image_shape, dtype=dtypes.float32) * 255,\n trainable=False,\n dtype=dtypes.float32)\n run_op = image_ops.random_flip_left_right(inputs)\n sess.run(variables.global_variables_initializer())\n for i in xrange(warmup_rounds + benchmark_rounds):\n if i == warmup_rounds:\n start = time.time()\n sess.run(run_op)\n end = time.time()\n step_time = (end - start) / benchmark_rounds\n tag = device + \"_%s\" % (cpu_count if cpu_count is not None else \"_all\")\n print(\"benchmarkBatchedRandomFlipLeftRight_16_299_299_3_%s step_time: \"\n \"%.2f us\" %\n (tag, step_time * 1e6))\n self.report_benchmark(\n name=\"benchmarkBatchedRandomFlipLeftRight_16_299_299_3_%s\" % (tag),\n iters=benchmark_rounds,\n wall_time=step_time)\n\n def benchmarkFlipLeftRightCpu1(self):\n self._benchmarkFlipLeftRight(\"/cpu:0\", 1)\n\n def benchmarkFlipLeftRightCpuAll(self):\n self._benchmarkFlipLeftRight(\"/cpu:0\", None)\n\n def benchmarkFlipLeftRightGpu(self):\n self._benchmarkFlipLeftRight(test.gpu_device_name(), None)\n\n def benchmarkRandomFlipLeftRightCpu1(self):\n self._benchmarkRandomFlipLeftRight(\"/cpu:0\", 1)\n\n def benchmarkRandomFlipLeftRightCpuAll(self):\n self._benchmarkRandomFlipLeftRight(\"/cpu:0\", None)\n\n def benchmarkRandomFlipLeftRightGpu(self):\n self._benchmarkRandomFlipLeftRight(test.gpu_device_name(), None)\n\n def benchmarkBatchedRandomFlipLeftRightCpu1(self):\n self._benchmarkBatchedRandomFlipLeftRight(\"/cpu:0\", 1)\n\n def benchmarkBatchedRandomFlipLeftRightCpuAll(self):\n self._benchmarkBatchedRandomFlipLeftRight(\"/cpu:0\", None)\n\n def benchmarkBatchedRandomFlipLeftRightGpu(self):\n self._benchmarkBatchedRandomFlipLeftRight(test.gpu_device_name(), None)\n\n\nclass AdjustHueBenchmark(test.Benchmark):\n\n def _benchmarkAdjustHue(self, device, cpu_count):\n image_shape = [299, 299, 3]\n warmup_rounds = 100\n benchmark_rounds = 1000\n config = config_pb2.ConfigProto()\n if cpu_count is not None:\n config.inter_op_parallelism_threads = 1\n config.intra_op_parallelism_threads = cpu_count\n with session.Session(\"\", graph=ops.Graph(), config=config) as sess:\n with ops.device(device):\n inputs = variables.Variable(\n random_ops.random_uniform(image_shape, dtype=dtypes.float32) * 255,\n trainable=False,\n dtype=dtypes.float32)\n delta = constant_op.constant(0.1, dtype=dtypes.float32)\n outputs = image_ops.adjust_hue(inputs, delta)\n run_op = control_flow_ops.group(outputs)\n sess.run(variables.global_variables_initializer())\n for i in xrange(warmup_rounds + benchmark_rounds):\n if i == warmup_rounds:\n start = time.time()\n sess.run(run_op)\n end = time.time()\n step_time = (end - start) / benchmark_rounds\n tag = device + \"_%s\" % (cpu_count if cpu_count is not None else \"_all\")\n print(\"benchmarkAdjustHue_299_299_3_%s step_time: %.2f us\" %\n (tag, step_time * 1e6))\n self.report_benchmark(\n name=\"benchmarkAdjustHue_299_299_3_%s\" % (tag),\n iters=benchmark_rounds,\n wall_time=step_time)\n\n def benchmarkAdjustHueCpu1(self):\n self._benchmarkAdjustHue(\"/cpu:0\", 1)\n\n def benchmarkAdjustHueCpuAll(self):\n self._benchmarkAdjustHue(\"/cpu:0\", None)\n\n def benchmarkAdjustHueGpu(self):\n self._benchmarkAdjustHue(test.gpu_device_name(), None)\n\n\nclass AdjustSaturationBenchmark(test.Benchmark):\n\n def _benchmarkAdjustSaturation(self, device, cpu_count):\n image_shape = [299, 299, 3]\n warmup_rounds = 100\n benchmark_rounds = 1000\n config = config_pb2.ConfigProto()\n if cpu_count is not None:\n config.inter_op_parallelism_threads = 1\n config.intra_op_parallelism_threads = cpu_count\n with session.Session(\"\", graph=ops.Graph(), config=config) as sess:\n with ops.device(device):\n inputs = variables.Variable(\n random_ops.random_uniform(image_shape, dtype=dtypes.float32) * 255,\n trainable=False,\n dtype=dtypes.float32)\n delta = constant_op.constant(0.1, dtype=dtypes.float32)\n outputs = image_ops.adjust_saturation(inputs, delta)\n run_op = control_flow_ops.group(outputs)\n sess.run(variables.global_variables_initializer())\n for _ in xrange(warmup_rounds):\n sess.run(run_op)\n start = time.time()\n for _ in xrange(benchmark_rounds):\n sess.run(run_op)\n end = time.time()\n step_time = (end - start) / benchmark_rounds\n tag = device + \"_%s\" % (cpu_count if cpu_count is not None else \"_all\")\n print(\"benchmarkAdjustSaturation_299_299_3_%s step_time: %.2f us\" %\n (tag, step_time * 1e6))\n self.report_benchmark(\n name=\"benchmarkAdjustSaturation_299_299_3_%s\" % (tag),\n iters=benchmark_rounds,\n wall_time=step_time)\n\n def benchmarkAdjustSaturationCpu1(self):\n self._benchmarkAdjustSaturation(\"/cpu:0\", 1)\n\n def benchmarkAdjustSaturationCpuAll(self):\n self._benchmarkAdjustSaturation(\"/cpu:0\", None)\n\n def benchmarkAdjustSaturationGpu(self):\n self._benchmarkAdjustSaturation(test.gpu_device_name(), None)\n\n\nclass ResizeBilinearBenchmark(test.Benchmark):\n\n def _benchmarkResize(self, image_size, num_channels):\n batch_size = 1\n num_ops = 1000\n img = variables.Variable(\n random_ops.random_normal(\n [batch_size, image_size[0], image_size[1], num_channels]),\n name=\"img\")\n\n deps = []\n for _ in xrange(num_ops):\n with ops.control_dependencies(deps):\n resize_op = image_ops.resize_bilinear(\n img, [299, 299], align_corners=False)\n deps = [resize_op]\n benchmark_op = control_flow_ops.group(*deps)\n\n with session.Session() as sess:\n sess.run(variables.global_variables_initializer())\n results = self.run_op_benchmark(\n sess,\n benchmark_op,\n name=(\"resize_bilinear_%s_%s_%s\" % (image_size[0], image_size[1],\n num_channels)))\n print(\"%s : %.2f ms/img\" %\n (results[\"name\"],\n 1000 * results[\"wall_time\"] / (batch_size * num_ops)))\n\n def benchmarkSimilar3Channel(self):\n self._benchmarkResize((183, 229), 3)\n\n def benchmarkScaleUp3Channel(self):\n self._benchmarkResize((141, 186), 3)\n\n def benchmarkScaleDown3Channel(self):\n self._benchmarkResize((749, 603), 3)\n\n def benchmarkSimilar1Channel(self):\n self._benchmarkResize((183, 229), 1)\n\n def benchmarkScaleUp1Channel(self):\n self._benchmarkResize((141, 186), 1)\n\n def benchmarkScaleDown1Channel(self):\n self._benchmarkResize((749, 603), 1)\n\n\nclass ResizeBicubicBenchmark(test.Benchmark):\n\n def _benchmarkResize(self, image_size, num_channels):\n batch_size = 1\n num_ops = 1000\n img = variables.Variable(\n random_ops.random_normal(\n [batch_size, image_size[0], image_size[1], num_channels]),\n name=\"img\")\n\n deps = []\n for _ in xrange(num_ops):\n with ops.control_dependencies(deps):\n resize_op = image_ops.resize_bicubic(\n img, [299, 299], align_corners=False)\n deps = [resize_op]\n benchmark_op = control_flow_ops.group(*deps)\n\n with session.Session() as sess:\n sess.run(variables.global_variables_initializer())\n results = self.run_op_benchmark(\n sess,\n benchmark_op,\n min_iters=20,\n name=(\"resize_bicubic_%s_%s_%s\" % (image_size[0], image_size[1],\n num_channels)))\n print(\"%s : %.2f ms/img\" %\n (results[\"name\"],\n 1000 * results[\"wall_time\"] / (batch_size * num_ops)))\n\n def benchmarkSimilar3Channel(self):\n self._benchmarkResize((183, 229), 3)\n\n def benchmarkScaleUp3Channel(self):\n self._benchmarkResize((141, 186), 3)\n\n def benchmarkScaleDown3Channel(self):\n self._benchmarkResize((749, 603), 3)\n\n def benchmarkSimilar1Channel(self):\n self._benchmarkResize((183, 229), 1)\n\n def benchmarkScaleUp1Channel(self):\n self._benchmarkResize((141, 186), 1)\n\n def benchmarkScaleDown1Channel(self):\n self._benchmarkResize((749, 603), 1)\n\n def benchmarkSimilar4Channel(self):\n self._benchmarkResize((183, 229), 4)\n\n def benchmarkScaleUp4Channel(self):\n self._benchmarkResize((141, 186), 4)\n\n def benchmarkScaleDown4Channel(self):\n self._benchmarkResize((749, 603), 4)\n\n\nclass ResizeAreaBenchmark(test.Benchmark):\n\n def _benchmarkResize(self, image_size, num_channels):\n batch_size = 1\n num_ops = 1000\n img = variables.Variable(\n random_ops.random_normal(\n [batch_size, image_size[0], image_size[1], num_channels]),\n name=\"img\")\n\n deps = []\n for _ in xrange(num_ops):\n with ops.control_dependencies(deps):\n resize_op = image_ops.resize_area(img, [299, 299], align_corners=False)\n deps = [resize_op]\n benchmark_op = control_flow_ops.group(*deps)\n\n with session.Session() as sess:\n sess.run(variables.global_variables_initializer())\n results = self.run_op_benchmark(\n sess,\n benchmark_op,\n name=(\"resize_area_%s_%s_%s\" % (image_size[0], image_size[1],\n num_channels)))\n print(\"%s : %.2f ms/img\" %\n (results[\"name\"],\n 1000 * results[\"wall_time\"] / (batch_size * num_ops)))\n\n def benchmarkSimilar3Channel(self):\n self._benchmarkResize((183, 229), 3)\n\n def benchmarkScaleUp3Channel(self):\n self._benchmarkResize((141, 186), 3)\n\n def benchmarkScaleDown3Channel(self):\n self._benchmarkResize((749, 603), 3)\n\n def benchmarkSimilar1Channel(self):\n self._benchmarkResize((183, 229), 1)\n\n def benchmarkScaleUp1Channel(self):\n self._benchmarkResize((141, 186), 1)\n\n def benchmarkScaleDown1Channel(self):\n self._benchmarkResize((749, 603), 1)\n\n\nclass AdjustSaturationTest(test_util.TensorFlowTestCase):\n\n def testHalfSaturation(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n saturation_factor = 0.5\n y_data = [6, 9, 13, 140, 180, 226, 135, 121, 234, 172, 255, 128]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_saturation(x, saturation_factor)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testTwiceSaturation(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n saturation_factor = 2.0\n y_data = [0, 5, 13, 0, 106, 226, 30, 0, 234, 89, 255, 0]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_saturation(x, saturation_factor)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testBatchSaturation(self):\n x_shape = [2, 1, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n saturation_factor = 0.5\n y_data = [6, 9, 13, 140, 180, 226, 135, 121, 234, 172, 255, 128]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.adjust_saturation(x, saturation_factor)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def _adjust_saturation(self, image, saturation_factor):\n image = ops.convert_to_tensor(image, name=\"image\")\n orig_dtype = image.dtype\n flt_image = image_ops.convert_image_dtype(image, dtypes.float32)\n saturation_adjusted_image = gen_image_ops.adjust_saturation(\n flt_image, saturation_factor)\n return image_ops.convert_image_dtype(saturation_adjusted_image, orig_dtype)\n\n def testHalfSaturationFused(self):\n x_shape = [2, 2, 3]\n x_rgb_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_rgb_data, dtype=np.uint8).reshape(x_shape)\n\n saturation_factor = 0.5\n y_rgb_data = [6, 9, 13, 140, 180, 226, 135, 121, 234, 172, 255, 128]\n y_np = np.array(y_rgb_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = self._adjust_saturation(x, saturation_factor)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testTwiceSaturationFused(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n saturation_factor = 2.0\n y_data = [0, 5, 13, 0, 106, 226, 30, 0, 234, 89, 255, 0]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = self._adjust_saturation(x, saturation_factor)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def _adjustSaturationNp(self, x_np, scale):\n self.assertEqual(x_np.shape[-1], 3)\n x_v = x_np.reshape([-1, 3])\n y_v = np.ndarray(x_v.shape, dtype=x_v.dtype)\n channel_count = x_v.shape[0]\n for i in xrange(channel_count):\n r = x_v[i][0]\n g = x_v[i][1]\n b = x_v[i][2]\n h, s, v = colorsys.rgb_to_hsv(r, g, b)\n s *= scale\n s = min(1.0, max(0.0, s))\n r, g, b = colorsys.hsv_to_rgb(h, s, v)\n y_v[i][0] = r\n y_v[i][1] = g\n y_v[i][2] = b\n return y_v.reshape(x_np.shape)\n\n def testAdjustRandomSaturation(self):\n x_shapes = [\n [2, 2, 3],\n [4, 2, 3],\n [2, 4, 3],\n [2, 5, 3],\n [1000, 1, 3],\n ]\n test_styles = [\n \"all_random\",\n \"rg_same\",\n \"rb_same\",\n \"gb_same\",\n \"rgb_same\",\n ]\n with self.test_session(use_gpu=True):\n for x_shape in x_shapes:\n for test_style in test_styles:\n x_np = np.random.rand(*x_shape) * 255.\n scale = np.random.rand()\n if test_style == \"all_random\":\n pass\n elif test_style == \"rg_same\":\n x_np[..., 1] = x_np[..., 0]\n elif test_style == \"rb_same\":\n x_np[..., 2] = x_np[..., 0]\n elif test_style == \"gb_same\":\n x_np[..., 2] = x_np[..., 1]\n elif test_style == \"rgb_same\":\n x_np[..., 1] = x_np[..., 0]\n x_np[..., 2] = x_np[..., 0]\n else:\n raise AssertionError(\"Invalid test style: %s\" % (test_style))\n y_baseline = self._adjustSaturationNp(x_np, scale)\n y_fused = self._adjust_saturation(x_np, scale).eval()\n self.assertAllClose(y_fused, y_baseline, rtol=2e-5, atol=1e-5)\n\n\nclass FlipTransposeRotateTest(test_util.TensorFlowTestCase):\n\n def testInvolutionLeftRight(self):\n x_np = np.array([[1, 2, 3], [1, 2, 3]], dtype=np.uint8).reshape([2, 3, 1])\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_left_right(image_ops.flip_left_right(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testInvolutionLeftRightWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_left_right(image_ops.flip_left_right(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testLeftRight(self):\n x_np = np.array([[1, 2, 3], [1, 2, 3]], dtype=np.uint8).reshape([2, 3, 1])\n y_np = np.array([[3, 2, 1], [3, 2, 1]], dtype=np.uint8).reshape([2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_left_right(x_tf)\n self.assertTrue(y.op.name.startswith(\"flip_left_right\"))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testLeftRightWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n y_np = np.array(\n [[[3, 2, 1], [3, 2, 1]], [[3, 2, 1], [3, 2, 1]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_left_right(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testRandomFlipLeftRight(self):\n x_np = np.array([[1, 2, 3], [1, 2, 3]], dtype=np.uint8).reshape([2, 3, 1])\n y_np = np.array([[3, 2, 1], [3, 2, 1]], dtype=np.uint8).reshape([2, 3, 1])\n seed = 42\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.random_flip_left_right(x_tf, seed=seed)\n self.assertTrue(y.op.name.startswith(\"random_flip_left_right\"))\n\n count_flipped = 0\n count_unflipped = 0\n for _ in range(100):\n y_tf = y.eval()\n if y_tf[0][0] == 1:\n self.assertAllEqual(y_tf, x_np)\n count_unflipped += 1\n else:\n self.assertAllEqual(y_tf, y_np)\n count_flipped += 1\n\n # 100 trials\n # Mean: 50\n # Std Dev: ~5\n # Six Sigma: 50 - (5 * 6) = 20\n self.assertGreaterEqual(count_flipped, 20)\n self.assertGreaterEqual(count_unflipped, 20)\n\n def testRandomFlipLeftRightWithBatch(self):\n batch_size = 16\n seed = 42\n\n # create single item of test data\n x_np_raw = np.array(\n [[1, 2, 3], [1, 2, 3]], dtype=np.uint8\n ).reshape([1, 2, 3, 1])\n y_np_raw = np.array(\n [[3, 2, 1], [3, 2, 1]], dtype=np.uint8\n ).reshape([1, 2, 3, 1])\n\n # create batched test data\n x_np = np.vstack([x_np_raw for _ in range(batch_size)])\n y_np = np.vstack([y_np_raw for _ in range(batch_size)])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.random_flip_left_right(x_tf, seed=seed)\n self.assertTrue(y.op.name.startswith(\"random_flip_left_right\"))\n\n count_flipped = 0\n count_unflipped = 0\n for _ in range(100):\n y_tf = y.eval()\n\n # check every element of the batch\n for i in range(batch_size):\n if y_tf[i][0][0] == 1:\n self.assertAllEqual(y_tf[i], x_np[i])\n count_unflipped += 1\n else:\n self.assertAllEqual(y_tf[i], y_np[i])\n count_flipped += 1\n\n # 100 trials, each containing batch_size elements\n # Mean: 50 * batch_size\n # Std Dev: ~5 * sqrt(batch_size)\n # Six Sigma: 50 * batch_size - (5 * 6 * sqrt(batch_size))\n # = 50 * batch_size - 30 * sqrt(batch_size) = 800 - 30 * 4 = 680\n six_sigma = 50 * batch_size - 30 * np.sqrt(batch_size)\n self.assertGreaterEqual(count_flipped, six_sigma)\n self.assertGreaterEqual(count_unflipped, six_sigma)\n\n def testInvolutionUpDown(self):\n x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8).reshape([2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_up_down(image_ops.flip_up_down(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testInvolutionUpDownWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_up_down(image_ops.flip_up_down(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testUpDown(self):\n x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8).reshape([2, 3, 1])\n y_np = np.array([[4, 5, 6], [1, 2, 3]], dtype=np.uint8).reshape([2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_up_down(x_tf)\n self.assertTrue(y.op.name.startswith(\"flip_up_down\"))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testUpDownWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n y_np = np.array(\n [[[4, 5, 6], [1, 2, 3]], [[10, 11, 12], [7, 8, 9]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.flip_up_down(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testRandomFlipUpDown(self):\n x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8).reshape([2, 3, 1])\n y_np = np.array([[4, 5, 6], [1, 2, 3]], dtype=np.uint8).reshape([2, 3, 1])\n\n seed = 42\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.random_flip_up_down(x_tf, seed=seed)\n self.assertTrue(y.op.name.startswith(\"random_flip_up_down\"))\n count_flipped = 0\n count_unflipped = 0\n for _ in range(100):\n y_tf = y.eval()\n if y_tf[0][0] == 1:\n self.assertAllEqual(y_tf, x_np)\n count_unflipped += 1\n else:\n self.assertAllEqual(y_tf, y_np)\n count_flipped += 1\n\n # 100 trials\n # Mean: 50\n # Std Dev: ~5\n # Six Sigma: 50 - (5 * 6) = 20\n self.assertGreaterEqual(count_flipped, 20)\n self.assertGreaterEqual(count_unflipped, 20)\n\n def testRandomFlipUpDownWithBatch(self):\n batch_size = 16\n seed = 42\n\n # create single item of test data\n x_np_raw = np.array(\n [[1, 2, 3], [4, 5, 6]], dtype=np.uint8\n ).reshape([1, 2, 3, 1])\n y_np_raw = np.array(\n [[4, 5, 6], [1, 2, 3]], dtype=np.uint8\n ).reshape([1, 2, 3, 1])\n\n # create batched test data\n x_np = np.vstack([x_np_raw for _ in range(batch_size)])\n y_np = np.vstack([y_np_raw for _ in range(batch_size)])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.random_flip_up_down(x_tf, seed=seed)\n self.assertTrue(y.op.name.startswith(\"random_flip_up_down\"))\n\n count_flipped = 0\n count_unflipped = 0\n for _ in range(100):\n y_tf = y.eval()\n\n # check every element of the batch\n for i in range(batch_size):\n if y_tf[i][0][0] == 1:\n self.assertAllEqual(y_tf[i], x_np[i])\n count_unflipped += 1\n else:\n self.assertAllEqual(y_tf[i], y_np[i])\n count_flipped += 1\n\n # 100 trials, each containing batch_size elements\n # Mean: 50 * batch_size\n # Std Dev: ~5 * sqrt(batch_size)\n # Six Sigma: 50 * batch_size - (5 * 6 * sqrt(batch_size))\n # = 50 * batch_size - 30 * sqrt(batch_size) = 800 - 30 * 4 = 680\n six_sigma = 50 * batch_size - 30 * np.sqrt(batch_size)\n self.assertGreaterEqual(count_flipped, six_sigma)\n self.assertGreaterEqual(count_unflipped, six_sigma)\n\n def testInvolutionTranspose(self):\n x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8).reshape([2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.transpose_image(image_ops.transpose_image(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testInvolutionTransposeWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.transpose_image(image_ops.transpose_image(x_tf))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n\n def testTranspose(self):\n x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8).reshape([2, 3, 1])\n y_np = np.array([[1, 4], [2, 5], [3, 6]], dtype=np.uint8).reshape([3, 2, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.transpose_image(x_tf)\n self.assertTrue(y.op.name.startswith(\"transpose_image\"))\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testTransposeWithBatch(self):\n x_np = np.array(\n [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],\n dtype=np.uint8).reshape([2, 2, 3, 1])\n\n y_np = np.array(\n [[[1, 4], [2, 5], [3, 6]], [[7, 10], [8, 11], [9, 12]]],\n dtype=np.uint8).reshape([2, 3, 2, 1])\n\n with self.test_session(use_gpu=True):\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.transpose_image(x_tf)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n\n def testPartialShapes(self):\n p_unknown_rank = array_ops.placeholder(dtypes.uint8)\n p_unknown_dims_3 = array_ops.placeholder(\n dtypes.uint8, shape=[None, None, None])\n p_unknown_dims_4 = array_ops.placeholder(\n dtypes.uint8, shape=[None, None, None, None])\n p_unknown_width = array_ops.placeholder(dtypes.uint8, shape=[64, None, 3])\n p_unknown_batch = array_ops.placeholder(\n dtypes.uint8, shape=[None, 64, 64, 3])\n p_wrong_rank = array_ops.placeholder(dtypes.uint8, shape=[None, None])\n p_zero_dim = array_ops.placeholder(dtypes.uint8, shape=[64, 0, 3])\n\n #Ops that support 3D input\n for op in [\n image_ops.flip_left_right, image_ops.flip_up_down,\n image_ops.random_flip_left_right, image_ops.random_flip_up_down,\n image_ops.transpose_image, image_ops.rot90\n ]:\n transformed_unknown_rank = op(p_unknown_rank)\n self.assertEqual(3, transformed_unknown_rank.get_shape().ndims)\n transformed_unknown_dims_3 = op(p_unknown_dims_3)\n self.assertEqual(3, transformed_unknown_dims_3.get_shape().ndims)\n transformed_unknown_width = op(p_unknown_width)\n self.assertEqual(3, transformed_unknown_width.get_shape().ndims)\n\n with self.assertRaisesRegexp(ValueError, \"must be > 0\"):\n op(p_zero_dim)\n\n #Ops that support 4D input\n for op in [\n image_ops.flip_left_right, image_ops.flip_up_down,\n image_ops.random_flip_left_right, image_ops.random_flip_up_down,\n image_ops.transpose_image, image_ops.rot90\n ]:\n transformed_unknown_dims_4 = op(p_unknown_dims_4)\n self.assertEqual(4, transformed_unknown_dims_4.get_shape().ndims)\n transformed_unknown_batch = op(p_unknown_batch)\n self.assertEqual(4, transformed_unknown_batch.get_shape().ndims)\n with self.assertRaisesRegexp(ValueError,\n \"must be at least three-dimensional\"):\n op(p_wrong_rank)\n\n def testRot90GroupOrder(self):\n image = np.arange(24, dtype=np.uint8).reshape([2, 4, 3])\n with self.test_session(use_gpu=True):\n rotated = image\n for _ in xrange(4):\n rotated = image_ops.rot90(rotated)\n self.assertAllEqual(image, rotated.eval())\n\n def testRot90GroupOrderWithBatch(self):\n image = np.arange(48, dtype=np.uint8).reshape([2, 2, 4, 3])\n with self.test_session(use_gpu=True):\n rotated = image\n for _ in xrange(4):\n rotated = image_ops.rot90(rotated)\n self.assertAllEqual(image, rotated.eval())\n\n def testRot90NumpyEquivalence(self):\n image = np.arange(24, dtype=np.uint8).reshape([2, 4, 3])\n with self.test_session(use_gpu=True):\n k_placeholder = array_ops.placeholder(dtypes.int32, shape=[])\n y_tf = image_ops.rot90(image, k_placeholder)\n for k in xrange(4):\n y_np = np.rot90(image, k=k)\n self.assertAllEqual(y_np, y_tf.eval({k_placeholder: k}))\n\n def testRot90NumpyEquivalenceWithBatch(self):\n image = np.arange(48, dtype=np.uint8).reshape([2, 2, 4, 3])\n with self.test_session(use_gpu=True):\n k_placeholder = array_ops.placeholder(dtypes.int32, shape=[])\n y_tf = image_ops.rot90(image, k_placeholder)\n for k in xrange(4):\n y_np = np.rot90(image, k=k, axes=(1, 2))\n self.assertAllEqual(y_np, y_tf.eval({k_placeholder: k}))\n\nclass AdjustContrastTest(test_util.TensorFlowTestCase):\n\n def _testContrast(self, x_np, y_np, contrast_factor):\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.adjust_contrast(x, contrast_factor)\n y_tf = y.eval()\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n def testDoubleContrastUint8(self):\n x_shape = [1, 2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n y_data = [0, 0, 0, 62, 169, 255, 28, 0, 255, 135, 255, 0]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n self._testContrast(x_np, y_np, contrast_factor=2.0)\n\n def testDoubleContrastFloat(self):\n x_shape = [1, 2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.float).reshape(x_shape) / 255.\n\n y_data = [\n -45.25, -90.75, -92.5, 62.75, 169.25, 333.5, 28.75, -84.75, 349.5,\n 134.75, 409.25, -116.5\n ]\n y_np = np.array(y_data, dtype=np.float).reshape(x_shape) / 255.\n\n self._testContrast(x_np, y_np, contrast_factor=2.0)\n\n def testHalfContrastUint8(self):\n x_shape = [1, 2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n y_data = [22, 52, 65, 49, 118, 172, 41, 54, 176, 67, 178, 59]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n self._testContrast(x_np, y_np, contrast_factor=0.5)\n\n def testBatchDoubleContrast(self):\n x_shape = [2, 1, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n y_data = [0, 0, 0, 81, 200, 255, 10, 0, 255, 116, 255, 0]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n self._testContrast(x_np, y_np, contrast_factor=2.0)\n\n def _adjustContrastNp(self, x_np, contrast_factor):\n mean = np.mean(x_np, (1, 2), keepdims=True)\n y_np = mean + contrast_factor * (x_np - mean)\n return y_np\n\n def _adjustContrastTf(self, x_np, contrast_factor):\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np)\n y = image_ops.adjust_contrast(x, contrast_factor)\n y_tf = y.eval()\n return y_tf\n\n def testRandomContrast(self):\n x_shapes = [\n [1, 2, 2, 3],\n [2, 1, 2, 3],\n [1, 2, 2, 3],\n [2, 5, 5, 3],\n [2, 1, 1, 3],\n ]\n for x_shape in x_shapes:\n x_np = np.random.rand(*x_shape) * 255.\n contrast_factor = np.random.rand() * 2.0 + 0.1\n y_np = self._adjustContrastNp(x_np, contrast_factor)\n y_tf = self._adjustContrastTf(x_np, contrast_factor)\n self.assertAllClose(y_tf, y_np, rtol=1e-5, atol=1e-5)\n\n def testContrastFactorShape(self):\n x_shape = [1, 2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n with self.assertRaisesRegexp(\n ValueError, 'Shape must be rank 0 but is rank 1'):\n image_ops.adjust_contrast(x_np, [2.0])\n\n\nclass AdjustBrightnessTest(test_util.TensorFlowTestCase):\n\n def _testBrightness(self, x_np, y_np, delta):\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_np.shape)\n y = image_ops.adjust_brightness(x, delta)\n y_tf = y.eval()\n self.assertAllClose(y_tf, y_np, 1e-6)\n\n def testPositiveDeltaUint8(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n y_data = [10, 15, 23, 64, 145, 236, 47, 18, 244, 100, 255, 11]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n self._testBrightness(x_np, y_np, delta=10. / 255.)\n\n def testPositiveDeltaFloat(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.float32).reshape(x_shape) / 255.\n\n y_data = [10, 15, 23, 64, 145, 236, 47, 18, 244, 100, 265, 11]\n y_np = np.array(y_data, dtype=np.float32).reshape(x_shape) / 255.\n\n self._testBrightness(x_np, y_np, delta=10. / 255.)\n\n def testNegativeDelta(self):\n x_shape = [2, 2, 3]\n x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]\n x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)\n\n y_data = [0, 0, 3, 44, 125, 216, 27, 0, 224, 80, 245, 0]\n y_np = np.array(y_data, dtype=np.uint8).reshape(x_shape)\n\n self._testBrightness(x_np, y_np, delta=-10. / 255.)\n\n\nclass PerImageWhiteningTest(test_util.TensorFlowTestCase):\n\n def _NumpyPerImageWhitening(self, x):\n num_pixels = np.prod(x.shape)\n x2 = np.square(x).astype(np.float32)\n mn = np.mean(x)\n vr = np.mean(x2) - (mn * mn)\n stddev = max(math.sqrt(vr), 1.0 / math.sqrt(num_pixels))\n\n y = x.astype(np.float32)\n y -= mn\n y /= stddev\n return y\n\n def testBasic(self):\n x_shape = [13, 9, 3]\n x_np = np.arange(0, np.prod(x_shape), dtype=np.int32).reshape(x_shape)\n y_np = self._NumpyPerImageWhitening(x_np)\n\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.per_image_standardization(x)\n self.assertTrue(y.op.name.startswith(\"per_image_standardization\"))\n y_tf = y.eval()\n self.assertAllClose(y_tf, y_np, atol=1e-4)\n\n def testUniformImage(self):\n im_np = np.ones([19, 19, 3]).astype(np.float32) * 249\n im = constant_op.constant(im_np)\n whiten = image_ops.per_image_standardization(im)\n with self.test_session(use_gpu=True):\n whiten_np = whiten.eval()\n self.assertFalse(np.any(np.isnan(whiten_np)))\n\n\nclass CropToBoundingBoxTest(test_util.TensorFlowTestCase):\n\n def _CropToBoundingBox(self, x, offset_height, offset_width, target_height,\n target_width, use_tensor_inputs):\n if use_tensor_inputs:\n offset_height = ops.convert_to_tensor(offset_height)\n offset_width = ops.convert_to_tensor(offset_width)\n target_height = ops.convert_to_tensor(target_height)\n target_width = ops.convert_to_tensor(target_width)\n x_tensor = array_ops.placeholder(x.dtype, shape=[None] * x.ndim)\n feed_dict = {x_tensor: x}\n else:\n x_tensor = x\n feed_dict = {}\n\n y = image_ops.crop_to_bounding_box(x_tensor, offset_height, offset_width,\n target_height, target_width)\n if not use_tensor_inputs:\n self.assertTrue(y.get_shape().is_fully_defined())\n\n with self.test_session(use_gpu=True):\n return y.eval(feed_dict=feed_dict)\n\n def _assertReturns(self,\n x,\n x_shape,\n offset_height,\n offset_width,\n y,\n y_shape,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width, _ = y_shape\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._CropToBoundingBox(x, offset_height, offset_width,\n target_height, target_width,\n use_tensor_inputs)\n self.assertAllClose(y, y_tf)\n\n def _assertRaises(self,\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n err_msg,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n x = np.array(x).reshape(x_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n try:\n self._CropToBoundingBox(x, offset_height, offset_width, target_height,\n target_width, use_tensor_inputs)\n except Exception as e:\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def _assertShapeInference(self, pre_shape, height, width, post_shape):\n image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.crop_to_bounding_box(image, 0, 0, height, width)\n self.assertEqual(y.get_shape().as_list(), post_shape)\n\n def testNoOp(self):\n x_shape = [10, 10, 10]\n x = np.random.uniform(size=x_shape)\n self._assertReturns(x, x_shape, 0, 0, x, x_shape)\n\n def testCrop(self):\n x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n x_shape = [3, 3, 1]\n\n offset_height, offset_width = [1, 0]\n y_shape = [2, 3, 1]\n y = [4, 5, 6, 7, 8, 9]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 1]\n y_shape = [3, 2, 1]\n y = [2, 3, 5, 6, 8, 9]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 0]\n y_shape = [2, 3, 1]\n y = [1, 2, 3, 4, 5, 6]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 0]\n y_shape = [3, 2, 1]\n y = [1, 2, 4, 5, 7, 8]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n def testShapeInference(self):\n self._assertShapeInference([55, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([59, 69, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 69, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([59, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, 66, None], 55, 66, [55, 66, None])\n self._assertShapeInference([59, 69, None], 55, 66, [55, 66, None])\n self._assertShapeInference([None, None, None], 55, 66, [55, 66, None])\n self._assertShapeInference(None, 55, 66, [55, 66, None])\n\n def testNon3DInput(self):\n # Input image is not 3D\n x = [0] * 15\n offset_height, offset_width = [0, 0]\n target_height, target_width = [2, 2]\n\n for x_shape in ([3, 5], [1, 3, 5, 1, 1]):\n self._assertRaises(x, x_shape, offset_height, offset_width, target_height,\n target_width,\n \"'image' must have either 3 or 4 dimensions.\")\n\n def testZeroLengthInput(self):\n # Input image has 0-length dimension(s).\n # Each line is a test configuration:\n # x_shape, target_height, target_width\n test_config = (([0, 2, 2], 1, 1), ([2, 0, 2], 1, 1), ([2, 2, 0], 1, 1),\n ([0, 2, 2], 0, 1), ([2, 0, 2], 1, 0))\n offset_height, offset_width = [0, 0]\n x = []\n\n for x_shape, target_height, target_width in test_config:\n self._assertRaises(\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n \"all dims of 'image.shape' must be > 0\",\n use_tensor_inputs_options=[False])\n # Multiple assertion could fail, but the evaluation order is arbitrary.\n # Match gainst generic pattern.\n self._assertRaises(\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n \"assertion failed:\",\n use_tensor_inputs_options=[True])\n\n def testBadParams(self):\n x_shape = [4, 4, 1]\n x = np.zeros(x_shape)\n\n # Each line is a test configuration:\n # (offset_height, offset_width, target_height, target_width), err_msg\n test_config = (([-1, 0, 3, 3], \"offset_height must be >= 0\"), ([\n 0, -1, 3, 3\n ], \"offset_width must be >= 0\"), ([0, 0, 0, 3],\n \"target_height must be > 0\"),\n ([0, 0, 3, 0], \"target_width must be > 0\"),\n ([2, 0, 3, 3], \"height must be >= target + offset\"),\n ([0, 2, 3, 3], \"width must be >= target + offset\"))\n\n for params, err_msg in test_config:\n self._assertRaises(x, x_shape, *params, err_msg=err_msg)\n\n def testNameScope(self):\n image = array_ops.placeholder(dtypes.float32, shape=[55, 66, 3])\n y = image_ops.crop_to_bounding_box(image, 0, 0, 55, 66)\n self.assertTrue(y.name.startswith(\"crop_to_bounding_box\"))\n\n\nclass CentralCropTest(test_util.TensorFlowTestCase):\n\n def _assertShapeInference(self, pre_shape, fraction, post_shape):\n image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.central_crop(image, fraction)\n if post_shape is None:\n self.assertEqual(y.get_shape().dims, None)\n else:\n self.assertEqual(y.get_shape().as_list(), post_shape)\n\n def testNoOp(self):\n x_shapes = [[13, 9, 3], [5, 13, 9, 3]]\n for x_shape in x_shapes:\n x_np = np.ones(x_shape, dtype=np.float32)\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.central_crop(x, 1.0)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, x_np)\n self.assertEqual(y.op.name, x.op.name)\n\n def testCropping(self):\n x_shape = [4, 8, 1]\n x_np = np.array(\n [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],\n [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8]],\n dtype=np.int32).reshape(x_shape)\n y_np = np.array([[3, 4, 5, 6], [3, 4, 5, 6]]).reshape([2, 4, 1])\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.central_crop(x, 0.5)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n self.assertAllEqual(y_tf.shape, y_np.shape)\n\n x_shape = [2, 4, 8, 1]\n x_np = np.array(\n [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],\n [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],\n [8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1],\n [8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1]],\n dtype=np.int32).reshape(x_shape)\n y_np = np.array([[[3, 4, 5, 6], [3, 4, 5, 6]],\n [[6, 5, 4, 3], [6, 5, 4, 3]]]).reshape([2, 2, 4, 1])\n with self.test_session(use_gpu=True):\n x = constant_op.constant(x_np, shape=x_shape)\n y = image_ops.central_crop(x, 0.5)\n y_tf = y.eval()\n self.assertAllEqual(y_tf, y_np)\n self.assertAllEqual(y_tf.shape, y_np.shape)\n\n def testCropping2(self):\n # Test case for 10315\n x_shapes = [[240, 320, 3], [5, 240, 320, 3]]\n expected_y_shapes = [[80, 106, 3], [5, 80, 106, 3]]\n\n for x_shape, y_shape in zip(x_shapes, expected_y_shapes):\n x_np = np.zeros(x_shape, dtype=np.int32)\n y_np = np.zeros(y_shape, dtype=np.int32)\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n x = array_ops.placeholder(shape=x_shape, dtype=dtypes.int32)\n y = image_ops.central_crop(x, 0.33)\n y_tf = y.eval(feed_dict={x: x_np})\n self.assertAllEqual(y_tf, y_np)\n self.assertAllEqual(y_tf.shape, y_np.shape)\n\n def testShapeInference(self):\n # Test no-op fraction=1.0, with 3-D tensors.\n self._assertShapeInference([50, 60, 3], 1.0, [50, 60, 3])\n self._assertShapeInference([None, 60, 3], 1.0, [None, 60, 3])\n self._assertShapeInference([50, None, 3], 1.0, [50, None, 3])\n self._assertShapeInference([None, None, 3], 1.0, [None, None, 3])\n self._assertShapeInference([50, 60, None], 1.0, [50, 60, None])\n self._assertShapeInference([None, None, None], 1.0, [None, None, None])\n\n # Test no-op fraction=0.5, with 3-D tensors.\n self._assertShapeInference([50, 60, 3], 0.5, [26, 30, 3])\n self._assertShapeInference([None, 60, 3], 0.5, [None, 30, 3])\n self._assertShapeInference([50, None, 3], 0.5, [26, None, 3])\n self._assertShapeInference([None, None, 3], 0.5, [None, None, 3])\n self._assertShapeInference([50, 60, None], 0.5, [26, 30, None])\n self._assertShapeInference([None, None, None], 0.5, [None, None, None])\n\n # Test no-op fraction=1.0, with 4-D tensors.\n self._assertShapeInference([5, 50, 60, 3], 1.0, [5, 50, 60, 3])\n self._assertShapeInference([5, None, 60, 3], 1.0, [5, None, 60, 3])\n self._assertShapeInference([5, 50, None, 3], 1.0, [5, 50, None, 3])\n self._assertShapeInference([5, None, None, 3], 1.0, [5, None, None, 3])\n self._assertShapeInference([5, 50, 60, None], 1.0, [5, 50, 60, None])\n self._assertShapeInference([5, None, None, None], 1.0,\n [5, None, None, None])\n self._assertShapeInference([None, None, None, None], 1.0,\n [None, None, None, None])\n\n # Test no-op fraction=0.5, with 4-D tensors.\n self._assertShapeInference([5, 50, 60, 3], 0.5, [5, 26, 30, 3])\n self._assertShapeInference([5, None, 60, 3], 0.5, [5, None, 30, 3])\n self._assertShapeInference([5, 50, None, 3], 0.5, [5, 26, None, 3])\n self._assertShapeInference([5, None, None, 3], 0.5, [5, None, None, 3])\n self._assertShapeInference([5, 50, 60, None], 0.5, [5, 26, 30, None])\n self._assertShapeInference([5, None, None, None], 0.5,\n [5, None, None, None])\n self._assertShapeInference([None, None, None, None], 0.5,\n [None, None, None, None])\n\n def testErrorOnInvalidCentralCropFractionValues(self):\n x_shape = [13, 9, 3]\n x_np = np.ones(x_shape, dtype=np.float32)\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n x = constant_op.constant(x_np, shape=x_shape)\n with self.assertRaises(ValueError):\n _ = image_ops.central_crop(x, 0.0)\n with self.assertRaises(ValueError):\n _ = image_ops.central_crop(x, 1.01)\n\n def testErrorOnInvalidShapes(self):\n x_shapes = [None, [], [3], [3, 9], [3, 9, 3, 9, 3]]\n for x_shape in x_shapes:\n x_np = np.ones(x_shape, dtype=np.float32)\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n x = constant_op.constant(x_np, shape=x_shape)\n with self.assertRaises(ValueError):\n _ = image_ops.central_crop(x, 0.5)\n\n def testNameScope(self):\n x_shape = [13, 9, 3]\n x_np = np.ones(x_shape, dtype=np.float32)\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n y = image_ops.central_crop(x_np, 1.0)\n self.assertTrue(y.op.name.startswith(\"central_crop\"))\n\n\nclass PadToBoundingBoxTest(test_util.TensorFlowTestCase):\n\n def _PadToBoundingBox(self, x, offset_height, offset_width, target_height,\n target_width, use_tensor_inputs):\n if use_tensor_inputs:\n offset_height = ops.convert_to_tensor(offset_height)\n offset_width = ops.convert_to_tensor(offset_width)\n target_height = ops.convert_to_tensor(target_height)\n target_width = ops.convert_to_tensor(target_width)\n x_tensor = array_ops.placeholder(x.dtype, shape=[None] * x.ndim)\n feed_dict = {x_tensor: x}\n else:\n x_tensor = x\n feed_dict = {}\n\n y = image_ops.pad_to_bounding_box(x_tensor, offset_height, offset_width,\n target_height, target_width)\n if not use_tensor_inputs:\n self.assertTrue(y.get_shape().is_fully_defined())\n\n with self.test_session(use_gpu=True):\n return y.eval(feed_dict=feed_dict)\n\n def _assertReturns(self,\n x,\n x_shape,\n offset_height,\n offset_width,\n y,\n y_shape,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width, _ = y_shape\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._PadToBoundingBox(x, offset_height, offset_width,\n target_height, target_width,\n use_tensor_inputs)\n self.assertAllClose(y, y_tf)\n\n def _assertRaises(self,\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n err_msg,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n x = np.array(x).reshape(x_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n try:\n self._PadToBoundingBox(x, offset_height, offset_width, target_height,\n target_width, use_tensor_inputs)\n except Exception as e:\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def _assertShapeInference(self, pre_shape, height, width, post_shape):\n image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.pad_to_bounding_box(image, 0, 0, height, width)\n self.assertEqual(y.get_shape().as_list(), post_shape)\n\n def testInt64(self):\n x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n x_shape = [3, 3, 1]\n\n y = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n y_shape = [4, 3, 1]\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n i = constant_op.constant([1, 0, 4, 3], dtype=dtypes.int64)\n y_tf = image_ops.pad_to_bounding_box(x, i[0], i[1], i[2], i[3])\n with self.test_session(use_gpu=True):\n self.assertAllClose(y, y_tf.eval())\n\n def testNoOp(self):\n x_shape = [10, 10, 10]\n x = np.random.uniform(size=x_shape)\n offset_height, offset_width = [0, 0]\n self._assertReturns(x, x_shape, offset_height, offset_width, x, x_shape)\n\n def testPadding(self):\n x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n x_shape = [3, 3, 1]\n\n offset_height, offset_width = [1, 0]\n y = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n y_shape = [4, 3, 1]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 1]\n y = [0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]\n y_shape = [3, 4, 1]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 0]\n y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0]\n y_shape = [4, 3, 1]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n offset_height, offset_width = [0, 0]\n y = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0]\n y_shape = [3, 4, 1]\n self._assertReturns(x, x_shape, offset_height, offset_width, y, y_shape)\n\n def testShapeInference(self):\n self._assertShapeInference([55, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([50, 60, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 60, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([50, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, 66, None], 55, 66, [55, 66, None])\n self._assertShapeInference([50, 60, None], 55, 66, [55, 66, None])\n self._assertShapeInference([None, None, None], 55, 66, [55, 66, None])\n self._assertShapeInference(None, 55, 66, [55, 66, None])\n\n def testNon3DInput(self):\n # Input image is not 3D\n x = [0] * 15\n offset_height, offset_width = [0, 0]\n target_height, target_width = [2, 2]\n\n for x_shape in ([3, 5], [1, 3, 5, 1, 1]):\n self._assertRaises(x, x_shape, offset_height, offset_width, target_height,\n target_width,\n \"'image' must have either 3 or 4 dimensions.\")\n\n def testZeroLengthInput(self):\n # Input image has 0-length dimension(s).\n # Each line is a test configuration:\n # x_shape, target_height, target_width\n test_config = (([0, 2, 2], 2, 2), ([2, 0, 2], 2, 2), ([2, 2, 0], 2, 2))\n offset_height, offset_width = [0, 0]\n x = []\n\n for x_shape, target_height, target_width in test_config:\n self._assertRaises(\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n \"all dims of 'image.shape' must be > 0\",\n use_tensor_inputs_options=[False])\n\n # The original error message does not contain back slashes. However, they\n # are added by either the assert op or the runtime. If this behavior\n # changes in the future, the match string will also needs to be changed.\n self._assertRaises(\n x,\n x_shape,\n offset_height,\n offset_width,\n target_height,\n target_width,\n \"all dims of \\\\'image.shape\\\\' must be > 0\",\n use_tensor_inputs_options=[True])\n\n def testBadParams(self):\n x_shape = [3, 3, 1]\n x = np.zeros(x_shape)\n\n # Each line is a test configuration:\n # offset_height, offset_width, target_height, target_width, err_msg\n test_config = ((-1, 0, 4, 4, \"offset_height must be >= 0\"),\n (0, -1, 4, 4, \"offset_width must be >= 0\"),\n (2, 0, 4, 4, \"height must be <= target - offset\"),\n (0, 2, 4, 4, \"width must be <= target - offset\"))\n\n for config_item in test_config:\n self._assertRaises(x, x_shape, *config_item)\n\n def testNameScope(self):\n image = array_ops.placeholder(dtypes.float32, shape=[55, 66, 3])\n y = image_ops.pad_to_bounding_box(image, 0, 0, 55, 66)\n self.assertTrue(y.op.name.startswith(\"pad_to_bounding_box\"))\n\n\nclass SelectDistortedCropBoxTest(test_util.TensorFlowTestCase):\n\n def _testSampleDistortedBoundingBox(self, image, bounding_box,\n min_object_covered, aspect_ratio_range,\n area_range):\n original_area = float(np.prod(image.shape))\n bounding_box_area = float((bounding_box[3] - bounding_box[1]) *\n (bounding_box[2] - bounding_box[0]))\n\n image_size_np = np.array(image.shape, dtype=np.int32)\n bounding_box_np = (\n np.array(bounding_box, dtype=np.float32).reshape([1, 1, 4]))\n\n aspect_ratios = []\n area_ratios = []\n\n fraction_object_covered = []\n\n num_iter = 1000\n with self.test_session(use_gpu=True):\n image_tf = constant_op.constant(image, shape=image.shape)\n image_size_tf = constant_op.constant(\n image_size_np, shape=image_size_np.shape)\n bounding_box_tf = constant_op.constant(\n bounding_box_np, dtype=dtypes.float32, shape=bounding_box_np.shape)\n\n begin, size, _ = image_ops.sample_distorted_bounding_box(\n image_size=image_size_tf,\n bounding_boxes=bounding_box_tf,\n min_object_covered=min_object_covered,\n aspect_ratio_range=aspect_ratio_range,\n area_range=area_range)\n y = array_ops.strided_slice(image_tf, begin, begin + size)\n\n for _ in xrange(num_iter):\n y_tf = y.eval()\n crop_height = y_tf.shape[0]\n crop_width = y_tf.shape[1]\n aspect_ratio = float(crop_width) / float(crop_height)\n area = float(crop_width * crop_height)\n\n aspect_ratios.append(aspect_ratio)\n area_ratios.append(area / original_area)\n fraction_object_covered.append(float(np.sum(y_tf)) / bounding_box_area)\n\n # min_object_covered as tensor\n min_object_covered_placeholder = array_ops.placeholder(dtypes.float32)\n begin, size, _ = image_ops.sample_distorted_bounding_box(\n image_size=image_size_tf,\n bounding_boxes=bounding_box_tf,\n min_object_covered=min_object_covered_placeholder,\n aspect_ratio_range=aspect_ratio_range,\n area_range=area_range)\n y = array_ops.strided_slice(image_tf, begin, begin + size)\n\n for _ in xrange(num_iter):\n y_tf = y.eval(feed_dict={\n min_object_covered_placeholder: min_object_covered\n })\n crop_height = y_tf.shape[0]\n crop_width = y_tf.shape[1]\n aspect_ratio = float(crop_width) / float(crop_height)\n area = float(crop_width * crop_height)\n\n aspect_ratios.append(aspect_ratio)\n area_ratios.append(area / original_area)\n fraction_object_covered.append(float(np.sum(y_tf)) / bounding_box_area)\n\n # Ensure that each entry is observed within 3 standard deviations.\n # num_bins = 10\n # aspect_ratio_hist, _ = np.histogram(aspect_ratios,\n # bins=num_bins,\n # range=aspect_ratio_range)\n # mean = np.mean(aspect_ratio_hist)\n # stddev = np.sqrt(mean)\n # TODO(wicke, shlens, dga): Restore this test so that it is no longer flaky.\n # TODO(irving): Since the rejection probability is not independent of the\n # aspect ratio, the aspect_ratio random value is not exactly uniformly\n # distributed in [min_aspect_ratio, max_aspect_ratio). This test should be\n # fixed to reflect the true statistical property, then tightened to enforce\n # a stricter bound. Or, ideally, the sample_distorted_bounding_box Op\n # be fixed to not use rejection sampling and generate correctly uniform\n # aspect ratios.\n # self.assertAllClose(aspect_ratio_hist,\n # [mean] * num_bins, atol=3.6 * stddev)\n\n # The resulting crop will not be uniformly distributed in area. In practice,\n # we find that the area skews towards the small sizes. Instead, we perform\n # a weaker test to ensure that the area ratios are merely within the\n # specified bounds.\n self.assertLessEqual(max(area_ratios), area_range[1])\n self.assertGreaterEqual(min(area_ratios), area_range[0])\n\n # For reference, here is what the distribution of area ratios look like.\n area_ratio_hist, _ = np.histogram(area_ratios, bins=10, range=area_range)\n print(\"area_ratio_hist \", area_ratio_hist)\n\n # Ensure that fraction_object_covered is satisfied.\n # TODO(wicke, shlens, dga): Restore this test so that it is no longer flaky.\n # self.assertGreaterEqual(min(fraction_object_covered), min_object_covered)\n\n def testWholeImageBoundingBox(self):\n height = 40\n width = 50\n image_size = [height, width, 1]\n bounding_box = [0.0, 0.0, 1.0, 1.0]\n image = np.arange(\n 0, np.prod(image_size), dtype=np.int32).reshape(image_size)\n self._testSampleDistortedBoundingBox(\n image,\n bounding_box,\n min_object_covered=0.1,\n aspect_ratio_range=(0.75, 1.33),\n area_range=(0.05, 1.0))\n\n def testWithBoundingBox(self):\n height = 40\n width = 50\n x_shape = [height, width, 1]\n image = np.zeros(x_shape, dtype=np.int32)\n\n # Create an object with 1's in a region with area A and require that\n # the total pixel values >= 0.1 * A.\n min_object_covered = 0.1\n\n xmin = 2\n ymin = 3\n xmax = 12\n ymax = 13\n for x in np.arange(xmin, xmax + 1, 1):\n for y in np.arange(ymin, ymax + 1, 1):\n image[x, y] = 1\n\n # Bounding box is specified as (ymin, xmin, ymax, xmax) in\n # relative coordinates.\n bounding_box = (float(ymin) / height, float(xmin) / width,\n float(ymax) / height, float(xmax) / width)\n\n self._testSampleDistortedBoundingBox(\n image,\n bounding_box=bounding_box,\n min_object_covered=min_object_covered,\n aspect_ratio_range=(0.75, 1.33),\n area_range=(0.05, 1.0))\n\n def testSampleDistortedBoundingBoxShape(self):\n with self.test_session(use_gpu=True):\n image_size = constant_op.constant(\n [40, 50, 1], shape=[3], dtype=dtypes.int32)\n bounding_box = constant_op.constant(\n [[[0.0, 0.0, 1.0, 1.0]]],\n shape=[1, 1, 4],\n dtype=dtypes.float32,\n )\n begin, end, bbox_for_drawing = image_ops.sample_distorted_bounding_box(\n image_size=image_size,\n bounding_boxes=bounding_box,\n min_object_covered=0.1,\n aspect_ratio_range=(0.75, 1.33),\n area_range=(0.05, 1.0))\n\n # Test that the shapes are correct.\n self.assertAllEqual([3], begin.get_shape().as_list())\n self.assertAllEqual([3], end.get_shape().as_list())\n self.assertAllEqual([1, 1, 4], bbox_for_drawing.get_shape().as_list())\n # Actual run to make sure shape is correct inside Compute().\n begin = begin.eval()\n end = end.eval()\n bbox_for_drawing = bbox_for_drawing.eval()\n\n begin, end, bbox_for_drawing = image_ops.sample_distorted_bounding_box(\n image_size=image_size,\n bounding_boxes=bounding_box,\n min_object_covered=array_ops.placeholder(dtypes.float32),\n aspect_ratio_range=(0.75, 1.33),\n area_range=(0.05, 1.0))\n\n # Test that the shapes are correct.\n self.assertAllEqual([3], begin.get_shape().as_list())\n self.assertAllEqual([3], end.get_shape().as_list())\n self.assertAllEqual([1, 1, 4], bbox_for_drawing.get_shape().as_list())\n\n def testDefaultMinObjectCovered(self):\n # By default min_object_covered=0.1 if not provided\n with self.test_session(use_gpu=True):\n image_size = constant_op.constant(\n [40, 50, 1], shape=[3], dtype=dtypes.int32)\n bounding_box = constant_op.constant(\n [[[0.0, 0.0, 1.0, 1.0]]],\n shape=[1, 1, 4],\n dtype=dtypes.float32,\n )\n begin, end, bbox_for_drawing = image_ops.sample_distorted_bounding_box(\n image_size=image_size,\n bounding_boxes=bounding_box,\n aspect_ratio_range=(0.75, 1.33),\n area_range=(0.05, 1.0))\n\n self.assertAllEqual([3], begin.get_shape().as_list())\n self.assertAllEqual([3], end.get_shape().as_list())\n self.assertAllEqual([1, 1, 4], bbox_for_drawing.get_shape().as_list())\n # Actual run to make sure shape is correct inside Compute().\n begin = begin.eval()\n end = end.eval()\n bbox_for_drawing = bbox_for_drawing.eval()\n\n\nclass ResizeImagesTest(test_util.TensorFlowTestCase):\n\n OPTIONS = [\n image_ops.ResizeMethod.BILINEAR, image_ops.ResizeMethod.NEAREST_NEIGHBOR,\n image_ops.ResizeMethod.BICUBIC, image_ops.ResizeMethod.AREA\n ]\n\n TYPES = [\n np.uint8, np.int8, np.uint16, np.int16, np.int32, np.int64, np.float16,\n np.float32, np.float64\n ]\n\n def _assertShapeInference(self, pre_shape, size, post_shape):\n # Try single image resize\n single_image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.resize_images(single_image, size)\n self.assertEqual(y.get_shape().as_list(), post_shape)\n # Try batch images resize with known batch size\n images = array_ops.placeholder(dtypes.float32, shape=[99] + pre_shape)\n y = image_ops.resize_images(images, size)\n self.assertEqual(y.get_shape().as_list(), [99] + post_shape)\n # Try batch images resize with unknown batch size\n images = array_ops.placeholder(dtypes.float32, shape=[None] + pre_shape)\n y = image_ops.resize_images(images, size)\n self.assertEqual(y.get_shape().as_list(), [None] + post_shape)\n\n def shouldRunOnGPU(self, opt, nptype):\n if (opt == image_ops.ResizeMethod.NEAREST_NEIGHBOR and\n nptype in [np.float32, np.float64]):\n return True\n else:\n return False\n\n def testNoOp(self):\n img_shape = [1, 6, 4, 1]\n single_shape = [6, 4, 1]\n # This test is also conducted with int8, so 127 is the maximum\n # value that can be used.\n data = [\n 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127,\n 50, 50, 100, 100, 50, 50, 100, 100\n ]\n target_height = 6\n target_width = 4\n\n for nptype in self.TYPES:\n img_np = np.array(data, dtype=nptype).reshape(img_shape)\n\n for opt in self.OPTIONS:\n with self.test_session(use_gpu=True) as sess:\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, [target_height, target_width], opt)\n yshape = array_ops.shape(y)\n resized, newshape = sess.run([y, yshape])\n self.assertAllEqual(img_shape, newshape)\n self.assertAllClose(resized, img_np, atol=1e-5)\n\n # Resizing with a single image must leave the shape unchanged also.\n with self.test_session(use_gpu=True):\n img_single = img_np.reshape(single_shape)\n image = constant_op.constant(img_single, shape=single_shape)\n y = image_ops.resize_images(image, [target_height, target_width],\n self.OPTIONS[0])\n yshape = array_ops.shape(y)\n newshape = yshape.eval()\n self.assertAllEqual(single_shape, newshape)\n\n def testTensorArguments(self):\n img_shape = [1, 6, 4, 1]\n single_shape = [6, 4, 1]\n # This test is also conducted with int8, so 127 is the maximum\n # value that can be used.\n data = [\n 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127,\n 50, 50, 100, 100, 50, 50, 100, 100\n ]\n new_size = array_ops.placeholder(dtypes.int32, shape=(2))\n\n img_np = np.array(data, dtype=np.uint8).reshape(img_shape)\n\n for opt in self.OPTIONS:\n with self.test_session(use_gpu=True) as sess:\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, new_size, opt)\n yshape = array_ops.shape(y)\n resized, newshape = sess.run([y, yshape], {new_size: [6, 4]})\n self.assertAllEqual(img_shape, newshape)\n self.assertAllClose(resized, img_np, atol=1e-5)\n\n # Resizing with a single image must leave the shape unchanged also.\n with self.test_session(use_gpu=True):\n img_single = img_np.reshape(single_shape)\n image = constant_op.constant(img_single, shape=single_shape)\n y = image_ops.resize_images(image, new_size, self.OPTIONS[0])\n yshape = array_ops.shape(y)\n resized, newshape = sess.run([y, yshape], {new_size: [6, 4]})\n self.assertAllEqual(single_shape, newshape)\n self.assertAllClose(resized, img_single, atol=1e-5)\n\n # Incorrect shape.\n with self.assertRaises(ValueError):\n new_size = constant_op.constant(4)\n _ = image_ops.resize_images(image, new_size,\n image_ops.ResizeMethod.BILINEAR)\n with self.assertRaises(ValueError):\n new_size = constant_op.constant([4])\n _ = image_ops.resize_images(image, new_size,\n image_ops.ResizeMethod.BILINEAR)\n with self.assertRaises(ValueError):\n new_size = constant_op.constant([1, 2, 3])\n _ = image_ops.resize_images(image, new_size,\n image_ops.ResizeMethod.BILINEAR)\n\n # Incorrect dtypes.\n with self.assertRaises(ValueError):\n new_size = constant_op.constant([6.0, 4])\n _ = image_ops.resize_images(image, new_size,\n image_ops.ResizeMethod.BILINEAR)\n with self.assertRaises(ValueError):\n _ = image_ops.resize_images(image, [6, 4.0],\n image_ops.ResizeMethod.BILINEAR)\n with self.assertRaises(ValueError):\n _ = image_ops.resize_images(image, [None, 4],\n image_ops.ResizeMethod.BILINEAR)\n with self.assertRaises(ValueError):\n _ = image_ops.resize_images(image, [6, None],\n image_ops.ResizeMethod.BILINEAR)\n\n def testReturnDtype(self):\n target_shapes = [[6, 4], [3, 2], [\n array_ops.placeholder(dtypes.int32),\n array_ops.placeholder(dtypes.int32)\n ]]\n for nptype in self.TYPES:\n image = array_ops.placeholder(nptype, shape=[1, 6, 4, 1])\n for opt in self.OPTIONS:\n for target_shape in target_shapes:\n y = image_ops.resize_images(image, target_shape, opt)\n if (opt == image_ops.ResizeMethod.NEAREST_NEIGHBOR or\n target_shape == image.shape[1:3]):\n expected_dtype = image.dtype\n else:\n expected_dtype = dtypes.float32\n self.assertEqual(y.dtype, expected_dtype)\n\n def testSumTensor(self):\n img_shape = [1, 6, 4, 1]\n # This test is also conducted with int8, so 127 is the maximum\n # value that can be used.\n data = [\n 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127,\n 50, 50, 100, 100, 50, 50, 100, 100\n ]\n # Test size where width is specified as a tensor which is a sum\n # of two tensors.\n width_1 = constant_op.constant(1)\n width_2 = constant_op.constant(3)\n width = math_ops.add(width_1, width_2)\n height = constant_op.constant(6)\n\n img_np = np.array(data, dtype=np.uint8).reshape(img_shape)\n\n for opt in self.OPTIONS:\n with self.cached_session() as sess:\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, [height, width], opt)\n yshape = array_ops.shape(y)\n resized, newshape = sess.run([y, yshape])\n self.assertAllEqual(img_shape, newshape)\n self.assertAllClose(resized, img_np, atol=1e-5)\n\n def testResizeDown(self):\n # This test is also conducted with int8, so 127 is the maximum\n # value that can be used.\n data = [\n 127, 127, 64, 64, 127, 127, 64, 64, 64, 64, 127, 127, 64, 64, 127, 127,\n 50, 50, 100, 100, 50, 50, 100, 100\n ]\n expected_data = [127, 64, 64, 127, 50, 100]\n target_height = 3\n target_width = 2\n\n # Test out 3-D and 4-D image shapes.\n img_shapes = [[1, 6, 4, 1], [6, 4, 1]]\n target_shapes = [[1, target_height, target_width, 1],\n [target_height, target_width, 1]]\n\n for target_shape, img_shape in zip(target_shapes, img_shapes):\n\n for nptype in self.TYPES:\n img_np = np.array(data, dtype=nptype).reshape(img_shape)\n\n for opt in self.OPTIONS:\n if test.is_gpu_available() and self.shouldRunOnGPU(opt, nptype):\n with self.test_session(use_gpu=True):\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, [target_height, target_width],\n opt)\n expected = np.array(expected_data).reshape(target_shape)\n resized = y.eval()\n self.assertAllClose(resized, expected, atol=1e-5)\n\n def testResizeUpAlignCornersFalse(self):\n img_shape = [1, 3, 2, 1]\n data = [64, 32, 32, 64, 50, 100]\n target_height = 6\n target_width = 4\n expected_data = {}\n expected_data[image_ops.ResizeMethod.BILINEAR] = [\n 64.0, 48.0, 32.0, 32.0, 48.0, 48.0, 48.0, 48.0, 32.0, 48.0, 64.0, 64.0,\n 41.0, 61.5, 82.0, 82.0, 50.0, 75.0, 100.0, 100.0, 50.0, 75.0, 100.0,\n 100.0\n ]\n expected_data[image_ops.ResizeMethod.NEAREST_NEIGHBOR] = [\n 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0,\n 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0,\n 100.0\n ]\n expected_data[image_ops.ResizeMethod.AREA] = [\n 64.0, 64.0, 32.0, 32.0, 64.0, 64.0, 32.0, 32.0, 32.0, 32.0, 64.0, 64.0,\n 32.0, 32.0, 64.0, 64.0, 50.0, 50.0, 100.0, 100.0, 50.0, 50.0, 100.0,\n 100.0\n ]\n\n for nptype in self.TYPES:\n for opt in [\n image_ops.ResizeMethod.BILINEAR,\n image_ops.ResizeMethod.NEAREST_NEIGHBOR, image_ops.ResizeMethod.AREA\n ]:\n with self.test_session(use_gpu=True):\n img_np = np.array(data, dtype=nptype).reshape(img_shape)\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(\n image, [target_height, target_width], opt, align_corners=False)\n resized = y.eval()\n expected = np.array(expected_data[opt]).reshape(\n [1, target_height, target_width, 1])\n self.assertAllClose(resized, expected, atol=1e-05)\n\n def testResizeUpAlignCornersTrue(self):\n img_shape = [1, 3, 2, 1]\n data = [6, 3, 3, 6, 6, 9]\n target_height = 5\n target_width = 4\n expected_data = {}\n expected_data[image_ops.ResizeMethod.BILINEAR] = [\n 6.0, 5.0, 4.0, 3.0, 4.5, 4.5, 4.5, 4.5, 3.0, 4.0, 5.0, 6.0, 4.5, 5.5,\n 6.5, 7.5, 6.0, 7.0, 8.0, 9.0\n ]\n expected_data[image_ops.ResizeMethod.NEAREST_NEIGHBOR] = [\n 6.0, 6.0, 3.0, 3.0, 3.0, 3.0, 6.0, 6.0, 3.0, 3.0, 6.0, 6.0, 6.0, 6.0,\n 9.0, 9.0, 6.0, 6.0, 9.0, 9.0\n ]\n # TODO(b/37749740): Improve alignment of ResizeMethod.AREA when\n # align_corners=True.\n expected_data[image_ops.ResizeMethod.AREA] = [\n 6.0, 6.0, 6.0, 3.0, 6.0, 6.0, 6.0, 3.0, 3.0, 3.0, 3.0, 6.0, 3.0, 3.0,\n 3.0, 6.0, 6.0, 6.0, 6.0, 9.0\n ]\n\n for nptype in self.TYPES:\n for opt in [\n image_ops.ResizeMethod.BILINEAR,\n image_ops.ResizeMethod.NEAREST_NEIGHBOR, image_ops.ResizeMethod.AREA\n ]:\n with self.test_session(use_gpu=True):\n img_np = np.array(data, dtype=nptype).reshape(img_shape)\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(\n image, [target_height, target_width], opt, align_corners=True)\n resized = y.eval()\n expected = np.array(expected_data[opt]).reshape(\n [1, target_height, target_width, 1])\n self.assertAllClose(resized, expected, atol=1e-05)\n\n def testResizeUpBicubic(self):\n img_shape = [1, 6, 6, 1]\n data = [\n 128, 128, 64, 64, 128, 128, 64, 64, 64, 64, 128, 128, 64, 64, 128, 128,\n 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100, 50, 50, 100, 100,\n 50, 50, 100, 100\n ]\n img_np = np.array(data, dtype=np.uint8).reshape(img_shape)\n\n target_height = 8\n target_width = 8\n expected_data = [\n 128, 135, 96, 55, 64, 114, 134, 128, 78, 81, 68, 52, 57, 118, 144, 136,\n 55, 49, 79, 109, 103, 89, 83, 84, 74, 70, 95, 122, 115, 69, 49, 55, 100,\n 105, 75, 43, 50, 89, 105, 100, 57, 54, 74, 96, 91, 65, 55, 58, 70, 69,\n 75, 81, 80, 72, 69, 70, 105, 112, 75, 36, 45, 92, 111, 105\n ]\n\n with self.test_session(use_gpu=True):\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, [target_height, target_width],\n image_ops.ResizeMethod.BICUBIC)\n resized = y.eval()\n expected = np.array(expected_data).reshape(\n [1, target_height, target_width, 1])\n self.assertAllClose(resized, expected, atol=1)\n\n def testResizeDownArea(self):\n img_shape = [1, 6, 6, 1]\n data = [\n 128, 64, 32, 16, 8, 4, 4, 8, 16, 32, 64, 128, 128, 64, 32, 16, 8, 4, 5,\n 10, 15, 20, 25, 30, 30, 25, 20, 15, 10, 5, 5, 10, 15, 20, 25, 30\n ]\n img_np = np.array(data, dtype=np.uint8).reshape(img_shape)\n\n target_height = 4\n target_width = 4\n expected_data = [\n 73, 33, 23, 39, 73, 33, 23, 39, 14, 16, 19, 21, 14, 16, 19, 21\n ]\n\n with self.test_session(use_gpu=True):\n image = constant_op.constant(img_np, shape=img_shape)\n y = image_ops.resize_images(image, [target_height, target_width],\n image_ops.ResizeMethod.AREA)\n expected = np.array(expected_data).reshape(\n [1, target_height, target_width, 1])\n resized = y.eval()\n self.assertAllClose(resized, expected, atol=1)\n\n def testCompareNearestNeighbor(self):\n if test.is_gpu_available():\n input_shape = [1, 5, 6, 3]\n target_height = 8\n target_width = 12\n for nptype in [np.float32, np.float64]:\n for align_corners in [True, False]:\n img_np = np.arange(\n 0, np.prod(input_shape), dtype=nptype).reshape(input_shape)\n with self.test_session(use_gpu=True):\n image = constant_op.constant(img_np, shape=input_shape)\n new_size = constant_op.constant([target_height, target_width])\n out_op = image_ops.resize_images(\n image,\n new_size,\n image_ops.ResizeMethod.NEAREST_NEIGHBOR,\n align_corners=align_corners)\n gpu_val = out_op.eval()\n with self.test_session(use_gpu=False):\n image = constant_op.constant(img_np, shape=input_shape)\n new_size = constant_op.constant([target_height, target_width])\n out_op = image_ops.resize_images(\n image,\n new_size,\n image_ops.ResizeMethod.NEAREST_NEIGHBOR,\n align_corners=align_corners)\n cpu_val = out_op.eval()\n self.assertAllClose(cpu_val, gpu_val, rtol=1e-5, atol=1e-5)\n\n def testCompareBilinear(self):\n if test.is_gpu_available():\n input_shape = [1, 5, 6, 3]\n target_height = 8\n target_width = 12\n for nptype in [np.float32, np.float64]:\n for align_corners in [True, False]:\n img_np = np.arange(\n 0, np.prod(input_shape), dtype=nptype).reshape(input_shape)\n value = {}\n for use_gpu in [True, False]:\n with self.test_session(use_gpu=use_gpu):\n image = constant_op.constant(img_np, shape=input_shape)\n new_size = constant_op.constant([target_height, target_width])\n out_op = image_ops.resize_images(\n image,\n new_size,\n image_ops.ResizeMethod.BILINEAR,\n align_corners=align_corners)\n value[use_gpu] = out_op.eval()\n self.assertAllClose(value[True], value[False], rtol=1e-5, atol=1e-5)\n\n def testShapeInference(self):\n self._assertShapeInference([50, 60, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([55, 66, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([59, 69, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([50, 69, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([59, 60, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([None, 60, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([None, 66, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([None, 69, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([50, None, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([55, None, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([59, None, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([None, None, 3], [55, 66], [55, 66, 3])\n self._assertShapeInference([50, 60, None], [55, 66], [55, 66, None])\n self._assertShapeInference([55, 66, None], [55, 66], [55, 66, None])\n self._assertShapeInference([59, 69, None], [55, 66], [55, 66, None])\n self._assertShapeInference([50, 69, None], [55, 66], [55, 66, None])\n self._assertShapeInference([59, 60, None], [55, 66], [55, 66, None])\n self._assertShapeInference([None, None, None], [55, 66], [55, 66, None])\n\n def testNameScope(self):\n img_shape = [1, 3, 2, 1]\n with self.test_session(use_gpu=True):\n single_image = array_ops.placeholder(dtypes.float32, shape=[50, 60, 3])\n y = image_ops.resize_images(single_image, [55, 66])\n self.assertTrue(y.op.name.startswith(\"resize_images\"))\n\n def _ResizeImageCall(self, x, max_h, max_w, preserve_aspect_ratio,\n use_tensor_inputs):\n if use_tensor_inputs:\n target_max = ops.convert_to_tensor([max_h, max_w])\n x_tensor = array_ops.placeholder(x.dtype, shape=[None] * x.ndim)\n feed_dict = {x_tensor: x}\n else:\n target_max = [max_h, max_w]\n x_tensor = x\n feed_dict = {}\n\n y = image_ops.resize_images(x_tensor, target_max,\n preserve_aspect_ratio=preserve_aspect_ratio)\n\n with self.test_session(use_gpu=True):\n return y.eval(feed_dict=feed_dict)\n\n def _assertResizeEqual(self, x, x_shape, y, y_shape,\n preserve_aspect_ratio=True,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width, _ = y_shape\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._ResizeImageCall(x, target_height, target_width,\n preserve_aspect_ratio, use_tensor_inputs)\n self.assertAllClose(y, y_tf)\n\n def _assertResizeCheckShape(self, x, x_shape, target_shape,\n y_shape, preserve_aspect_ratio=True,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width = target_shape\n x = np.array(x).reshape(x_shape)\n y = np.zeros(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._ResizeImageCall(x, target_height, target_width,\n preserve_aspect_ratio, use_tensor_inputs)\n self.assertShapeEqual(y, ops.convert_to_tensor(y_tf))\n\n def testPreserveAspectRatioMultipleImages(self):\n x_shape = [10, 100, 100, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeCheckShape(x, x_shape, [250, 250], [10, 250, 250, 10],\n preserve_aspect_ratio=False)\n\n def testPreserveAspectRatioNoOp(self):\n x_shape = [10, 10, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeEqual(x, x_shape, x, x_shape)\n\n def testPreserveAspectRatioSmaller(self):\n x_shape = [100, 100, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeCheckShape(x, x_shape, [75, 50], [50, 50, 10])\n\n def testPreserveAspectRatioSmallerMultipleImages(self):\n x_shape = [10, 100, 100, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeCheckShape(x, x_shape, [75, 50], [10, 50, 50, 10])\n\n def testPreserveAspectRatioLarger(self):\n x_shape = [100, 100, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeCheckShape(x, x_shape, [150, 200], [150, 150, 10])\n\n def testPreserveAspectRatioSameRatio(self):\n x_shape = [1920, 1080, 3]\n x = np.random.uniform(size=x_shape)\n\n self._assertResizeCheckShape(x, x_shape, [3840, 2160], [3840, 2160, 3])\n\n\nclass ResizeImageWithPadTest(test_util.TensorFlowTestCase):\n\n def _ResizeImageWithPad(self, x, target_height, target_width,\n use_tensor_inputs):\n if use_tensor_inputs:\n target_height = ops.convert_to_tensor(target_height)\n target_width = ops.convert_to_tensor(target_width)\n x_tensor = array_ops.placeholder(x.dtype, shape=[None] * x.ndim)\n feed_dict = {x_tensor: x}\n else:\n x_tensor = x\n feed_dict = {}\n\n y = image_ops.resize_image_with_pad(x_tensor, target_height,\n target_width)\n if not use_tensor_inputs:\n self.assertTrue(y.get_shape().is_fully_defined())\n\n with self.test_session(use_gpu=True):\n return y.eval(feed_dict=feed_dict)\n\n def _assertReturns(self,\n x,\n x_shape,\n y,\n y_shape,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width, _ = y_shape\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._ResizeImageWithPad(x, target_height, target_width,\n use_tensor_inputs)\n self.assertAllClose(y, y_tf)\n\n def _assertRaises(self,\n x,\n x_shape,\n target_height,\n target_width,\n err_msg,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n x = np.array(x).reshape(x_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n try:\n self._ResizeImageWithPad(x, target_height, target_width,\n use_tensor_inputs)\n except Exception as e: # pylint: disable=broad-except\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def _assertShapeInference(self, pre_shape, height, width, post_shape):\n image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.resize_image_with_pad(image, height, width)\n self.assertEqual(y.get_shape().as_list(), post_shape)\n\n def testNoOp(self):\n x_shape = [10, 10, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertReturns(x, x_shape, x, x_shape)\n\n def testPad(self):\n # Reduce vertical dimension\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 1, 3, 0]\n y_shape = [1, 4, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Reduce horizontal dimension\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [1, 3, 0, 0]\n y_shape = [2, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [1, 3]\n y_shape = [1, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n\nclass ResizeImageWithCropOrPadTest(test_util.TensorFlowTestCase):\n\n def _ResizeImageWithCropOrPad(self, x, target_height, target_width,\n use_tensor_inputs):\n if use_tensor_inputs:\n target_height = ops.convert_to_tensor(target_height)\n target_width = ops.convert_to_tensor(target_width)\n x_tensor = array_ops.placeholder(x.dtype, shape=[None] * x.ndim)\n feed_dict = {x_tensor: x}\n else:\n x_tensor = x\n feed_dict = {}\n\n y = image_ops.resize_image_with_crop_or_pad(x_tensor, target_height,\n target_width)\n if not use_tensor_inputs:\n self.assertTrue(y.get_shape().is_fully_defined())\n\n with self.test_session(use_gpu=True):\n return y.eval(feed_dict=feed_dict)\n\n def _assertReturns(self,\n x,\n x_shape,\n y,\n y_shape,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n target_height, target_width, _ = y_shape\n x = np.array(x).reshape(x_shape)\n y = np.array(y).reshape(y_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n y_tf = self._ResizeImageWithCropOrPad(x, target_height, target_width,\n use_tensor_inputs)\n self.assertAllClose(y, y_tf)\n\n def _assertRaises(self,\n x,\n x_shape,\n target_height,\n target_width,\n err_msg,\n use_tensor_inputs_options=None):\n use_tensor_inputs_options = use_tensor_inputs_options or [False, True]\n x = np.array(x).reshape(x_shape)\n\n for use_tensor_inputs in use_tensor_inputs_options:\n try:\n self._ResizeImageWithCropOrPad(x, target_height, target_width,\n use_tensor_inputs)\n except Exception as e:\n if err_msg not in str(e):\n raise\n else:\n raise AssertionError(\"Exception not raised: %s\" % err_msg)\n\n def _assertShapeInference(self, pre_shape, height, width, post_shape):\n image = array_ops.placeholder(dtypes.float32, shape=pre_shape)\n y = image_ops.resize_image_with_crop_or_pad(image, height, width)\n self.assertEqual(y.get_shape().as_list(), post_shape)\n\n def testNoOp(self):\n x_shape = [10, 10, 10]\n x = np.random.uniform(size=x_shape)\n\n self._assertReturns(x, x_shape, x, x_shape)\n\n def testPad(self):\n # Pad even along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 1, 2, 3, 4, 0, 0, 5, 6, 7, 8, 0]\n y_shape = [2, 6, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Pad odd along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 1, 2, 3, 4, 0, 0, 0, 5, 6, 7, 8, 0, 0]\n y_shape = [2, 7, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Pad even along row.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0]\n y_shape = [4, 4, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Pad odd along row.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]\n y_shape = [5, 4, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n def testCrop(self):\n # Crop even along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [2, 3, 6, 7]\n y_shape = [2, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Crop odd along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n x_shape = [2, 6, 1]\n\n y = [2, 3, 4, 8, 9, 10]\n y_shape = [2, 3, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Crop even along row.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [4, 2, 1]\n\n y = [3, 4, 5, 6]\n y_shape = [2, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Crop odd along row.\n x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]\n x_shape = [8, 2, 1]\n\n y = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n y_shape = [5, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n def testCropAndPad(self):\n # Pad along row but crop along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [2, 4, 1]\n\n y = [0, 0, 2, 3, 6, 7, 0, 0]\n y_shape = [4, 2, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n # Crop along row but pad along col.\n x = [1, 2, 3, 4, 5, 6, 7, 8]\n x_shape = [4, 2, 1]\n\n y = [0, 3, 4, 0, 0, 5, 6, 0]\n y_shape = [2, 4, 1]\n\n self._assertReturns(x, x_shape, y, y_shape)\n\n def testShapeInference(self):\n self._assertShapeInference([50, 60, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([59, 69, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([50, 69, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([59, 60, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 60, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 66, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, 69, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([50, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([55, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([59, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([None, None, 3], 55, 66, [55, 66, 3])\n self._assertShapeInference([50, 60, None], 55, 66, [55, 66, None])\n self._assertShapeInference([55, 66, None], 55, 66, [55, 66, None])\n self._assertShapeInference([59, 69, None], 55, 66, [55, 66, None])\n self._assertShapeInference([50, 69, None], 55, 66, [55, 66, None])\n self._assertShapeInference([59, 60, None], 55, 66, [55, 66, None])\n self._assertShapeInference([None, None, None], 55, 66, [55, 66, None])\n self._assertShapeInference(None, 55, 66, [55, 66, None])\n\n def testNon3DInput(self):\n # Input image is not 3D\n x = [0] * 15\n target_height, target_width = [4, 4]\n\n for x_shape in ([3, 5],):\n self._assertRaises(x, x_shape, target_height, target_width,\n \"'image' must have either 3 or 4 dimensions.\")\n\n for x_shape in ([1, 3, 5, 1, 1],):\n self._assertRaises(x, x_shape, target_height, target_width,\n \"'image' must have either 3 or 4 dimensions.\")\n\n def testZeroLengthInput(self):\n # Input image has 0-length dimension(s).\n target_height, target_width = [1, 1]\n x = []\n\n for x_shape in ([0, 2, 2], [2, 0, 2], [2, 2, 0]):\n self._assertRaises(\n x,\n x_shape,\n target_height,\n target_width,\n \"all dims of 'image.shape' must be > 0\",\n use_tensor_inputs_options=[False])\n\n # The original error message does not contain back slashes. However, they\n # are added by either the assert op or the runtime. If this behavior\n # changes in the future, the match string will also needs to be changed.\n self._assertRaises(\n x,\n x_shape,\n target_height,\n target_width,\n \"all dims of \\\\'image.shape\\\\' must be > 0\",\n use_tensor_inputs_options=[True])\n\n def testBadParams(self):\n x_shape = [4, 4, 1]\n x = np.zeros(x_shape)\n\n # target_height <= 0\n target_height, target_width = [0, 5]\n self._assertRaises(x, x_shape, target_height, target_width,\n \"target_height must be > 0\")\n\n # target_width <= 0\n target_height, target_width = [5, 0]\n self._assertRaises(x, x_shape, target_height, target_width,\n \"target_width must be > 0\")\n\n def testNameScope(self):\n image = array_ops.placeholder(dtypes.float32, shape=[50, 60, 3])\n y = image_ops.resize_image_with_crop_or_pad(image, 55, 66)\n self.assertTrue(y.op.name.startswith(\"resize_image_with_crop_or_pad\"))\n\n\ndef _SimpleColorRamp():\n \"\"\"Build a simple color ramp RGB image.\"\"\"\n w, h = 256, 200\n i = np.arange(h)[:, None]\n j = np.arange(w)\n image = np.empty((h, w, 3), dtype=np.uint8)\n image[:, :, 0] = i\n image[:, :, 1] = j\n image[:, :, 2] = (i + j) >> 1\n return image\n\n\nclass JpegTest(test_util.TensorFlowTestCase):\n\n # TODO(irving): Add self.assertAverageLess or similar to test_util\n def averageError(self, image0, image1):\n self.assertEqual(image0.shape, image1.shape)\n image0 = image0.astype(int) # Avoid overflow\n return np.abs(image0 - image1).sum() / np.prod(image0.shape)\n\n def testExisting(self):\n # Read a real jpeg and verify shape\n path = (\"tensorflow/core/lib/jpeg/testdata/\"\n \"jpeg_merge_test1.jpg\")\n with self.test_session(use_gpu=True) as sess:\n jpeg0 = io_ops.read_file(path)\n image0 = image_ops.decode_jpeg(jpeg0)\n image1 = image_ops.decode_jpeg(image_ops.encode_jpeg(image0))\n jpeg0, image0, image1 = sess.run([jpeg0, image0, image1])\n self.assertEqual(len(jpeg0), 3771)\n self.assertEqual(image0.shape, (256, 128, 3))\n self.assertLess(self.averageError(image0, image1), 1.4)\n\n def testCmyk(self):\n # Confirm that CMYK reads in as RGB\n base = \"tensorflow/core/lib/jpeg/testdata\"\n rgb_path = os.path.join(base, \"jpeg_merge_test1.jpg\")\n cmyk_path = os.path.join(base, \"jpeg_merge_test1_cmyk.jpg\")\n shape = 256, 128, 3\n for channels in 3, 0:\n with self.test_session(use_gpu=True) as sess:\n rgb = image_ops.decode_jpeg(\n io_ops.read_file(rgb_path), channels=channels)\n cmyk = image_ops.decode_jpeg(\n io_ops.read_file(cmyk_path), channels=channels)\n rgb, cmyk = sess.run([rgb, cmyk])\n self.assertEqual(rgb.shape, shape)\n self.assertEqual(cmyk.shape, shape)\n error = self.averageError(rgb, cmyk)\n self.assertLess(error, 4)\n\n def testCropAndDecodeJpeg(self):\n with self.cached_session() as sess:\n # Encode it, then decode it, then encode it\n base = \"tensorflow/core/lib/jpeg/testdata\"\n jpeg0 = io_ops.read_file(os.path.join(base, \"jpeg_merge_test1.jpg\"))\n\n h, w, _ = 256, 128, 3\n crop_windows = [[0, 0, 5, 5], [0, 0, 5, w], [0, 0, h, 5],\n [h - 6, w - 5, 6, 5], [6, 5, 15, 10], [0, 0, h, w]]\n for crop_window in crop_windows:\n # Explicit two stages: decode + crop.\n image1 = image_ops.decode_jpeg(jpeg0)\n y, x, h, w = crop_window\n image1_crop = image_ops.crop_to_bounding_box(image1, y, x, h, w)\n\n # Combined decode+crop.\n image2 = image_ops.decode_and_crop_jpeg(jpeg0, crop_window)\n\n # Combined decode+crop should have the same shape inference\n self.assertAllEqual(image1_crop.get_shape().as_list(),\n image2.get_shape().as_list())\n\n # CropAndDecode should be equal to DecodeJpeg+Crop.\n image1_crop, image2 = sess.run([image1_crop, image2])\n self.assertAllEqual(image1_crop, image2)\n\n def testCropAndDecodeJpegWithInvalidCropWindow(self):\n with self.cached_session() as sess:\n # Encode it, then decode it, then encode it\n base = \"tensorflow/core/lib/jpeg/testdata\"\n jpeg0 = io_ops.read_file(os.path.join(base, \"jpeg_merge_test1.jpg\"))\n\n h, w, _ = 256, 128, 3\n # Invalid crop windows.\n crop_windows = [[-1, 11, 11, 11], [11, -1, 11, 11], [11, 11, -1, 11],\n [11, 11, 11, -1], [11, 11, 0, 11], [11, 11, 11, 0],\n [0, 0, h + 1, w], [0, 0, h, w + 1]]\n for crop_window in crop_windows:\n result = image_ops.decode_and_crop_jpeg(jpeg0, crop_window)\n with self.assertRaisesWithPredicateMatch(\n errors.InvalidArgumentError,\n lambda e: \"Invalid JPEG data or crop window\" in str(e)):\n sess.run(result)\n\n def testSynthetic(self):\n with self.test_session(use_gpu=True) as sess:\n # Encode it, then decode it, then encode it\n image0 = constant_op.constant(_SimpleColorRamp())\n jpeg0 = image_ops.encode_jpeg(image0)\n image1 = image_ops.decode_jpeg(jpeg0, dct_method=\"INTEGER_ACCURATE\")\n image2 = image_ops.decode_jpeg(\n image_ops.encode_jpeg(image1), dct_method=\"INTEGER_ACCURATE\")\n jpeg0, image0, image1, image2 = sess.run([jpeg0, image0, image1, image2])\n\n # The decoded-encoded image should be similar to the input\n self.assertLess(self.averageError(image0, image1), 0.6)\n\n # We should be very close to a fixpoint\n self.assertLess(self.averageError(image1, image2), 0.02)\n\n # Smooth ramps compress well (input size is 153600)\n self.assertGreaterEqual(len(jpeg0), 5000)\n self.assertLessEqual(len(jpeg0), 6000)\n\n def testSyntheticFasterAlgorithm(self):\n with self.test_session(use_gpu=True) as sess:\n # Encode it, then decode it, then encode it\n image0 = constant_op.constant(_SimpleColorRamp())\n jpeg0 = image_ops.encode_jpeg(image0)\n image1 = image_ops.decode_jpeg(jpeg0, dct_method=\"INTEGER_FAST\")\n image2 = image_ops.decode_jpeg(\n image_ops.encode_jpeg(image1), dct_method=\"INTEGER_FAST\")\n jpeg0, image0, image1, image2 = sess.run([jpeg0, image0, image1, image2])\n\n # The decoded-encoded image should be similar to the input, but\n # note this is worse than the slower algorithm because it is\n # less accurate.\n self.assertLess(self.averageError(image0, image1), 0.95)\n\n # Repeated compression / decompression will have a higher error\n # with a lossier algorithm.\n self.assertLess(self.averageError(image1, image2), 1.05)\n\n # Smooth ramps compress well (input size is 153600)\n self.assertGreaterEqual(len(jpeg0), 5000)\n self.assertLessEqual(len(jpeg0), 6000)\n\n def testDefaultDCTMethodIsIntegerFast(self):\n with self.test_session(use_gpu=True) as sess:\n # Compare decoding with both dct_option=INTEGER_FAST and\n # default. They should be the same.\n image0 = constant_op.constant(_SimpleColorRamp())\n jpeg0 = image_ops.encode_jpeg(image0)\n image1 = image_ops.decode_jpeg(jpeg0, dct_method=\"INTEGER_FAST\")\n image2 = image_ops.decode_jpeg(jpeg0)\n image1, image2 = sess.run([image1, image2])\n\n # The images should be the same.\n self.assertAllClose(image1, image2)\n\n def testShape(self):\n with self.test_session(use_gpu=True) as sess:\n jpeg = constant_op.constant(\"nonsense\")\n for channels in 0, 1, 3:\n image = image_ops.decode_jpeg(jpeg, channels=channels)\n self.assertEqual(image.get_shape().as_list(),\n [None, None, channels or None])\n\n def testExtractJpegShape(self):\n # Read a real jpeg and verify shape.\n path = (\"tensorflow/core/lib/jpeg/testdata/\"\n \"jpeg_merge_test1.jpg\")\n with self.test_session(use_gpu=True) as sess:\n jpeg = io_ops.read_file(path)\n # Extract shape without decoding.\n [image_shape] = sess.run([image_ops.extract_jpeg_shape(jpeg)])\n self.assertEqual(image_shape.tolist(), [256, 128, 3])\n\n def testExtractJpegShapeforCmyk(self):\n # Read a cmyk jpeg image, and verify its shape.\n path = (\"tensorflow/core/lib/jpeg/testdata/\"\n \"jpeg_merge_test1_cmyk.jpg\")\n with self.test_session(use_gpu=True) as sess:\n jpeg = io_ops.read_file(path)\n [image_shape] = sess.run([image_ops.extract_jpeg_shape(jpeg)])\n # Cmyk jpeg image has 4 channels.\n self.assertEqual(image_shape.tolist(), [256, 128, 4])\n\n\nclass PngTest(test_util.TensorFlowTestCase):\n\n def testExisting(self):\n # Read some real PNGs, converting to different channel numbers\n prefix = \"tensorflow/core/lib/png/testdata/\"\n inputs = ((1, \"lena_gray.png\"), (4, \"lena_rgba.png\"),\n (3, \"lena_palette.png\"), (4, \"lena_palette_trns.png\"))\n for channels_in, filename in inputs:\n for channels in 0, 1, 3, 4:\n with self.test_session(use_gpu=True) as sess:\n png0 = io_ops.read_file(prefix + filename)\n image0 = image_ops.decode_png(png0, channels=channels)\n png0, image0 = sess.run([png0, image0])\n self.assertEqual(image0.shape, (26, 51, channels or channels_in))\n if channels == channels_in:\n image1 = image_ops.decode_png(image_ops.encode_png(image0))\n self.assertAllEqual(image0, image1.eval())\n\n def testSynthetic(self):\n with self.test_session(use_gpu=True) as sess:\n # Encode it, then decode it\n image0 = constant_op.constant(_SimpleColorRamp())\n png0 = image_ops.encode_png(image0, compression=7)\n image1 = image_ops.decode_png(png0)\n png0, image0, image1 = sess.run([png0, image0, image1])\n\n # PNG is lossless\n self.assertAllEqual(image0, image1)\n\n # Smooth ramps compress well, but not too well\n self.assertGreaterEqual(len(png0), 400)\n self.assertLessEqual(len(png0), 750)\n\n def testSyntheticUint16(self):\n with self.test_session(use_gpu=True) as sess:\n # Encode it, then decode it\n image0 = constant_op.constant(_SimpleColorRamp(), dtype=dtypes.uint16)\n png0 = image_ops.encode_png(image0, compression=7)\n image1 = image_ops.decode_png(png0, dtype=dtypes.uint16)\n png0, image0, image1 = sess.run([png0, image0, image1])\n\n # PNG is lossless\n self.assertAllEqual(image0, image1)\n\n # Smooth ramps compress well, but not too well\n self.assertGreaterEqual(len(png0), 800)\n self.assertLessEqual(len(png0), 1500)\n\n def testSyntheticTwoChannel(self):\n with self.test_session(use_gpu=True) as sess:\n # Strip the b channel from an rgb image to get a two-channel image.\n gray_alpha = _SimpleColorRamp()[:, :, 0:2]\n image0 = constant_op.constant(gray_alpha)\n png0 = image_ops.encode_png(image0, compression=7)\n image1 = image_ops.decode_png(png0)\n png0, image0, image1 = sess.run([png0, image0, image1])\n self.assertEqual(2, image0.shape[-1])\n self.assertAllEqual(image0, image1)\n\n def testSyntheticTwoChannelUint16(self):\n with self.test_session(use_gpu=True) as sess:\n # Strip the b channel from an rgb image to get a two-channel image.\n gray_alpha = _SimpleColorRamp()[:, :, 0:2]\n image0 = constant_op.constant(gray_alpha, dtype=dtypes.uint16)\n png0 = image_ops.encode_png(image0, compression=7)\n image1 = image_ops.decode_png(png0, dtype=dtypes.uint16)\n png0, image0, image1 = sess.run([png0, image0, image1])\n self.assertEqual(2, image0.shape[-1])\n self.assertAllEqual(image0, image1)\n\n def testShape(self):\n with self.test_session(use_gpu=True):\n png = constant_op.constant(\"nonsense\")\n for channels in 0, 1, 3:\n image = image_ops.decode_png(png, channels=channels)\n self.assertEqual(image.get_shape().as_list(),\n [None, None, channels or None])\n\n\nclass GifTest(test_util.TensorFlowTestCase):\n\n def _testValid(self, filename):\n # Read some real GIFs\n prefix = \"tensorflow/core/lib/gif/testdata/\"\n WIDTH = 20\n HEIGHT = 40\n STRIDE = 5\n shape = (12, HEIGHT, WIDTH, 3)\n\n with self.test_session(use_gpu=True) as sess:\n gif0 = io_ops.read_file(prefix + filename)\n image0 = image_ops.decode_gif(gif0)\n gif0, image0 = sess.run([gif0, image0])\n\n self.assertEqual(image0.shape, shape)\n\n for frame_idx, frame in enumerate(image0):\n gt = np.zeros(shape[1:], dtype=np.uint8)\n start = frame_idx * STRIDE\n end = (frame_idx + 1) * STRIDE\n print(frame_idx)\n if end <= WIDTH:\n gt[:, start:end, :] = 255\n else:\n start -= WIDTH\n end -= WIDTH\n gt[start:end, :, :] = 255\n\n self.assertAllClose(frame, gt)\n\n def testValid(self):\n self._testValid(\"scan.gif\")\n self._testValid(\"optimized.gif\")\n\n def testShape(self):\n with self.test_session(use_gpu=True) as sess:\n gif = constant_op.constant(\"nonsense\")\n image = image_ops.decode_gif(gif)\n self.assertEqual(image.get_shape().as_list(), [None, None, None, 3])\n\n\nclass ConvertImageTest(test_util.TensorFlowTestCase):\n\n def _convert(self, original, original_dtype, output_dtype, expected):\n x_np = np.array(original, dtype=original_dtype.as_numpy_dtype())\n y_np = np.array(expected, dtype=output_dtype.as_numpy_dtype())\n\n with self.test_session(use_gpu=True):\n image = constant_op.constant(x_np)\n y = image_ops.convert_image_dtype(image, output_dtype)\n self.assertTrue(y.dtype == output_dtype)\n self.assertAllClose(y.eval(), y_np, atol=1e-5)\n if output_dtype in [\n dtypes.float32, dtypes.float64, dtypes.int32, dtypes.int64\n ]:\n y_saturate = image_ops.convert_image_dtype(\n image, output_dtype, saturate=True)\n self.assertTrue(y_saturate.dtype == output_dtype)\n self.assertAllClose(y_saturate.eval(), y_np, atol=1e-5)\n\n def testNoConvert(self):\n # Make sure converting to the same data type creates only an identity op\n with self.test_session(use_gpu=True):\n image = constant_op.constant([1], dtype=dtypes.uint8)\n image_ops.convert_image_dtype(image, dtypes.uint8)\n y = image_ops.convert_image_dtype(image, dtypes.uint8)\n self.assertEquals(y.op.type, \"Identity\")\n self.assertEquals(y.op.inputs[0], image)\n\n def testConvertBetweenInteger(self):\n # Make sure converting to between integer types scales appropriately\n with self.test_session(use_gpu=True):\n self._convert([0, 255], dtypes.uint8, dtypes.int16, [0, 255 * 128])\n self._convert([0, 32767], dtypes.int16, dtypes.uint8, [0, 255])\n self._convert([0, 2**32], dtypes.int64, dtypes.int32, [0, 1])\n self._convert([0, 1], dtypes.int32, dtypes.int64, [0, 2**32])\n\n def testConvertBetweenFloat(self):\n # Make sure converting to between float types does nothing interesting\n with self.test_session(use_gpu=True):\n self._convert([-1.0, 0, 1.0, 200000], dtypes.float32, dtypes.float64,\n [-1.0, 0, 1.0, 200000])\n self._convert([-1.0, 0, 1.0, 200000], dtypes.float64, dtypes.float32,\n [-1.0, 0, 1.0, 200000])\n\n def testConvertBetweenIntegerAndFloat(self):\n # Make sure converting from and to a float type scales appropriately\n with self.test_session(use_gpu=True):\n self._convert([0, 1, 255], dtypes.uint8, dtypes.float32,\n [0, 1.0 / 255.0, 1])\n self._convert([0, 1.1 / 255.0, 1], dtypes.float32, dtypes.uint8,\n [0, 1, 255])\n\n def testConvertBetweenInt16AndInt8(self):\n with self.test_session(use_gpu=True):\n # uint8, uint16\n self._convert([0, 255 * 256], dtypes.uint16, dtypes.uint8, [0, 255])\n self._convert([0, 255], dtypes.uint8, dtypes.uint16, [0, 255 * 256])\n # int8, uint16\n self._convert([0, 127 * 2 * 256], dtypes.uint16, dtypes.int8, [0, 127])\n self._convert([0, 127], dtypes.int8, dtypes.uint16, [0, 127 * 2 * 256])\n # int16, uint16\n self._convert([0, 255 * 256], dtypes.uint16, dtypes.int16, [0, 255 * 128])\n self._convert([0, 255 * 128], dtypes.int16, dtypes.uint16, [0, 255 * 256])\n\n\nclass TotalVariationTest(test_util.TensorFlowTestCase):\n \"\"\"Tests the function total_variation() in image_ops.\n\n We test a few small handmade examples, as well as\n some larger examples using an equivalent numpy\n implementation of the total_variation() function.\n\n We do NOT test for overflows and invalid / edge-case arguments.\n \"\"\"\n\n def _test(self, x_np, y_np):\n \"\"\"Test that the TensorFlow implementation of\n total_variation(x_np) calculates the values in y_np.\n\n Note that these may be float-numbers so we only test\n for approximate equality within some narrow error-bound.\n \"\"\"\n\n # Create a TensorFlow session.\n with self.test_session(use_gpu=True):\n # Add a constant to the TensorFlow graph that holds the input.\n x_tf = constant_op.constant(x_np, shape=x_np.shape)\n\n # Add ops for calculating the total variation using TensorFlow.\n y = image_ops.total_variation(images=x_tf)\n\n # Run the TensorFlow session to calculate the result.\n y_tf = y.eval()\n\n # Assert that the results are as expected within\n # some small error-bound in case they are float-values.\n self.assertAllClose(y_tf, y_np)\n\n def _total_variation_np(self, x_np):\n \"\"\"Calculate the total variation of x_np using numpy.\n This implements the same function as TensorFlow but\n using numpy instead.\n\n Args:\n x_np: Numpy array with 3 or 4 dimensions.\n \"\"\"\n\n dim = len(x_np.shape)\n\n if dim == 3:\n # Calculate differences for neighboring pixel-values using slices.\n dif1 = x_np[1:, :, :] - x_np[:-1, :, :]\n dif2 = x_np[:, 1:, :] - x_np[:, :-1, :]\n\n # Sum for all axis.\n sum_axis = None\n elif dim == 4:\n # Calculate differences for neighboring pixel-values using slices.\n dif1 = x_np[:, 1:, :, :] - x_np[:, :-1, :, :]\n dif2 = x_np[:, :, 1:, :] - x_np[:, :, :-1, :]\n\n # Only sum for the last 3 axis.\n sum_axis = (1, 2, 3)\n else:\n # This should not occur in this test-code.\n pass\n\n tot_var = np.sum(np.abs(dif1), axis=sum_axis) + \\\n np.sum(np.abs(dif2), axis=sum_axis)\n\n return tot_var\n\n def _test_tensorflow_vs_numpy(self, x_np):\n \"\"\"Test the TensorFlow implementation against a numpy implementation.\n\n Args:\n x_np: Numpy array with 3 or 4 dimensions.\n \"\"\"\n\n # Calculate the y-values using the numpy implementation.\n y_np = self._total_variation_np(x_np)\n\n self._test(x_np, y_np)\n\n def _generateArray(self, shape):\n \"\"\"Generate an array of the given shape for use in testing.\n The numbers are calculated as the cumulative sum, which\n causes the difference between neighboring numbers to vary.\"\"\"\n\n # Flattened length of the array.\n flat_len = np.prod(shape)\n\n a = np.array(range(flat_len), dtype=int)\n a = np.cumsum(a)\n a = a.reshape(shape)\n\n return a\n\n def testTotalVariationNumpy(self):\n \"\"\"Test the TensorFlow implementation against a numpy implementation.\n The two implementations are very similar so it is possible that both\n have the same bug, which would not be detected by this test. It is\n therefore necessary to test with manually crafted data as well.\"\"\"\n\n # Generate a test-array.\n # This is an 'image' with 100x80 pixels and 3 color channels.\n a = self._generateArray(shape=(100, 80, 3))\n\n # Test the TensorFlow implementation vs. numpy implementation.\n # We use a numpy implementation to check the results that are\n # calculated using TensorFlow are correct.\n self._test_tensorflow_vs_numpy(a)\n self._test_tensorflow_vs_numpy(a + 1)\n self._test_tensorflow_vs_numpy(-a)\n self._test_tensorflow_vs_numpy(1.1 * a)\n\n # Expand to a 4-dim array.\n b = a[np.newaxis, :]\n\n # Combine several variations of the image into a single 4-dim array.\n multi = np.vstack((b, b + 1, -b, 1.1 * b))\n\n # Test that the TensorFlow function can also handle 4-dim arrays.\n self._test_tensorflow_vs_numpy(multi)\n\n def testTotalVariationHandmade(self):\n \"\"\"Test the total variation for a few handmade examples.\"\"\"\n\n # We create an image that is 2x2 pixels with 3 color channels.\n # The image is very small so we can check the result by hand.\n\n # Red color channel.\n # The following are the sum of absolute differences between the pixels.\n # sum row dif = (4-1) + (7-2) = 3 + 5 = 8\n # sum col dif = (2-1) + (7-4) = 1 + 3 = 4\n r = [[1, 2], [4, 7]]\n\n # Blue color channel.\n # sum row dif = 18 + 29 = 47\n # sum col dif = 7 + 18 = 25\n g = [[11, 18], [29, 47]]\n\n # Green color channel.\n # sum row dif = 120 + 193 = 313\n # sum col dif = 47 + 120 = 167\n b = [[73, 120], [193, 313]]\n\n # Combine the 3 color channels into a single 3-dim array.\n # The shape is (2, 2, 3) corresponding to (height, width and color).\n a = np.dstack((r, g, b))\n\n # Total variation for this image.\n # Sum of all pixel differences = 8 + 4 + 47 + 25 + 313 + 167 = 564\n tot_var = 564\n\n # Calculate the total variation using TensorFlow and assert it is correct.\n self._test(a, tot_var)\n\n # If we add 1 to all pixel-values then the total variation is unchanged.\n self._test(a + 1, tot_var)\n\n # If we negate all pixel-values then the total variation is unchanged.\n self._test(-a, tot_var)\n\n # Scale the pixel-values by a float. This scales the total variation as well.\n b = 1.1 * a\n self._test(b, 1.1 * tot_var)\n\n # Scale by another float.\n c = 1.2 * a\n self._test(c, 1.2 * tot_var)\n\n # Combine these 3 images into a single array of shape (3, 2, 2, 3)\n # where the first dimension is for the image-number.\n multi = np.vstack((a[np.newaxis, :], b[np.newaxis, :], c[np.newaxis, :]))\n\n # Check that TensorFlow correctly calculates the total variation\n # for each image individually and returns the correct array.\n self._test(multi, tot_var * np.array([1.0, 1.1, 1.2]))\n\n\nclass FormatTest(test_util.TensorFlowTestCase):\n\n def testFormats(self):\n prefix = \"tensorflow/core/lib\"\n paths = (\"png/testdata/lena_gray.png\", \"jpeg/testdata/jpeg_merge_test1.jpg\",\n \"gif/testdata/lena.gif\")\n decoders = {\n \"jpeg\": functools.partial(image_ops.decode_jpeg, channels=3),\n \"png\": functools.partial(image_ops.decode_png, channels=3),\n \"gif\": lambda s: array_ops.squeeze(image_ops.decode_gif(s), axis=0),\n }\n with self.cached_session():\n for path in paths:\n contents = io_ops.read_file(os.path.join(prefix, path)).eval()\n images = {}\n for name, decode in decoders.items():\n image = decode(contents).eval()\n self.assertEqual(image.ndim, 3)\n for prev_name, prev in images.items():\n print(\"path %s, names %s %s, shapes %s %s\" %\n (path, name, prev_name, image.shape, prev.shape))\n self.assertAllEqual(image, prev)\n images[name] = image\n\n def testError(self):\n path = \"tensorflow/core/lib/gif/testdata/scan.gif\"\n with self.cached_session():\n for decode in image_ops.decode_jpeg, image_ops.decode_png:\n with self.assertRaisesOpError(r\"Got 12 frames\"):\n decode(io_ops.read_file(path)).eval()\n\n\nclass NonMaxSuppressionTest(test_util.TensorFlowTestCase):\n\n def testSelectFromThreeClusters(self):\n boxes_np = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9],\n [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]]\n scores_np = [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]\n max_output_size_np = 3\n iou_threshold_np = 0.5\n with self.cached_session():\n boxes = constant_op.constant(boxes_np)\n scores = constant_op.constant(scores_np)\n max_output_size = constant_op.constant(max_output_size_np)\n iou_threshold = constant_op.constant(iou_threshold_np)\n selected_indices = image_ops.non_max_suppression(\n boxes, scores, max_output_size, iou_threshold).eval()\n self.assertAllClose(selected_indices, [3, 0, 5])\n\n def testInvalidShape(self):\n # The boxes should be 2D of shape [num_boxes, 4].\n with self.assertRaisesRegexp(ValueError,\n \"Shape must be rank 2 but is rank 1\"):\n boxes = constant_op.constant([0.0, 0.0, 1.0, 1.0])\n scores = constant_op.constant([0.9])\n image_ops.non_max_suppression(boxes, scores, 3, 0.5)\n\n with self.assertRaisesRegexp(ValueError, \"Dimension must be 4 but is 3\"):\n boxes = constant_op.constant([[0.0, 0.0, 1.0]])\n scores = constant_op.constant([0.9])\n image_ops.non_max_suppression(boxes, scores, 3, 0.5)\n\n # The boxes is of shape [num_boxes, 4], and the scores is\n # of shape [num_boxes]. So an error will thrown.\n with self.assertRaisesRegexp(ValueError,\n \"Dimensions must be equal, but are 1 and 2\"):\n boxes = constant_op.constant([[0.0, 0.0, 1.0, 1.0]])\n scores = constant_op.constant([0.9, 0.75])\n selected_indices = image_ops.non_max_suppression(boxes, scores, 3, 0.5)\n\n # The scores should be 1D of shape [num_boxes].\n with self.assertRaisesRegexp(ValueError,\n \"Shape must be rank 1 but is rank 2\"):\n boxes = constant_op.constant([[0.0, 0.0, 1.0, 1.0]])\n scores = constant_op.constant([[0.9]])\n image_ops.non_max_suppression(boxes, scores, 3, 0.5)\n\n # The max_output_size should be a scaler (0-D).\n with self.assertRaisesRegexp(ValueError,\n \"Shape must be rank 0 but is rank 1\"):\n boxes = constant_op.constant([[0.0, 0.0, 1.0, 1.0]])\n scores = constant_op.constant([0.9])\n image_ops.non_max_suppression(boxes, scores, [3], 0.5)\n\n # The iou_threshold should be a scaler (0-D).\n with self.assertRaisesRegexp(ValueError,\n \"Shape must be rank 0 but is rank 2\"):\n boxes = constant_op.constant([[0.0, 0.0, 1.0, 1.0]])\n scores = constant_op.constant([0.9])\n image_ops.non_max_suppression(boxes, scores, 3, [[0.5]])\n\n\nclass NonMaxSuppressionPaddedTest(test_util.TensorFlowTestCase):\n\n def testSelectFromThreeClusters(self):\n boxes_np = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9],\n [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]]\n scores_np = [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]\n max_output_size_np = 5\n iou_threshold_np = 0.5\n boxes = constant_op.constant(boxes_np)\n scores = constant_op.constant(scores_np)\n max_output_size = constant_op.constant(max_output_size_np)\n iou_threshold = constant_op.constant(iou_threshold_np)\n selected_indices_padded, num_valid_padded = \\\n image_ops.non_max_suppression_padded(\n boxes,\n scores,\n max_output_size,\n iou_threshold,\n pad_to_max_output_size=True)\n selected_indices, num_valid = image_ops.non_max_suppression_padded(\n boxes,\n scores,\n max_output_size,\n iou_threshold,\n pad_to_max_output_size=False)\n # The output shape of the padded operation must be fully defined.\n self.assertEqual(selected_indices_padded.shape.is_fully_defined(), True)\n self.assertEqual(selected_indices.shape.is_fully_defined(), False)\n with self.cached_session():\n self.assertAllClose(selected_indices_padded.eval(), [3, 0, 5, 0, 0])\n self.assertEqual(num_valid_padded.eval(), 3)\n self.assertAllClose(selected_indices.eval(), [3, 0, 5])\n self.assertEqual(num_valid.eval(), 3)\n\n\nclass VerifyCompatibleImageShapesTest(test_util.TensorFlowTestCase):\n \"\"\"Tests utility function used by ssim() and psnr().\"\"\"\n\n def testWrongDims(self):\n img = array_ops.placeholder(dtype=dtypes.float32)\n img_np = np.array((2, 2))\n\n with self.test_session(use_gpu=True) as sess:\n _, _, checks = image_ops_impl._verify_compatible_image_shapes(img, img)\n with self.assertRaises(errors.InvalidArgumentError):\n sess.run(checks, {img: img_np})\n\n def testShapeMismatch(self):\n img1 = array_ops.placeholder(dtype=dtypes.float32)\n img2 = array_ops.placeholder(dtype=dtypes.float32)\n\n img1_np = np.array([1, 2, 2, 1])\n img2_np = np.array([1, 3, 3, 1])\n\n with self.test_session(use_gpu=True) as sess:\n _, _, checks = image_ops_impl._verify_compatible_image_shapes(img1, img2)\n with self.assertRaises(errors.InvalidArgumentError):\n sess.run(checks, {img1: img1_np, img2: img2_np})\n\n\nclass PSNRTest(test_util.TensorFlowTestCase):\n \"\"\"Tests for PSNR.\"\"\"\n\n def _LoadTestImage(self, sess, filename):\n content = io_ops.read_file(os.path.join(\n \"tensorflow/core/lib/psnr/testdata\", filename))\n im = image_ops.decode_jpeg(content, dct_method=\"INTEGER_ACCURATE\")\n im = image_ops.convert_image_dtype(im, dtypes.float32)\n im, = sess.run([im])\n return np.expand_dims(im, axis=0)\n\n def _LoadTestImages(self):\n with self.test_session(use_gpu=True) as sess:\n q20 = self._LoadTestImage(sess, \"cat_q20.jpg\")\n q72 = self._LoadTestImage(sess, \"cat_q72.jpg\")\n q95 = self._LoadTestImage(sess, \"cat_q95.jpg\")\n return q20, q72, q95\n\n def _PSNR_NumPy(self, orig, target, max_value):\n \"\"\"Numpy implementation of PSNR.\"\"\"\n mse = ((orig - target) ** 2).mean(axis=(-3, -2, -1))\n return 20 * np.log10(max_value) - 10 * np.log10(mse)\n\n def _RandomImage(self, shape, max_val):\n \"\"\"Returns an image or image batch with given shape.\"\"\"\n return np.random.rand(*shape).astype(np.float32) * max_val\n\n def testPSNRSingleImage(self):\n image1 = self._RandomImage((8, 8, 1), 1)\n image2 = self._RandomImage((8, 8, 1), 1)\n psnr = self._PSNR_NumPy(image1, image2, 1)\n\n with self.test_session(use_gpu=True):\n tf_image1 = constant_op.constant(image1, shape=image1.shape,\n dtype=dtypes.float32)\n tf_image2 = constant_op.constant(image2, shape=image2.shape,\n dtype=dtypes.float32)\n tf_psnr = image_ops.psnr(tf_image1, tf_image2, 1.0, \"psnr\").eval()\n self.assertAllClose(psnr, tf_psnr, atol=0.001)\n\n def testPSNRMultiImage(self):\n image1 = self._RandomImage((10, 8, 8, 1), 1)\n image2 = self._RandomImage((10, 8, 8, 1), 1)\n psnr = self._PSNR_NumPy(image1, image2, 1)\n\n with self.test_session(use_gpu=True):\n tf_image1 = constant_op.constant(image1, shape=image1.shape,\n dtype=dtypes.float32)\n tf_image2 = constant_op.constant(image2, shape=image2.shape,\n dtype=dtypes.float32)\n tf_psnr = image_ops.psnr(tf_image1, tf_image2, 1, \"psnr\").eval()\n self.assertAllClose(psnr, tf_psnr, atol=0.001)\n\n def testGoldenPSNR(self):\n q20, q72, q95 = self._LoadTestImages()\n\n # Verify NumPy implementation first.\n # Golden values are generated using GNU Octave's psnr() function.\n psnr1 = self._PSNR_NumPy(q20, q72, 1)\n self.assertNear(30.321, psnr1, 0.001, msg=\"q20.dtype=\" + str(q20.dtype))\n psnr2 = self._PSNR_NumPy(q20, q95, 1)\n self.assertNear(29.994, psnr2, 0.001)\n psnr3 = self._PSNR_NumPy(q72, q95, 1)\n self.assertNear(35.302, psnr3, 0.001)\n\n # Test TensorFlow implementation.\n with self.test_session(use_gpu=True):\n tf_q20 = constant_op.constant(q20, shape=q20.shape, dtype=dtypes.float32)\n tf_q72 = constant_op.constant(q72, shape=q72.shape, dtype=dtypes.float32)\n tf_q95 = constant_op.constant(q95, shape=q95.shape, dtype=dtypes.float32)\n tf_psnr1 = image_ops.psnr(tf_q20, tf_q72, 1, \"psnr1\").eval()\n tf_psnr2 = image_ops.psnr(tf_q20, tf_q95, 1, \"psnr2\").eval()\n tf_psnr3 = image_ops.psnr(tf_q72, tf_q95, 1, \"psnr3\").eval()\n self.assertAllClose(psnr1, tf_psnr1, atol=0.001)\n self.assertAllClose(psnr2, tf_psnr2, atol=0.001)\n self.assertAllClose(psnr3, tf_psnr3, atol=0.001)\n\n def testInfinity(self):\n q20, _, _ = self._LoadTestImages()\n psnr = self._PSNR_NumPy(q20, q20, 1)\n with self.test_session(use_gpu=True):\n tf_q20 = constant_op.constant(q20, shape=q20.shape, dtype=dtypes.float32)\n tf_psnr = image_ops.psnr(tf_q20, tf_q20, 1, \"psnr\").eval()\n self.assertAllClose(psnr, tf_psnr, atol=0.001)\n\n def testInt(self):\n img1 = self._RandomImage((10, 8, 8, 1), 255)\n img2 = self._RandomImage((10, 8, 8, 1), 255)\n img1 = constant_op.constant(img1, dtypes.uint8)\n img2 = constant_op.constant(img2, dtypes.uint8)\n psnr_uint8 = image_ops.psnr(img1, img2, 255)\n img1 = image_ops.convert_image_dtype(img1, dtypes.float32)\n img2 = image_ops.convert_image_dtype(img2, dtypes.float32)\n psnr_float32 = image_ops.psnr(img1, img2, 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(psnr_uint8.eval(), psnr_float32.eval(), atol=0.001)\n\n\nclass SSIMTest(test_util.TensorFlowTestCase):\n \"\"\"Tests for SSIM.\"\"\"\n\n _filenames = [\"checkerboard1.png\",\n \"checkerboard2.png\",\n \"checkerboard3.png\",]\n\n _ssim = np.asarray([[1.000000, 0.230880, 0.231153],\n [0.230880, 1.000000, 0.996828],\n [0.231153, 0.996828, 1.000000]])\n\n def _LoadTestImage(self, sess, filename):\n content = io_ops.read_file(os.path.join(\n \"tensorflow/core/lib/ssim/testdata\", filename))\n im = image_ops.decode_png(content)\n im = image_ops.convert_image_dtype(im, dtypes.float32)\n im, = sess.run([im])\n return np.expand_dims(im, axis=0)\n\n def _LoadTestImages(self):\n with self.test_session(use_gpu=True) as sess:\n return [self._LoadTestImage(sess, f) for f in self._filenames]\n\n def _RandomImage(self, shape, max_val):\n \"\"\"Returns an image or image batch with given shape.\"\"\"\n return np.random.rand(*shape).astype(np.float32) * max_val\n\n def testAgainstMatlab(self):\n \"\"\"Tests against values produced by Matlab.\"\"\"\n img = self._LoadTestImages()\n expected = self._ssim[np.triu_indices(3)]\n\n ph = [array_ops.placeholder(dtype=dtypes.float32) for _ in range(2)]\n ssim = image_ops.ssim(*ph, max_val=1.0)\n with self.test_session(use_gpu=True):\n scores = [ssim.eval(dict(zip(ph, t)))\n for t in itertools.combinations_with_replacement(img, 2)]\n self.assertAllClose(expected, np.squeeze(scores), atol=1e-4)\n\n def testBatch(self):\n img = self._LoadTestImages()\n expected = self._ssim[np.triu_indices(3, k=1)]\n\n img1, img2 = zip(*itertools.combinations(img, 2))\n img1 = np.concatenate(img1)\n img2 = np.concatenate(img2)\n\n ssim = image_ops.ssim(constant_op.constant(img1),\n constant_op.constant(img2), 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(expected, ssim.eval(), atol=1e-4)\n\n def testBroadcast(self):\n img = self._LoadTestImages()[:2]\n expected = self._ssim[:2, :2]\n\n img = constant_op.constant(np.concatenate(img))\n img1 = array_ops.expand_dims(img, axis=0) # batch dims: 1, 2.\n img2 = array_ops.expand_dims(img, axis=1) # batch dims: 2, 1.\n\n ssim = image_ops.ssim(img1, img2, 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(expected, ssim.eval(), atol=1e-4)\n\n def testNegative(self):\n \"\"\"Tests against negative SSIM index.\"\"\"\n step = np.expand_dims(np.arange(0, 256, 16, dtype=np.uint8), axis=0)\n img1 = np.tile(step, (16, 1))\n img2 = np.fliplr(img1)\n\n img1 = img1.reshape((1, 16, 16, 1))\n img2 = img2.reshape((1, 16, 16, 1))\n\n ssim = image_ops.ssim(constant_op.constant(img1),\n constant_op.constant(img2), 255)\n with self.test_session(use_gpu=True):\n self.assertLess(ssim.eval(), 0)\n\n def testInt(self):\n img1 = self._RandomImage((1, 16, 16, 3), 255)\n img2 = self._RandomImage((1, 16, 16, 3), 255)\n img1 = constant_op.constant(img1, dtypes.uint8)\n img2 = constant_op.constant(img2, dtypes.uint8)\n ssim_uint8 = image_ops.ssim(img1, img2, 255)\n img1 = image_ops.convert_image_dtype(img1, dtypes.float32)\n img2 = image_ops.convert_image_dtype(img2, dtypes.float32)\n ssim_float32 = image_ops.ssim(img1, img2, 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(ssim_uint8.eval(), ssim_float32.eval(), atol=0.001)\n\n\nclass MultiscaleSSIMTest(test_util.TensorFlowTestCase):\n \"\"\"Tests for MS-SSIM.\"\"\"\n\n _filenames = [\"checkerboard1.png\",\n \"checkerboard2.png\",\n \"checkerboard3.png\",]\n\n _msssim = np.asarray([[1.000000, 0.091016, 0.091025],\n [0.091016, 1.000000, 0.999567],\n [0.091025, 0.999567, 1.000000]])\n\n def _LoadTestImage(self, sess, filename):\n content = io_ops.read_file(os.path.join(\n \"tensorflow/core/lib/ssim/testdata\", filename))\n im = image_ops.decode_png(content)\n im = image_ops.convert_image_dtype(im, dtypes.float32)\n im, = sess.run([im])\n return np.expand_dims(im, axis=0)\n\n def _LoadTestImages(self):\n with self.test_session(use_gpu=True) as sess:\n return [self._LoadTestImage(sess, f) for f in self._filenames]\n\n def _RandomImage(self, shape, max_val):\n \"\"\"Returns an image or image batch with given shape.\"\"\"\n return np.random.rand(*shape).astype(np.float32) * max_val\n\n def testAgainstMatlab(self):\n \"\"\"Tests against MS-SSIM computed with Matlab implementation.\n\n For color images, MS-SSIM scores are averaged over color channels.\n \"\"\"\n img = self._LoadTestImages()\n expected = self._msssim[np.triu_indices(3)]\n\n ph = [array_ops.placeholder(dtype=dtypes.float32) for _ in range(2)]\n msssim = image_ops.ssim_multiscale(*ph, max_val=1.0)\n with self.test_session(use_gpu=True):\n scores = [msssim.eval(dict(zip(ph, t)))\n for t in itertools.combinations_with_replacement(img, 2)]\n\n self.assertAllClose(expected, np.squeeze(scores), atol=1e-4)\n\n def testUnweightedIsDifferentiable(self):\n img = self._LoadTestImages()\n ph = [array_ops.placeholder(dtype=dtypes.float32) for _ in range(2)]\n scalar = constant_op.constant(1.0, dtype=dtypes.float32)\n scaled_ph = [x * scalar for x in ph]\n msssim = image_ops.ssim_multiscale(*scaled_ph, max_val=1.0,\n power_factors=(1, 1, 1, 1, 1))\n grads = gradients.gradients(msssim, scalar)\n with self.test_session(use_gpu=True) as sess:\n np_grads = sess.run(grads, feed_dict={ph[0]: img[0], ph[1]: img[1]})\n self.assertTrue(np.isfinite(np_grads).all())\n\n def testBatch(self):\n \"\"\"Tests MS-SSIM computed in batch.\"\"\"\n img = self._LoadTestImages()\n expected = self._msssim[np.triu_indices(3, k=1)]\n\n img1, img2 = zip(*itertools.combinations(img, 2))\n img1 = np.concatenate(img1)\n img2 = np.concatenate(img2)\n\n msssim = image_ops.ssim_multiscale(constant_op.constant(img1),\n constant_op.constant(img2), 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(expected, msssim.eval(), 1e-4)\n\n def testBroadcast(self):\n \"\"\"Tests MS-SSIM broadcasting.\"\"\"\n img = self._LoadTestImages()[:2]\n expected = self._msssim[:2, :2]\n\n img = constant_op.constant(np.concatenate(img))\n img1 = array_ops.expand_dims(img, axis=0) # batch dims: 1, 2.\n img2 = array_ops.expand_dims(img, axis=1) # batch dims: 2, 1.\n\n score_tensor = image_ops.ssim_multiscale(img1, img2, 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(expected, score_tensor.eval(), 1e-4)\n\n def testRange(self):\n \"\"\"Tests against low MS-SSIM score.\n\n MS-SSIM is a geometric mean of SSIM and CS scores of various scales.\n If any of the value is negative so that the geometric mean is not\n well-defined, then treat the MS-SSIM score as zero.\n \"\"\"\n with self.test_session(use_gpu=True) as sess:\n img1 = self._LoadTestImage(sess, \"checkerboard1.png\")\n img2 = self._LoadTestImage(sess, \"checkerboard3.png\")\n images = [img1, img2, np.zeros_like(img1),\n np.full_like(img1, fill_value=255)]\n\n images = [ops.convert_to_tensor(x, dtype=dtypes.float32) for x in images]\n msssim_ops = [image_ops.ssim_multiscale(x, y, 1.0)\n for x, y in itertools.combinations(images, 2)]\n msssim = sess.run(msssim_ops)\n msssim = np.squeeze(msssim)\n\n self.assertTrue(np.all(msssim >= 0.0))\n self.assertTrue(np.all(msssim <= 1.0))\n\n def testInt(self):\n img1 = self._RandomImage((1, 180, 240, 3), 255)\n img2 = self._RandomImage((1, 180, 240, 3), 255)\n img1 = constant_op.constant(img1, dtypes.uint8)\n img2 = constant_op.constant(img2, dtypes.uint8)\n ssim_uint8 = image_ops.ssim_multiscale(img1, img2, 255)\n img1 = image_ops.convert_image_dtype(img1, dtypes.float32)\n img2 = image_ops.convert_image_dtype(img2, dtypes.float32)\n ssim_float32 = image_ops.ssim_multiscale(img1, img2, 1.0)\n with self.test_session(use_gpu=True):\n self.assertAllClose(ssim_uint8.eval(), ssim_float32.eval(), atol=0.001)\n\n\nclass ImageGradientsTest(test_util.TensorFlowTestCase):\n\n def testImageGradients(self):\n shape = [1, 2, 4, 1]\n img = constant_op.constant([[1, 3, 4, 2], [8, 7, 5, 6]])\n img = array_ops.reshape(img, shape)\n\n expected_dy = np.reshape([[7, 4, 1, 4], [0, 0, 0, 0]], shape)\n expected_dx = np.reshape([[2, 1, -2, 0], [-1, -2, 1, 0]], shape)\n\n dy, dx = image_ops.image_gradients(img)\n with self.cached_session():\n actual_dy = dy.eval()\n actual_dx = dx.eval()\n self.assertAllClose(expected_dy, actual_dy)\n self.assertAllClose(expected_dx, actual_dx)\n\n def testImageGradientsMultiChannelBatch(self):\n batch = [[[[1, 2], [2, 5], [3, 3]],\n [[8, 4], [5, 1], [9, 8]]],\n [[[5, 3], [7, 9], [1, 6]],\n [[1, 2], [6, 3], [6, 3]]]]\n\n expected_dy = [[[[7, 2], [3, -4], [6, 5]],\n [[0, 0], [0, 0], [0, 0]]],\n [[[-4, -1], [-1, -6], [5, -3]],\n [[0, 0], [0, 0], [0, 0]]]]\n\n expected_dx = [[[[1, 3], [1, -2], [0, 0]],\n [[-3, -3], [4, 7], [0, 0]]],\n [[[2, 6], [-6, -3], [0, 0]],\n [[5, 1], [0, 0], [0, 0]]]]\n\n batch = constant_op.constant(batch)\n assert batch.get_shape().as_list() == [2, 2, 3, 2]\n dy, dx = image_ops.image_gradients(batch)\n with self.test_session(use_gpu=True):\n actual_dy = dy.eval()\n actual_dx = dx.eval()\n self.assertAllClose(expected_dy, actual_dy)\n self.assertAllClose(expected_dx, actual_dx)\n\n def testImageGradientsBadShape(self):\n # [2 x 4] image but missing batch and depth dimensions.\n img = constant_op.constant([[1, 3, 4, 2], [8, 7, 5, 6]])\n with self.assertRaises(ValueError):\n image_ops.image_gradients(img)\n\n\nclass SobelEdgesTest(test_util.TensorFlowTestCase):\n\n def testSobelEdges1x2x3x1(self):\n img = constant_op.constant([[1, 3, 6], [4, 1, 5]],\n dtype=dtypes.float32, shape=[1, 2, 3, 1])\n expected = np.reshape([[[0, 0], [0, 12], [0, 0]],\n [[0, 0], [0, 12], [0, 0]]], [1, 2, 3, 1, 2])\n sobel = image_ops.sobel_edges(img)\n with self.test_session(use_gpu=True):\n actual_sobel = sobel.eval()\n self.assertAllClose(expected, actual_sobel)\n\n def testSobelEdges5x3x4x2(self):\n batch_size = 5\n plane = np.reshape([[1, 3, 6, 2], [4, 1, 5, 7], [2, 5, 1, 4]],\n [1, 3, 4, 1])\n two_channel = np.concatenate([plane, plane], axis=3)\n batch = np.concatenate([two_channel] * batch_size, axis=0)\n img = constant_op.constant(batch, dtype=dtypes.float32,\n shape=[batch_size, 3, 4, 2])\n\n expected_plane = np.reshape([[[0, 0], [0, 12], [0, 10], [0, 0]],\n [[6, 0], [0, 6], [-6, 10], [-6, 0]],\n [[0, 0], [0, 0], [0, 10], [0, 0]]],\n [1, 3, 4, 1, 2])\n expected_two_channel = np.concatenate(\n [expected_plane, expected_plane], axis=3)\n expected_batch = np.concatenate([expected_two_channel] * batch_size, axis=0)\n\n sobel = image_ops.sobel_edges(img)\n with self.test_session(use_gpu=True):\n actual_sobel = sobel.eval()\n self.assertAllClose(expected_batch, actual_sobel)\n\n\nclass DecodeImageTest(test_util.TensorFlowTestCase):\n\n def testJpegUint16(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/jpeg/testdata\"\n jpeg0 = io_ops.read_file(os.path.join(base, \"jpeg_merge_test1.jpg\"))\n image0 = image_ops.decode_image(jpeg0, dtype=dtypes.uint16)\n image1 = image_ops.convert_image_dtype(image_ops.decode_jpeg(jpeg0),\n dtypes.uint16)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testPngUint16(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/png/testdata\"\n png0 = io_ops.read_file(os.path.join(base, \"lena_rgba.png\"))\n image0 = image_ops.decode_image(png0, dtype=dtypes.uint16)\n image1 = image_ops.convert_image_dtype(\n image_ops.decode_png(png0, dtype=dtypes.uint16), dtypes.uint16)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testGifUint16(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/gif/testdata\"\n gif0 = io_ops.read_file(os.path.join(base, \"scan.gif\"))\n image0 = image_ops.decode_image(gif0, dtype=dtypes.uint16)\n image1 = image_ops.convert_image_dtype(image_ops.decode_gif(gif0),\n dtypes.uint16)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testBmpUint16(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/bmp/testdata\"\n bmp0 = io_ops.read_file(os.path.join(base, \"lena.bmp\"))\n image0 = image_ops.decode_image(bmp0, dtype=dtypes.uint16)\n image1 = image_ops.convert_image_dtype(image_ops.decode_bmp(bmp0),\n dtypes.uint16)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testJpegFloat32(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/jpeg/testdata\"\n jpeg0 = io_ops.read_file(os.path.join(base, \"jpeg_merge_test1.jpg\"))\n image0 = image_ops.decode_image(jpeg0, dtype=dtypes.float32)\n image1 = image_ops.convert_image_dtype(image_ops.decode_jpeg(jpeg0),\n dtypes.float32)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testPngFloat32(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/png/testdata\"\n png0 = io_ops.read_file(os.path.join(base, \"lena_rgba.png\"))\n image0 = image_ops.decode_image(png0, dtype=dtypes.float32)\n image1 = image_ops.convert_image_dtype(\n image_ops.decode_png(png0, dtype=dtypes.uint16), dtypes.float32)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testGifFloat32(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/gif/testdata\"\n gif0 = io_ops.read_file(os.path.join(base, \"scan.gif\"))\n image0 = image_ops.decode_image(gif0, dtype=dtypes.float32)\n image1 = image_ops.convert_image_dtype(image_ops.decode_gif(gif0),\n dtypes.float32)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n def testBmpFloat32(self):\n with self.test_session(use_gpu=True) as sess:\n base = \"tensorflow/core/lib/bmp/testdata\"\n bmp0 = io_ops.read_file(os.path.join(base, \"lena.bmp\"))\n image0 = image_ops.decode_image(bmp0, dtype=dtypes.float32)\n image1 = image_ops.convert_image_dtype(image_ops.decode_bmp(bmp0),\n dtypes.float32)\n image0, image1 = sess.run([image0, image1])\n self.assertAllEqual(image0, image1)\n\n\nif __name__ == \"__main__\":\n googletest.main()\n", "# Copyright 2017 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Tests for Momentum.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\nfrom tensorflow.compiler.tests import xla_test\nfrom tensorflow.python.framework import constant_op\nfrom tensorflow.python.framework import dtypes\nfrom tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import resource_variable_ops\nfrom tensorflow.python.ops import variables\nfrom tensorflow.python.platform import test\nfrom tensorflow.python.training import momentum as momentum_lib\n\n\nclass MomentumOptimizerTest(xla_test.XLATestCase):\n\n def _update_nesterov_momentum_numpy(self, var, accum, g, lr, momentum):\n var += accum * lr * momentum\n accum = accum * momentum + g\n var -= lr * accum\n var -= accum * lr * momentum\n return var, accum\n\n def testBasic(self):\n for dtype in self.float_types:\n with self.cached_session(), self.test_scope():\n var0 = resource_variable_ops.ResourceVariable([1.0, 2.0], dtype=dtype)\n var1 = resource_variable_ops.ResourceVariable([3.0, 4.0], dtype=dtype)\n grads0 = constant_op.constant([0.1, 0.1], dtype=dtype)\n grads1 = constant_op.constant([0.01, 0.01], dtype=dtype)\n mom_opt = momentum_lib.MomentumOptimizer(\n learning_rate=2.0, momentum=0.9)\n mom_update = mom_opt.apply_gradients(\n zip([grads0, grads1], [var0, var1]))\n variables.global_variables_initializer().run()\n # Check we have slots\n self.assertEqual([\"momentum\"], mom_opt.get_slot_names())\n slot0 = mom_opt.get_slot(var0, \"momentum\")\n self.assertEquals(slot0.get_shape(), var0.get_shape())\n self.assertFalse(slot0 in variables.trainable_variables())\n slot1 = mom_opt.get_slot(var1, \"momentum\")\n self.assertEquals(slot1.get_shape(), var1.get_shape())\n self.assertFalse(slot1 in variables.trainable_variables())\n\n # Fetch params to validate initial values\n self.assertAllClose([1.0, 2.0], var0.eval())\n self.assertAllClose([3.0, 4.0], var1.eval())\n # Step 1: the momentum accumulators where 0. So we should see a normal\n # update: v -= grad * learning_rate\n mom_update.run()\n # Check that the momentum accumulators have been updated.\n self.assertAllCloseAccordingToType(np.array([0.1, 0.1]), slot0.eval())\n self.assertAllCloseAccordingToType(np.array([0.01, 0.01]), slot1.eval())\n # Check that the parameters have been updated.\n self.assertAllCloseAccordingToType(\n np.array([1.0 - (0.1 * 2.0), 2.0 - (0.1 * 2.0)]), var0.eval())\n self.assertAllCloseAccordingToType(\n np.array([3.0 - (0.01 * 2.0), 4.0 - (0.01 * 2.0)]), var1.eval())\n # Step 2: the momentum accumulators contain the previous update.\n mom_update.run()\n # Check that the momentum accumulators have been updated.\n self.assertAllCloseAccordingToType(\n np.array([(0.9 * 0.1 + 0.1), (0.9 * 0.1 + 0.1)]), slot0.eval())\n self.assertAllCloseAccordingToType(\n np.array([(0.9 * 0.01 + 0.01), (0.9 * 0.01 + 0.01)]), slot1.eval())\n # Check that the parameters have been updated.\n self.assertAllCloseAccordingToType(\n np.array([\n 1.0 - (0.1 * 2.0) - ((0.9 * 0.1 + 0.1) * 2.0),\n 2.0 - (0.1 * 2.0) - ((0.9 * 0.1 + 0.1) * 2.0)\n ]), var0.eval())\n self.assertAllCloseAccordingToType(\n np.array([\n 2.98 - ((0.9 * 0.01 + 0.01) * 2.0), 3.98 - (\n (0.9 * 0.01 + 0.01) * 2.0)\n ]), var1.eval())\n\n def testNesterovMomentum(self):\n for dtype in self.float_types:\n with self.cached_session(), self.test_scope():\n var0 = resource_variable_ops.ResourceVariable([0.1, 0.2], dtype=dtype)\n var1 = resource_variable_ops.ResourceVariable([0.3, 0.4], dtype=dtype)\n var0_np = np.array([0.1, 0.2], dtype=dtype)\n var1_np = np.array([0.3, 0.4], dtype=dtype)\n accum0_np = np.array([0.0, 0.0], dtype=dtype)\n accum1_np = np.array([0.0, 0.0], dtype=dtype)\n cost = 0.4 * var0 * var0 + 0.9 * var1\n global_step = resource_variable_ops.ResourceVariable(\n array_ops.zeros([], dtypes.int32), name=\"global_step\")\n mom_op = momentum_lib.MomentumOptimizer(\n learning_rate=0.1, momentum=0.9, use_nesterov=True)\n opt_op = mom_op.minimize(cost, global_step, [var0, var1])\n variables.global_variables_initializer().run()\n for _ in range(1, 5):\n opt_op.run()\n var0_np, accum0_np = self._update_nesterov_momentum_numpy(\n var0_np, accum0_np, var0_np * 0.8, 0.1, 0.9)\n var1_np, accum1_np = self._update_nesterov_momentum_numpy(\n var1_np, accum1_np, 0.9, 0.1, 0.9)\n self.assertAllCloseAccordingToType(var0_np, var0.eval())\n self.assertAllCloseAccordingToType(var1_np, var1.eval())\n\n def testTensorLearningRateAndMomentum(self):\n for dtype in self.float_types:\n with self.cached_session(), self.test_scope():\n var0 = resource_variable_ops.ResourceVariable([1.0, 2.0], dtype=dtype)\n var1 = resource_variable_ops.ResourceVariable([3.0, 4.0], dtype=dtype)\n grads0 = constant_op.constant([0.1, 0.1], dtype=dtype)\n grads1 = constant_op.constant([0.01, 0.01], dtype=dtype)\n mom_opt = momentum_lib.MomentumOptimizer(\n learning_rate=constant_op.constant(2.0),\n momentum=constant_op.constant(0.9))\n mom_update = mom_opt.apply_gradients(\n zip([grads0, grads1], [var0, var1]))\n variables.global_variables_initializer().run()\n # Check we have slots\n self.assertEqual([\"momentum\"], mom_opt.get_slot_names())\n slot0 = mom_opt.get_slot(var0, \"momentum\")\n self.assertEquals(slot0.get_shape(), var0.get_shape())\n self.assertFalse(slot0 in variables.trainable_variables())\n slot1 = mom_opt.get_slot(var1, \"momentum\")\n self.assertEquals(slot1.get_shape(), var1.get_shape())\n self.assertFalse(slot1 in variables.trainable_variables())\n\n # Fetch params to validate initial values\n self.assertAllClose([1.0, 2.0], var0.eval())\n self.assertAllClose([3.0, 4.0], var1.eval())\n # Step 1: the momentum accumulators where 0. So we should see a normal\n # update: v -= grad * learning_rate\n mom_update.run()\n # Check that the momentum accumulators have been updated.\n self.assertAllCloseAccordingToType(np.array([0.1, 0.1]), slot0.eval())\n self.assertAllCloseAccordingToType(np.array([0.01, 0.01]), slot1.eval())\n # Check that the parameters have been updated.\n self.assertAllCloseAccordingToType(\n np.array([1.0 - (0.1 * 2.0), 2.0 - (0.1 * 2.0)]), var0.eval())\n self.assertAllCloseAccordingToType(\n np.array([3.0 - (0.01 * 2.0), 4.0 - (0.01 * 2.0)]), var1.eval())\n # Step 2: the momentum accumulators contain the previous update.\n mom_update.run()\n # Check that the momentum accumulators have been updated.\n self.assertAllCloseAccordingToType(\n np.array([(0.9 * 0.1 + 0.1), (0.9 * 0.1 + 0.1)]), slot0.eval())\n self.assertAllCloseAccordingToType(\n np.array([(0.9 * 0.01 + 0.01), (0.9 * 0.01 + 0.01)]), slot1.eval())\n # Check that the parameters have been updated.\n self.assertAllCloseAccordingToType(\n np.array([\n 1.0 - (0.1 * 2.0) - ((0.9 * 0.1 + 0.1) * 2.0),\n 2.0 - (0.1 * 2.0) - ((0.9 * 0.1 + 0.1) * 2.0)\n ]), var0.eval())\n self.assertAllCloseAccordingToType(\n np.array([\n 2.98 - ((0.9 * 0.01 + 0.01) * 2.0), 3.98 - (\n (0.9 * 0.01 + 0.01) * 2.0)\n ]), var1.eval())\n\n\nif __name__ == \"__main__\":\n test.main()\n" ]
[ [ "tensorflow.python.ops.nn_ops.dropout", "tensorflow.python.ops.math_ops.mod", "tensorflow.python.ops.rnn_cell_impl.assert_like_rnncell", "tensorflow.python.ops.math_ops.tanh", "tensorflow.python.platform.tf_logging.warn", "tensorflow.python.ops.variable_scope.variable_scope", "tensorflow.python.util.nest.flatten", "tensorflow.python.ops.nn_ops.bias_add", "tensorflow.python.util.nest.is_sequence", "tensorflow.python.ops.array_ops.expand_dims", "tensorflow.python.framework.tensor_shape.TensorShape", "tensorflow.python.util.nest.assert_same_structure", "tensorflow.contrib.layers.python.layers.layers.layer_norm", "tensorflow.python.framework.constant_op.constant", "tensorflow.python.ops.init_ops.random_uniform_initializer", "tensorflow.python.framework.ops.add_to_collection", "tensorflow.python.ops.variable_scope.get_variable", "tensorflow.python.ops.math_ops.matmul", "tensorflow.python.ops.array_ops.slice", "tensorflow.python.ops.init_ops.zeros_initializer", "tensorflow.python.ops.gen_array_ops.tile", "tensorflow.python.ops.init_ops.constant_initializer", "tensorflow.python.ops.nn_ops.xw_plus_b", "tensorflow.python.ops.array_ops.zeros", "tensorflow.python.ops.array_ops.shape", "tensorflow.python.ops.nn_impl.l2_normalize", "tensorflow.python.framework.ops.control_dependencies", "tensorflow.python.ops.math_ops.cast", "tensorflow.python.ops.array_ops.constant", "tensorflow.python.ops.clip_ops.clip_by_value", "tensorflow.python.ops.variable_scope.get_variable_scope", "tensorflow.python.layers.base.InputSpec", "tensorflow.python.ops.math_ops.sigmoid", "tensorflow.python.ops.math_ops.log", "tensorflow.python.framework.ops.get_collection", "tensorflow.python.ops.array_ops.split", "tensorflow.python.ops.array_ops.concat", "tensorflow.python.ops.rnn_cell_impl.LSTMStateTuple", "tensorflow.python.ops.array_ops.reshape", "tensorflow.python.ops.array_ops.where", "tensorflow.python.framework.op_def_registry.get_registered_ops", "tensorflow.python.util.nest.map_structure", "tensorflow.contrib.compiler.jit.experimental_jit_scope" ], [ "numpy.ones", "tensorflow.python.ops.image_ops.psnr", "tensorflow.python.platform.test.is_gpu_available", "numpy.asarray", "numpy.dstack", "tensorflow.python.ops.image_ops.non_max_suppression", "tensorflow.python.ops.image_ops.convert_image_dtype", "tensorflow.python.ops.io_ops.read_file", "numpy.abs", "tensorflow.python.ops.image_ops.image_gradients", "tensorflow.python.ops.image_ops.decode_gif", "tensorflow.python.ops.gen_image_ops.adjust_saturation", "tensorflow.python.ops.gradients.gradients", "numpy.mean", "tensorflow.python.ops.array_ops.unstack", "tensorflow.python.framework.ops.device", "tensorflow.python.ops.array_ops.shape", "tensorflow.python.ops.image_ops.rgb_to_yiq", "tensorflow.python.framework.ops.control_dependencies", "tensorflow.python.ops.image_ops.yuv_to_rgb", "tensorflow.python.ops.image_ops.adjust_brightness", "tensorflow.python.ops.image_ops.decode_image", "tensorflow.python.framework.ops.convert_to_tensor", "numpy.square", "tensorflow.python.ops.image_ops.resize_image_with_crop_or_pad", "tensorflow.python.ops.image_ops.ssim_multiscale", "tensorflow.core.protobuf.config_pb2.ConfigProto", "numpy.rot90", "tensorflow.python.ops.array_ops.strided_slice", "numpy.array", "tensorflow.python.ops.image_ops.adjust_gamma", "tensorflow.python.ops.image_ops.hsv_to_rgb", "numpy.random.seed", "tensorflow.python.ops.image_ops.crop_to_bounding_box", "tensorflow.python.ops.image_ops.adjust_hue", "tensorflow.python.ops.image_ops.rgb_to_yuv", "tensorflow.python.ops.image_ops.random_flip_left_right", "tensorflow.python.framework.constant_op.constant", "numpy.vstack", "tensorflow.python.ops.image_ops.sobel_edges", "numpy.reshape", "tensorflow.python.ops.image_ops.rgb_to_grayscale", "tensorflow.python.ops.image_ops.sample_distorted_bounding_box", "tensorflow.python.ops.image_ops.flip_left_right", "numpy.tile", "tensorflow.python.platform.test.gpu_device_name", "tensorflow.python.ops.image_ops.resize_bicubic", "numpy.triu_indices", "tensorflow.python.ops.image_ops.per_image_standardization", "tensorflow.python.ops.image_ops.decode_jpeg", "tensorflow.python.ops.image_ops.resize_area", "numpy.zeros_like", "numpy.sqrt", "numpy.concatenate", "tensorflow.python.ops.image_ops.resize_bilinear", "tensorflow.python.ops.image_ops.random_flip_up_down", "numpy.sum", "numpy.histogram", "tensorflow.python.ops.array_ops.expand_dims", "numpy.full_like", "tensorflow.python.ops.image_ops.decode_bmp", "tensorflow.python.ops.image_ops.adjust_contrast", "tensorflow.python.ops.math_ops.add", "numpy.fliplr", "tensorflow.python.ops.image_ops.resize_images", "tensorflow.python.ops.image_ops.grayscale_to_rgb", "tensorflow.python.client.session.Session", "numpy.random.rand", "numpy.isnan", "tensorflow.python.ops.control_flow_ops.group", "tensorflow.python.ops.random_ops.random_uniform", "numpy.random.uniform", "tensorflow.python.ops.image_ops.rot90", "tensorflow.python.ops.image_ops.transpose_image", "tensorflow.python.ops.image_ops.decode_and_crop_jpeg", "numpy.zeros", "numpy.arange", "tensorflow.python.ops.image_ops.non_max_suppression_padded", "tensorflow.python.ops.image_ops.flip_up_down", "numpy.prod", "numpy.cumsum", "tensorflow.python.ops.image_ops.extract_jpeg_shape", "tensorflow.python.ops.array_ops.reshape", "tensorflow.python.ops.image_ops.yiq_to_rgb", "tensorflow.python.ops.image_ops.encode_jpeg", "numpy.isfinite", "tensorflow.python.ops.image_ops.adjust_saturation", "tensorflow.python.framework.ops.Graph", "tensorflow.python.ops.array_ops.placeholder", "tensorflow.python.ops.image_ops.encode_png", "tensorflow.python.ops.image_ops.total_variation", "numpy.ndarray", "numpy.expand_dims", "numpy.log10", "tensorflow.python.ops.image_ops.pad_to_bounding_box", "tensorflow.python.ops.image_ops.resize_image_with_pad", "tensorflow.python.ops.image_ops.ssim", "tensorflow.python.ops.variables.global_variables_initializer", "tensorflow.python.ops.image_ops.decode_png", "numpy.all", "tensorflow.python.ops.random_ops.random_normal", "tensorflow.python.ops.image_ops.central_crop", "tensorflow.python.ops.image_ops.rgb_to_hsv", "numpy.empty", "numpy.squeeze", "tensorflow.python.platform.googletest.main", "tensorflow.python.ops.image_ops_impl._verify_compatible_image_shapes", "tensorflow.python.ops.array_ops.stack" ], [ "tensorflow.python.ops.array_ops.zeros", "tensorflow.python.ops.variables.global_variables_initializer", "tensorflow.python.ops.variables.trainable_variables", "tensorflow.python.platform.test.main", "tensorflow.python.ops.resource_variable_ops.ResourceVariable", "tensorflow.python.training.momentum.MomentumOptimizer", "numpy.array", "tensorflow.python.framework.constant_op.constant" ] ]
aribasadme/CrimeDB
[ "a16b35204921726ec6f3cf9a7ec5d9b51cbe7d49" ]
[ "crime_db.py" ]
[ "import pandas as pd\nfrom police_api import PoliceAPI\n\n\ndef first_job(api, dates, t_current):\n \"\"\"\n Creates the tables and populates them with the historical data\n from ​T​_0\n​ ​to ​T​_current\n \"\"\"\n # subset of dates\n dates_hist = dates[dates <= t_current]\n\n # crime_categories table\n s_crime_cat = set()\n for date in dates_hist:\n s_crime_cat.update(api.get_crime_categories(date))\n\n crime_categories['id'] = [c.url for c in s_crime_cat]\n crime_categories['description'] = [c.name for c in s_crime_cat]\n crime_categories.set_index('id', inplace=True)\n\n # To get the crimes for each force and neighbourhood\n cr = []\n for d in date_hist:\n cr.append([api.get_crimes_area(n.boundary, date=d) for n in s_nb_flat])\n # Flattern the list\n crimes_flat = [c for sublist1 in cr for sublist2 in sublist1 for c in sublist2]\n # Subset for those containing a valid \"persistent_id\"\n crimes_flat[:] = [c.__dict__ for c in crimes_flat if c.persistent_id != '']\n # Convert to DataFrame\n df_crimes = pd.DataFrame(crimes_flat)\n df_crimes = df_crimes[['month', 'category', 'id', 'persistent_id', \n 'location', 'context', 'outcome_status']]\n # Get the key values for the objects in each column\n crimes['latitude'] = df_crimes['location'].apply(lambda x: x.latitude)\n crimes['longitude'] = df_crimes['location'].apply(lambda x: x.longitude)\n crimes['street'] = df_crimes['location'].apply(lambda x: x.street)\n\n ### outcome_categories table ###\n # Get outcome_status to populate outcome_categories table\n outcome_status = crimes.pop('outcome_status')\n df_outcomes = pd.DataFrame(outcome_status.apply(lambda x: x.__dict__).to_list())\n df_outcomes.pop('api')\n outcome_categories['id'] = df_outcomes['category'].apply(lambda x: x['id'])\n outcome_categories['name'] = df_outcomes['category'].apply(lambda x: x['name'])\n # Drop duplicates\n outcome_categories = outcome_categories.loc[outcome_categories.name.drop_duplicates().index]\n outcome_categories.set_index('id', inplace=True)\n\n ### streets table ###\n # Get streets to populate streets table\n s_streets = crimes['street']\n streets['id'] = s_streets.apply(lambda x: x['id'])\n streets['name'] = s_streets.apply(lambda x: x['name'])\n # Drop duplicates\n streets = streets.loc[streets.id.drop_duplicates().index]\n streets.set_index('id', inplace=True)\n\n # Clean crimes table\n crimes['street'] = crimes['street'].apply(lambda x: x['id'])\n # rename 'month' to 'date'\n crimes.rename(columns={\"month\": \"date\"}, inplace=True)\n # Ordering columns\n cols = ['persistent_id', 'category', 'street', 'latitude', 'longitude', 'date', 'context']\n crimes = crimes[cols]\n crimes.set_index('persistent_id', inplace=True)\n\n ### outcomes table ###\n crime_idx = crimes.index.to_list()\n l_outcomes = [api.get_crime(idx).outcomes for idx in crime_idx]\n l_outcomes_flat = [o for sublist in l_outcomes for o in sublist]\n outcomes['crime'] = [o.crime.id for o in l_outcomes_flat]\n outcomes['category'] = [o.category.id for o in l_outcomes_flat]\n outcomes['date'] = [o.date for o in l_outcomes_flat]\n outcomes['person_id'] = [' ' for o in l_outcomes_flat] # person_id is empty given by the api\n outcomes.drop_duplicates(['crime', 'category'], inplace=True)\n outcomes.set_index(['crime', 'category'], inplace=True)\n\n\ndef second_job(api, dates, t_last_update, t_current):\n dates_upd = dates[dates <= t_current and dates >= t_last_update]\n\n s_crime_cat = set()\n for date in dates_upd:\n s_crime_cat.update(api.get_crime_categories(date))\n url = [c.url for c in s_crime_cat]\n name = [c.name for c in s_crime_cat]\n df_crime_categories = pd.DataFrame.from_dict({'id': url, 'description': name})\n df_crime_categories.set_index('id')\n\n crime_categories.append(df_crime_categories, ignore_index=True)\n\n cr = []\n for d in dates_upd:\n cr.append([api.get_crimes_area(n.boundary, date=d) for n in s_nb_flat])\n # Flattern the list\n crimes_flat = [c for sublist1 in cr for sublist2 in sublist1 for c in sublist2]\n # Subset for those containing a valid \"persistent_id\"\n crimes_flat[:] = [c.__dict__ for c in crimes_flat if c.persistent_id!='']\n # Convert to DataFrame\n df_crimes = pd.DataFrame(crimes_flat)\n df_crimes = df_crimes[['month', 'category', 'id', 'persistent_id', 'location', 'context', 'outcome_status']]\n # Get the key values for the objects in each column\n df_crimes['latitude'] = df_crimes['location'].apply(lambda x: x.latitude)\n df_crimes['longitude'] = df_crimes['location'].apply(lambda x: x.longitude)\n df_crimes['street'] = df_crimes['location'].apply(lambda x: x.street)\n\n ### outcome_categories table ###\n # Get outcome_status to populate outcome_categories table\n outcome_status = df_crimes.pop('outcome_status')\n df_outcomes = pd.DataFrame(outcome_status.apply(lambda x: x.__dict__).to_list())\n df_outcomes.pop('api')\n df_outcome_categories = pd.DataFrame({'id': [], 'description': []})\n df_outcome_categories['id'] = df_outcomes['category'].apply(lambda x: x['id'])\n df_outcome_categories['description'] = df_outcomes['category'].apply(lambda x: x['name'])\n # Drop duplicates\n df_outcome_categories = df_outcome_categories.loc[df_outcome_categoriesdf_outcome_categories.name.drop_duplicates().index]\n df_outcome_categories.set_index('id', inplace=True)\n\n outcome_categories.append(df_outcome_categories, ignore_index=True)\n\n\n ### streets table ###\n # Get streets to populate streets table\n s_streets = crimes['street']\n df_streets = pd.DataFrame({'id': [], 'name': []})\n df_streets['id'] = s_streets.apply(lambda x: x['id'])\n df_streets['name'] = s_streets.apply(lambda x: x['name'])\n # Drop duplicates\n df_streets = df_streets.loc[df_streets.id.drop_duplicates().index]\n df_streets.set_index('id', inplace=True)\n streets.append(df_streets, ignore_index=True)\n\n # Clean crimes table\n df_crimes['street'] = df_crimes['street'].apply(lambda x: x['id'])\n # rename 'month' to 'date'\n df_crimes.rename(columns={\"month\": \"date\"}, inplace=True)\n # Ordering columns\n cols = ['persistent_id', 'category', 'street', 'latitude', 'longitude', 'date', 'context']\n df_crimes = crimes[cols]\n df_crimes.set_index('persistent_id', inplace=True)\n\n crimes.append(df_crimes, ignore_index=True)\n\n ### outcomes table ###\n crime_idx = crimes.index.to_list()\n l_outcomes = [api.get_crime(idx).outcomes for idx in crime_idx]\n l_outcomes_flat = [o for sublist in l_outcomes for o in sublist]\n df_outcomes = pd.DataFrame({'crime': [], 'category': [], 'date': [], 'person_id': []})\n df_outcomes['crime'] = [o.crime.id for o in l_outcomes_flat]\n df_outcomes['category'] = [o.category.id for o in l_outcomes_flat]\n df_outcomes['date'] = [o.date for o in l_outcomes_flat]\n df_outcomes['person_id'] = [' ' for o in l_outcomes_flat] # person_id is empty given by the api\n df_outcomes.drop_duplicates(['crime', 'category'], inplace=True)\n df_outcomes.set_index(['crime', 'category'], inplace=True)\n\n outcomes.append(df_outcomes, ignore_index=True)\n\ndef last_job(api, dates, t_current):\n dates_upd = dates[dates == t_current]\n\n s_crime_cat = set()\n for date in dates_upd:\n s_crime_cat.update(api.get_crime_categories(date))\n url = [c.url for c in s_crime_cat]\n name = [c.name for c in s_crime_cat]\n df_crime_categories = pd.DataFrame.from_dict({'id': url, 'description': name})\n df_crime_categories.set_index('id')\n\n crime_categories.append(df_crime_categories, ignore_index=True)\n\n cr = []\n for d in dates_upd:\n cr.append([api.get_crimes_area(n.boundary, date=d) for n in s_nb_flat])\n # Flattern the list\n crimes_flat = [c for sublist1 in cr for sublist2 in sublist1 for c in sublist2]\n # Subset for those containing a valid \"persistent_id\"\n crimes_flat[:] = [c.__dict__ for c in crimes_flat if c.persistent_id!='']\n # Convert to DataFrame\n df_crimes = pd.DataFrame(crimes_flat)\n df_crimes = df_crimes[['month', 'category', 'id', 'persistent_id', 'location', 'context', 'outcome_status']]\n # Get the key values for the objects in each column\n df_crimes['latitude'] = df_crimes['location'].apply(lambda x: x.latitude)\n df_crimes['longitude'] = df_crimes['location'].apply(lambda x: x.longitude)\n df_crimes['street'] = df_crimes['location'].apply(lambda x: x.street)\n\n\n ## outcome_categories table ##\n # Get outcome_status to populate outcome_categories table\n outcome_status = df_crimes.pop('outcome_status')\n df_outcomes = pd.DataFrame(outcome_status.apply(lambda x: x.__dict__).to_list())\n df_outcomes.pop('api')\n df_outcome_categories = pd.DataFrame({'id': [], 'description': []})\n df_outcome_categories['id'] = df_outcomes['category'].apply(lambda x: x['id'])\n df_outcome_categories['description'] = df_outcomes['category'].apply(lambda x: x['name'])\n # Drop duplicates\n df_outcome_categories = df_outcome_categories.loc[df_outcome_categoriesdf_outcome_categories.name.drop_duplicates().index]\n df_outcome_categories.set_index('id', inplace=True)\n\n outcome_categories.append(df_outcome_categories, ignore_index=True)\n\n\n ### streets table ###\n # Get streets to populate streets table\n s_streets = crimes['street']\n df_streets = pd.DataFrame({'id': [], 'name': []})\n df_streets['id'] = s_streets.apply(lambda x: x['id'])\n df_streets['name'] = s_streets.apply(lambda x: x['name'])\n # Drop duplicates\n df_streets = df_streets.loc[df_streets.id.drop_duplicates().index]\n df_streets.set_index('id', inplace=True)\n streets.append(df_streets, ignore_index=True)\n\n # Clean crimes table\n df_crimes['street'] = df_crimes['street'].apply(lambda x: x['id'])\n # rename 'month' to 'date'\n df_crimes.rename(columns={\"month\": \"date\"}, inplace=True)\n # Ordering columns\n cols = ['persistent_id', 'category', 'street', 'latitude', 'longitude', 'date', 'context']\n df_crimes = crimes[cols]\n df_crimes.set_index('persistent_id', inplace=True)\n\n crimes.append(df_crimes, ignore_index=True)\n\n ### outcomes table ###\n crime_idx = crimes.index.to_list()\n l_outcomes = [api.get_crime(idx).outcomes for idx in crime_idx]\n l_outcomes_flat = [o for sublist in l_outcomes for o in sublist]\n df_outcomes = pd.DataFrame({'crime': [], 'category': [], 'date': [], 'person_id': []})\n df_outcomes['crime'] = [o.crime.id for o in l_outcomes_flat]\n df_outcomes['category'] = [o.category.id for o in l_outcomes_flat]\n df_outcomes['date'] = [o.date for o in l_outcomes_flat]\n df_outcomes['person_id'] = [' ' for o in l_outcomes_flat] # person_id is empty given by the api\n df_outcomes.drop_duplicates(['crime', 'category'], inplace=True)\n df_outcomes.set_index(['crime', 'category'], inplace=True)\n\n outcomes.append(df_outcomes, ignore_index=True)\n\n\ndef main(t_current):\n # Call the police API\n api = PoliceAPI()\n\n # Define tables\n crime_categories = pd.DataFrame({'id': [], 'description': []})\n outcome_categories = pd.DataFrame({'id': [], 'description': []})\n streets = pd.DataFrame({'id': [], 'name': []})\n crimes = pd.DataFrame({'persistent_id': [], 'category': [], 'street': [], 'city': [], 'latitude': [], 'longitude': [], 'date': [], 'context': []})\n outcomes = pd.DataFrame({'crime': [], 'category': [], 'date': [], 'person_id': []})\n\n # Transform dates into pandas Series for better manipulation\n dates = pd.Series(api.get_dates())\n\n # Get Forces\n forces = api.get_forces()\n # Get neighbourhoods\n neighbourhoods = [f.neighbourhoods for f in forces]\n nb_flat = [n for sublist in neighbourhoods for n in sublist]\n s_nb_flat = pd.Series(nb_flat).unique()\n\n first_job(api, dates, t_current)\n\n t_last_update = api.get_latest_date()\n second_job(api, dates, t_last_update, t_current)\n\n last_job(api, t_current)\n\n\nif __name__ == \"__main__\":\n main(t_current)\n" ]
[ [ "pandas.Series", "pandas.DataFrame", "pandas.DataFrame.from_dict" ] ]
mothguib/maptrainer
[ "335334fed073f8d14a4c5137eaa0424efcbcac63" ]
[ "maptrainer/model/LinRNNModel.py" ]
[ "# -*- coding: utf-8 -*-\n\nimport numpy as np\nimport torch\nfrom torch import autograd\nfrom torch.autograd import Variable\nimport torch.nn as nn\n\nfrom maptrainer.model.MAPModel import MAPModel\nfrom ..data import INIT_RANGE_BOUND\n\n\nclass LinRNNModel(MAPModel):\n \"\"\"\n `LinRNNModel`: Linear-output RNN model\n\n Container module standing for an RNN with a linear output layer\n \"\"\"\n\n def __init__(self,\n n_input,\n _n_hid,\n _nlayers,\n variant=\"LSTM\",\n dropout=0.0,\n **kwargs):\n\n super(LinRNNModel, self).__init__()\n self.variant = variant\n self.nhid = _n_hid\n self.nlayers = _nlayers\n self.drop = nn.Dropout(dropout)\n\n # The linear layer as projection that maps hidden state space to\n # vertices' space namely that this linear layer has as many units\n # as there are vertices\n self.linearise = nn.Linear(_n_hid, n_input)\n\n if variant in ['LSTM', 'GRU']:\n self.rnn = getattr(nn, variant)(n_input, _n_hid, _nlayers,\n dropout=dropout)\n else:\n try:\n nonlinearity = {'RNN_TANH': 'tanh', 'RNN_RELU': 'relu'}[\n variant]\n except KeyError:\n raise ValueError(\"\"\"An invalid option for `--model` was \n supplied, options are ['LSTM', 'GRU', 'RNN_TANH' or \n RNN_RELU']\"\"\")\n self.rnn = nn.RNN(n_input, _n_hid, _nlayers,\n nonlinearity=nonlinearity, dropout=dropout)\n\n self.init_parameters()\n\n def forward(self, _input, hidden):\n \"\"\"\n\n :param _input:\n Shape: N x T x n_in\n :type _input: FloatTensor or Variable\n :param hidden: (h_t, c_t)\n :type hidden:\n :return:\n :rtype:\n \"\"\"\n _input = _input.permute(1, 0, 2) # The dimension representing the\n # index of elements in a sequence (or the tth element of the\n # sequence) is put into the 1st dim (axis 0) and the one\n # representing indices of sequence (the nth sequence) into the 2nd\n # dim (axis 1). Henceforth, `_input` will have a shape of `T x N x\n # n_ins`.\n\n dropped_out_input = self.drop(_input)\n self.rnn.flatten_parameters()\n output, hidden = self.rnn(dropped_out_input, hidden)\n dropped_out_output = self.drop(output)\n linearised = self.linearise(dropped_out_output)\n\n return linearised, hidden\n\n def init_hidden(self, bsz):\n weight = next(self.parameters()).data\n if self.variant == 'LSTM':\n return (\n Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()),\n Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()))\n # returns (h_t, c_t)\n else:\n return Variable(weight.new(self.nlayers, bsz, self.nhid).zero_())\n\n def predict(self, _input, bsz):\n if isinstance(_input, np.ndarray):\n _input = autograd.Variable(torch.from_numpy(_input).float())\n\n if isinstance(_input, autograd.Variable):\n if len(_input.size()) == 2:\n _input = _input.view(len(_input), 1, -1)\n\n sizes = _input.size()\n if sizes[1] == 1:\n _input = _input.expand(sizes[0], bsz, sizes[2])\n else:\n raise TypeError(\n \"_input must be a np.ndarray or an autograd.Variable\")\n\n hidden = self.init_hidden(bsz)\n\n outputs, hidden = self(_input, hidden)\n\n return outputs[:, 0, :], hidden[:, 0, :]\n" ]
[ [ "torch.nn.RNN", "torch.from_numpy", "torch.nn.Linear", "torch.nn.Dropout" ] ]
sjdv1982/silk
[ "232e759cabfc7a87550d1e50ed9c4de4e0e57bf4" ]
[ "tests/silk/test-complex.py" ]
[ "import sys\nfrom pprint import pprint\nfrom silk import Silk, ValidationError\n\ndef adder(self, other):\n return other + self.x\n\ns = Silk()\ns.__add__ = adder\ns.bla = adder\ns.x = 80\nprint(s.x.data)\nprint(s.bla(5))\nprint(s+5)\n\ns2 = Silk(schema=s.schema)\ns2.x = 10\nprint(s2+5)\n\ns3 = Silk(schema=s2.schema)\ns3.x = 10\nprint(s3+25)\n\ndef xy(self):\n return self.x + self.y\n\ns.x = 1\ns.y = 2\nprint(s.x + s.y)\ns3.xy = property(xy) # all three Silks use the same schema\nprint(s.xy)\n\ndef xx_get(self):\n return self.x * self.x\ndef xx_set(self, xx):\n import math\n self.x = int(math.sqrt(xx))\n\ns.x = 3\ns.xx = property(xx_get, xx_set)\nprint(s.xx)\ns.xx = 16\nprint(s.xx)\nprint(s.x.data)\n\ns.z = {}\ns.z.q = 12\ns.z.r = 24\nsz = s.z\nprint(sz.q.data, sz.r.data)\ns.z.r = 25\nprint(sz.q.data, sz.r.data)\ns.z.qr = property(lambda self: self.q * self.r)\nprint(s.z.qr)\n\ndef validate_z(self):\n print(\"VALIDATE\", self.q.data, self.r.data)\n assert self.q < self.r\ntry:\n s.z.add_validator(validate_z)\nexcept Exception:\n pprint(s.schema)\n\ns.z.validate()\npprint(s.schema)\n\ns.lis = [1,2,3]\ns.lis.append(10)\ns.validate()\nprint(s.lis.data)\ns.lis += [5]\ns.validate()\nprint(s.lis*2)\nfor a in s.lis[1:3]:\n print(a.data)\nprint(hasattr(s, \"lis\"), \"lis\" in s)\nprint(hasattr(s, \"lis2\"), \"lis2\" in s)\n\nfor v in s:\n print(s[v].data)\nprint(\"\")\nfor v in s.lis:\n print(v.data)\nprint()\n\ns = Silk().set(5)\ninc = lambda self: self + 1\ns.x = inc\nprint(s.x())\ns.y = property(inc)\nprint(s.y)\ndef setter(self,v):\n self.set(v - 1)\ns.z = property(inc, setter)\nprint(s.z)\ns.z = 10\nprint(s.data)\nprint(s.z)\n\nimport numpy as np\narr = np.array([1.0,2.0,3.0])\ns2.arr = arr\n# Need .self.data or .unsilk for Numpy arrays, because Numpy arrays have a .data method\nprint(s2.arr.self.data, arr)\nprint(s2.arr.unsilk, arr)\nprint(type(s2.arr.self.data), type(arr))\nprint(s2.arr[2].self.data, arr[2])\nprint(type(s2.arr[2].self.data), type(arr[2]))\n\n#s2.arr.schema[\"type\"] = \"array\" # inferred\nprint(s2.arr.schema[\"type\"])\nitem = Silk().set(5.0)\n#item.schema[\"type\"] = \"number\" # inferred\ndef func(self):\n assert self > 0\nitem.add_validator(func)\ns2.arr.schema[\"items\"] = item.schema\ns2.x.validate(full=False)\nprint(\"ARR\", s2.arr, type(s2.arr))\nfor nr, ele in enumerate(s2.arr):\n print(\"ELE\", nr, ele, type(ele))\n ele.validate(full=False)\ns2.x.validate(full=False)\ns2.validate()\n\ns2.arr[0] = 5\nprint(s2.arr.unsilk)\n\ns = Silk()\ns.x = 1.0\ns.y = 0.0\ns.z = 0.0\ndef func(self):\n assert abs(self.x**2+self.y**2+self.z**2 - 1) < 0.001\ns.add_validator(func)\ns.validate()\ntry:\n s.y = 1.0 # would fail\n s.validate()\nexcept ValidationError:\n s.y = 0\n\ns.x = 0.0\ns.y = 0.0\ns.z = 1.0\ns.validate()\n\ns.x = 0.0\ns.y = 1.0\ns.z = 0.0\ns.validate()\n\nprint(s.data)\n\nimport numpy as np\na = Silk()\na.coor = [0.0,0.0,1.0]\npprint(a.coor.schema)\nprint(a.coor.data)\nprint(\"START\")\nnp.array(a.coor.data)\nprint(np.array(a.coor.data))\ndef func(self):\n import numpy as np #necessary!\n arr = np.array(self.data)\n assert abs(np.sum(arr**2) - 1) < 0.01\na.coor.add_validator(func)\n\nc = Silk()\nc.set( [0.0, 0.0, 0.0] )\nc.schema.clear()\nc.schema.update(a.coor.schema)\n\ndef set_x(self, value):\n self[0] = value\nc.x = property(lambda self: self[0], set_x)\ndef set_y(self, value):\n self[1] = value\nc.y = property(lambda self: self[1], set_y)\ndef set_z(self, value):\n self[2] = value\nc.z = property(lambda self: self[2], set_z)\n\ndef set_xyz(self, xyz):\n x,y,z = xyz\n self.x = x\n self.y = y\n self.z = z\n self.validate()\nc.xyz = property(lambda self: tuple(self.data), set_xyz)\n\nc.x = 0.2\ntry:\n c.validate()\nexcept ValidationError as exc:\n print(exc)\nc.y = -0.3\nc.z = 0.93\nc.validate()\nprint(c.data)\nc.xyz = -1,0,0\nprint(c.data, c.xyz)\npprint(c.schema)\n\nTest = Silk()\ndef __init__(self, a, b):\n self.a = a\n self.b = b\ndef __call__(self, c):\n return self.a + self.b + c\nTest.__init__ = __init__\nTest.__call__ = __call__\ntest = Test(7,8)\ntest.validate()\nprint(test.data)\nprint(test(5))\npprint(test.schema)\n\nprint(\"START\")\ntest.l = []\nl = test.l\nl.append(\"bla\")\ntest.validate()\ntry:\n l.append(10) #Error\n l.validate()\nexcept ValidationError as exc:\n print(exc)\n l.pop(-1)\nprint(test.l.data)" ]
[ [ "numpy.array", "numpy.sum" ] ]
jayzed82/scikit-learn
[ "f52c0d441502117020ac0152cea8e89367f55ed6" ]
[ "sklearn/feature_selection/_univariate_selection.py" ]
[ "\"\"\"Univariate features selection.\"\"\"\n\n# Authors: V. Michel, B. Thirion, G. Varoquaux, A. Gramfort, E. Duchesnay.\n# L. Buitinck, A. Joly\n# License: BSD 3 clause\n\n\nimport numpy as np\nimport warnings\n\nfrom scipy import special, stats\nfrom scipy.sparse import issparse\n\nfrom ..base import BaseEstimator\nfrom ..preprocessing import LabelBinarizer\nfrom ..utils import (as_float_array, check_array, check_X_y, safe_sqr,\n safe_mask)\nfrom ..utils.extmath import safe_sparse_dot, row_norms\nfrom ..utils.validation import check_is_fitted\nfrom ._base import SelectorMixin\n\n\ndef _clean_nans(scores):\n \"\"\"\n Fixes Issue #1240: NaNs can't be properly compared, so change them to the\n smallest value of scores's dtype. -inf seems to be unreliable.\n \"\"\"\n # XXX where should this function be called? fit? scoring functions\n # themselves?\n scores = as_float_array(scores, copy=True)\n scores[np.isnan(scores)] = np.finfo(scores.dtype).min\n return scores\n\n\n######################################################################\n# Scoring functions\n\n\n# The following function is a rewriting of scipy.stats.f_oneway\n# Contrary to the scipy.stats.f_oneway implementation it does not\n# copy the data while keeping the inputs unchanged.\ndef f_oneway(*args):\n \"\"\"Performs a 1-way ANOVA.\n\n The one-way ANOVA tests the null hypothesis that 2 or more groups have\n the same population mean. The test is applied to samples from two or\n more groups, possibly with differing sizes.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n *args : {array-like, sparse matrix}\n sample1, sample2... The sample measurements should be given as\n arguments.\n\n Returns\n -------\n f_statistic : float\n The computed F-value of the test.\n p_value : float\n The associated p-value from the F-distribution.\n\n Notes\n -----\n The ANOVA test has important assumptions that must be satisfied in order\n for the associated p-value to be valid.\n\n 1. The samples are independent\n 2. Each sample is from a normally distributed population\n 3. The population standard deviations of the groups are all equal. This\n property is known as homoscedasticity.\n\n If these assumptions are not true for a given set of data, it may still be\n possible to use the Kruskal-Wallis H-test (`scipy.stats.kruskal`_) although\n with some loss of power.\n\n The algorithm is from Heiman[2], pp.394-7.\n\n See ``scipy.stats.f_oneway`` that should give the same results while\n being less efficient.\n\n References\n ----------\n\n .. [1] Lowry, Richard. \"Concepts and Applications of Inferential\n Statistics\". Chapter 14.\n http://faculty.vassar.edu/lowry/ch14pt1.html\n\n .. [2] Heiman, G.W. Research Methods in Statistics. 2002.\n\n \"\"\"\n n_classes = len(args)\n args = [as_float_array(a) for a in args]\n n_samples_per_class = np.array([a.shape[0] for a in args])\n n_samples = np.sum(n_samples_per_class)\n ss_alldata = sum(safe_sqr(a).sum(axis=0) for a in args)\n sums_args = [np.asarray(a.sum(axis=0)) for a in args]\n square_of_sums_alldata = sum(sums_args) ** 2\n square_of_sums_args = [s ** 2 for s in sums_args]\n sstot = ss_alldata - square_of_sums_alldata / float(n_samples)\n ssbn = 0.\n for k, _ in enumerate(args):\n ssbn += square_of_sums_args[k] / n_samples_per_class[k]\n ssbn -= square_of_sums_alldata / float(n_samples)\n sswn = sstot - ssbn\n dfbn = n_classes - 1\n dfwn = n_samples - n_classes\n msb = ssbn / float(dfbn)\n msw = sswn / float(dfwn)\n constant_features_idx = np.where(msw == 0.)[0]\n if (np.nonzero(msb)[0].size != msb.size and constant_features_idx.size):\n warnings.warn(\"Features %s are constant.\" % constant_features_idx,\n UserWarning)\n f = msb / msw\n # flatten matrix to vector in sparse case\n f = np.asarray(f).ravel()\n prob = special.fdtrc(dfbn, dfwn, f)\n return f, prob\n\n\ndef f_classif(X, y):\n \"\"\"Compute the ANOVA F-value for the provided sample.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n X : {array-like, sparse matrix} of shape (n_samples, n_features)\n The set of regressors that will be tested sequentially.\n\n y : ndarray of shape (n_samples,)\n The target vector.\n\n Returns\n -------\n f_statistic : ndarray of shape (n_features,)\n F-statistic for each feature.\n\n p_values : ndarray of shape (n_features,)\n P-values associated with the F-statistic.\n\n See Also\n --------\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n \"\"\"\n X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'])\n args = [X[safe_mask(X, y == k)] for k in np.unique(y)]\n return f_oneway(*args)\n\n\ndef _chisquare(f_obs, f_exp):\n \"\"\"Fast replacement for scipy.stats.chisquare.\n\n Version from https://github.com/scipy/scipy/pull/2525 with additional\n optimizations.\n \"\"\"\n f_obs = np.asarray(f_obs, dtype=np.float64)\n\n k = len(f_obs)\n # Reuse f_obs for chi-squared statistics\n chisq = f_obs\n chisq -= f_exp\n chisq **= 2\n with np.errstate(invalid=\"ignore\"):\n chisq /= f_exp\n chisq = chisq.sum(axis=0)\n return chisq, special.chdtrc(k - 1, chisq)\n\n\ndef chi2(X, y):\n \"\"\"Compute chi-squared stats between each non-negative feature and class.\n\n This score can be used to select the n_features features with the\n highest values for the test chi-squared statistic from X, which must\n contain only non-negative features such as booleans or frequencies\n (e.g., term counts in document classification), relative to the classes.\n\n Recall that the chi-square test measures dependence between stochastic\n variables, so using this function \"weeds out\" the features that are the\n most likely to be independent of class and therefore irrelevant for\n classification.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n X : {array-like, sparse matrix} of shape (n_samples, n_features)\n Sample vectors.\n\n y : array-like of shape (n_samples,)\n Target vector (class labels).\n\n Returns\n -------\n chi2 : ndarray of shape (n_features,)\n Chi2 statistics for each feature.\n\n p_values : ndarray of shape (n_features,)\n P-values for each feature.\n\n Notes\n -----\n Complexity of this algorithm is O(n_classes * n_features).\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n \"\"\"\n\n # XXX: we might want to do some of the following in logspace instead for\n # numerical stability.\n X = check_array(X, accept_sparse='csr')\n if np.any((X.data if issparse(X) else X) < 0):\n raise ValueError(\"Input X must be non-negative.\")\n\n Y = LabelBinarizer().fit_transform(y)\n if Y.shape[1] == 1:\n Y = np.append(1 - Y, Y, axis=1)\n\n observed = safe_sparse_dot(Y.T, X) # n_classes * n_features\n\n feature_count = X.sum(axis=0).reshape(1, -1)\n class_prob = Y.mean(axis=0).reshape(1, -1)\n expected = np.dot(class_prob.T, feature_count)\n\n return _chisquare(observed, expected)\n\n\ndef r_regression(X, y, *, center=True):\n \"\"\"Compute Pearson's r for each features and the target.\n\n Pearson's r is also known as the Pearson correlation coefficient.\n\n .. versionadded:: 1.0\n\n Linear model for testing the individual effect of each of many regressors.\n This is a scoring function to be used in a feature selection procedure, not\n a free standing feature selection procedure.\n\n The cross correlation between each regressor and the target is computed\n as ((X[:, i] - mean(X[:, i])) * (y - mean_y)) / (std(X[:, i]) * std(y)).\n\n For more on usage see the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n X : {array-like, sparse matrix} of shape (n_samples, n_features)\n The data matrix.\n\n y : array-like of shape (n_samples,)\n The target vector.\n\n center : bool, default=True\n Whether or not to center the data matrix `X` and the target vector `y`.\n By default, `X` and `y` will be centered.\n\n Returns\n -------\n correlation_coefficient : ndarray of shape (n_features,)\n Pearson's R correlation coefficients of features.\n\n See Also\n --------\n f_regression: Univariate linear regression tests returning f-statistic\n and p-values\n mutual_info_regression: Mutual information for a continuous target.\n f_classif: ANOVA F-value between label/feature for classification tasks.\n chi2: Chi-squared stats of non-negative features for classification tasks.\n \"\"\"\n X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],\n dtype=np.float64)\n n_samples = X.shape[0]\n\n # Compute centered values\n # Note that E[(x - mean(x))*(y - mean(y))] = E[x*(y - mean(y))], so we\n # need not center X\n if center:\n y = y - np.mean(y)\n if issparse(X):\n X_means = X.mean(axis=0).getA1()\n else:\n X_means = X.mean(axis=0)\n # Compute the scaled standard deviations via moments\n X_norms = np.sqrt(row_norms(X.T, squared=True) -\n n_samples * X_means ** 2)\n else:\n X_norms = row_norms(X.T)\n\n correlation_coefficient = safe_sparse_dot(y, X)\n correlation_coefficient /= X_norms\n correlation_coefficient /= np.linalg.norm(y)\n return correlation_coefficient\n\n\ndef f_regression(X, y, *, center=True):\n \"\"\"Univariate linear regression tests returning F-statistic and p-values.\n\n Quick linear model for testing the effect of a single regressor,\n sequentially for many regressors.\n\n This is done in 2 steps:\n\n 1. The cross correlation between each regressor and the target is computed,\n that is, ((X[:, i] - mean(X[:, i])) * (y - mean_y)) / (std(X[:, i]) *\n std(y)) using r_regression function.\n 2. It is converted to an F score and then to a p-value.\n\n :func:`f_regression` is derived from :func:`r_regression` and will rank\n features in the same order if all the features are positively correlated\n with the target.\n\n Note however that contrary to :func:`f_regression`, :func:`r_regression`\n values lie in [-1, 1] and can thus be negative. :func:`f_regression` is\n therefore recommended as a feature selection criterion to identify\n potentially predictive feature for a downstream classifier, irrespective of\n the sign of the association with the target variable.\n\n Furthermore :func:`f_regression` returns p-values while\n :func:`r_regression` does not.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n X : {array-like, sparse matrix} of shape (n_samples, n_features)\n The data matrix.\n\n y : array-like of shape (n_samples,)\n The target vector.\n\n center : bool, default=True\n Whether or not to center the data matrix `X` and the target vector `y`.\n By default, `X` and `y` will be centered.\n\n Returns\n -------\n f_statistic : ndarray of shape (n_features,)\n F-statistic for each feature.\n\n p_values : ndarray of shape (n_features,)\n P-values associated with the F-statistic.\n\n See Also\n --------\n r_regression: Pearson's R between label/feature for regression tasks.\n f_classif: ANOVA F-value between label/feature for classification tasks.\n chi2: Chi-squared stats of non-negative features for classification tasks.\n SelectKBest: Select features based on the k highest scores.\n SelectFpr: Select features based on a false positive rate test.\n SelectFdr: Select features based on an estimated false discovery rate.\n SelectFwe: Select features based on family-wise error rate.\n SelectPercentile: Select features based on percentile of the highest\n scores.\n \"\"\"\n correlation_coefficient = r_regression(X, y, center=center)\n deg_of_freedom = y.size - (2 if center else 1)\n\n corr_coef_squared = correlation_coefficient ** 2\n f_statistic = corr_coef_squared / (1 - corr_coef_squared) * deg_of_freedom\n p_values = stats.f.sf(f_statistic, 1, deg_of_freedom)\n return f_statistic, p_values\n\n\n######################################################################\n# Base classes\n\nclass _BaseFilter(SelectorMixin, BaseEstimator):\n \"\"\"Initialize the univariate feature selection.\n\n Parameters\n ----------\n score_func : callable\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues) or a single array with scores.\n \"\"\"\n\n def __init__(self, score_func):\n self.score_func = score_func\n\n def fit(self, X, y):\n \"\"\"Run score function on (X, y) and get the appropriate features.\n\n Parameters\n ----------\n X : array-like of shape (n_samples, n_features)\n The training input samples.\n\n y : array-like of shape (n_samples,)\n The target values (class labels in classification, real numbers in\n regression).\n\n Returns\n -------\n self : object\n \"\"\"\n X, y = self._validate_data(X, y, accept_sparse=['csr', 'csc'],\n multi_output=True)\n\n if not callable(self.score_func):\n raise TypeError(\"The score function should be a callable, %s (%s) \"\n \"was passed.\"\n % (self.score_func, type(self.score_func)))\n\n self._check_params(X, y)\n score_func_ret = self.score_func(X, y)\n if isinstance(score_func_ret, (list, tuple)):\n self.scores_, self.pvalues_ = score_func_ret\n self.pvalues_ = np.asarray(self.pvalues_)\n else:\n self.scores_ = score_func_ret\n self.pvalues_ = None\n\n self.scores_ = np.asarray(self.scores_)\n\n return self\n\n def _check_params(self, X, y):\n pass\n\n def _more_tags(self):\n return {'requires_y': True}\n\n\n######################################################################\n# Specific filters\n######################################################################\nclass SelectPercentile(_BaseFilter):\n \"\"\"Select features according to a percentile of the highest scores.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues) or a single array with scores.\n Default is f_classif (see below \"See Also\"). The default function only\n works with classification tasks.\n\n .. versionadded:: 0.18\n\n percentile : int, default=10\n Percent of features to keep.\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores, None if `score_func` returned only scores.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n Examples\n --------\n >>> from sklearn.datasets import load_digits\n >>> from sklearn.feature_selection import SelectPercentile, chi2\n >>> X, y = load_digits(return_X_y=True)\n >>> X.shape\n (1797, 64)\n >>> X_new = SelectPercentile(chi2, percentile=10).fit_transform(X, y)\n >>> X_new.shape\n (1797, 7)\n\n Notes\n -----\n Ties between features with equal scores will be broken in an unspecified\n way.\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n mutual_info_classif : Mutual information for a discrete target.\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n mutual_info_regression : Mutual information for a continuous target.\n SelectKBest : Select features based on the k highest scores.\n SelectFpr : Select features based on a false positive rate test.\n SelectFdr : Select features based on an estimated false discovery rate.\n SelectFwe : Select features based on family-wise error rate.\n GenericUnivariateSelect : Univariate feature selector with configurable\n mode.\n \"\"\"\n def __init__(self, score_func=f_classif, *, percentile=10):\n super().__init__(score_func=score_func)\n self.percentile = percentile\n\n def _check_params(self, X, y):\n if not 0 <= self.percentile <= 100:\n raise ValueError(\"percentile should be >=0, <=100; got %r\"\n % self.percentile)\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n # Cater for NaNs\n if self.percentile == 100:\n return np.ones(len(self.scores_), dtype=bool)\n elif self.percentile == 0:\n return np.zeros(len(self.scores_), dtype=bool)\n\n scores = _clean_nans(self.scores_)\n threshold = np.percentile(scores, 100 - self.percentile)\n mask = scores > threshold\n ties = np.where(scores == threshold)[0]\n if len(ties):\n max_feats = int(len(scores) * self.percentile / 100)\n kept_ties = ties[:max_feats - mask.sum()]\n mask[kept_ties] = True\n return mask\n\n\nclass SelectKBest(_BaseFilter):\n \"\"\"Select features according to the k highest scores.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues) or a single array with scores.\n Default is f_classif (see below \"See Also\"). The default function only\n works with classification tasks.\n\n .. versionadded:: 0.18\n\n k : int or \"all\", default=10\n Number of top features to select.\n The \"all\" option bypasses selection, for use in a parameter search.\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores, None if `score_func` returned only scores.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n Examples\n --------\n >>> from sklearn.datasets import load_digits\n >>> from sklearn.feature_selection import SelectKBest, chi2\n >>> X, y = load_digits(return_X_y=True)\n >>> X.shape\n (1797, 64)\n >>> X_new = SelectKBest(chi2, k=20).fit_transform(X, y)\n >>> X_new.shape\n (1797, 20)\n\n Notes\n -----\n Ties between features with equal scores will be broken in an unspecified\n way.\n\n See Also\n --------\n f_classif: ANOVA F-value between label/feature for classification tasks.\n mutual_info_classif: Mutual information for a discrete target.\n chi2: Chi-squared stats of non-negative features for classification tasks.\n f_regression: F-value between label/feature for regression tasks.\n mutual_info_regression: Mutual information for a continuous target.\n SelectPercentile: Select features based on percentile of the highest\n scores.\n SelectFpr : Select features based on a false positive rate test.\n SelectFdr : Select features based on an estimated false discovery rate.\n SelectFwe : Select features based on family-wise error rate.\n GenericUnivariateSelect : Univariate feature selector with configurable\n mode.\n \"\"\"\n def __init__(self, score_func=f_classif, *, k=10):\n super().__init__(score_func=score_func)\n self.k = k\n\n def _check_params(self, X, y):\n if not (self.k == \"all\" or 0 <= self.k <= X.shape[1]):\n raise ValueError(\"k should be >=0, <= n_features = %d; got %r. \"\n \"Use k='all' to return all features.\"\n % (X.shape[1], self.k))\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n if self.k == 'all':\n return np.ones(self.scores_.shape, dtype=bool)\n elif self.k == 0:\n return np.zeros(self.scores_.shape, dtype=bool)\n else:\n scores = _clean_nans(self.scores_)\n mask = np.zeros(scores.shape, dtype=bool)\n\n # Request a stable sort. Mergesort takes more memory (~40MB per\n # megafeature on x86-64).\n mask[np.argsort(scores, kind=\"mergesort\")[-self.k:]] = 1\n return mask\n\n\nclass SelectFpr(_BaseFilter):\n \"\"\"Filter: Select the pvalues below alpha based on a FPR test.\n\n FPR test stands for False Positive Rate test. It controls the total\n amount of false detections.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues).\n Default is f_classif (see below \"See Also\"). The default function only\n works with classification tasks.\n\n alpha : float, default=5e-2\n The highest p-value for features to be kept.\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n Examples\n --------\n >>> from sklearn.datasets import load_breast_cancer\n >>> from sklearn.feature_selection import SelectFpr, chi2\n >>> X, y = load_breast_cancer(return_X_y=True)\n >>> X.shape\n (569, 30)\n >>> X_new = SelectFpr(chi2, alpha=0.01).fit_transform(X, y)\n >>> X_new.shape\n (569, 16)\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n mutual_info_classif: Mutual information for a discrete target.\n f_regression : F-value between label/feature for regression tasks.\n mutual_info_regression : Mutual information for a continuous target.\n SelectPercentile : Select features based on percentile of the highest\n scores.\n SelectKBest : Select features based on the k highest scores.\n SelectFdr : Select features based on an estimated false discovery rate.\n SelectFwe : Select features based on family-wise error rate.\n GenericUnivariateSelect : Univariate feature selector with configurable\n mode.\n \"\"\"\n def __init__(self, score_func=f_classif, *, alpha=5e-2):\n super().__init__(score_func=score_func)\n self.alpha = alpha\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n return self.pvalues_ < self.alpha\n\n\nclass SelectFdr(_BaseFilter):\n \"\"\"Filter: Select the p-values for an estimated false discovery rate\n\n This uses the Benjamini-Hochberg procedure. ``alpha`` is an upper bound\n on the expected false discovery rate.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues).\n Default is f_classif (see below \"See Also\"). The default function only\n works with classification tasks.\n\n alpha : float, default=5e-2\n The highest uncorrected p-value for features to keep.\n\n Examples\n --------\n >>> from sklearn.datasets import load_breast_cancer\n >>> from sklearn.feature_selection import SelectFdr, chi2\n >>> X, y = load_breast_cancer(return_X_y=True)\n >>> X.shape\n (569, 30)\n >>> X_new = SelectFdr(chi2, alpha=0.01).fit_transform(X, y)\n >>> X_new.shape\n (569, 16)\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n References\n ----------\n https://en.wikipedia.org/wiki/False_discovery_rate\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n mutual_info_classif : Mutual information for a discrete target.\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n mutual_info_regression : Mutual information for a contnuous target.\n SelectPercentile : Select features based on percentile of the highest\n scores.\n SelectKBest : Select features based on the k highest scores.\n SelectFpr : Select features based on a false positive rate test.\n SelectFwe : Select features based on family-wise error rate.\n GenericUnivariateSelect : Univariate feature selector with configurable\n mode.\n \"\"\"\n def __init__(self, score_func=f_classif, *, alpha=5e-2):\n super().__init__(score_func=score_func)\n self.alpha = alpha\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n n_features = len(self.pvalues_)\n sv = np.sort(self.pvalues_)\n selected = sv[sv <= float(self.alpha) / n_features *\n np.arange(1, n_features + 1)]\n if selected.size == 0:\n return np.zeros_like(self.pvalues_, dtype=bool)\n return self.pvalues_ <= selected.max()\n\n\nclass SelectFwe(_BaseFilter):\n \"\"\"Filter: Select the p-values corresponding to Family-wise error rate\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues).\n Default is f_classif (see below \"See Also\"). The default function only\n works with classification tasks.\n\n alpha : float, default=5e-2\n The highest uncorrected p-value for features to keep.\n\n Examples\n --------\n >>> from sklearn.datasets import load_breast_cancer\n >>> from sklearn.feature_selection import SelectFwe, chi2\n >>> X, y = load_breast_cancer(return_X_y=True)\n >>> X.shape\n (569, 30)\n >>> X_new = SelectFwe(chi2, alpha=0.01).fit_transform(X, y)\n >>> X_new.shape\n (569, 15)\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n SelectPercentile : Select features based on percentile of the highest\n scores.\n SelectKBest : Select features based on the k highest scores.\n SelectFpr : Select features based on a false positive rate test.\n SelectFdr : Select features based on an estimated false discovery rate.\n GenericUnivariateSelect : Univariate feature selector with configurable\n mode.\n \"\"\"\n def __init__(self, score_func=f_classif, *, alpha=5e-2):\n super().__init__(score_func=score_func)\n self.alpha = alpha\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n return (self.pvalues_ < self.alpha / len(self.pvalues_))\n\n\n######################################################################\n# Generic filter\n######################################################################\n\n# TODO this class should fit on either p-values or scores,\n# depending on the mode.\nclass GenericUnivariateSelect(_BaseFilter):\n \"\"\"Univariate feature selector with configurable strategy.\n\n Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n Parameters\n ----------\n score_func : callable, default=f_classif\n Function taking two arrays X and y, and returning a pair of arrays\n (scores, pvalues). For modes 'percentile' or 'kbest' it can return\n a single array scores.\n\n mode : {'percentile', 'k_best', 'fpr', 'fdr', 'fwe'}, default='percentile'\n Feature selection mode.\n\n param : float or int depending on the feature selection mode, default=1e-5\n Parameter of the corresponding mode.\n\n Attributes\n ----------\n scores_ : array-like of shape (n_features,)\n Scores of features.\n\n pvalues_ : array-like of shape (n_features,)\n p-values of feature scores, None if `score_func` returned scores only.\n\n n_features_in_ : int\n Number of features seen during :term:`fit`.\n\n .. versionadded:: 0.24\n\n Examples\n --------\n >>> from sklearn.datasets import load_breast_cancer\n >>> from sklearn.feature_selection import GenericUnivariateSelect, chi2\n >>> X, y = load_breast_cancer(return_X_y=True)\n >>> X.shape\n (569, 30)\n >>> transformer = GenericUnivariateSelect(chi2, mode='k_best', param=20)\n >>> X_new = transformer.fit_transform(X, y)\n >>> X_new.shape\n (569, 20)\n\n See Also\n --------\n f_classif : ANOVA F-value between label/feature for classification tasks.\n mutual_info_classif : Mutual information for a discrete target.\n chi2 : Chi-squared stats of non-negative features for classification tasks.\n f_regression : F-value between label/feature for regression tasks.\n mutual_info_regression : Mutual information for a continuous target.\n SelectPercentile : Select features based on percentile of the highest\n scores.\n SelectKBest : Select features based on the k highest scores.\n SelectFpr : Select features based on a false positive rate test.\n SelectFdr : Select features based on an estimated false discovery rate.\n SelectFwe : Select features based on family-wise error rate.\n \"\"\"\n\n _selection_modes: dict = {'percentile': SelectPercentile,\n 'k_best': SelectKBest,\n 'fpr': SelectFpr,\n 'fdr': SelectFdr,\n 'fwe': SelectFwe}\n\n def __init__(self, score_func=f_classif, *, mode='percentile', param=1e-5):\n super().__init__(score_func=score_func)\n self.mode = mode\n self.param = param\n\n def _make_selector(self):\n selector = self._selection_modes[self.mode](score_func=self.score_func)\n\n # Now perform some acrobatics to set the right named parameter in\n # the selector\n possible_params = selector._get_param_names()\n possible_params.remove('score_func')\n selector.set_params(**{possible_params[0]: self.param})\n\n return selector\n\n def _check_params(self, X, y):\n if self.mode not in self._selection_modes:\n raise ValueError(\"The mode passed should be one of %s, %r,\"\n \" (type %s) was passed.\"\n % (self._selection_modes.keys(), self.mode,\n type(self.mode)))\n\n self._make_selector()._check_params(X, y)\n\n def _get_support_mask(self):\n check_is_fitted(self)\n\n selector = self._make_selector()\n selector.pvalues_ = self.pvalues_\n selector.scores_ = self.scores_\n return selector._get_support_mask()\n" ]
[ [ "numpy.sum", "numpy.ones", "numpy.argsort", "numpy.asarray", "scipy.special.fdtrc", "scipy.special.chdtrc", "numpy.append", "numpy.isnan", "numpy.where", "numpy.nonzero", "numpy.unique", "numpy.mean", "numpy.zeros", "scipy.stats.f.sf", "numpy.arange", "numpy.sort", "numpy.finfo", "numpy.linalg.norm", "numpy.percentile", "numpy.zeros_like", "scipy.sparse.issparse", "numpy.errstate", "numpy.array", "numpy.dot" ] ]
AeRabelais/bdt_pipeline
[ "35d0cc3f7ada35e082c384d0755916605daa5feb" ]
[ "scripts/dataToParquet.py" ]
[ "\"\"\"\r\n@Title: dataToParquet.py\r\n@author: Ashia Lewis\r\n\r\nGOAL: Create and update the parquet files for the air and soil data, separately.\r\n\"\"\"\r\nimport os\r\nimport glob\r\nimport pandas as pd\r\nimport pyarrow as pa\r\nimport pyarrow.parquet as pq\r\n\r\n#CODE TO BE USED FOR THE BATCH DATA\r\n\"\"\"\r\n#file directories for the air and soil files\r\nair_dir = r\"D:\\sample_biodiversitree\\data\\export_data\\air_data\"\r\nsoil_dir = r\"D:\\sample_biodiversitree\\scripts\\data\\export_data\\soil_data\"\r\n\r\n\r\n\r\n#all_air_files = glob.glob(air_dir + '/**/*.csv', recursive=True)\r\n\r\nall_soil_files = glob.glob(soil_dir + '/**/*.csv', recursive=True)\r\n\r\n\r\n#air_data = pd.concat((pd.read_csv(f) for f in all_air_files ))\r\n#air_data.to_parquet('air_data.parquet')\r\n\r\n#need to look at soil's clean up job\r\n\r\nsoil_data = pd.concat((pd.read_csv(f) for f in all_soil_files ))\r\nsoil_data.to_parquet('soil_data.parquet')\r\n\r\n\"\"\"\r\n\r\n#CODE TO BE USED IN THE ACTUAL PIPELINE\r\n\r\n# file directories for the air and soil files \r\nair_dir = r\"D:\\sample_biodiversitree\\data\\export_data\\air_data\"\r\nsoil_dir = r\"D:\\sample_biodiversitree\\data\\export_data\\soil_data\"\r\n\r\n#concatentate all of files' data\r\nall_air_files = glob.glob(air_dir + '/**/*.csv', recursive=True)\r\nall_soil_files = glob.glob(soil_dir + '/**/*.csv', recursive=True)\r\n\r\n#put the data in a dataframe \r\nair_data = pd.concat((pd.read_csv(f) for f in all_air_files))\r\nsoil_data = pd.concat((pd.read_csv(f) for f in all_soil_files))\r\n\r\n#add data to existing parquet files\r\nair_table = pa.Table.from_pandas(air_data)\r\nsoil_table = pa.Table.from_pandas(soil_data)\r\n\r\n\r\nair_writer = pq.ParquetWriter('air_data.parquet', air_table.schema)\r\nair_writer.write_table(table = air_table)\r\n\r\nif air_writer:\r\n air_writer.close()\r\n\r\nsoil_writer = pq.ParquetWriter('soil_data.parquet', soil_table.schema)\r\nsoil_writer.write_table(table = soil_table)\r\n\r\nif soil_writer:\r\n soil_writer.close()\r\n\r\n\r\n\r\n\r\n" ]
[ [ "pandas.read_csv" ] ]
arushi-08/pandas
[ "014ea2e5a8a647cfa5e3050a5c1299eb39b293d3" ]
[ "pandas/tests/plotting/test_datetimelike.py" ]
[ "\"\"\" Test cases for time series specific (freq conversion, etc) \"\"\"\nfrom datetime import (\n date,\n datetime,\n time,\n timedelta,\n)\nimport pickle\n\nimport numpy as np\nimport pytest\n\nfrom pandas._libs.tslibs import (\n BaseOffset,\n to_offset,\n)\nimport pandas.util._test_decorators as td\n\nfrom pandas import (\n DataFrame,\n Index,\n NaT,\n Series,\n isna,\n to_datetime,\n)\nimport pandas._testing as tm\nfrom pandas.core.indexes.datetimes import (\n DatetimeIndex,\n bdate_range,\n date_range,\n)\nfrom pandas.core.indexes.period import (\n Period,\n PeriodIndex,\n period_range,\n)\nfrom pandas.core.indexes.timedeltas import timedelta_range\nfrom pandas.tests.plotting.common import TestPlotBase\n\nfrom pandas.tseries.offsets import WeekOfMonth\n\npytestmark = pytest.mark.slow\n\n\[email protected]_if_no_mpl\nclass TestTSPlot(TestPlotBase):\n def setup_method(self, method):\n TestPlotBase.setup_method(self, method)\n\n self.freq = [\"S\", \"T\", \"H\", \"D\", \"W\", \"M\", \"Q\", \"A\"]\n idx = [period_range(\"12/31/1999\", freq=x, periods=100) for x in self.freq]\n self.period_ser = [Series(np.random.randn(len(x)), x) for x in idx]\n self.period_df = [\n DataFrame(np.random.randn(len(x), 3), index=x, columns=[\"A\", \"B\", \"C\"])\n for x in idx\n ]\n\n freq = [\"S\", \"T\", \"H\", \"D\", \"W\", \"M\", \"Q-DEC\", \"A\", \"1B30Min\"]\n idx = [date_range(\"12/31/1999\", freq=x, periods=100) for x in freq]\n self.datetime_ser = [Series(np.random.randn(len(x)), x) for x in idx]\n self.datetime_df = [\n DataFrame(np.random.randn(len(x), 3), index=x, columns=[\"A\", \"B\", \"C\"])\n for x in idx\n ]\n\n def teardown_method(self, method):\n tm.close()\n\n def test_ts_plot_with_tz(self, tz_aware_fixture):\n # GH2877, GH17173, GH31205, GH31580\n tz = tz_aware_fixture\n index = date_range(\"1/1/2011\", periods=2, freq=\"H\", tz=tz)\n ts = Series([188.5, 328.25], index=index)\n with tm.assert_produces_warning(None):\n _check_plot_works(ts.plot)\n ax = ts.plot()\n xdata = list(ax.get_lines())[0].get_xdata()\n # Check first and last points' labels are correct\n assert (xdata[0].hour, xdata[0].minute) == (0, 0)\n assert (xdata[-1].hour, xdata[-1].minute) == (1, 0)\n\n def test_fontsize_set_correctly(self):\n # For issue #8765\n df = DataFrame(np.random.randn(10, 9), index=range(10))\n fig, ax = self.plt.subplots()\n df.plot(fontsize=2, ax=ax)\n for label in ax.get_xticklabels() + ax.get_yticklabels():\n assert label.get_fontsize() == 2\n\n def test_frame_inferred(self):\n # inferred freq\n idx = date_range(\"1/1/1987\", freq=\"MS\", periods=100)\n idx = DatetimeIndex(idx.values, freq=None)\n\n df = DataFrame(np.random.randn(len(idx), 3), index=idx)\n _check_plot_works(df.plot)\n\n # axes freq\n idx = idx[0:40].union(idx[45:99])\n df2 = DataFrame(np.random.randn(len(idx), 3), index=idx)\n _check_plot_works(df2.plot)\n\n # N > 1\n idx = date_range(\"2008-1-1 00:15:00\", freq=\"15T\", periods=10)\n idx = DatetimeIndex(idx.values, freq=None)\n df = DataFrame(np.random.randn(len(idx), 3), index=idx)\n _check_plot_works(df.plot)\n\n def test_is_error_nozeroindex(self):\n # GH11858\n i = np.array([1, 2, 3])\n a = DataFrame(i, index=i)\n _check_plot_works(a.plot, xerr=a)\n _check_plot_works(a.plot, yerr=a)\n\n def test_nonnumeric_exclude(self):\n idx = date_range(\"1/1/1987\", freq=\"A\", periods=3)\n df = DataFrame({\"A\": [\"x\", \"y\", \"z\"], \"B\": [1, 2, 3]}, idx)\n\n fig, ax = self.plt.subplots()\n df.plot(ax=ax) # it works\n assert len(ax.get_lines()) == 1 # B was plotted\n self.plt.close(fig)\n\n msg = \"no numeric data to plot\"\n with pytest.raises(TypeError, match=msg):\n df[\"A\"].plot()\n\n def test_tsplot(self):\n\n _, ax = self.plt.subplots()\n ts = tm.makeTimeSeries()\n\n for s in self.period_ser:\n _check_plot_works(s.plot, ax=ax)\n\n for s in self.datetime_ser:\n _check_plot_works(s.plot, ax=ax)\n\n _, ax = self.plt.subplots()\n ts.plot(style=\"k\", ax=ax)\n color = (0.0, 0.0, 0.0, 1)\n assert color == ax.get_lines()[0].get_color()\n\n def test_both_style_and_color(self):\n\n ts = tm.makeTimeSeries()\n msg = (\n \"Cannot pass 'style' string with a color symbol and 'color' \"\n \"keyword argument. Please use one or the other or pass 'style' \"\n \"without a color symbol\"\n )\n with pytest.raises(ValueError, match=msg):\n ts.plot(style=\"b-\", color=\"#000099\")\n\n s = ts.reset_index(drop=True)\n with pytest.raises(ValueError, match=msg):\n s.plot(style=\"b-\", color=\"#000099\")\n\n def test_high_freq(self):\n freaks = [\"ms\", \"us\"]\n for freq in freaks:\n _, ax = self.plt.subplots()\n rng = date_range(\"1/1/2012\", periods=100, freq=freq)\n ser = Series(np.random.randn(len(rng)), rng)\n _check_plot_works(ser.plot, ax=ax)\n\n def test_get_datevalue(self):\n from pandas.plotting._matplotlib.converter import get_datevalue\n\n assert get_datevalue(None, \"D\") is None\n assert get_datevalue(1987, \"A\") == 1987\n assert get_datevalue(Period(1987, \"A\"), \"M\") == Period(\"1987-12\", \"M\").ordinal\n assert get_datevalue(\"1/1/1987\", \"D\") == Period(\"1987-1-1\", \"D\").ordinal\n\n def test_ts_plot_format_coord(self):\n def check_format_of_first_point(ax, expected_string):\n first_line = ax.get_lines()[0]\n first_x = first_line.get_xdata()[0].ordinal\n first_y = first_line.get_ydata()[0]\n try:\n assert expected_string == ax.format_coord(first_x, first_y)\n except (ValueError):\n pytest.skip(\n \"skipping test because issue forming test comparison GH7664\"\n )\n\n annual = Series(1, index=date_range(\"2014-01-01\", periods=3, freq=\"A-DEC\"))\n _, ax = self.plt.subplots()\n annual.plot(ax=ax)\n check_format_of_first_point(ax, \"t = 2014 y = 1.000000\")\n\n # note this is added to the annual plot already in existence, and\n # changes its freq field\n daily = Series(1, index=date_range(\"2014-01-01\", periods=3, freq=\"D\"))\n daily.plot(ax=ax)\n check_format_of_first_point(ax, \"t = 2014-01-01 y = 1.000000\")\n tm.close()\n\n def test_line_plot_period_series(self):\n for s in self.period_ser:\n _check_plot_works(s.plot, s.index.freq)\n\n @pytest.mark.parametrize(\n \"frqncy\", [\"1S\", \"3S\", \"5T\", \"7H\", \"4D\", \"8W\", \"11M\", \"3A\"]\n )\n def test_line_plot_period_mlt_series(self, frqncy):\n # test period index line plot for series with multiples (`mlt`) of the\n # frequency (`frqncy`) rule code. tests resolution of issue #14763\n idx = period_range(\"12/31/1999\", freq=frqncy, periods=100)\n s = Series(np.random.randn(len(idx)), idx)\n _check_plot_works(s.plot, s.index.freq.rule_code)\n\n def test_line_plot_datetime_series(self):\n for s in self.datetime_ser:\n _check_plot_works(s.plot, s.index.freq.rule_code)\n\n def test_line_plot_period_frame(self):\n for df in self.period_df:\n _check_plot_works(df.plot, df.index.freq)\n\n @pytest.mark.parametrize(\n \"frqncy\", [\"1S\", \"3S\", \"5T\", \"7H\", \"4D\", \"8W\", \"11M\", \"3A\"]\n )\n def test_line_plot_period_mlt_frame(self, frqncy):\n # test period index line plot for DataFrames with multiples (`mlt`)\n # of the frequency (`frqncy`) rule code. tests resolution of issue\n # #14763\n idx = period_range(\"12/31/1999\", freq=frqncy, periods=100)\n df = DataFrame(np.random.randn(len(idx), 3), index=idx, columns=[\"A\", \"B\", \"C\"])\n freq = df.index.asfreq(df.index.freq.rule_code).freq\n _check_plot_works(df.plot, freq)\n\n def test_line_plot_datetime_frame(self):\n for df in self.datetime_df:\n freq = df.index.to_period(df.index.freq.rule_code).freq\n _check_plot_works(df.plot, freq)\n\n def test_line_plot_inferred_freq(self):\n for ser in self.datetime_ser:\n ser = Series(ser.values, Index(np.asarray(ser.index)))\n _check_plot_works(ser.plot, ser.index.inferred_freq)\n\n ser = ser[[0, 3, 5, 6]]\n _check_plot_works(ser.plot)\n\n def test_fake_inferred_business(self):\n _, ax = self.plt.subplots()\n rng = date_range(\"2001-1-1\", \"2001-1-10\")\n ts = Series(range(len(rng)), index=rng)\n ts = ts[:3].append(ts[5:])\n ts.plot(ax=ax)\n assert not hasattr(ax, \"freq\")\n\n def test_plot_offset_freq(self):\n ser = tm.makeTimeSeries()\n _check_plot_works(ser.plot)\n\n dr = date_range(ser.index[0], freq=\"BQS\", periods=10)\n ser = Series(np.random.randn(len(dr)), index=dr)\n _check_plot_works(ser.plot)\n\n def test_plot_multiple_inferred_freq(self):\n dr = Index([datetime(2000, 1, 1), datetime(2000, 1, 6), datetime(2000, 1, 11)])\n ser = Series(np.random.randn(len(dr)), index=dr)\n _check_plot_works(ser.plot)\n\n def test_uhf(self):\n import pandas.plotting._matplotlib.converter as conv\n\n idx = date_range(\"2012-6-22 21:59:51.960928\", freq=\"L\", periods=500)\n df = DataFrame(np.random.randn(len(idx), 2), index=idx)\n\n _, ax = self.plt.subplots()\n df.plot(ax=ax)\n axis = ax.get_xaxis()\n\n tlocs = axis.get_ticklocs()\n tlabels = axis.get_ticklabels()\n for loc, label in zip(tlocs, tlabels):\n xp = conv._from_ordinal(loc).strftime(\"%H:%M:%S.%f\")\n rs = str(label.get_text())\n if len(rs):\n assert xp == rs\n\n def test_irreg_hf(self):\n idx = date_range(\"2012-6-22 21:59:51\", freq=\"S\", periods=100)\n df = DataFrame(np.random.randn(len(idx), 2), index=idx)\n\n irreg = df.iloc[[0, 1, 3, 4]]\n _, ax = self.plt.subplots()\n irreg.plot(ax=ax)\n diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff()\n\n sec = 1.0 / 24 / 60 / 60\n assert (np.fabs(diffs[1:] - [sec, sec * 2, sec]) < 1e-8).all()\n\n _, ax = self.plt.subplots()\n df2 = df.copy()\n df2.index = df.index.astype(object)\n df2.plot(ax=ax)\n diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff()\n assert (np.fabs(diffs[1:] - sec) < 1e-8).all()\n\n def test_irregular_datetime64_repr_bug(self):\n ser = tm.makeTimeSeries()\n ser = ser[[0, 1, 2, 7]]\n\n _, ax = self.plt.subplots()\n\n ret = ser.plot(ax=ax)\n assert ret is not None\n\n for rs, xp in zip(ax.get_lines()[0].get_xdata(), ser.index):\n assert rs == xp\n\n def test_business_freq(self):\n bts = tm.makePeriodSeries()\n _, ax = self.plt.subplots()\n bts.plot(ax=ax)\n assert ax.get_lines()[0].get_xydata()[0, 0] == bts.index[0].ordinal\n idx = ax.get_lines()[0].get_xdata()\n assert PeriodIndex(data=idx).freqstr == \"B\"\n\n def test_business_freq_convert(self):\n bts = tm.makeTimeSeries(300).asfreq(\"BM\")\n ts = bts.to_period(\"M\")\n _, ax = self.plt.subplots()\n bts.plot(ax=ax)\n assert ax.get_lines()[0].get_xydata()[0, 0] == ts.index[0].ordinal\n idx = ax.get_lines()[0].get_xdata()\n assert PeriodIndex(data=idx).freqstr == \"M\"\n\n def test_freq_with_no_period_alias(self):\n # GH34487\n freq = WeekOfMonth()\n bts = tm.makeTimeSeries(5).asfreq(freq)\n _, ax = self.plt.subplots()\n bts.plot(ax=ax)\n\n idx = ax.get_lines()[0].get_xdata()\n msg = \"freq not specified and cannot be inferred\"\n with pytest.raises(ValueError, match=msg):\n PeriodIndex(data=idx)\n\n def test_nonzero_base(self):\n # GH2571\n idx = date_range(\"2012-12-20\", periods=24, freq=\"H\") + timedelta(minutes=30)\n df = DataFrame(np.arange(24), index=idx)\n _, ax = self.plt.subplots()\n df.plot(ax=ax)\n rs = ax.get_lines()[0].get_xdata()\n assert not Index(rs).is_normalized\n\n def test_dataframe(self):\n bts = DataFrame({\"a\": tm.makeTimeSeries()})\n _, ax = self.plt.subplots()\n bts.plot(ax=ax)\n idx = ax.get_lines()[0].get_xdata()\n tm.assert_index_equal(bts.index.to_period(), PeriodIndex(idx))\n\n def test_axis_limits(self):\n def _test(ax):\n xlim = ax.get_xlim()\n ax.set_xlim(xlim[0] - 5, xlim[1] + 10)\n result = ax.get_xlim()\n assert result[0] == xlim[0] - 5\n assert result[1] == xlim[1] + 10\n\n # string\n expected = (Period(\"1/1/2000\", ax.freq), Period(\"4/1/2000\", ax.freq))\n ax.set_xlim(\"1/1/2000\", \"4/1/2000\")\n result = ax.get_xlim()\n assert int(result[0]) == expected[0].ordinal\n assert int(result[1]) == expected[1].ordinal\n\n # datetime\n expected = (Period(\"1/1/2000\", ax.freq), Period(\"4/1/2000\", ax.freq))\n ax.set_xlim(datetime(2000, 1, 1), datetime(2000, 4, 1))\n result = ax.get_xlim()\n assert int(result[0]) == expected[0].ordinal\n assert int(result[1]) == expected[1].ordinal\n fig = ax.get_figure()\n self.plt.close(fig)\n\n ser = tm.makeTimeSeries()\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n _test(ax)\n\n _, ax = self.plt.subplots()\n df = DataFrame({\"a\": ser, \"b\": ser + 1})\n df.plot(ax=ax)\n _test(ax)\n\n df = DataFrame({\"a\": ser, \"b\": ser + 1})\n axes = df.plot(subplots=True)\n\n for ax in axes:\n _test(ax)\n\n def test_get_finder(self):\n import pandas.plotting._matplotlib.converter as conv\n\n assert conv.get_finder(to_offset(\"B\")) == conv._daily_finder\n assert conv.get_finder(to_offset(\"D\")) == conv._daily_finder\n assert conv.get_finder(to_offset(\"M\")) == conv._monthly_finder\n assert conv.get_finder(to_offset(\"Q\")) == conv._quarterly_finder\n assert conv.get_finder(to_offset(\"A\")) == conv._annual_finder\n assert conv.get_finder(to_offset(\"W\")) == conv._daily_finder\n\n def test_finder_daily(self):\n day_lst = [10, 40, 252, 400, 950, 2750, 10000]\n\n xpl1 = xpl2 = [Period(\"1999-1-1\", freq=\"B\").ordinal] * len(day_lst)\n rs1 = []\n rs2 = []\n for n in day_lst:\n rng = bdate_range(\"1999-1-1\", periods=n)\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs1.append(xaxis.get_majorticklocs()[0])\n\n vmin, vmax = ax.get_xlim()\n ax.set_xlim(vmin + 0.9, vmax)\n rs2.append(xaxis.get_majorticklocs()[0])\n self.plt.close(ax.get_figure())\n\n assert rs1 == xpl1\n assert rs2 == xpl2\n\n def test_finder_quarterly(self):\n yrs = [3.5, 11]\n\n xpl1 = xpl2 = [Period(\"1988Q1\").ordinal] * len(yrs)\n rs1 = []\n rs2 = []\n for n in yrs:\n rng = period_range(\"1987Q2\", periods=int(n * 4), freq=\"Q\")\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs1.append(xaxis.get_majorticklocs()[0])\n\n (vmin, vmax) = ax.get_xlim()\n ax.set_xlim(vmin + 0.9, vmax)\n rs2.append(xaxis.get_majorticklocs()[0])\n self.plt.close(ax.get_figure())\n\n assert rs1 == xpl1\n assert rs2 == xpl2\n\n def test_finder_monthly(self):\n yrs = [1.15, 2.5, 4, 11]\n\n xpl1 = xpl2 = [Period(\"Jan 1988\").ordinal] * len(yrs)\n rs1 = []\n rs2 = []\n for n in yrs:\n rng = period_range(\"1987Q2\", periods=int(n * 12), freq=\"M\")\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs1.append(xaxis.get_majorticklocs()[0])\n\n vmin, vmax = ax.get_xlim()\n ax.set_xlim(vmin + 0.9, vmax)\n rs2.append(xaxis.get_majorticklocs()[0])\n self.plt.close(ax.get_figure())\n\n assert rs1 == xpl1\n assert rs2 == xpl2\n\n def test_finder_monthly_long(self):\n rng = period_range(\"1988Q1\", periods=24 * 12, freq=\"M\")\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs = xaxis.get_majorticklocs()[0]\n xp = Period(\"1989Q1\", \"M\").ordinal\n assert rs == xp\n\n def test_finder_annual(self):\n xp = [1987, 1988, 1990, 1990, 1995, 2020, 2070, 2170]\n xp = [Period(x, freq=\"A\").ordinal for x in xp]\n rs = []\n for nyears in [5, 10, 19, 49, 99, 199, 599, 1001]:\n rng = period_range(\"1987\", periods=nyears, freq=\"A\")\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs.append(xaxis.get_majorticklocs()[0])\n self.plt.close(ax.get_figure())\n\n assert rs == xp\n\n def test_finder_minutely(self):\n nminutes = 50 * 24 * 60\n rng = date_range(\"1/1/1999\", freq=\"Min\", periods=nminutes)\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs = xaxis.get_majorticklocs()[0]\n xp = Period(\"1/1/1999\", freq=\"Min\").ordinal\n\n assert rs == xp\n\n def test_finder_hourly(self):\n nhours = 23\n rng = date_range(\"1/1/1999\", freq=\"H\", periods=nhours)\n ser = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n xaxis = ax.get_xaxis()\n rs = xaxis.get_majorticklocs()[0]\n xp = Period(\"1/1/1999\", freq=\"H\").ordinal\n\n assert rs == xp\n\n def test_gaps(self):\n ts = tm.makeTimeSeries()\n ts[5:25] = np.nan\n _, ax = self.plt.subplots()\n ts.plot(ax=ax)\n lines = ax.get_lines()\n assert len(lines) == 1\n line = lines[0]\n data = line.get_xydata()\n\n if self.mpl_ge_3_0_0 or not self.mpl_ge_2_2_3:\n data = np.ma.MaskedArray(data, mask=isna(data), fill_value=np.nan)\n\n assert isinstance(data, np.ma.core.MaskedArray)\n mask = data.mask\n assert mask[5:25, 1].all()\n self.plt.close(ax.get_figure())\n\n # irregular\n ts = tm.makeTimeSeries()\n ts = ts[[0, 1, 2, 5, 7, 9, 12, 15, 20]]\n ts[2:5] = np.nan\n _, ax = self.plt.subplots()\n ax = ts.plot(ax=ax)\n lines = ax.get_lines()\n assert len(lines) == 1\n line = lines[0]\n data = line.get_xydata()\n\n if self.mpl_ge_3_0_0 or not self.mpl_ge_2_2_3:\n data = np.ma.MaskedArray(data, mask=isna(data), fill_value=np.nan)\n\n assert isinstance(data, np.ma.core.MaskedArray)\n mask = data.mask\n assert mask[2:5, 1].all()\n self.plt.close(ax.get_figure())\n\n # non-ts\n idx = [0, 1, 2, 5, 7, 9, 12, 15, 20]\n ser = Series(np.random.randn(len(idx)), idx)\n ser[2:5] = np.nan\n _, ax = self.plt.subplots()\n ser.plot(ax=ax)\n lines = ax.get_lines()\n assert len(lines) == 1\n line = lines[0]\n data = line.get_xydata()\n if self.mpl_ge_3_0_0 or not self.mpl_ge_2_2_3:\n data = np.ma.MaskedArray(data, mask=isna(data), fill_value=np.nan)\n\n assert isinstance(data, np.ma.core.MaskedArray)\n mask = data.mask\n assert mask[2:5, 1].all()\n\n def test_gap_upsample(self):\n low = tm.makeTimeSeries()\n low[5:25] = np.nan\n _, ax = self.plt.subplots()\n low.plot(ax=ax)\n\n idxh = date_range(low.index[0], low.index[-1], freq=\"12h\")\n s = Series(np.random.randn(len(idxh)), idxh)\n s.plot(secondary_y=True)\n lines = ax.get_lines()\n assert len(lines) == 1\n assert len(ax.right_ax.get_lines()) == 1\n\n line = lines[0]\n data = line.get_xydata()\n if self.mpl_ge_3_0_0 or not self.mpl_ge_2_2_3:\n data = np.ma.MaskedArray(data, mask=isna(data), fill_value=np.nan)\n\n assert isinstance(data, np.ma.core.MaskedArray)\n mask = data.mask\n assert mask[5:25, 1].all()\n\n def test_secondary_y(self):\n ser = Series(np.random.randn(10))\n ser2 = Series(np.random.randn(10))\n fig, _ = self.plt.subplots()\n ax = ser.plot(secondary_y=True)\n assert hasattr(ax, \"left_ax\")\n assert not hasattr(ax, \"right_ax\")\n axes = fig.get_axes()\n line = ax.get_lines()[0]\n xp = Series(line.get_ydata(), line.get_xdata())\n tm.assert_series_equal(ser, xp)\n assert ax.get_yaxis().get_ticks_position() == \"right\"\n assert not axes[0].get_yaxis().get_visible()\n self.plt.close(fig)\n\n _, ax2 = self.plt.subplots()\n ser2.plot(ax=ax2)\n assert ax2.get_yaxis().get_ticks_position() == self.default_tick_position\n self.plt.close(ax2.get_figure())\n\n ax = ser2.plot()\n ax2 = ser.plot(secondary_y=True)\n assert ax.get_yaxis().get_visible()\n assert not hasattr(ax, \"left_ax\")\n assert hasattr(ax, \"right_ax\")\n assert hasattr(ax2, \"left_ax\")\n assert not hasattr(ax2, \"right_ax\")\n\n def test_secondary_y_ts(self):\n idx = date_range(\"1/1/2000\", periods=10)\n ser = Series(np.random.randn(10), idx)\n ser2 = Series(np.random.randn(10), idx)\n fig, _ = self.plt.subplots()\n ax = ser.plot(secondary_y=True)\n assert hasattr(ax, \"left_ax\")\n assert not hasattr(ax, \"right_ax\")\n axes = fig.get_axes()\n line = ax.get_lines()[0]\n xp = Series(line.get_ydata(), line.get_xdata()).to_timestamp()\n tm.assert_series_equal(ser, xp)\n assert ax.get_yaxis().get_ticks_position() == \"right\"\n assert not axes[0].get_yaxis().get_visible()\n self.plt.close(fig)\n\n _, ax2 = self.plt.subplots()\n ser2.plot(ax=ax2)\n assert ax2.get_yaxis().get_ticks_position() == self.default_tick_position\n self.plt.close(ax2.get_figure())\n\n ax = ser2.plot()\n ax2 = ser.plot(secondary_y=True)\n assert ax.get_yaxis().get_visible()\n\n @td.skip_if_no_scipy\n def test_secondary_kde(self):\n\n ser = Series(np.random.randn(10))\n fig, ax = self.plt.subplots()\n ax = ser.plot(secondary_y=True, kind=\"density\", ax=ax)\n assert hasattr(ax, \"left_ax\")\n assert not hasattr(ax, \"right_ax\")\n axes = fig.get_axes()\n assert axes[1].get_yaxis().get_ticks_position() == \"right\"\n\n def test_secondary_bar(self):\n ser = Series(np.random.randn(10))\n fig, ax = self.plt.subplots()\n ser.plot(secondary_y=True, kind=\"bar\", ax=ax)\n axes = fig.get_axes()\n assert axes[1].get_yaxis().get_ticks_position() == \"right\"\n\n def test_secondary_frame(self):\n df = DataFrame(np.random.randn(5, 3), columns=[\"a\", \"b\", \"c\"])\n axes = df.plot(secondary_y=[\"a\", \"c\"], subplots=True)\n assert axes[0].get_yaxis().get_ticks_position() == \"right\"\n assert axes[1].get_yaxis().get_ticks_position() == self.default_tick_position\n assert axes[2].get_yaxis().get_ticks_position() == \"right\"\n\n def test_secondary_bar_frame(self):\n df = DataFrame(np.random.randn(5, 3), columns=[\"a\", \"b\", \"c\"])\n axes = df.plot(kind=\"bar\", secondary_y=[\"a\", \"c\"], subplots=True)\n assert axes[0].get_yaxis().get_ticks_position() == \"right\"\n assert axes[1].get_yaxis().get_ticks_position() == self.default_tick_position\n assert axes[2].get_yaxis().get_ticks_position() == \"right\"\n\n def test_mixed_freq_regular_first(self):\n # TODO\n s1 = tm.makeTimeSeries()\n s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]]\n\n # it works!\n _, ax = self.plt.subplots()\n s1.plot(ax=ax)\n\n ax2 = s2.plot(style=\"g\", ax=ax)\n lines = ax2.get_lines()\n idx1 = PeriodIndex(lines[0].get_xdata())\n idx2 = PeriodIndex(lines[1].get_xdata())\n\n tm.assert_index_equal(idx1, s1.index.to_period(\"B\"))\n tm.assert_index_equal(idx2, s2.index.to_period(\"B\"))\n\n left, right = ax2.get_xlim()\n pidx = s1.index.to_period()\n assert left <= pidx[0].ordinal\n assert right >= pidx[-1].ordinal\n\n def test_mixed_freq_irregular_first(self):\n s1 = tm.makeTimeSeries()\n s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]]\n _, ax = self.plt.subplots()\n s2.plot(style=\"g\", ax=ax)\n s1.plot(ax=ax)\n assert not hasattr(ax, \"freq\")\n lines = ax.get_lines()\n x1 = lines[0].get_xdata()\n tm.assert_numpy_array_equal(x1, s2.index.astype(object).values)\n x2 = lines[1].get_xdata()\n tm.assert_numpy_array_equal(x2, s1.index.astype(object).values)\n\n def test_mixed_freq_regular_first_df(self):\n # GH 9852\n s1 = tm.makeTimeSeries().to_frame()\n s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15], :]\n _, ax = self.plt.subplots()\n s1.plot(ax=ax)\n ax2 = s2.plot(style=\"g\", ax=ax)\n lines = ax2.get_lines()\n idx1 = PeriodIndex(lines[0].get_xdata())\n idx2 = PeriodIndex(lines[1].get_xdata())\n assert idx1.equals(s1.index.to_period(\"B\"))\n assert idx2.equals(s2.index.to_period(\"B\"))\n left, right = ax2.get_xlim()\n pidx = s1.index.to_period()\n assert left <= pidx[0].ordinal\n assert right >= pidx[-1].ordinal\n\n def test_mixed_freq_irregular_first_df(self):\n # GH 9852\n s1 = tm.makeTimeSeries().to_frame()\n s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15], :]\n _, ax = self.plt.subplots()\n s2.plot(style=\"g\", ax=ax)\n s1.plot(ax=ax)\n assert not hasattr(ax, \"freq\")\n lines = ax.get_lines()\n x1 = lines[0].get_xdata()\n tm.assert_numpy_array_equal(x1, s2.index.astype(object).values)\n x2 = lines[1].get_xdata()\n tm.assert_numpy_array_equal(x2, s1.index.astype(object).values)\n\n def test_mixed_freq_hf_first(self):\n idxh = date_range(\"1/1/1999\", periods=365, freq=\"D\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n high.plot(ax=ax)\n low.plot(ax=ax)\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == \"D\"\n\n def test_mixed_freq_alignment(self):\n ts_ind = date_range(\"2012-01-01 13:00\", \"2012-01-02\", freq=\"H\")\n ts_data = np.random.randn(12)\n\n ts = Series(ts_data, index=ts_ind)\n ts2 = ts.asfreq(\"T\").interpolate()\n\n _, ax = self.plt.subplots()\n ax = ts.plot(ax=ax)\n ts2.plot(style=\"r\", ax=ax)\n\n assert ax.lines[0].get_xdata()[0] == ax.lines[1].get_xdata()[0]\n\n def test_mixed_freq_lf_first(self):\n\n idxh = date_range(\"1/1/1999\", periods=365, freq=\"D\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n low.plot(legend=True, ax=ax)\n high.plot(legend=True, ax=ax)\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == \"D\"\n leg = ax.get_legend()\n assert len(leg.texts) == 2\n self.plt.close(ax.get_figure())\n\n idxh = date_range(\"1/1/1999\", periods=240, freq=\"T\")\n idxl = date_range(\"1/1/1999\", periods=4, freq=\"H\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n low.plot(ax=ax)\n high.plot(ax=ax)\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == \"T\"\n\n def test_mixed_freq_irreg_period(self):\n ts = tm.makeTimeSeries()\n irreg = ts[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 29]]\n rng = period_range(\"1/3/2000\", periods=30, freq=\"B\")\n ps = Series(np.random.randn(len(rng)), rng)\n _, ax = self.plt.subplots()\n irreg.plot(ax=ax)\n ps.plot(ax=ax)\n\n def test_mixed_freq_shared_ax(self):\n\n # GH13341, using sharex=True\n idx1 = date_range(\"2015-01-01\", periods=3, freq=\"M\")\n idx2 = idx1[:1].union(idx1[2:])\n s1 = Series(range(len(idx1)), idx1)\n s2 = Series(range(len(idx2)), idx2)\n\n fig, (ax1, ax2) = self.plt.subplots(nrows=2, sharex=True)\n s1.plot(ax=ax1)\n s2.plot(ax=ax2)\n\n assert ax1.freq == \"M\"\n assert ax2.freq == \"M\"\n assert ax1.lines[0].get_xydata()[0, 0] == ax2.lines[0].get_xydata()[0, 0]\n\n # using twinx\n fig, ax1 = self.plt.subplots()\n ax2 = ax1.twinx()\n s1.plot(ax=ax1)\n s2.plot(ax=ax2)\n\n assert ax1.lines[0].get_xydata()[0, 0] == ax2.lines[0].get_xydata()[0, 0]\n\n # TODO (GH14330, GH14322)\n # plotting the irregular first does not yet work\n # fig, ax1 = plt.subplots()\n # ax2 = ax1.twinx()\n # s2.plot(ax=ax1)\n # s1.plot(ax=ax2)\n # assert (ax1.lines[0].get_xydata()[0, 0] ==\n # ax2.lines[0].get_xydata()[0, 0])\n\n def test_nat_handling(self):\n\n _, ax = self.plt.subplots()\n\n dti = DatetimeIndex([\"2015-01-01\", NaT, \"2015-01-03\"])\n s = Series(range(len(dti)), dti)\n s.plot(ax=ax)\n xdata = ax.get_lines()[0].get_xdata()\n # plot x data is bounded by index values\n assert s.index.min() <= Series(xdata).min()\n assert Series(xdata).max() <= s.index.max()\n\n def test_to_weekly_resampling(self):\n idxh = date_range(\"1/1/1999\", periods=52, freq=\"W\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n high.plot(ax=ax)\n low.plot(ax=ax)\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == idxh.freq\n\n def test_from_weekly_resampling(self):\n idxh = date_range(\"1/1/1999\", periods=52, freq=\"W\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n low.plot(ax=ax)\n high.plot(ax=ax)\n\n expected_h = idxh.to_period().asi8.astype(np.float64)\n expected_l = np.array(\n [1514, 1519, 1523, 1527, 1531, 1536, 1540, 1544, 1549, 1553, 1558, 1562],\n dtype=np.float64,\n )\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == idxh.freq\n xdata = line.get_xdata(orig=False)\n if len(xdata) == 12: # idxl lines\n tm.assert_numpy_array_equal(xdata, expected_l)\n else:\n tm.assert_numpy_array_equal(xdata, expected_h)\n tm.close()\n\n def test_from_resampling_area_line_mixed(self):\n idxh = date_range(\"1/1/1999\", periods=52, freq=\"W\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = DataFrame(np.random.rand(len(idxh), 3), index=idxh, columns=[0, 1, 2])\n low = DataFrame(np.random.rand(len(idxl), 3), index=idxl, columns=[0, 1, 2])\n\n # low to high\n for kind1, kind2 in [(\"line\", \"area\"), (\"area\", \"line\")]:\n _, ax = self.plt.subplots()\n low.plot(kind=kind1, stacked=True, ax=ax)\n high.plot(kind=kind2, stacked=True, ax=ax)\n\n # check low dataframe result\n expected_x = np.array(\n [\n 1514,\n 1519,\n 1523,\n 1527,\n 1531,\n 1536,\n 1540,\n 1544,\n 1549,\n 1553,\n 1558,\n 1562,\n ],\n dtype=np.float64,\n )\n expected_y = np.zeros(len(expected_x), dtype=np.float64)\n for i in range(3):\n line = ax.lines[i]\n assert PeriodIndex(line.get_xdata()).freq == idxh.freq\n tm.assert_numpy_array_equal(line.get_xdata(orig=False), expected_x)\n # check stacked values are correct\n expected_y += low[i].values\n tm.assert_numpy_array_equal(line.get_ydata(orig=False), expected_y)\n\n # check high dataframe result\n expected_x = idxh.to_period().asi8.astype(np.float64)\n expected_y = np.zeros(len(expected_x), dtype=np.float64)\n for i in range(3):\n line = ax.lines[3 + i]\n assert PeriodIndex(data=line.get_xdata()).freq == idxh.freq\n tm.assert_numpy_array_equal(line.get_xdata(orig=False), expected_x)\n expected_y += high[i].values\n tm.assert_numpy_array_equal(line.get_ydata(orig=False), expected_y)\n\n # high to low\n for kind1, kind2 in [(\"line\", \"area\"), (\"area\", \"line\")]:\n _, ax = self.plt.subplots()\n high.plot(kind=kind1, stacked=True, ax=ax)\n low.plot(kind=kind2, stacked=True, ax=ax)\n\n # check high dataframe result\n expected_x = idxh.to_period().asi8.astype(np.float64)\n expected_y = np.zeros(len(expected_x), dtype=np.float64)\n for i in range(3):\n line = ax.lines[i]\n assert PeriodIndex(data=line.get_xdata()).freq == idxh.freq\n tm.assert_numpy_array_equal(line.get_xdata(orig=False), expected_x)\n expected_y += high[i].values\n tm.assert_numpy_array_equal(line.get_ydata(orig=False), expected_y)\n\n # check low dataframe result\n expected_x = np.array(\n [\n 1514,\n 1519,\n 1523,\n 1527,\n 1531,\n 1536,\n 1540,\n 1544,\n 1549,\n 1553,\n 1558,\n 1562,\n ],\n dtype=np.float64,\n )\n expected_y = np.zeros(len(expected_x), dtype=np.float64)\n for i in range(3):\n lines = ax.lines[3 + i]\n assert PeriodIndex(data=lines.get_xdata()).freq == idxh.freq\n tm.assert_numpy_array_equal(lines.get_xdata(orig=False), expected_x)\n expected_y += low[i].values\n tm.assert_numpy_array_equal(lines.get_ydata(orig=False), expected_y)\n\n def test_mixed_freq_second_millisecond(self):\n # GH 7772, GH 7760\n idxh = date_range(\"2014-07-01 09:00\", freq=\"S\", periods=50)\n idxl = date_range(\"2014-07-01 09:00\", freq=\"100L\", periods=500)\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n # high to low\n _, ax = self.plt.subplots()\n high.plot(ax=ax)\n low.plot(ax=ax)\n assert len(ax.get_lines()) == 2\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == \"L\"\n tm.close()\n\n # low to high\n _, ax = self.plt.subplots()\n low.plot(ax=ax)\n high.plot(ax=ax)\n assert len(ax.get_lines()) == 2\n for line in ax.get_lines():\n assert PeriodIndex(data=line.get_xdata()).freq == \"L\"\n\n def test_irreg_dtypes(self):\n # date\n idx = [date(2000, 1, 1), date(2000, 1, 5), date(2000, 1, 20)]\n df = DataFrame(np.random.randn(len(idx), 3), Index(idx, dtype=object))\n _check_plot_works(df.plot)\n\n # np.datetime64\n idx = date_range(\"1/1/2000\", periods=10)\n idx = idx[[0, 2, 5, 9]].astype(object)\n df = DataFrame(np.random.randn(len(idx), 3), idx)\n _, ax = self.plt.subplots()\n _check_plot_works(df.plot, ax=ax)\n\n def test_time(self):\n t = datetime(1, 1, 1, 3, 30, 0)\n deltas = np.random.randint(1, 20, 3).cumsum()\n ts = np.array([(t + timedelta(minutes=int(x))).time() for x in deltas])\n df = DataFrame(\n {\"a\": np.random.randn(len(ts)), \"b\": np.random.randn(len(ts))}, index=ts\n )\n fig, ax = self.plt.subplots()\n df.plot(ax=ax)\n\n # verify tick labels\n ticks = ax.get_xticks()\n labels = ax.get_xticklabels()\n for t, l in zip(ticks, labels):\n m, s = divmod(int(t), 60)\n h, m = divmod(m, 60)\n rs = l.get_text()\n if len(rs) > 0:\n if s != 0:\n xp = time(h, m, s).strftime(\"%H:%M:%S\")\n else:\n xp = time(h, m, s).strftime(\"%H:%M\")\n assert xp == rs\n\n def test_time_change_xlim(self):\n t = datetime(1, 1, 1, 3, 30, 0)\n deltas = np.random.randint(1, 20, 3).cumsum()\n ts = np.array([(t + timedelta(minutes=int(x))).time() for x in deltas])\n df = DataFrame(\n {\"a\": np.random.randn(len(ts)), \"b\": np.random.randn(len(ts))}, index=ts\n )\n fig, ax = self.plt.subplots()\n df.plot(ax=ax)\n\n # verify tick labels\n ticks = ax.get_xticks()\n labels = ax.get_xticklabels()\n for t, l in zip(ticks, labels):\n m, s = divmod(int(t), 60)\n h, m = divmod(m, 60)\n rs = l.get_text()\n if len(rs) > 0:\n if s != 0:\n xp = time(h, m, s).strftime(\"%H:%M:%S\")\n else:\n xp = time(h, m, s).strftime(\"%H:%M\")\n assert xp == rs\n\n # change xlim\n ax.set_xlim(\"1:30\", \"5:00\")\n\n # check tick labels again\n ticks = ax.get_xticks()\n labels = ax.get_xticklabels()\n for t, l in zip(ticks, labels):\n m, s = divmod(int(t), 60)\n h, m = divmod(m, 60)\n rs = l.get_text()\n if len(rs) > 0:\n if s != 0:\n xp = time(h, m, s).strftime(\"%H:%M:%S\")\n else:\n xp = time(h, m, s).strftime(\"%H:%M\")\n assert xp == rs\n\n def test_time_musec(self):\n t = datetime(1, 1, 1, 3, 30, 0)\n deltas = np.random.randint(1, 20, 3).cumsum()\n ts = np.array([(t + timedelta(microseconds=int(x))).time() for x in deltas])\n df = DataFrame(\n {\"a\": np.random.randn(len(ts)), \"b\": np.random.randn(len(ts))}, index=ts\n )\n fig, ax = self.plt.subplots()\n ax = df.plot(ax=ax)\n\n # verify tick labels\n ticks = ax.get_xticks()\n labels = ax.get_xticklabels()\n for t, l in zip(ticks, labels):\n m, s = divmod(int(t), 60)\n\n us = round((t - int(t)) * 1e6)\n\n h, m = divmod(m, 60)\n rs = l.get_text()\n if len(rs) > 0:\n if (us % 1000) != 0:\n xp = time(h, m, s, us).strftime(\"%H:%M:%S.%f\")\n elif (us // 1000) != 0:\n xp = time(h, m, s, us).strftime(\"%H:%M:%S.%f\")[:-3]\n elif s != 0:\n xp = time(h, m, s, us).strftime(\"%H:%M:%S\")\n else:\n xp = time(h, m, s, us).strftime(\"%H:%M\")\n assert xp == rs\n\n def test_secondary_upsample(self):\n idxh = date_range(\"1/1/1999\", periods=365, freq=\"D\")\n idxl = date_range(\"1/1/1999\", periods=12, freq=\"M\")\n high = Series(np.random.randn(len(idxh)), idxh)\n low = Series(np.random.randn(len(idxl)), idxl)\n _, ax = self.plt.subplots()\n low.plot(ax=ax)\n ax = high.plot(secondary_y=True, ax=ax)\n for line in ax.get_lines():\n assert PeriodIndex(line.get_xdata()).freq == \"D\"\n assert hasattr(ax, \"left_ax\")\n assert not hasattr(ax, \"right_ax\")\n for line in ax.left_ax.get_lines():\n assert PeriodIndex(line.get_xdata()).freq == \"D\"\n\n def test_secondary_legend(self):\n fig = self.plt.figure()\n ax = fig.add_subplot(211)\n\n # ts\n df = tm.makeTimeDataFrame()\n df.plot(secondary_y=[\"A\", \"B\"], ax=ax)\n leg = ax.get_legend()\n assert len(leg.get_lines()) == 4\n assert leg.get_texts()[0].get_text() == \"A (right)\"\n assert leg.get_texts()[1].get_text() == \"B (right)\"\n assert leg.get_texts()[2].get_text() == \"C\"\n assert leg.get_texts()[3].get_text() == \"D\"\n assert ax.right_ax.get_legend() is None\n colors = set()\n for line in leg.get_lines():\n colors.add(line.get_color())\n\n # TODO: color cycle problems\n assert len(colors) == 4\n self.plt.close(fig)\n\n fig = self.plt.figure()\n ax = fig.add_subplot(211)\n df.plot(secondary_y=[\"A\", \"C\"], mark_right=False, ax=ax)\n leg = ax.get_legend()\n assert len(leg.get_lines()) == 4\n assert leg.get_texts()[0].get_text() == \"A\"\n assert leg.get_texts()[1].get_text() == \"B\"\n assert leg.get_texts()[2].get_text() == \"C\"\n assert leg.get_texts()[3].get_text() == \"D\"\n self.plt.close(fig)\n\n fig, ax = self.plt.subplots()\n df.plot(kind=\"bar\", secondary_y=[\"A\"], ax=ax)\n leg = ax.get_legend()\n assert leg.get_texts()[0].get_text() == \"A (right)\"\n assert leg.get_texts()[1].get_text() == \"B\"\n self.plt.close(fig)\n\n fig, ax = self.plt.subplots()\n df.plot(kind=\"bar\", secondary_y=[\"A\"], mark_right=False, ax=ax)\n leg = ax.get_legend()\n assert leg.get_texts()[0].get_text() == \"A\"\n assert leg.get_texts()[1].get_text() == \"B\"\n self.plt.close(fig)\n\n fig = self.plt.figure()\n ax = fig.add_subplot(211)\n df = tm.makeTimeDataFrame()\n ax = df.plot(secondary_y=[\"C\", \"D\"], ax=ax)\n leg = ax.get_legend()\n assert len(leg.get_lines()) == 4\n assert ax.right_ax.get_legend() is None\n colors = set()\n for line in leg.get_lines():\n colors.add(line.get_color())\n\n # TODO: color cycle problems\n assert len(colors) == 4\n self.plt.close(fig)\n\n # non-ts\n df = tm.makeDataFrame()\n fig = self.plt.figure()\n ax = fig.add_subplot(211)\n ax = df.plot(secondary_y=[\"A\", \"B\"], ax=ax)\n leg = ax.get_legend()\n assert len(leg.get_lines()) == 4\n assert ax.right_ax.get_legend() is None\n colors = set()\n for line in leg.get_lines():\n colors.add(line.get_color())\n\n # TODO: color cycle problems\n assert len(colors) == 4\n self.plt.close()\n\n fig = self.plt.figure()\n ax = fig.add_subplot(211)\n ax = df.plot(secondary_y=[\"C\", \"D\"], ax=ax)\n leg = ax.get_legend()\n assert len(leg.get_lines()) == 4\n assert ax.right_ax.get_legend() is None\n colors = set()\n for line in leg.get_lines():\n colors.add(line.get_color())\n\n # TODO: color cycle problems\n assert len(colors) == 4\n\n def test_format_date_axis(self):\n rng = date_range(\"1/1/2012\", periods=12, freq=\"M\")\n df = DataFrame(np.random.randn(len(rng), 3), rng)\n _, ax = self.plt.subplots()\n ax = df.plot(ax=ax)\n xaxis = ax.get_xaxis()\n for line in xaxis.get_ticklabels():\n if len(line.get_text()) > 0:\n assert line.get_rotation() == 30\n\n def test_ax_plot(self):\n x = date_range(start=\"2012-01-02\", periods=10, freq=\"D\")\n y = list(range(len(x)))\n _, ax = self.plt.subplots()\n lines = ax.plot(x, y, label=\"Y\")\n tm.assert_index_equal(DatetimeIndex(lines[0].get_xdata()), x)\n\n def test_mpl_nopandas(self):\n dates = [date(2008, 12, 31), date(2009, 1, 31)]\n values1 = np.arange(10.0, 11.0, 0.5)\n values2 = np.arange(11.0, 12.0, 0.5)\n\n kw = {\"fmt\": \"-\", \"lw\": 4}\n\n _, ax = self.plt.subplots()\n ax.plot_date([x.toordinal() for x in dates], values1, **kw)\n ax.plot_date([x.toordinal() for x in dates], values2, **kw)\n\n line1, line2 = ax.get_lines()\n\n exp = np.array([x.toordinal() for x in dates], dtype=np.float64)\n tm.assert_numpy_array_equal(line1.get_xydata()[:, 0], exp)\n exp = np.array([x.toordinal() for x in dates], dtype=np.float64)\n tm.assert_numpy_array_equal(line2.get_xydata()[:, 0], exp)\n\n def test_irregular_ts_shared_ax_xlim(self):\n # GH 2960\n from pandas.plotting._matplotlib.converter import DatetimeConverter\n\n ts = tm.makeTimeSeries()[:20]\n ts_irregular = ts[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]]\n\n # plot the left section of the irregular series, then the right section\n _, ax = self.plt.subplots()\n ts_irregular[:5].plot(ax=ax)\n ts_irregular[5:].plot(ax=ax)\n\n # check that axis limits are correct\n left, right = ax.get_xlim()\n assert left <= DatetimeConverter.convert(ts_irregular.index.min(), \"\", ax)\n assert right >= DatetimeConverter.convert(ts_irregular.index.max(), \"\", ax)\n\n def test_secondary_y_non_ts_xlim(self):\n # GH 3490 - non-timeseries with secondary y\n index_1 = [1, 2, 3, 4]\n index_2 = [5, 6, 7, 8]\n s1 = Series(1, index=index_1)\n s2 = Series(2, index=index_2)\n\n _, ax = self.plt.subplots()\n s1.plot(ax=ax)\n left_before, right_before = ax.get_xlim()\n s2.plot(secondary_y=True, ax=ax)\n left_after, right_after = ax.get_xlim()\n\n assert left_before >= left_after\n assert right_before < right_after\n\n def test_secondary_y_regular_ts_xlim(self):\n # GH 3490 - regular-timeseries with secondary y\n index_1 = date_range(start=\"2000-01-01\", periods=4, freq=\"D\")\n index_2 = date_range(start=\"2000-01-05\", periods=4, freq=\"D\")\n s1 = Series(1, index=index_1)\n s2 = Series(2, index=index_2)\n\n _, ax = self.plt.subplots()\n s1.plot(ax=ax)\n left_before, right_before = ax.get_xlim()\n s2.plot(secondary_y=True, ax=ax)\n left_after, right_after = ax.get_xlim()\n\n assert left_before >= left_after\n assert right_before < right_after\n\n def test_secondary_y_mixed_freq_ts_xlim(self):\n # GH 3490 - mixed frequency timeseries with secondary y\n rng = date_range(\"2000-01-01\", periods=10000, freq=\"min\")\n ts = Series(1, index=rng)\n\n _, ax = self.plt.subplots()\n ts.plot(ax=ax)\n left_before, right_before = ax.get_xlim()\n ts.resample(\"D\").mean().plot(secondary_y=True, ax=ax)\n left_after, right_after = ax.get_xlim()\n\n # a downsample should not have changed either limit\n assert left_before == left_after\n assert right_before == right_after\n\n def test_secondary_y_irregular_ts_xlim(self):\n # GH 3490 - irregular-timeseries with secondary y\n from pandas.plotting._matplotlib.converter import DatetimeConverter\n\n ts = tm.makeTimeSeries()[:20]\n ts_irregular = ts[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]]\n\n _, ax = self.plt.subplots()\n ts_irregular[:5].plot(ax=ax)\n # plot higher-x values on secondary axis\n ts_irregular[5:].plot(secondary_y=True, ax=ax)\n # ensure secondary limits aren't overwritten by plot on primary\n ts_irregular[:5].plot(ax=ax)\n\n left, right = ax.get_xlim()\n assert left <= DatetimeConverter.convert(ts_irregular.index.min(), \"\", ax)\n assert right >= DatetimeConverter.convert(ts_irregular.index.max(), \"\", ax)\n\n def test_plot_outofbounds_datetime(self):\n # 2579 - checking this does not raise\n values = [date(1677, 1, 1), date(1677, 1, 2)]\n _, ax = self.plt.subplots()\n ax.plot(values)\n\n values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)]\n ax.plot(values)\n\n def test_format_timedelta_ticks_narrow(self):\n\n expected_labels = [f\"00:00:00.0000000{i:0>2d}\" for i in np.arange(10)]\n\n rng = timedelta_range(\"0\", periods=10, freq=\"ns\")\n df = DataFrame(np.random.randn(len(rng), 3), rng)\n fig, ax = self.plt.subplots()\n df.plot(fontsize=2, ax=ax)\n self.plt.draw()\n labels = ax.get_xticklabels()\n\n result_labels = [x.get_text() for x in labels]\n assert len(result_labels) == len(expected_labels)\n assert result_labels == expected_labels\n\n def test_format_timedelta_ticks_wide(self):\n expected_labels = [\n \"00:00:00\",\n \"1 days 03:46:40\",\n \"2 days 07:33:20\",\n \"3 days 11:20:00\",\n \"4 days 15:06:40\",\n \"5 days 18:53:20\",\n \"6 days 22:40:00\",\n \"8 days 02:26:40\",\n \"9 days 06:13:20\",\n ]\n\n rng = timedelta_range(\"0\", periods=10, freq=\"1 d\")\n df = DataFrame(np.random.randn(len(rng), 3), rng)\n fig, ax = self.plt.subplots()\n ax = df.plot(fontsize=2, ax=ax)\n self.plt.draw()\n labels = ax.get_xticklabels()\n\n result_labels = [x.get_text() for x in labels]\n assert len(result_labels) == len(expected_labels)\n assert result_labels == expected_labels\n\n def test_timedelta_plot(self):\n # test issue #8711\n s = Series(range(5), timedelta_range(\"1day\", periods=5))\n _, ax = self.plt.subplots()\n _check_plot_works(s.plot, ax=ax)\n\n # test long period\n index = timedelta_range(\"1 day 2 hr 30 min 10 s\", periods=10, freq=\"1 d\")\n s = Series(np.random.randn(len(index)), index)\n _, ax = self.plt.subplots()\n _check_plot_works(s.plot, ax=ax)\n\n # test short period\n index = timedelta_range(\"1 day 2 hr 30 min 10 s\", periods=10, freq=\"1 ns\")\n s = Series(np.random.randn(len(index)), index)\n _, ax = self.plt.subplots()\n _check_plot_works(s.plot, ax=ax)\n\n def test_hist(self):\n # https://github.com/matplotlib/matplotlib/issues/8459\n rng = date_range(\"1/1/2011\", periods=10, freq=\"H\")\n x = rng\n w1 = np.arange(0, 1, 0.1)\n w2 = np.arange(0, 1, 0.1)[::-1]\n _, ax = self.plt.subplots()\n ax.hist([x, x], weights=[w1, w2])\n\n def test_overlapping_datetime(self):\n # GB 6608\n s1 = Series(\n [1, 2, 3],\n index=[\n datetime(1995, 12, 31),\n datetime(2000, 12, 31),\n datetime(2005, 12, 31),\n ],\n )\n s2 = Series(\n [1, 2, 3],\n index=[\n datetime(1997, 12, 31),\n datetime(2003, 12, 31),\n datetime(2008, 12, 31),\n ],\n )\n\n # plot first series, then add the second series to those axes,\n # then try adding the first series again\n _, ax = self.plt.subplots()\n s1.plot(ax=ax)\n s2.plot(ax=ax)\n s1.plot(ax=ax)\n\n @pytest.mark.xfail(reason=\"GH9053 matplotlib does not use ax.xaxis.converter\")\n def test_add_matplotlib_datetime64(self):\n # GH9053 - ensure that a plot with PeriodConverter still understands\n # datetime64 data. This still fails because matplotlib overrides the\n # ax.xaxis.converter with a DatetimeConverter\n s = Series(np.random.randn(10), index=date_range(\"1970-01-02\", periods=10))\n ax = s.plot()\n with tm.assert_produces_warning(DeprecationWarning):\n # multi-dimensional indexing\n ax.plot(s.index, s.values, color=\"g\")\n l1, l2 = ax.lines\n tm.assert_numpy_array_equal(l1.get_xydata(), l2.get_xydata())\n\n def test_matplotlib_scatter_datetime64(self):\n # https://github.com/matplotlib/matplotlib/issues/11391\n df = DataFrame(np.random.RandomState(0).rand(10, 2), columns=[\"x\", \"y\"])\n df[\"time\"] = date_range(\"2018-01-01\", periods=10, freq=\"D\")\n fig, ax = self.plt.subplots()\n ax.scatter(x=\"time\", y=\"y\", data=df)\n self.plt.draw()\n label = ax.get_xticklabels()[0]\n if self.mpl_ge_3_2_0:\n expected = \"2018-01-01\"\n elif self.mpl_ge_3_0_0:\n expected = \"2017-12-08\"\n else:\n expected = \"2017-12-12\"\n assert label.get_text() == expected\n\n def test_check_xticks_rot(self):\n # https://github.com/pandas-dev/pandas/issues/29460\n # regular time series\n x = to_datetime([\"2020-05-01\", \"2020-05-02\", \"2020-05-03\"])\n df = DataFrame({\"x\": x, \"y\": [1, 2, 3]})\n axes = df.plot(x=\"x\", y=\"y\")\n self._check_ticks_props(axes, xrot=0)\n\n # irregular time series\n x = to_datetime([\"2020-05-01\", \"2020-05-02\", \"2020-05-04\"])\n df = DataFrame({\"x\": x, \"y\": [1, 2, 3]})\n axes = df.plot(x=\"x\", y=\"y\")\n self._check_ticks_props(axes, xrot=30)\n\n # use timeseries index or not\n axes = df.set_index(\"x\").plot(y=\"y\", use_index=True)\n self._check_ticks_props(axes, xrot=30)\n axes = df.set_index(\"x\").plot(y=\"y\", use_index=False)\n self._check_ticks_props(axes, xrot=0)\n\n # separate subplots\n axes = df.plot(x=\"x\", y=\"y\", subplots=True, sharex=True)\n self._check_ticks_props(axes, xrot=30)\n axes = df.plot(x=\"x\", y=\"y\", subplots=True, sharex=False)\n self._check_ticks_props(axes, xrot=0)\n\n\ndef _check_plot_works(f, freq=None, series=None, *args, **kwargs):\n import matplotlib.pyplot as plt\n\n fig = plt.gcf()\n\n try:\n plt.clf()\n ax = fig.add_subplot(211)\n orig_ax = kwargs.pop(\"ax\", plt.gca())\n orig_axfreq = getattr(orig_ax, \"freq\", None)\n\n ret = f(*args, **kwargs)\n assert ret is not None # do something more intelligent\n\n ax = kwargs.pop(\"ax\", plt.gca())\n if series is not None:\n dfreq = series.index.freq\n if isinstance(dfreq, BaseOffset):\n dfreq = dfreq.rule_code\n if orig_axfreq is None:\n assert ax.freq == dfreq\n\n if freq is not None and orig_axfreq is None:\n assert ax.freq == freq\n\n ax = fig.add_subplot(212)\n kwargs[\"ax\"] = ax\n ret = f(*args, **kwargs)\n assert ret is not None # TODO: do something more intelligent\n\n with tm.ensure_clean(return_filelike=True) as path:\n plt.savefig(path)\n\n # GH18439, GH#24088, statsmodels#4772\n with tm.ensure_clean(return_filelike=True) as path:\n pickle.dump(fig, path)\n finally:\n plt.close(fig)\n" ]
[ [ "pandas._testing.assert_numpy_array_equal", "pandas.Series", "pandas._testing.ensure_clean", "numpy.asarray", "numpy.random.RandomState", "pandas.core.indexes.period.period_range", "pandas._testing.assert_series_equal", "pandas._testing.makeDataFrame", "pandas.core.indexes.period.Period", "numpy.fabs", "pandas.tests.plotting.common.TestPlotBase.setup_method", "matplotlib.pyplot.gcf", "pandas.tseries.offsets.WeekOfMonth", "pandas._testing.assert_produces_warning", "matplotlib.pyplot.gca", "matplotlib.pyplot.savefig", "pandas._testing.close", "pandas.to_datetime", "pandas._libs.tslibs.to_offset", "pandas.core.indexes.timedeltas.timedelta_range", "pandas.plotting._matplotlib.converter.get_datevalue", "pandas.plotting._matplotlib.converter._from_ordinal", "pandas.core.indexes.datetimes.date_range", "matplotlib.pyplot.clf", "numpy.arange", "pandas.core.indexes.datetimes.DatetimeIndex", "matplotlib.pyplot.close", "pandas.Index", "pandas._testing.makeTimeSeries", "pandas._testing.makeTimeDataFrame", "pandas.DataFrame", "numpy.random.randn", "pandas.core.indexes.datetimes.bdate_range", "pandas.core.indexes.period.PeriodIndex", "pandas._testing.makePeriodSeries", "numpy.array", "numpy.random.randint", "pandas.isna" ] ]
krassowski/jupyter-helpers
[ "9feb0af6563b56d02688c18e1a9a415d15d9b1a2" ]
[ "jupyter_helpers/table.py" ]
[ "import pandas as pd\nfrom IPython.core.display import display\n\n\ndef bordered_table(hide_headers=[], color='#ddd'):\n return [\n {'selector': 'th', 'props': [('text-align', 'center'), ('border', f'1px solid {color}')]},\n {'selector': 'td', 'props': [('border', f'1px solid {color}')]},\n *[\n {'selector': f'thead tr:nth-child({row})', 'props': [('display', 'none')]}\n for row in hide_headers\n ]\n ]\n\n\ndef display_table(table, n_rows=50, n_cols=None, long_names=-1):\n if not n_cols:\n n_cols = n_rows\n with pd.option_context(\n 'display.max_rows', n_rows,\n 'display.max_columns', n_cols,\n 'display.max_colwidth', long_names\n ):\n display(table)\n" ]
[ [ "pandas.option_context" ] ]
chelffey/tilebot
[ "3fa4cc3ea7c03786fb8ac02458ef4e6d464603ec" ]
[ "src/tiler.py" ]
[ "# Purpose: takes a list of filenames AND/OR publically accessible urls. \n# Returns a tiled image file of tiles SIZExSIZE, separated by spaces of width \n# DIFF, in rows if length ROWSIZE. \n# files that can't be retrieved are returned blank. \n\nimport os\nimport numpy as np\nfrom PIL import Image\nimport urllib.request\nimport validators\n\n# global vars\nEMPTY = \"empty\"\n\n'''\nCrops given 'Image' object to the largest square possible. \nCenters the image. \n'''\ndef crop_square(im):\n # crops to square, based on smallest length.\n width, height = im.size\n side_length = min(width, height)\n width_pad = (width - side_length) // 2\n height_pad = (height - side_length) // 2\n left = width_pad\n top = height_pad\n right = width - width_pad\n bottom = height - height_pad \n return im.crop((left, top, right, bottom))\n\n\n'''\nPurpose: transparent-pads images to precisely correct size. Robustness for images that are too small, or one pixel off the correct size. \nInput: a = a numpy array representing thumbnail. \n Assumption: thumbnail x, y dim are NO GREATER than size. \nside = desired side length. \nReturns thumbnail of precisely (SIZE x SIZE x 4).\nPadding will CENTRE the thumbnail. \n'''\ndef pad_thumbnail(a, side):\n ax, ay, az = a.shape \n if (ax < side): # not tall enough. add row of (padx x y).\n padx = side - ax\n x1 = padx // 2\n x2 = x1 + padx % 2\n row1 = np.full((x1, ay, 4), [255, 255, 255, 0], np.uint8) \n row2 = np.full((x2, ay, 4), [255, 255, 255, 0], np.uint8) \n a = np.concatenate((row1, a, row2))\n elif (ax > side): # too tall, crop. \n cutoff = side - ax\n a = a[:cutoff]\n if (ay < side): # not wide enough. add col of (pady x side)\n pady = side - ay\n y1 = pady // 2\n y2 = y1 + pady % 2\n col1 = np.full((side, y1, 4), [255, 255, 255, 0], np.uint8) \n col2 = np.full((side, y2, 4), [255, 255, 255, 0], np.uint8) \n a = np.hstack((col1, a, col2))\n elif (ay > side): # too wide, crop. \n cutoff = side - ay\n a = a[:, :cutoff]\n return a\n\n\n'''\nOpens image file from local directory. \nReturns as an np array of thumbnail SIZE, in 4dim RGBA format. \n'''\ndef gen_thumbnail(filename, tileSize, default):\n if (filename == EMPTY):\n return default\n \n # save from web into folder if url. \n if (validators.url(filename)):\n try:\n urllib.request.urlretrieve(filename, \".temp_web_images/temp_image\")\n filename = \".temp_web_images/temp_image\"\n except:\n print(\"error: url could not be retrieved.\")\n return default # if image can't be retrieved. \n\n with Image.open(filename) as im:\n im = im.convert(\"RGBA\") # add transparency\n\n x, y = im.size # scale down to thumbnail.\n tsize = int(tileSize * (max(x, y) / min(x, y)))\n im.thumbnail((tsize, tsize), Image.ANTIALIAS) \n\n im = crop_square(im) # THIS LINE: toggle to change whether square or original aspect ratio.\n \n a = np.asarray(im) # create np array from values.\n a = pad_thumbnail(a, tileSize) # for robustness. \n \n # delete temp saved image\n if (filename == \".temp_web_images/temp_image\"):\n os.remove(\".temp_web_images/temp_image\")\n \n return a\n\n\n'''\nMain functionality. Converts list of filenames into a tiled grid of thumbnails.\nReturns as Image object. \n'''\ndef tile_images(files, tileSize, rowLength, space):\n # initialise transparent padding\n row_space = np.full((space, tileSize, 4), [255, 255, 255, 0], np.uint8) \n col_space = np.full((tileSize, space, 4), [255, 255, 255, 0], np.uint8) \n square = np.full((tileSize, tileSize, 4), [255, 255, 255, 0], np.uint8)\n row_div = np.full((space, tileSize*rowLength + space*(rowLength-1), 4), [255, 255, 255, 0], np.uint8)\n\n # initialise folder to save web images into \n if not os.path.exists('.temp_web_images'):\n os.makedirs('.temp_web_images')\n\n # reshape 1D file list into 2D structured grid of row length rowLength\n to_add = rowLength - (len(files) % rowLength)\n if to_add != rowLength:\n files.extend([EMPTY]*to_add)\n arr = np.array(files)\n newFiles = arr.reshape(len(files) // rowLength, rowLength)\n\n # create each row array and add to list.\n rowList = []\n for row in newFiles:\n thisRow = []\n for file in row:\n thisRow.extend([gen_thumbnail(file, tileSize, square), col_space])\n rowArr = np.hstack([np.array(i) for i in thisRow[:-1]])\n rowList.extend([rowArr, row_div])\n\n # concat row arrays into a single grid array\n arr = np.concatenate([np.array(i) for i in rowList[:-1]]) # elegant numpy approach: from https://stackoverflow.com/questions/10346336/list-of-lists-into-numpy-array\n im = Image.fromarray(arr)\n return im\n \n\n\n\nif __name__ == \"__main__\":\n print(\"hello world!! im tilebot\")\n\n files = [\n \"./pic/bamboo.jpg\",\n \"./pic/coconut.png\",\n \"./pic/fish.png\",\n \"./pic/shiro.jpg\",\n \"./pic/calico-cat.png\",\n \"./pic/ghost.png\",\n \"./pic/field.jpg\",\n \"./pic/blue.gif\",\n \"./pic/boy.jpg\"\n ]\n\n urls = [\n \"./pic/bamboo.jpg\",\n \"./pic/coconut.png\",\n \"./pic/fish.png\",\n \"./pic/shiro.jpg\",\n \"https://cdn.i-scmp.com/sites/default/files/styles/1200x800/public/d8/images/methode/2020/10/30/8caac9de-1a82-11eb-8f67-a484f6db61a1_image_hires_175647.jpg?itok=T-dFsg-A&v=1604051814\",\n \"https://www.nme.com/wp-content/uploads/2021/03/Genshin-Impact-miHoYo.jpg\",\n \"https://www.indiewire.com/wp-content/uploads/2020/12/genshin1.jpg\",\n \"./pic/calico-cat.png\",\n \"./pic/ghost.png\",\n \"./pic/field.jpg\",\n \"./pic/blue.gif\",\n \"./pic/boy.jpg\",\n \"https://blog.playstation.com/tachyon/2020/11/Featured-Image-Genshin-Impact-update-out-tomorrow.jpg?fit=1024,720\",\n \"https://cdn.vox-cdn.com/thumbor/pot2y4VQxXpzedEZ8eDMrFR2wLg=/0x308:7680x4320/1200x800/filters:focal(3413x728:4641x1956)/cdn.vox-cdn.com/uploads/chorus_image/image/67716030/ba84dbaad79d15323968a64863c1e069.0.jpg\",\n \"https://gamerbraves.sgp1.cdn.digitaloceanspaces.com/2020/01/arknights-feature-c.jpg\",\n \"https://webusstatic.yo-star.com/uy0news/ae/19c9d44c8cf7d7bc770ee588b52dc2e0.png\"\n ]\n\n # doesn't work - these urls aren't publically accessible. \n disc_urls = [\n \"https://cdn.discordapp.com/attachments/841255574330408981/841266535376486460/EzFyC5ZVcAA1-_m.jpg\",\n \"https://cdn.discordapp.com/attachments/841255574330408981/841266037214806046/Elu0GiWVkAEzrHm.png\",\n \"https://cdn.discordapp.com/attachments/841255574330408981/841265455237824512/tumblr_nayd2yGcBC1rscimho1_500.png\"\n ]\n\n tilesize = 136\n rowlength = 6\n spacing = 4\n\n im = tile_images(files, tilesize, rowlength, spacing)\n im.save(\"./pic/merge-GRID.png\", \"PNG\")\n\n im = tile_images(urls, tilesize, rowlength, spacing)\n im.save(\"./pic/url_merged_2.png\", \"PNG\")\n" ]
[ [ "numpy.asarray", "numpy.hstack", "numpy.array", "numpy.concatenate", "numpy.full" ] ]
guntbert/astropy
[ "f2d2add09e5b1638b2698f19a4d46fcca19e82be" ]
[ "astropy/visualization/wcsaxes/tests/test_display_world_coordinates.py" ]
[ "# Licensed under a 3-clause BSD style license - see LICENSE.rst\nfrom astropy.visualization.wcsaxes.core import WCSAxes\nimport matplotlib.pyplot as plt\nfrom matplotlib.backend_bases import KeyEvent\n\nfrom astropy.wcs import WCS\nfrom astropy.coordinates import FK5\nfrom astropy.time import Time\nfrom astropy.tests.image_tests import ignore_matplotlibrc\n\nfrom .test_images import BaseImageTests\n\n\nclass TestDisplayWorldCoordinate(BaseImageTests):\n\n @ignore_matplotlibrc\n def test_overlay_coords(self, tmpdir):\n wcs = WCS(self.msx_header)\n\n fig = plt.figure(figsize=(4, 4))\n canvas = fig.canvas\n\n ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs)\n fig.add_axes(ax)\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test1.png').strpath)\n\n # Testing default displayed world coordinates\n string_world = ax._display_world_coords(0.523412, 0.518311)\n assert string_world == '0\\xb029\\'45\" -0\\xb029\\'20\" (world)'\n\n # Test pixel coordinates\n event1 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event1.key, guiEvent=event1)\n string_pixel = ax._display_world_coords(0.523412, 0.523412)\n assert string_pixel == \"0.523412 0.523412 (pixel)\"\n\n event3 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event3.key, guiEvent=event3)\n # Test that it still displays world coords when there are no overlay coords\n string_world2 = ax._display_world_coords(0.523412, 0.518311)\n assert string_world2 == '0\\xb029\\'45\" -0\\xb029\\'20\" (world)'\n\n overlay = ax.get_coords_overlay('fk5')\n\n # Regression test for bug that caused format to always be taken from\n # main world coordinates.\n overlay[0].set_major_formatter('d.ddd')\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test2.png').strpath)\n\n event4 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event4.key, guiEvent=event4)\n # Test that it displays the overlay world coordinates\n string_world3 = ax._display_world_coords(0.523412, 0.518311)\n\n assert string_world3 == '267.176\\xb0 -28\\xb045\\'56\" (world, overlay 1)'\n\n overlay = ax.get_coords_overlay(FK5())\n\n # Regression test for bug that caused format to always be taken from\n # main world coordinates.\n overlay[0].set_major_formatter('d.ddd')\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test3.png').strpath)\n\n event5 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event4.key, guiEvent=event4)\n # Test that it displays the overlay world coordinates\n string_world4 = ax._display_world_coords(0.523412, 0.518311)\n\n assert string_world4 == '267.176\\xb0 -28\\xb045\\'56\" (world, overlay 2)'\n\n overlay = ax.get_coords_overlay(FK5(equinox=Time(\"J2030\")))\n\n # Regression test for bug that caused format to always be taken from\n # main world coordinates.\n overlay[0].set_major_formatter('d.ddd')\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test4.png').strpath)\n\n event6 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event5.key, guiEvent=event6)\n # Test that it displays the overlay world coordinates\n string_world5 = ax._display_world_coords(0.523412, 0.518311)\n\n assert string_world5 == '267.652\\xb0 -28\\xb046\\'23\" (world, overlay 3)'\n\n @ignore_matplotlibrc\n def test_cube_coords(self, tmpdir):\n wcs = WCS(self.cube_header)\n\n fig = plt.figure(figsize=(4, 4))\n canvas = fig.canvas\n\n ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs, slices=('y', 50, 'x'))\n fig.add_axes(ax)\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test.png').strpath)\n\n # Testing default displayed world coordinates\n string_world = ax._display_world_coords(0.523412, 0.518311)\n assert string_world == '3h26m52.0s 30\\xb037\\'17\\\" 2563 (world)'\n\n # Test pixel coordinates\n event1 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event1.key, guiEvent=event1)\n string_pixel = ax._display_world_coords(0.523412, 0.523412)\n assert string_pixel == \"0.523412 0.523412 (pixel)\"\n\n @ignore_matplotlibrc\n def test_cube_coords_uncorr_slicing(self, tmpdir):\n\n # Regression test for a bug that occurred with coordinate formatting if\n # some dimensions were uncorrelated and sliced out.\n\n wcs = WCS(self.cube_header)\n\n fig = plt.figure(figsize=(4, 4))\n canvas = fig.canvas\n\n ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs, slices=('x', 'y', 2))\n fig.add_axes(ax)\n\n # On some systems, fig.canvas.draw is not enough to force a draw, so we\n # save to a temporary file.\n fig.savefig(tmpdir.join('test.png').strpath)\n\n # Testing default displayed world coordinates\n string_world = ax._display_world_coords(0.523412, 0.518311)\n assert string_world == '3h26m56.6s 30\\xb018\\'19\\\" (world)'\n\n # Test pixel coordinates\n event1 = KeyEvent('test_pixel_coords', canvas, 'w')\n fig.canvas.key_press_event(event1.key, guiEvent=event1)\n string_pixel = ax._display_world_coords(0.523412, 0.523412)\n assert string_pixel == \"0.523412 0.523412 (pixel)\"\n" ]
[ [ "matplotlib.backend_bases.KeyEvent", "matplotlib.pyplot.figure" ] ]
2018-B-GR1-Python/Velasco-Yepez-Andres-David
[ "0c017d6e5f169f31207ddec5ceffc8dd82d327eb" ]
[ "03_spyder/proyecto_spyder.py" ]
[ "\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Wed Nov 28 01:15:31 2018\n\n@author: Andres\n\"\"\"\n\nimport pandas as pd\nurl = 'http://catalogo.datosabiertos.gob.ec/api/action/datastore_search?resource_id=8513f446-1c94-426e-8592-d4cbdd295f33&limit=1000'\n\ndatos = pd.read_json(url, typ='frame')\ndatos =pd.DataFrame.from_dict(datos[\"result\"][\"records\"]).set_index(\"_id\")\n#datos[ datos['Sexo'] != 'MASCULINO' ]\ndatos.loc[289,'Canton']\nclass Homicidio:\n\n def __init__(self,Canton,Circuito,Distrito,Edad,Estado_Civil,Fecha_infraccion,Hora_infraccion,Nacionalidad,Provincia,Sexo,Zona,tipo_muert_matriz):\n self.Canton=Canton\n self.Circuito=Circuito\n self.Distrito=Distrito\n self.Edad = Edad\n self.Estado_civil=Estado_Civil\n self.Fecha_infraccion=Fecha_infraccion\n self.Hora_infraccion=Hora_infraccion\n self.Nacionalidad=Nacionalidad\n self.Provincia = Provincia\n self.Sexo = Sexo\n self.Zona = Zona\n self.tipo = tipo_muert_matriz\n\n def get_list(self):\n return [self.Canton,self.Circuito,self.Distrito,self.Edad,self.Estado_civil,self.Fecha_infraccion,\n self.Hora_infraccion,self.Nacionalidad,self.Provincia,self.Sexo,self.Zona,self.tipo]\n\ndef insertar(Canton,Circuito,Distrito,Edad,Estado_Civil,Fecha_infraccion,Hora_infraccion,Nacionalidad,Provincia,Sexo,Zona,tipo_muert_matriz):\n global datos\n _id = datos.index+1\n homicidio=Homicidio(Canton,Circuito,Distrito,Edad,Estado_Civil,Fecha_infraccion,Hora_infraccion,Nacionalidad,Provincia,Sexo,Zona,tipo_muert_matriz)\n s = homicidio.get_list()\n serie = pd.Series(s,index=datos.columns)\n datos = datos.append(serie,ignore_index=True) # adding a row\n \n\n\n\ninsertar(\"MIAMI\",\t\"CI\",\"W\",\"211\",\"SOLTERO\",\t\"2019-04-05T00:00:00\",\"2015-12-02T23:00:00\",\t\n \"EEUU\",\"FLORIDA\",\"MASCULINO\",\t\"ZONA 80\",\"Asesinatos\"\n)\n\n\n \n\n" ]
[ [ "pandas.Series", "pandas.DataFrame.from_dict", "pandas.read_json" ] ]
leonidk/cupdice
[ "f3386337922337eaaae2f244607f1af73516843f" ]
[ "lavalle_rrts.py" ]
[ "#!/usr/bin/env python\n\n# rrt.py\n# This program generates a simple rapidly\n# exploring random tree (RRT) in a rectangular region.\n#\n# Written by Steve LaValle\n# May 2011\n\nimport sys, random, math, pygame\nfrom pygame.locals import *\nfrom math import sqrt,cos,sin,atan2\nimport heapq\nimport numpy as np\n\n#constants\nXDIM = 500\nYDIM = 500\nWINSIZE = np.array([XDIM, YDIM])\nMAX_STEP_SIZE = 12\nNUMNODES = 5000\nNUM_OBSTACLES = 30\nOBSTACLE_WIDTH = 80\nOBSTACLE_HEIGHT = 80\nRAND_SEARCH_PROB = 0.25\nGOAL_TOL = 1e-3\n\nstart = WINSIZE/2\ngoal1 = np.zeros((1,2))\ngoal2 = WINSIZE.reshape((1,2))\n\n\n\ndef step_from_to(p1,p2):\n if np.linalg.norm(p1-p2) < MAX_STEP_SIZE:\n return p2\n else:\n diff = p2-p1\n return p1 + MAX_STEP_SIZE*diff/np.linalg.norm(diff)\n\ndef main():\n #initialize and prepare screen\n pygame.init()\n screen = pygame.display.set_mode(WINSIZE)\n pygame.display.set_caption('RRT S. LaValle May 2011')\n white = 255, 240, 200\n black = 20, 20, 40\n screen.fill(black)\n\n obstacles = []\n for _ in range(NUM_OBSTACLES):\n rand_rect = np.random.rand(4)*np.array([XDIM,YDIM,OBSTACLE_WIDTH,OBSTACLE_HEIGHT]) + np.ones(4)*MAX_STEP_SIZE\n if (rand_rect[:2] < start).all() and (rand_rect[:2]+rand_rect[2:] > start).all():\n print('skip!')\n continue\n if (rand_rect[:2] < goal1).all() and (rand_rect[:2]+rand_rect[2:] > goal1).all():\n print('skip!')\n continue\n if (rand_rect[:2] < goal2).all() and (rand_rect[:2]+rand_rect[2:] > goal2).all():\n print('skip!')\n continue\n obstacles.append(rand_rect)\n for idx,o in enumerate(obstacles):\n weight = idx/(len(obstacles)-1)\n color = (240-240*weight,128,40+(255-40)*weight)\n screen.fill(color,o)\n\n nodes = np.array([start])[:np.newaxis]\n connections = np.array([0])\n print(nodes.shape,connections.shape)\n for goal in [goal1,goal2]:\n searching = True\n prev_node = None\n for i in range(NUMNODES):\n if searching:\n # get a random configuration\n #valid = False\n #while not valid:\n if prev_node is None:\n rand = np.random.rand(1,2)*WINSIZE if np.random.rand() > RAND_SEARCH_PROB else goal\n else:\n rand = prev_node\n #valid = True\n #for o in obstacles:\n #if (o[:2] < rand[0]).all() and (o[:2]+o[2:] > rand[0]).all():\n #valid = False\n #break\n\n dists = np.linalg.norm(nodes-rand,axis=1)\n #print(dists)\n closest_idx = np.argmin(dists)\n closest = nodes[closest_idx]\n new_node = step_from_to(closest,rand)\n valid_new_node = True\n for o in obstacles:\n if (o[:2] < new_node[0]).all() and (o[:2]+o[2:] > new_node[0]).all():\n valid_new_node = False\n break\n if valid_new_node:\n if (rand == goal).all() and np.linalg.norm(new_node - goal) < dists.min():\n prev_node = new_node\n #print('new')\n else:\n prev_node = None\n #print('cancel')\n if np.linalg.norm(new_node - goal) > GOAL_TOL:\n #print(goal,new_node)\n\n nodes = np.append(nodes,new_node,0)\n connections = np.append(connections,closest_idx)\n #print(np.linalg.norm(new_node - goal),nodes.shape,connections.shape)\n\n pygame.draw.line(screen,white,np.squeeze(closest),np.squeeze(new_node))\n else:\n print(new_node,goal)\n path_node = closest_idx\n while path_node != 0:\n print(path_node,end=',',flush=True)\n path_node = connections[path_node]\n print(0)\n searching = False\n break\n else:\n prev_node = None\n pygame.display.update()\n #print i, \" \", nodes\n\n for e in pygame.event.get():\n if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):\n sys.exit(\"Leaving because you requested it.\")\n \n while True:\n for e in pygame.event.get():\n if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):\n sys.exit(\"Leaving because you requested it.\")\n# if python says run, then we should run\nif __name__ == '__main__':\n main()\n\n" ]
[ [ "numpy.ones", "numpy.append", "numpy.zeros", "numpy.squeeze", "numpy.argmin", "numpy.random.rand", "numpy.array", "numpy.linalg.norm" ] ]
mehrdadzakershahrak/Online-Explanation-Generation
[ "e41ad9b5a390abdaf271562a56105c191e33b74d" ]
[ "rovers/fastdownward/experiments/issue750/relativescatter.py" ]
[ "# -*- coding: utf-8 -*-\n\nfrom collections import defaultdict\n\nfrom matplotlib import ticker\n\nfrom downward.reports.scatter import ScatterPlotReport\nfrom downward.reports.plot import PlotReport, Matplotlib, MatplotlibPlot\n\n\n# TODO: handle outliers\n\n# TODO: this is mostly copied from ScatterMatplotlib (scatter.py)\nclass RelativeScatterMatplotlib(Matplotlib):\n @classmethod\n def _plot(cls, report, axes, categories, styles):\n # Display grid\n axes.grid(b=True, linestyle='-', color='0.75')\n\n has_points = False\n # Generate the scatter plots\n for category, coords in sorted(categories.items()):\n X, Y = zip(*coords)\n axes.scatter(X, Y, s=42, label=category, **styles[category])\n if X and Y:\n has_points = True\n\n if report.xscale == 'linear' or report.yscale == 'linear':\n plot_size = report.missing_val * 1.01\n else:\n plot_size = report.missing_val * 1.25\n\n # make 5 ticks above and below 1\n yticks = []\n tick_step = report.ylim_top**(1/5.0)\n for i in xrange(-5, 6):\n yticks.append(tick_step**i)\n axes.set_yticks(yticks)\n axes.get_yaxis().set_major_formatter(ticker.ScalarFormatter())\n\n axes.set_xlim(report.xlim_left or -1, report.xlim_right or plot_size)\n axes.set_ylim(report.ylim_bottom or -1, report.ylim_top or plot_size)\n\n for axis in [axes.xaxis, axes.yaxis]:\n MatplotlibPlot.change_axis_formatter(\n axis,\n report.missing_val if report.show_missing else None)\n return has_points\n\n\nclass RelativeScatterPlotReport(ScatterPlotReport):\n \"\"\"\n Generate a scatter plot that shows a relative comparison of two\n algorithms with regard to the given attribute. The attribute value\n of algorithm 1 is shown on the x-axis and the relation to the value\n of algorithm 2 on the y-axis.\n \"\"\"\n\n def __init__(self, show_missing=True, get_category=None, **kwargs):\n ScatterPlotReport.__init__(self, show_missing, get_category, **kwargs)\n if self.output_format == 'tex':\n raise \"not supported\"\n else:\n self.writer = RelativeScatterMatplotlib\n\n def _fill_categories(self, runs):\n # We discard the *runs* parameter.\n # Map category names to value tuples\n categories = defaultdict(list)\n self.ylim_bottom = 2\n self.ylim_top = 0.5\n self.xlim_left = float(\"inf\")\n for (domain, problem), runs in self.problem_runs.items():\n if len(runs) != 2:\n continue\n run1, run2 = runs\n assert (run1['algorithm'] == self.algorithms[0] and\n run2['algorithm'] == self.algorithms[1])\n val1 = run1.get(self.attribute)\n val2 = run2.get(self.attribute)\n if val1 is None or val2 is None:\n continue\n category = self.get_category(run1, run2)\n assert val1 > 0, (domain, problem, self.algorithms[0], val1)\n assert val2 > 0, (domain, problem, self.algorithms[1], val2)\n x = val1\n y = val2 / float(val1)\n\n categories[category].append((x, y))\n\n self.ylim_top = max(self.ylim_top, y)\n self.ylim_bottom = min(self.ylim_bottom, y)\n self.xlim_left = min(self.xlim_left, x)\n\n # center around 1\n if self.ylim_bottom < 1:\n self.ylim_top = max(self.ylim_top, 1 / float(self.ylim_bottom))\n if self.ylim_top > 1:\n self.ylim_bottom = min(self.ylim_bottom, 1 / float(self.ylim_top))\n return categories\n\n def _set_scales(self, xscale, yscale):\n # ScatterPlot uses log-scaling on the x-axis by default.\n PlotReport._set_scales(\n self, xscale or self.attribute.scale or 'log', 'log')\n" ]
[ [ "matplotlib.ticker.ScalarFormatter" ] ]
lerongil/qiskit-terra
[ "a25af2a2378bc3d4f5ec73b948d048d1b707454c" ]
[ "test/python/quantum_info/test_weyl.py" ]
[ "# -*- coding: utf-8 -*-\n\n# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2017, 2019.\n#\n# This code is licensed under the Apache License, Version 2.0. You may\n# obtain a copy of this license in the LICENSE.txt file in the root directory\n# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.\n#\n# Any modifications or derivative works of this code must retain this\n# copyright notice, and modified files need to carry a notice indicating\n# that they have been altered from the originals.\n# pylint: disable=invalid-name\n\n\"\"\"Tests for Weyl coorindate routines.\"\"\"\n\nimport unittest\nimport numpy as np\nfrom numpy.testing import assert_allclose\n\nfrom qiskit.test import QiskitTestCase\nfrom qiskit.quantum_info.random import random_unitary\nfrom qiskit.quantum_info.synthesis.weyl import weyl_coordinates\nfrom qiskit.quantum_info.synthesis.local_invariance import (two_qubit_local_invariants,\n local_equivalence)\n\n\nclass TestWeyl(QiskitTestCase):\n \"\"\"Test Weyl coordinate routines\"\"\"\n\n def test_weyl_coordinates_simple(self):\n \"\"\"Check Weyl coordinates against known cases.\n \"\"\"\n # Identity [0,0,0]\n U = np.identity(4)\n weyl = weyl_coordinates(U)\n assert_allclose(weyl, [0, 0, 0])\n\n # CNOT [pi/4, 0, 0]\n U = np.array([[1, 0, 0, 0],\n [0, 0, 0, 1],\n [0, 0, 1, 0],\n [0, 1, 0, 0]], dtype=complex)\n weyl = weyl_coordinates(U)\n assert_allclose(weyl, [np.pi / 4, 0, 0], atol=1e-07)\n\n # SWAP [pi/4, pi/4 ,pi/4]\n U = np.array([[1, 0, 0, 0],\n [0, 0, 1, 0],\n [0, 1, 0, 0],\n [0, 0, 0, 1]], dtype=complex)\n\n weyl = weyl_coordinates(U)\n assert_allclose(weyl, [np.pi / 4, np.pi / 4, np.pi / 4])\n\n # SQRT ISWAP [pi/8, pi/8, 0]\n U = np.array([[1, 0, 0, 0],\n [0, 1 / np.sqrt(2), 1j / np.sqrt(2), 0],\n [0, 1j / np.sqrt(2), 1 / np.sqrt(2), 0],\n [0, 0, 0, 1]], dtype=complex)\n\n weyl = weyl_coordinates(U)\n assert_allclose(weyl, [np.pi / 8, np.pi / 8, 0])\n\n def test_weyl_coordinates_random(self):\n \"\"\"Randomly check Weyl coordinates with local invariants.\n \"\"\"\n for _ in range(10):\n U = random_unitary(4).data\n weyl = weyl_coordinates(U)\n local_equiv = local_equivalence(weyl)\n local = two_qubit_local_invariants(U)\n assert_allclose(local, local_equiv)\n\n\nif __name__ == '__main__':\n unittest.main()\n" ]
[ [ "numpy.array", "numpy.sqrt", "numpy.identity", "numpy.testing.assert_allclose" ] ]
YodaEmbedding/experiments
[ "567c6a1c18fac2d951fe2af54aaa4917b7d529d2" ]
[ "py/pyanaconda/tf_straightline.py" ]
[ "# Fit a straight line, of the form y=m*x+b\n\nimport tensorflow as tf\n\nxs = [0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00] # Features\nys = [-0.82, -0.94, -0.12, 0.26, 0.39, 0.64, 1.02, 1.00] # Labels\n\n\"\"\"\nwith enough iterations, initial weights dont matter since our cost function is convex.\n\"\"\"\nm_initial = -0.5 # Initial guesses\nb_initial = 1.0\n\n\n\"\"\"\ndefine free variables to be solved. we'll be taking partial derivatives of m and b with respect to j (cost).\n\"\"\"\nm = tf.Variable(m_initial) # Parameters\nb = tf.Variable(b_initial)\n\nerror = 0.0\nfor i in range(len(xs)):\n y_model = m * xs[i] + b # Output of the model aka yhat\n error += (\n ys[i] - y_model\n ) ** 2 # Difference squared - this is the \"cost\" to be minimized\n\n\n\"\"\"\nonce cost function is defined, use gradient descent to find global minimum.\n\"\"\"\n\noperation = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(\n error\n) # Does one step\n\n\nwith tf.Session() as session:\n session.run(tf.initialize_all_variables()) # Initialize session\n\n _EPOCHS = 10000 # number of \"sweeps\" across data\n\n for iteration in range(_EPOCHS):\n session.run(operation)\n\nprint(\"Slope:\", m.eval(), \"Intercept:\", b.eval())\n" ]
[ [ "tensorflow.initialize_all_variables", "tensorflow.train.GradientDescentOptimizer", "tensorflow.Session", "tensorflow.Variable" ] ]
CDInstitute/CompoNET.github.io
[ "f978f31a78628c70b1033ed02a75de8a50aa905d" ]
[ "dataset/generator.py" ]
[ "import bpy, bmesh\nfrom math import radians\nimport numpy as np\nimport os\n\nimport random\nimport sys\n\nsys.path.append(\"D:\\ProgramFiles\\Anaconda\\envs\\py37\\Lib\\site-packages\")\nfrom pyntcloud import PyntCloud\n\nfile_dir = os.path.dirname(__file__)\nsys.path.append(file_dir)\n\nfrom blender_utils import extrude, gancio, get_min_max\nfrom dataset_config import *\nfrom material import Material\nfrom module import *\nfrom point_cloud import PointCloud\nfrom renderer import Renderer\nfrom shp2obj import Collection, deselect_all\nfrom volume import *\n\n\nclass BuildingFactory:\n\t\"\"\"\n\tFactory that produces volumes.\n\t\"\"\"\n\tdef __init__(self):\n\n\t\tself.mapping = {'Patio': (Patio, 4),\n\t\t\t 'L': (LBuilding, 2),\n\t\t\t 'C': (CBuilding, 3),\n\t\t\t 'Single': (ComposedBuilding, 1),\n\t\t\t 'Skyscraper': (Skyscraper, 1),\n\t\t\t 'Closedpatio': (ClosedPatio, 2),\n\t\t\t 'Equalpatio': (PatioEqual, 4)}\n\t\tself.mapping = {x: y for x, y in self.mapping.items() if x in BUILDINGS}\n\n\tdef produce(self, name=None):\n\t\t\"\"\"\n\t\tFunction that produces a volume based on the given scale.\n\t\t:param scale: tuple (width, length, height)\n\t\t:return: generated volume, Volume\n\t\t\"\"\"\n\t\tif name:\n\t\t\tname = name.lower().capitalize()\n\t\t\tassert name in list(self.mapping.keys()), \"{} building typology \" \\\n\t\t\t \"does not exist\".format(name)\n\t\telse:\n\t\t\tname = np.random.choice(list(self.mapping.keys()))\n\t\t_volumes = CollectionFactory().produce(number=self.mapping[name][1]).collection\n\t\treturn self.mapping[name][0](_volumes)\n\n\nclass ComposedBuilding:\n\t\"\"\"\n\tClass that represents a building composed of one or several volumes.\n\t\"\"\"\n\tdef __init__(self, volumes):\n\t\tassert isinstance(volumes, list), \"Expected volumes as list,\" \\\n\t\t \" got {}\".format(type(volumes))\n\t\tself.volumes = volumes\n\n\t# def demolish(self):\n\t# \tfor v in self.volumes:\n\t# \t\ttry:\n\t# \t\t\tdeselect_all()\n\t# \t\t\tv.mesh.select_set(True)\n\t# \t\t\tbpy.ops.object.delete()\n\t# \t\texcept Exception:\n\t# \t\t\tpass\n\n\tdef demolish(self):\n\t\tfor _mesh in bpy.data.collections['Building'].objects:\n\t\t\ttry:\n\t\t\t\tdeselect_all()\n\t\t\t\t_mesh.select_set(True)\n\t\t\t\tbpy.ops.object.delete()\n\t\t\texcept Exception:\n\t\t\t\tpass\n\n\tdef get_bb(self):\n\t\t\"\"\"\n\t\tFunction that gets the bounding box of the Building\n\t\t:return: bounding box, list of float\n\t\t[width_from, height_from, width_to, height_to]\n\t\t\"\"\"\n\t\tx_min, y_min, x_max, y_max = list(get_min_max(self.volumes[0].mesh, 0)) + \\\n\t\t list(get_min_max(self.volumes[0].mesh, 1))\n\t\tfor v in self.volumes[1:]:\n\t\t\t_bb = list(get_min_max(v.mesh, 0)) + \\\n\t\t\t list(get_min_max(v.mesh, 1))\n\t\t\tx_min, y_min = min(_bb[0], x_min), min(_bb[1], y_min)\n\t\t\tx_max, y_max = max(_bb[2], x_max), max(_bb[3], y_max)\n\t\treturn [round(x_min, 3), round(y_min, 3), round(x_max, 3),\n\t\t round(y_max, 3)]\n\n\tdef make(self):\n\t\t\"\"\"\n\t\tFunction that composes the building based on its typology.\n\t\t:return:\n\t\t\"\"\"\n\t\tself._correct_volumes()\n\t\treturn self.volumes\n\n\tdef save(self, filename='test', ext='obj'):\n\t\t\"\"\"\n\t\tFunction that saves the building as a separate file.\n\t\t:param filename: name of the file to write without extension, str,\n\t\tdefault='test'\n\t\t:param ext: file extension, str, default='obj'\n\t\t:return:\n\t\t\"\"\"\n\t\tdeselect_all()\n\t\tfor v in self.volumes:\n\t\t\tv.mesh.select_set(True)\n\t\tif not MODEL_SAVE in os.listdir(file_dir):\n\t\t\tos.mkdir(file_dir + '/' + MODEL_SAVE)\n\t\tif ext == 'obj':\n\t\t\tbpy.ops.export_scene.obj(filepath='{}/Models/{}.{}'.format(file_dir,\n\t\t\t filename,\n\t\t\t ext),\n\t\t\t use_selection=False)\n\t\telif ext == 'ply':\n\t\t\tbpy.ops.export_mesh.ply(\n\t\t\t\tfilepath='{}/{}/{}.{}'.format(file_dir, CLOUD_SAVE, filename, ext),\n\t\t\t\tuse_selection=False)\n\t\telse:\n\t\t\treturn NotImplementedError\n\n\tdef _correct_volumes(self):\n\t\tfor v in self.volumes:\n\t\t\tv.create()\n\n\nclass LBuilding(ComposedBuilding):\n\t\"\"\"\n\tClass that represents an L-shaped building.\n\t\"\"\"\n\tdef __init__(self, volumes):\n\t\tComposedBuilding.__init__(self, volumes)\n\n\tdef make(self):\n\t\t# add rotation if len > width (or vice versa)\n\t\tself._correct_volumes()\n\t\tgancio(self.volumes[0], self.volumes[1], 0, 0, 0)\n\t\treturn self.volumes\n\n\tdef _correct_volumes(self):\n\n\t\tif np.random.random() < 0.5: # same height\n\t\t\t_height = max(min(self.volumes[0].height,\n\t\t\t min(self.volumes[0].width * 3, MAX_HEIGHT)),\n\t\t\t MIN_HEIGHT)\n\t\t\tfor v in self.volumes:\n\t\t\t\tv.height = _height\n\n\t\tfor v in self.volumes:\n\t\t\tv.create()\n\t\tself.volumes = sorted(self.volumes, key=lambda x: x.length,\n\t\t reverse=True)\n\n\nclass CBuilding(LBuilding):\n\tdef __init__(self, volumes):\n\t\tLBuilding.__init__(self, volumes)\n\t\tassert len(\n\t\t\tvolumes) == 3, \"C-shaped bulding can be composed of 3 volumes\" \\\n\t\t \"only, got {}\".format(len(volumes))\n\n\tdef make(self):\n\t\tself._correct_volumes()\n\t\tfor v in self.volumes[1:]:\n\t\t\tif v.width < v.length:\n\t\t\t\tv.mesh.rotation_euler[2] = radians(90)\n\n\t\tgancio(self.volumes[0], self.volumes[1], 0, 1, 0)\n\t\tgancio(self.volumes[0], self.volumes[2], 0, 0, 0)\n\t\treturn self.volumes\n\n\nclass Patio(ComposedBuilding):\n\t\"\"\"\n\tClass that represents an L-shaped building.\n\t\"\"\"\n\tdef __init__(self, volumes):\n\t\tComposedBuilding.__init__(self, volumes)\n\t\tassert len(volumes) in [2, 4], \"Patio bulding can be composed of 4 \" \\\n\t\t \"volumes only, got {}\".format(len(volumes))\n\t\tself.width = [3, 12]\n\t\tself.length = [6, 20]\n\n\tdef make(self):\n\n\t\tself._correct_volumes()\n\t\tif np.random.random() < 0.5:\n\t\t\t# circular linkage between buildings\n\t\t\tfor i, _v in enumerate(self.volumes[:-1]):\n\t\t\t\tif i % 2 == 0:\n\t\t\t\t\tself.volumes[i + 1].mesh.rotation_euler[2] = radians(90)\n\t\t\t\tif i == 0:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 0, 1, 1)\n\t\t\t\telif i == 1:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 1, 1, 0)\n\t\t\t\telif i == 2:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 0, 0, 0)\n\t\telse:\n\t\t\t# cap linkage between buildings\n\t\t\tfor i, _v in enumerate(self.volumes[:-1]):\n\t\t\t\tif i % 2 == 0:\n\t\t\t\t\tself.volumes[i + 1].mesh.rotation_euler[2] = radians(90)\n\t\t\t\tif i == 0:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 1, 1, 0)\n\t\t\t\telif i == 1:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 1, 1, 0)\n\t\t\t\telif i == 2:\n\t\t\t\t\tgancio(_v, self.volumes[i + 1], 1, 0, 1)\n\n\t\treturn self.volumes\n\n\tdef _correct_volumes(self):\n\t\tfor v in self.volumes:\n\t\t\tv.width = min(max(v.width, self.width[0]), self.width[1])\n\t\t\tv.length = v.width * (np.random.random() + 1.5)\n\t\t\tv.height = max(min(v.height, min(v.width * 3, MAX_HEIGHT)), MIN_HEIGHT)\n\t\t\tv.create()\n\t\tself.volumes = sorted(self.volumes, key=lambda x: x.length)\n\n\nclass PatioEqual(Patio):\n\t\"\"\"\n\tClass that represents a Patio building with equal height volumes.\n\t\"\"\"\n\n\tdef __init__(self, volumes):\n\t\tPatio.__init__(self, volumes)\n\n\tdef _correct_volumes(self):\n\t\t_height = max(min(self.volumes[0].height, min(self.volumes[0].width * 3,\n\t\t MAX_HEIGHT)), MIN_HEIGHT)\n\t\tfor v in self.volumes:\n\t\t\tv.width = min(max(v.width, self.width[0]), self.width[1])\n\t\t\tv.length = v.width * (np.random.random() + 1.5)\n\t\t\tv.height = _height\n\t\t\tv.create()\n\t\tself.volumes = sorted(self.volumes, key=lambda x: x.length)\n\n\nclass ClosedPatio(Patio):\n\t\"\"\"\n\tClass that represents a Patio building with equal height volumes.\n\t\"\"\"\n\n\tdef __init__(self, volumes):\n\t\tPatio.__init__(self, volumes)\n\t\tassert len(self.volumes) == 2, \"Expected 2 volumes for Closed Patio, \" \\\n\t\t \"got {}\".format(len(self.volumes))\n\n\tdef _correct_volumes(self):\n\t\tfor v in self.volumes:\n\t\t\tv.width = min(max(v.width, self.width[0]), self.width[1])\n\t\t\tv.length = v.width * (np.random.random() + 1.5)\n\t\t\tv.height = max(min(v.height, min(v.width * 3, MAX_HEIGHT)),\n\t\t MIN_HEIGHT)\n\t\t\tv.create()\n\n\t\tfor v in self.volumes[:2]:\n\t\t\tv1 = Factory().produce(scale=(v.width, v.length, v.height))\n\t\t\tself.volumes.append(v1)\n\n\nclass TBuilding(ComposedBuilding):\n\t\"\"\"\n\tClass that represents a T-shaped building with random location of the\n\tsecond volume along the side of the first volume.\n\t\"\"\"\n\tdef __init__(self, volumes):\n\t\tComposedBuilding.__init__(self, volumes)\n\t\tassert len(volumes) == 2, \"L-shaped bulding can be composed of 2 volumes\" \\\n\t\t \"only, got {}\".format(len(volumes))\n\n\tdef make(self):\n\t\tself._correct_volumes()\n\t\tx_min, x_max = get_min_max(self.volumes[0].mesh, 0) # width\n\t\ty_min, y_max = get_min_max(self.volumes[0].mesh, 1) # length\n\n\t\tif random.random() < 0.5:\n\t\t\tself.volumes[1].mesh.location[0] = random.choice(np.linspace(int(x_min + (self.volumes[1].length)),\n\t\t\t\t int(x_max - (self.volumes[1].length)), 10))\n\t\t\tself.volumes[1].mesh.location[1] = y_min - self.volumes[1].width\n\n\t\telse:\n\t\t\tself.volumes[1].mesh.location[1] = random.choice(np.linspace(\n\t\t\t\tint(y_min + (self.volumes[1].width)),\n\t\t\t\tint(y_max - (self.volumes[1].width)), 10))\n\t\t\tself.volumes[1].mesh.location[0] = x_min - self.volumes[1].length\n\n\t\treturn self.volumes\n\n\nclass Skyscraper(ComposedBuilding):\n\t\"\"\"\n\tClass that represents a Skyscraper building with height significantly larger\n\tthan width or length of the building.\n\t\"\"\"\n\n\tdef __init__(self, volumes):\n\t\tComposedBuilding.__init__(self, volumes)\n\n\tdef _correct_volumes(self):\n\t\tfor _v in self.volumes:\n\t\t\t_v.height = np.random.randint(100, 200)\n\t\t\t_v.length = max(30, _v.length)\n\t\t\t_v.width = max(30, _v.width)\n\t\t\t_v.create()\n\n\nclass EBuilding(ComposedBuilding):\n\t\"\"\"\n\tClass that represents a E-shaped building with random locations of the\n\tvolumes along the side of the first volume.\n\t\"\"\"\n\tdef __init__(self, volumes):\n\t\tComposedBuilding.__init__(self, volumes)\n\n\tdef make(self):\n\n\t\tself._correct_volumes()\n\t\tx_min, x_max = get_min_max(self.volumes[0].mesh, 0) # width\n\t\ty_min, y_max = get_min_max(self.volumes[0].mesh, 1) # length\n\n\t\tif random.random() < 0.5:\n\t\t\tfor _volume in self.volumes[1:]:\n\t\t\t\t_volume.mesh.location[0] = random.choice(np.linspace(int(x_min + (_volume.length)),\n\t\t\t\t\t int(x_max - (_volume.length)), 10))\n\t\t\t\t_volume.mesh.location[1] = y_min - _volume.width\n\n\t\telse:\n\t\t\tfor _volume in self.volumes[1:]:\n\t\t\t\t_volume.mesh.location[1] = random.choice(np.linspace(\n\t\t\t\t\tint(y_min + (_volume.width)),\n\t\t\t\t\tint(y_max - (_volume.width)), 10))\n\t\t\t\t_volume.mesh.location[0] = x_min - _volume.length\n\n\t\treturn self.volumes\n\n\nif __name__ == '__main__':\n\n\tNUM_IMAGES = 1\n\tfor image in range(NUM_IMAGES):\n\t\tf = CollectionFactory()\n\t\tcollection = f.produce(number=np.random.randint(1, 4))\n\t\tbuilding = ComposedBuilding(collection.collection)\n\t\tbuilding.make()\n\n\t\taxis = 1\n\n\t\tfor j, v in enumerate(collection.collection):\n\n\t\t\tmod = GridApplier(Window)\n\t\t\tw = Window()\n\t\t\tw.connect(v, 1)\n\t\t\tstep = (np.random.randint(1, 6), np.random.randint(1, 6))\n\t\t\tif j == 0:\n\t\t\t\tmod.apply(w, step=step, offset=(2.0, 2.0, 2.0, 1.0))\n\t\t\telse:\n\t\t\t\tmod.apply(w, step=step)\n\n\t\t\tw = Window()\n\t\t\tw.connect(v, 0, 0)\n\t\t\tstep = (np.random.randint(1, 6), np.random.randint(1, 6))\n\t\t\tif j == 0:\n\t\t\t\tmod.apply(w, step=step, offset=(2.0, 2.0, 2.0, 1.0))\n\t\t\telse:\n\t\t\t\tmod.apply(w, step=step)\n\n\t\trenderer = Renderer(mode=0)\n\t\trenderer.render(filename='building_{}'.format(image))\n\t\tbuilding.save(image)\n\t\tbuilding.save(image, ext='ply')\n\t\tbuilding.demolish()\n\t\tcloud = PointCloud()\n\t\tcloud.make(image)\n\t\t# cloud = PyntCloud.from_file(\"Models/{}.obj\".format(image))\n\t\t# cloud.to_file(\"{}.ply\".format(image))\n\t\t# cloud.to_file(\"{}.npz\".format(image))\n\n\n" ]
[ [ "numpy.random.random", "numpy.random.randint" ] ]
genisplaja/cunet
[ "58a200c84810f20099265e30200327eefddb3eff", "58a200c84810f20099265e30200327eefddb3eff" ]
[ "cunet/ftanet/network/ftanet.py", "cunet/ftanet/evaluator.py" ]
[ "import tensorflow as tf\r\nfrom tensorflow.keras import backend as K\r\nfrom tensorflow.keras import Input, Model\r\nfrom tensorflow.keras.layers import Dense, Conv2D, BatchNormalization, Dropout, Lambda, \\\r\n GlobalAveragePooling2D, Activation, MaxPooling2D, AveragePooling2D, \\\r\n Concatenate, Add, Multiply, Softmax, Reshape, UpSampling2D, Permute, Conv1D\r\n\r\n\r\ndef SF_Module(x_list, n_channel, reduction, limitation):\r\n ## Split\r\n fused = None\r\n for x_s in x_list:\r\n if fused==None:\r\n fused = x_s\r\n else:\r\n fused = Add()([fused, x_s])\r\n \r\n ## Fuse\r\n fused = GlobalAveragePooling2D()(fused)\r\n fused = BatchNormalization()(fused)\r\n fused = Dense(max(n_channel // reduction, limitation), activation='selu')(fused)\r\n\r\n ## Select\r\n masks = []\r\n for i in range(len(x_list)):\r\n masks.append(Dense(n_channel)(fused))\r\n mask_stack = Lambda(K.stack, arguments={'axis': -1})(masks)\r\n mask_stack = Softmax(axis=-2)(mask_stack) # (n_channel, n_kernel)\r\n\r\n selected = None\r\n for i, x_s in enumerate(x_list):\r\n mask = Lambda(lambda z: z[:, :, i])(mask_stack)\r\n mask = Reshape((1, 1, n_channel))(mask)\r\n x_s = Multiply()([x_s, mask])\r\n if selected==None:\r\n selected = x_s\r\n else:\r\n selected = Add()([selected, x_s])\r\n\r\n return selected\r\n\r\n\r\ndef FTA_Module(x, shape, kt, kf):\r\n x = BatchNormalization()(x)\r\n\r\n ## Residual\r\n x_r = Conv2D(shape[2], (1, 1), padding='same', activation='relu')(x)\r\n\r\n ## Time Attention\r\n # Attn Map (1, T, C), FC\r\n a_t = Lambda(K.mean, arguments={'axis': -3})(x)\r\n a_t = Conv1D(shape[2], kt, padding='same', activation='selu')(a_t)\r\n a_t = Conv1D(shape[2], kt, padding='same', activation='selu')(a_t) #2\r\n a_t = Softmax(axis=-2)(a_t)\r\n a_t = Reshape((1, shape[1], shape[2]))(a_t)\r\n # Reweight\r\n x_t = Conv2D(shape[2], (3, 3), padding='same', activation='selu')(x)\r\n x_t = Conv2D(shape[2], (5, 5), padding='same', activation='selu')(x_t)\r\n x_t = Multiply()([x_t, a_t])\r\n\r\n # Frequency Attention\r\n # Attn Map (F, 1, C), Conv1D\r\n a_f = Lambda(K.mean, arguments={'axis': -2})(x)\r\n a_f = Conv1D(shape[2], kf, padding='same', activation='selu')(a_f)\r\n a_f = Conv1D(shape[2], kf, padding='same', activation='selu')(a_f)\r\n a_f = Softmax(axis=-2)(a_f)\r\n a_f = Reshape((shape[0], 1, shape[2]))(a_f)\r\n # Reweight\r\n x_f = Conv2D(shape[2], (3, 3), padding='same', activation='selu')(x)\r\n x_f = Conv2D(shape[2], (5, 5), padding='same', activation='selu')(x_f)\r\n x_f = Multiply()([x_f, a_f])\r\n\r\n return x_r, x_t, x_f\r\n\r\n\r\ndef create_model(input_shape=(320, 430, 3)):\r\n visible = Input(shape=input_shape)\r\n x = BatchNormalization()(visible)\r\n\r\n ## Bottom\r\n # bm = BatchNormalization()(x)\r\n bm = x\r\n bm = Conv2D(16, (4, 1), padding='valid', strides=(4, 1), activation='selu')(bm) # 80\r\n bm = Conv2D(16, (4, 1), padding='valid', strides=(4, 1), activation='selu')(bm) # 20\r\n bm = Conv2D(16, (4, 1), padding='valid', strides=(4, 1), activation='selu')(bm) # 5\r\n bm = Conv2D(1, (5, 1), padding='valid', strides=(5, 1), activation='selu')(bm) # 1\r\n\r\n # 保持高分辨率,关注细节\r\n shape=input_shape\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0], shape[1], 32), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 32, 4, 4)\r\n x = MaxPooling2D((2, 2))(x)\r\n\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0]//2, shape[1]//2, 64), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 64, 4, 4)\r\n x = MaxPooling2D((2, 2))(x)\r\n\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0]//4, shape[1]//4, 128), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 128, 4, 4)\r\n\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0]//4, shape[1]//4, 128), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 128, 4, 4)\r\n\r\n x = UpSampling2D((2, 2))(x)\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0]//2, shape[1]//2, 64), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 64, 4, 4)\r\n\r\n x = UpSampling2D((2, 2))(x)\r\n x_r, x_t, x_f = FTA_Module(x, (shape[0], shape[1], 32), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 32, 4, 4)\r\n \r\n x_r, x_t, x_f = FTA_Module(x, (shape[0], shape[1], 1), 3, 3)\r\n x = SF_Module([x_r, x_t, x_f], 1, 4, 4)\r\n x = Concatenate(axis=1)([bm, x])\r\n \r\n # Softmax\r\n x = Lambda(K.squeeze, arguments={'axis': -1})(x) # (321, 430)\r\n x = Softmax(axis=-2)(x)\r\n\r\n return Model(inputs=visible, outputs=x)", "import re\r\nimport numpy as np\r\nimport pandas as pd\r\nimport numpy\r\nimport glob\r\nfrom tqdm import tqdm\r\nimport random\r\nimport pickle\r\nfrom numpy.core.fromnumeric import std\r\nimport mir_eval\r\nfrom cfp import cfp_process\r\nfrom tensorflow import keras\r\n\r\nimport os\r\nimport tensorflow as tf\r\nfrom tensorflow.keras import backend as K\r\nfrom tensorflow.keras.models import load_model\r\nfrom tensorflow.keras.metrics import categorical_accuracy\r\nfrom loader import load_data_for_test, load_data\r\n\r\nfrom tensorflow.keras.models import load_model\r\n\r\nfrom constant import *\r\nfrom loader import *\r\n\r\nfrom network.ftanet import create_model\r\nfrom loader import get_CenFreq\r\n\r\n\r\ndef std_normalize(data): \r\n # normalize as 64 bit, to avoid numpy warnings\r\n data = data.astype(np.float64)\r\n mean = np.mean(data)\r\n std = np.std(data)\r\n data = data.copy() - mean\r\n if std != 0.:\r\n data = data / std\r\n return data.astype(np.float32)\r\n\r\n\r\ndef est(output, CenFreq, time_arr):\r\n # output: (freq_bins, T)\r\n CenFreq[0] = 0\r\n est_time = time_arr\r\n est_freq = np.argmax(output, axis=0)\r\n\r\n for j in range(len(est_freq)):\r\n est_freq[j] = CenFreq[int(est_freq[j])]\r\n\r\n if len(est_freq) != len(est_time):\r\n new_length = min(len(est_freq), len(est_time))\r\n est_freq = est_freq[:new_length]\r\n est_time = est_time[:new_length]\r\n\r\n est_arr = np.concatenate((est_time[:, None], est_freq[:, None]), axis=1)\r\n\r\n return est_arr\r\n\r\n\r\ndef iseg(data):\r\n # data: (batch_size, freq_bins, seg_len)\r\n new_length = data.shape[0] * data.shape[-1] # T = batch_size * seg_len\r\n new_data = np.zeros((data.shape[1], new_length)) # (freq_bins, T)\r\n for i in range(len(data)):\r\n new_data[:, i * data.shape[-1] : (i + 1) * data.shape[-1]] = data[i]\r\n return new_data\r\n\r\n\r\ndef get_est_arr(model, x_list, y_list, batch_size):\r\n for i in range(len(x_list)):\r\n x = x_list[i]\r\n y = y_list[i]\r\n \r\n # predict and concat\r\n num = x.shape[0] // batch_size\r\n if x.shape[0] % batch_size != 0:\r\n num += 1\r\n preds = []\r\n for j in range(num):\r\n # x: (batch_size, freq_bins, seg_len)\r\n if j == num - 1:\r\n X = x[j * batch_size:]\r\n length = x.shape[0] - j * batch_size\r\n else:\r\n X = x[j * batch_size: (j + 1) * batch_size]\r\n length = batch_size\r\n \r\n # for k in range(length): # normalization\r\n # X[k] = std_normalize(X[k])\r\n prediction = model.predict(X, length)\r\n preds.append(prediction)\r\n \r\n # (num*bs, freq_bins, seg_len) to (freq_bins, T)\r\n preds = np.concatenate(preds, axis=0)\r\n preds = iseg(preds)\r\n \r\n # ground-truth\r\n \r\n # trnasform to f0ref\r\n CenFreq = get_CenFreq(StartFreq=31, StopFreq=1250, NumPerOct=60)\r\n # CenFreq = get_CenFreq(StartFreq=20, StopFreq=2048, NumPerOct=60)\r\n # CenFreq = get_CenFreq(StartFreq=81, StopFreq=600, NumPerOct=111)\r\n # CenFreq = get_CenFreq(StartFreq=81, StopFreq=600, NumPerOct=190)\r\n est_arr = est(preds, CenFreq, y)\r\n \r\n # VR, VFA, RPA, RCA, OA\r\n return est_arr\r\n\r\n\r\ndef get_pitch_track(filename):\r\n print('Loading model...')\r\n model = create_model(input_shape=IN_SHAPE)\r\n model.load_weights(\r\n filepath='./model/baseline/OA/best_OA'\r\n ).expect_partial()\r\n print('Model loaded!')\r\n \r\n xlist = []\r\n timestamps = []\r\n # feature = np.load(data_folder + 'cfp/' + fname + '.npy')\r\n feature, _, time_arr = cfp_process(filename, sr=8000, hop=80)\r\n print('feature', np.shape(feature))\r\n \r\n data = batchize_test(feature, size=128)\r\n xlist.append(data)\r\n timestamps.append(time_arr)\r\n \r\n estimation = get_est_arr(model, xlist, timestamps, batch_size=16)\r\n\r\n return estimation[:, 0], estimation[:, 1]\r\n\r\n\r\ndef save_pitch_track_to_dataset(filename, est_time, est_freq):\r\n # Write txt annotation to file\r\n with open(filename, 'w') as f:\r\n for i, j in zip(est_time, est_freq):\r\n f.write(\"{}, {}\\n\".format(i, j))\r\n print('Saved with exit to {}'.format(filename))\r\n\r\n\r\ndef select_vocal_track(ypath, lpath):\r\n ycsv = pd.read_csv(ypath, names=[\"time\", \"freq\"])\r\n gt0 = ycsv['time'].values\r\n gt0 = gt0[:, np.newaxis]\r\n \r\n gt1 = ycsv['freq'].values\r\n gt1 = gt1[:, np.newaxis]\r\n \r\n z = np.zeros(gt1.shape)\r\n \r\n f = open(lpath, 'r')\r\n lines = f.readlines()\r\n \r\n for line in lines:\r\n \r\n if 'start_time' in line.split(',')[0]:\r\n continue\r\n st = float(line.split(',')[0])\r\n et = float(line.split(',')[1])\r\n sid = line.split(',')[2]\r\n for i in range(len(gt1)):\r\n if st < gt0[i, 0] < et and 'singer' in sid:\r\n z[i, 0] = gt1[i, 0]\r\n \r\n gt = np.concatenate((gt0, z), axis=1)\r\n return gt\r\n \r\n\r\ndef get_files_to_test(fp, artist, artists_to_track_mapping):\r\n # Get track to train\r\n tracks_to_test = artists_to_track_mapping[artist]\r\n\r\n # Get filenames to train\r\n files_to_test = []\r\n for track in tracks_to_test:\r\n files_to_test.append(fp + 'audio/' + track + '.wav')\r\n \r\n return files_to_test\r\n\r\n\r\nif __name__ == '__main__':\r\n fp_synth = '/home/genis/Saraga-Melody-Synth/'\r\n fp_hindustani = '/home/genis/Hindustani-Synth-Dataset/'\r\n fp_medley = '/mnt/sda1/genis/carnatic_melody_dataset/resources/medley_aux/'\r\n fp_western_synth = '/home/genis/Western-Synth-Dataset_2/'\r\n \r\n #dataset_filelist_nosynth = glob.glob(fp_nosynth + 'audio/*.wav')\r\n #with open(fp_nosynth + 'artists_to_track_mapping.pkl', 'rb') as map_file:\r\n # artists_to_track_mapping_nosynth = pickle.load(map_file)\r\n\r\n dataset_filelist_synth = glob.glob(fp_synth + 'audio/*.wav')\r\n with open(fp_synth + 'artists_to_track_mapping.pkl', 'rb') as map_file:\r\n artists_to_track_mapping = pickle.load(map_file)\r\n \r\n mahati_test = get_files_to_test(fp_synth, 'Mahati', artists_to_track_mapping)\r\n sumithra_test = get_files_to_test(fp_synth, 'Sumithra Vasudev', artists_to_track_mapping)\r\n modhumudi_test = get_files_to_test(fp_synth, 'Modhumudi Sudhakar', artists_to_track_mapping)\r\n chertala_test = get_files_to_test(fp_synth, 'Cherthala Ranganatha Sharma', artists_to_track_mapping)\r\n test_carnatic_list = [mahati_test, sumithra_test, modhumudi_test, chertala_test]\r\n\r\n test_files = []\r\n for i in test_carnatic_list:\r\n test_files = test_files + random.sample(i, 50)\r\n\r\n #carnatic_synth = test_model(test_files)\r\n\r\n '''\r\n mahati_test_synth = get_files_to_test(fp_synth, 'Mahati', artists_to_track_mapping_synth)\r\n sumithra_test_synth = get_files_to_test(fp_synth, 'Sumithra Vasudev', artists_to_track_mapping_synth)\r\n modhumudi_test_synth = get_files_to_test(fp_synth, 'Modhumudi Sudhakar', artists_to_track_mapping_synth)\r\n chertala_test_synth = get_files_to_test(fp_synth, 'Cherthala Ranganatha Sharma', artists_to_track_mapping_synth)\r\n test_carnatic_list = [mahati_test_synth, sumithra_test_synth, modhumudi_test_synth, chertala_test_synth]\r\n \r\n test_files = []\r\n for i in test_carnatic_list:\r\n test_files = test_files + random.sample(i, 50)\r\n \r\n medley_tracks = glob.glob(fp_medley + 'audio/*.wav')\r\n \r\n hindustani_testing_files = [\r\n 'Raag_Kedar_43.wav',\r\n 'Raag_Kedar_10.wav',\r\n 'Raag_Kalyan_61.wav',\r\n 'Raag_Kalyan_39.wav',\r\n 'Raag_Kalyan_67.wav',\r\n 'Raag_Kalyan_61.wav',\r\n 'Raag_Kedar_20.wav',\r\n 'Raag_Kedar_30.wav',\r\n 'Raag_Kedar_43.wav',\r\n 'Raag_Jog_47.wav',\r\n 'Raag_Jog_37.wav',\r\n 'Raag_Jog_29.wav',\r\n 'Raag_Jog_18.wav',\r\n 'Raag_Jog_12.wav',\r\n 'Raag_Jog_2.wav',\r\n 'Raag_Saraswati_6.wav',\r\n 'Raag_Saraswati_25.wav',\r\n 'Raag_Bhimpalasi_23.wav',\r\n 'Raag_Bhimpalasi_33.wav',\r\n 'Raag_Bhimpalasi_43.wav',\r\n 'Raag_Bhimpalasi_45.wav',\r\n 'Raag_Bhimpalasi_50.wav',\r\n 'Raag_Shree_66.wav',\r\n 'Raag_Dhani_8.wav',\r\n 'Raag_Dhani_25.wav',\r\n 'Raag_Dhani_33.wav',\r\n 'Raag_Dhani_58.wav',\r\n 'Raag_Dhani_35.wav',\r\n 'Raag_Dhani_57.wav',\r\n 'Raag_Bahar_44.wav',\r\n 'Raag_Bahar_29.wav',\r\n 'Multani_17.wav',\r\n 'Raag_Rageshri_61.wav',\r\n 'Raag_Rageshri_36.wav',\r\n 'Maru_Bihag_4.wav',\r\n 'Raageshree_10.wav',\r\n 'Raageshree_12.wav',\r\n 'Raag_Desh_9.wav',\r\n 'Raag_Bhoopali_83.wav',\r\n 'Bhairavi_Bhajan_6.wav',\r\n 'Raag_Bairagi_22.wav',\r\n 'Raag_Multani_11.wav',\r\n 'Raga_Shree_-_Khayal_38.wav',\r\n 'Todi_16.wav',\r\n 'Todi_10.wav',\r\n 'Todi_17.wav',\r\n 'Todi_3.wav',\r\n 'Sudh_Sarang_21.wav',\r\n 'Sudh_Kalyan_25.wav',\r\n 'Raga_Shree_-_Khayal_96.wav',\r\n 'Raga_Lalit_-_Khayal_76.wav',\r\n 'Raag_Yaman_20.wav',\r\n 'Raag_Sooha_Kanada_28.wav',\r\n 'Raag_Sohani_15.wav',\r\n 'Raag_Shree_81.wav',\r\n 'Raag_Sawani_16.wav',\r\n 'Raag_Ramdasi_Malhar_15.wav',\r\n 'Raag_Puriya_55.wav',\r\n 'Raag_Poorva_96.wav',\r\n 'Raag_Poorva_77.wav',\r\n 'Raag_Paraj_25.wav',\r\n 'Raag_Multani_74.wav',\r\n 'Raag_Megh_41.wav',\r\n 'Raag_Malkauns_71.wav',\r\n 'Raag_Lalita_Gauri_24.wav',\r\n 'Raag_Bhoopali_11.wav',\r\n 'Raag_Bhimpalasi_56.wav',\r\n 'Raag_Bibhas_24.wav',\r\n 'Raag_Bihag_11.wav',\r\n 'Raag_Bihag_27.wav',\r\n 'Raag_Bhatiyar_7.wav',\r\n 'Raag_Ahir_Bhairon_58.wav',\r\n 'Raag_Ahir_Bhairon_3.wav',\r\n 'Nirgun_Bhajan_15.wav',\r\n 'Nat_Bhairon_8.wav',\r\n 'Multani_0.wav',\r\n 'Malkauns_1.wav',\r\n 'Malkauns_3.wav',\r\n 'Malkauns_10.wav',\r\n 'Kalavati_10.wav',\r\n 'Aahir_Bhairon_16.wav',\r\n ]\r\n '''\r\n \r\n hindustani_testing_files = glob.glob(fp_hindustani + 'audio/*.wav')\r\n #hindustani_testing_filenames = [fp_hindustani + 'audio/' + x for x in hindustani_testing_files]\r\n\r\n testing_files = [x for x in hindustani_testing_files if 'Deepki' in x] + \\\r\n [x for x in hindustani_testing_files if 'Raag_Jog' in x] + \\\r\n [x for x in hindustani_testing_files if 'Raag_Dhani' in x] + \\\r\n [x for x in hindustani_testing_files if 'Todi' in x] + \\\r\n [x for x in hindustani_testing_files if 'Malkauns' in x] + \\\r\n [x for x in hindustani_testing_files if 'Piloo' in x]\r\n hindustani_synth = test_model(testing_files)\r\n\r\n '''\r\n #mahati = test_model(mahati_test)\r\n #sumithra = test_model(sumithra_test, 'sumithra_nosynth')\r\n #modhumudi = test_model(modhumudi_test, 'modhumudi_nosynth')\r\n #chertala = test_model(chertala_test, 'chertala_nosynth')\r\n #mahati_synth = test_model(mahati_test_synth, 'mahati')\r\n #sumithra_synth = test_model(sumithra_test_synth, 'sumithra')\r\n #modhumudi_synth = test_model(modhumudi_test_synth, 'modhumudi')\r\n #carnatic_synth = test_model(test_files, 'carnatic')\r\n\r\n #hindustani_synth = test_model(hindustani_testing_files, 'hindustani')\r\n #carnatic_synth = test_model(test_files, 'carnatic')\r\n #scores_synth = test_model_on_medley(medley_tracks)\r\n\r\n adc_synth_files = [x for x in files_to_test if 'daisy' in x] + \\\r\n [x for x in files_to_test if 'pop' in x] + \\\r\n [x for x in files_to_test if 'opera' in x]\r\n \r\n mirex05_synth_files = [x for x in files_to_test if 'train' in x]\r\n #medley_synth_files = [x for x in files_to_test if x not in adc_synth_files and 'train' not in x]\r\n adc_scores = test_model(adc_synth_files)\r\n mirex_scores = test_model(mirex05_synth_files)\r\n #medley_scores = test_model(medley_synth_files)\r\n\r\n #scores_western_synth = test_model(files_to_test)\r\n adc_filelist = glob.glob(\r\n '/home/genis/FTANet-melodic/eval_datasets/ADC2004/*.wav'\r\n )\r\n scores_adc = test_model_on_ADC(adc_filelist)\r\n mirex05_filelist = glob.glob(\r\n '/home/genis/FTANet-melodic/eval_datasets/MIREX05/*.wav'\r\n )\r\n scores_mirex05 = test_model_on_MIREX05(mirex05_filelist)\r\n\r\n print('Mahati:', mahati_synth)\r\n print('Sumithra:', sumithra_synth)\r\n print('Modhmudi:', modhumudi_synth)\r\n print('Chertala:', chertala_synth)\r\n print('Complete carnatic:', carnatic_synth)\r\n print('Hindustani:', hindustani_synth)\r\n '''\r\n\r\n" ]
[ [ "tensorflow.keras.Input", "tensorflow.keras.layers.UpSampling2D", "tensorflow.keras.layers.Concatenate", "tensorflow.keras.layers.MaxPooling2D", "tensorflow.keras.layers.Conv1D", "tensorflow.keras.layers.Softmax", "tensorflow.keras.Model", "tensorflow.keras.layers.Lambda", "tensorflow.keras.layers.BatchNormalization", "tensorflow.keras.layers.Multiply", "tensorflow.keras.layers.Add", "tensorflow.keras.layers.Dense", "tensorflow.keras.layers.Conv2D", "tensorflow.keras.layers.Reshape", "tensorflow.keras.layers.GlobalAveragePooling2D" ], [ "numpy.zeros", "pandas.read_csv", "numpy.argmax", "numpy.shape", "numpy.std", "numpy.concatenate", "numpy.mean" ] ]
seungkee/2nd-place-solution-to-facebook-image-similarity-matching-track
[ "716667bf416239f448e4ea2730a2cc5146536719" ]
[ "code/descriptor_track/train_eval/make_n_perquery_df_gpu.py" ]
[ "import cv2\nimport os\nimport random\nimport time\nimport torch\nimport torch.backends.cudnn as cudnn\nimport models\nfrom utils.logger import Logger\nimport myexman\nfrom utils import utils\nimport sys\nimport torch.multiprocessing as mp\nimport torch.distributed as dist\nimport socket\nfrom torchvision import transforms,datasets\nfrom eval_metrics import get_matching_from_descs, evaluate_metrics\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport torch.nn as nn\nfrom tqdm import tqdm\nimport h5py\ndef add_learner_params(parser):\n parser.add_argument('--problem', default='sim-clr',\n help='The problem to train',\n choices=models.REGISTERED_MODELS,\n )\n parser.add_argument('--name', default='',\n help='Name for the experiment',\n )\n parser.add_argument('--ckpt', default='',\n help='Optional checkpoint to init the model.'\n )\n parser.add_argument('--n_perquery', default=100, type=int)\n parser.add_argument('--query_features', default='')\n parser.add_argument('--ref_features', default='')\n\n parser.add_argument('--verbose', default=False, type=bool)\n parser.add_argument('--num_classes', default=3, type=int)\n # optimizer params\n parser.add_argument('--lr_schedule', default='warmup-anneal')\n parser.add_argument('--opt', default='lars', help='Optimizer to use', choices=['sgd', 'adam', 'lars'])\n parser.add_argument('--iters', default=-1, type=int, help='The number of optimizer updates')\n parser.add_argument('--warmup', default=0, type=float, help='The number of warmup iterations in proportion to \\'iters\\'')\n parser.add_argument('--lr', default=0.1, type=float, help='Base learning rate')\n parser.add_argument('--wd', '--weight_decay', default=1e-4, type=float, dest='weight_decay')\n\n\n # trainer params\n parser.add_argument('--save_freq', default=1000, type=int, help='Frequency to save the model')\n parser.add_argument('--log_freq', default=100, type=int, help='Logging frequency')\n parser.add_argument('--eval_freq', default=10000000000000000, type=int, help='Evaluation frequency')\n parser.add_argument('-j', '--workers', default=4, type=int, help='The number of data loader workers')\n parser.add_argument('--eval_only', default=False, type=bool, help='Skips the training step if True')\n parser.add_argument('--seed', default=-1, type=int, help='Random seed')\n # transfrom params\n parser.add_argument('--im_size', default=224, type=int)\n parser.add_argument('--allgray', default=0, type=int)\n # parallelizm params:\n parser.add_argument('--dist', default='dp', type=str,\n help='dp: DataParallel, ddp: DistributedDataParallel',\n choices=['dp', 'ddp'],\n )\n parser.add_argument('--dist_address', default='127.0.0.1:1234', type=str,\n help='the address and a port of the main node in the <address>:<port> format'\n )\n parser.add_argument('--node_rank', default=0, type=int,\n help='Rank of the node (script launched): 0 for the main node and 1,... for the others',\n )\n parser.add_argument('--world_size', default=1, type=int,\n help='the number of nodes (scripts launched)',\n )\n parser.add_argument('--best_valid_score', default=0, type=float)\n \ndef main():\n parser = myexman.ExParser(file=os.path.basename(__file__))\n add_learner_params(parser)\n\n is_help = False\n if '--help' in sys.argv or '-h' in sys.argv:\n sys.argv.pop(sys.argv.index('--help' if '--help' in sys.argv else '-h'))\n is_help = True\n\n args, _ = parser.parse_known_args(log_params=False)\n\n models.REGISTERED_MODELS[args.problem].add_model_hparams(parser)\n\n if is_help:\n sys.argv.append('--help')\n\n args = parser.parse_args(namespace=args)\n\n if args.data == 'imagenet' and args.aug == False:\n raise Exception('ImageNet models should be eval with aug=True!')\n\n if args.seed != -1:\n random.seed(args.seed)\n torch.manual_seed(args.seed)\n cudnn.deterministic = True\n\n args.gpu = 0\n ngpus = torch.cuda.device_count()\n args.number_of_processes = 1\n if args.dist == 'ddp':\n # add additional argument to be able to retrieve # of processes from logs\n # and don't change initial arguments to reproduce the experiment\n args.number_of_processes = args.world_size * ngpus\n parser.update_params_file(args)\n\n args.world_size *= ngpus\n mp.spawn(\n main_worker,\n nprocs=ngpus,\n args=(ngpus, args),\n )\n else:\n parser.update_params_file(args)\n main_worker(args.gpu, -1, args)\n\nclass FacebookDataset(torch.utils.data.Dataset):\n def __init__(self, split, transform=None, imsize=None):\n if split == 'query':\n self.dirname = '/facebook/data/images/query/'\n self.samples = list(np.load('/facebook/data/images/query_imlist.npy'))\n #gt_df=pd.read_csv('/facebook/data/public_ground_truth.csv')\n #gt_df=gt_df[~gt_df['reference_id'].isnull()]\n #self.samples = [x+'.jpg' for x in list(gt_df['query_id'])]\n elif split == 'query_total':\n self.dirname = '/facebook/data/images/query/'\n self.samples = list(np.load('/facebook/data/images/query_total_imlist.npy'))\n else:\n self.dirname = '/facebook/data/images/reference_1M_root/reference_1M/'\n self.samples = list(np.load('/facebook/data/images/ref_imlist.npy'))\n self.transform = transform\n self.imsize = imsize\n def __len__(self):\n return len(self.samples)\n def __getitem__(self, index):\n path = os.path.join(self.dirname, self.samples[index])\n with open(path, 'rb') as f:\n img = Image.open(f)\n img = img.convert('RGB')\n if self.imsize is not None:\n img.thumbnail((self.imsize, self.imsize), Image.ANTIALIAS)\n if self.transform is not None:\n img = self.transform(img)\n return img,index\n\nclass mmDataset(torch.utils.data.Dataset):\n def __init__(self, args):\n self.query_features = nn.functional.normalize(torch.tensor(np.load(args.query_features)),dim=1,p=2)\n def __len__(self):\n return len(self.query_features)\n def __getitem__(self, index): \n return self.query_features[index], index\n \[email protected]_grad()\ndef extract_features(data_loader, args, use_cuda=True):\n features =np.zeros((len(data_loader.dataset),args.n_perquery), dtype=int)\n print('features',features.shape)\n ref_features = torch.tensor(np.load(args.ref_features))\n ref_features = nn.functional.normalize(ref_features, dim=1, p=2)\n ref_features = ref_features.t().cuda(non_blocking=True)\n for samples, index in tqdm(data_loader):\n samples = samples.cuda(non_blocking=True)\n index = index.cuda(non_blocking=True)\n #print(samples.shape)\n #print(ref_features.shape)\n feats = torch.argsort(torch.mm(samples,ref_features),dim=-1)#[:,:100]\n #feats = model(samples).clone()\n feats = feats.reshape((feats.shape[0],-1))\n # init storage feature matrix\n \"\"\"\n if dist.get_rank() == 0 and features is None:\n features = torch.zeros(len(data_loader.dataset), 100)#feats.shape[-1])\n #if use_cuda:\n # features = features.cuda(non_blocking=True)\n #print(f\"Storing features into tensor of shape {features.shape}\")\n \"\"\"\n # get indexes from all processes\n y_all = torch.empty(dist.get_world_size(), index.size(0), dtype=index.dtype, device=index.device)\n y_l = list(y_all.unbind(0))\n y_all_reduce = torch.distributed.all_gather(y_l, index, async_op=True)\n y_all_reduce.wait()\n index_all = torch.cat(y_l)\n\n # share features between processes\n feats_all = torch.empty(\n dist.get_world_size(),\n feats.size(0),\n feats.size(1),\n dtype=feats.dtype,\n device=feats.device,\n )\n #print('6',feats_all.shape)\n output_l = list(feats_all.unbind(0))\n output_all_reduce = torch.distributed.all_gather(output_l, feats, async_op=True)\n output_all_reduce.wait()\n \n # update storage feature matrix\n if dist.get_rank() == 0:\n if use_cuda:\n output_l = torch.cat(output_l)\n output_l = output_l[:,-args.n_perquery:]#torch.argsort(output_l, dim=-1)[:,-100:]\n features[index_all.cpu().numpy()]= output_l.cpu().numpy()\n #output_l = torch.tensor(np.argpartition(output_l.cpu().numpy(), -100)[:,-100:])\n #features[index_all.cpu().numpy()]=output_l\n #print('4',torch.cat(output_l).shape)\n #features.index_copy_(0, index_all, output_l)\n # features.index_copy_(0, index_all, torch.cat(output_l))\n else:\n features.index_copy_(0, index_all.cpu(), torch.cat(output_l).cpu())\n return features\n\ndef valid_mm(args):\n dataset_mm = mmDataset(args)\n sampler = torch.utils.data.DistributedSampler(dataset_mm, shuffle=False)\n data_loader_mm = torch.utils.data.DataLoader(\n dataset_mm,\n sampler=sampler,\n batch_size=4,\n num_workers=4,\n pin_memory=True,\n drop_last=False,\n )\n mm_features = extract_features(data_loader_mm,args,True)\n \n if args.rank==0:\n ref_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/ref_imlist.npy'))])\n query_total_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/query_total_imlist.npy'))]) \n \n len_ref = len(ref_id_list)\n len_query = len(query_total_id_list)\n\n new_query=[]\n new_ref=[]\n for i in tqdm(range(len(mm_features))):\n new_query+=[query_total_id_list[i%len_query]]*args.n_perquery\n new_ref += list(ref_id_list[mm_features[i]%len_ref])\n\n df = pd.DataFrame({'query_id':new_query,'reference_id':new_ref})\n #df=df.drop_duplicates(subset=['query_id','reference_id'], keep='last').reset_index(drop=True)\n df.to_csv(args.query_features+'_'+args.ref_features.split('/')[-1]+f'{args.n_perquery}pq.csv',index=False)\n\ndef valid_all(model, args):\n transform = transforms.Compose([\n transforms.Resize((args.im_size, args.im_size)),\n transforms.ToTensor(),\n transforms.Normalize((0.485,0.456,0.406),(0.229,0.224,0.225)),\n ])\n if args.allgray >= 1:\n transform = transforms.Compose([\n transforms.Resize((args.im_size, args.im_size)),\n transforms.RandomGrayscale(p=1.0),\n transforms.ToTensor(),\n transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),\n ])\n dataset_ref=FacebookDataset(split=\"ref\",transform=transform)\n dataset_query = FacebookDataset(split=\"query\", transform=transform)\n sampler = torch.utils.data.DistributedSampler(dataset_ref, shuffle=False)\n data_loader_ref = torch.utils.data.DataLoader(\n dataset_ref,\n sampler=sampler,\n batch_size=16,\n num_workers=16,\n pin_memory=True,\n drop_last=False,\n )\n sampler2=torch.utils.data.DistributedSampler(dataset_query,shuffle=False)\n data_loader_query = torch.utils.data.DataLoader(\n dataset_query,\n sampler=sampler2,\n batch_size=16,\n num_workers=16,\n pin_memory=True,\n drop_last=False,\n )\n\n dataset_query_total = FacebookDataset(split=\"query_total\", transform=transform)\n sampler3 = torch.utils.data.DistributedSampler(dataset_query_total, shuffle=False)\n data_loader_query_total = torch.utils.data.DataLoader(\n dataset_query_total,\n sampler=sampler3,\n batch_size=16,\n num_workers=16,\n pin_memory=True,\n drop_last=False,\n )\n query_total_features = extract_features(model, data_loader_query_total, True)\n ref_features = extract_features(model, data_loader_ref, True)\n query_features = extract_features(model, data_loader_query, True)\n if args.rank==0:\n np.save(args.ckpt+'_ref_features.npy',ref_features.cpu().numpy())\n np.save(args.ckpt+'_query_features.npy',query_features.cpu().numpy())\n np.save(args.ckpt+'_query_total_features.npy', query_total_features.cpu().numpy())\n \n ref_features = nn.functional.normalize(ref_features, dim=1, p=2).cpu().numpy()\n query_features = nn.functional.normalize(query_features, dim=1, p=2).cpu().numpy()\n query_total_features = nn.functional.normalize(query_total_features, dim=1, p=2).cpu().numpy()\n\n qry_ids = ['Q' + str(x).zfill(5) for x in range(50_000)]\n ref_ids = ['R' + str(x).zfill(6) for x in range(1_000_000)]\n\n out = args.ckpt+\"_fb-isc-submission.h5\"\n with h5py.File(out, \"w\") as f:\n f.create_dataset(\"query\", data=query_total_features)\n f.create_dataset(\"reference\", data=ref_features)\n f.create_dataset('query_ids', data=qry_ids)\n f.create_dataset('reference_ids', data=ref_ids)\n\n query_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/query_imlist.npy'))])\n ref_truth_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/ref_imlist.npy'))])\n gt_df=pd.read_csv('/facebook/data/public_ground_truth.csv')\n submission_df = get_matching_from_descs(query_features, ref_features, query_id_list, ref_truth_id_list, gt_df)\n ap, rp90 = evaluate_metrics(submission_df, gt_df)\n print(ap, rp90)\n\n query_total_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/query_total_imlist.npy'))])\n total_submission_df = get_matching_from_descs(query_total_features, ref_features, query_total_id_list, ref_truth_id_list, gt_df)\n total_submission_df.to_csv(args.ckpt+'_total_submission_df.csv',index=False)\n\n \"\"\"\n if args.best_valid_score < ap :\n args.best_valid_score = ap\n np.save(os.path.join(args.root,f'ref_features.npy'),ref_features)\n np.save(os.path.join(args.root,f'query_features.npy'),query_features)\n with open(os.path.join(args.root,'logs.out'),\"a\") as f:\n f.write(f'ap : {ap}, rp90 : {rp90}\\n')\n print(f'ap : {ap}, rp90 : {rp90}')\n \"\"\"\n \"\"\"\n pts=[]\n pts.append([65,60])\n pts.append([105,60])\n pts.append([105,135])\n pts.append([65,135])\n blank_image = np.zeros((224,224),np.uint8)\n mask = cv2.fillPoly(blank_image, pts=[np.array(pts)],color=1)\n mask = np.expand_dims(mask,-1)\n mask = mask.astype(np.float32)\n mask = mask.transpose(2,0,1).clip(0,1)\n mask = np.expand_dims(mask,0)\n loss_value = criterion(feats,torch.tensor(mask).cuda()).item()\n print(f'valid loss : {loss_value}')\n with open(os.path.join(args.root,'logs.out'),\"a\") as f:\n f.write(f'valid loss : {loss_value}\\n')\n \"\"\"\ndef valid_one_epoch(model, args):\n transform = transforms.Compose([\n transforms.Resize((args.im_size,args.im_size)),\n transforms.ToTensor(),\n transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),\n ])\n if args.allgray >= 1:\n transform = transforms.Compose([\n transforms.Resize((args.im_size, args.im_size)),\n transforms.RandomGrayscale(p=1.0),\n transforms.ToTensor(),\n transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225)),\n ])\n #dataset_ref = FacebookDataset(split=\"ref\", transform=transform)\n dataset_query = FacebookDataset(split=\"query\", transform=transform)\n \"\"\"\n sampler = torch.utils.data.DistributedSampler(dataset_ref, shuffle=False)\n data_loader_ref = torch.utils.data.DataLoader(\n dataset_ref,\n sampler=sampler,\n batch_size=16,\n num_workers=16,\n pin_memory=True,\n drop_last=False,\n )\n \"\"\"\n \"\"\"\n sampler2 = torch.utils.data.DistributedSampler(dataset_query,shuffle=False)\n data_loader_query = torch.utils.data.DataLoader(\n dataset_query,\n sampler=sampler2,\n batch_size=16,\n num_workers=16,\n pin_memory=True,\n drop_last=False,\n )\n \"\"\"\n data_loader_query = torch.utils.data.DataLoader(\n dataset_query,\n batch_size=1,\n num_workers=1,\n pin_memory=True,\n drop_last=False\n )\n #ref_features = extract_features(model, data_loader_ref, True)\n #query_features = extract_features(model, data_loader_query, True)\n criterion = nn.BCEWithLogitsLoss()\n\n query_features = []\n for samples, index in data_loader_query:\n samples = samples.cuda()\n feats=model(samples).clone()\n \n if args.rank==0:\n #ref_features = nn.functional.normalize(ref_features, dim=1, p=2).cpu().numpy()\n #query_features = nn.functional.normalize(query_features, dim=1, p=2).cpu().numpy()\n #query_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/query_imlist.npy'))])\n #ref_truth_id_list = np.array([x[:-4] for x in list(np.load('/facebook/data/images/ref_truth_imlist.npy'))])\n #gt_df=pd.read_csv('/facebook/data/public_ground_truth.csv')\n #submission_df = get_matching_from_descs(query_features, ref_features, query_id_list, ref_truth_id_list, gt_df)\n #ap, rp90 = evaluate_metrics(submission_df, gt_df)\n \"\"\"\n if args.best_valid_score < ap :\n args.best_valid_score = ap\n np.save(os.path.join(args.root,f'ref_features.npy'),ref_features)\n np.save(os.path.join(args.root,f'query_features.npy'),query_features)\n with open(os.path.join(args.root,'logs.out'),\"a\") as f:\n f.write(f'ap : {ap}, rp90 : {rp90}\\n')\n print(f'ap : {ap}, rp90 : {rp90}')\n \"\"\"\n pts=[]\n pts.append([65,60])\n pts.append([105,60])\n pts.append([105,135])\n pts.append([65,135])\n blank_image = np.zeros((224,224),np.uint8)\n mask = cv2.fillPoly(blank_image, pts=[np.array(pts)],color=1)\n mask = np.expand_dims(mask,-1)\n mask = mask.astype(np.float32)\n mask = mask.transpose(2,0,1).clip(0,1)\n mask = np.expand_dims(mask,0)\n loss_value = criterion(feats,torch.tensor(mask).cuda()).item()\n print(f'valid loss : {loss_value}')\n with open(os.path.join(args.root,'logs.out'),\"a\") as f:\n f.write(f'valid loss : {loss_value}\\n')\n\n \ndef main_worker(gpu, ngpus, args):\n fmt = {\n 'train_time': '.3f',\n 'val_time': '.3f',\n 'lr': '.1e',\n }\n logger = Logger('logs', base=args.root, fmt=fmt)\n\n args.gpu = gpu\n torch.cuda.set_device(gpu)\n args.rank = args.node_rank * ngpus + gpu\n\n device = torch.device('cuda:%d' % args.gpu)\n\n if args.dist == 'ddp':\n dist.init_process_group(\n backend='nccl',\n init_method = 'tcp://%s' % args.dist_address, #'env://',\n world_size=args.world_size,\n rank=args.rank,\n )\n n_gpus_total = dist.get_world_size()\n assert args.batch_size % n_gpus_total == 0\n args.batch_size //= n_gpus_total\n if args.rank == 0:\n print(f'===> {n_gpus_total} GPUs total; batch_size={args.batch_size} per GPU')\n\n print(f'===> Proc {dist.get_rank()}/{dist.get_world_size()}@{socket.gethostname()}', flush=True)\n\n # Data loading code\n #model.prepare_data()\n #train_loader, val_loader = model.dataloaders(iters=args.iters)\n # define optimizer\n cur_iter=0\n #optimizer,scheduler = models.ssl.configure_optimizers(args, model, cur_iter - 1)\n\n # optionally resume from a checkpoint\n #if args.ckpt and not args.eval_only:\n # optimizer.load_state_dict(ckpt['opt_state_dict'])\n\n cudnn.benchmark = True\n\n continue_training = args.iters != 0\n data_time, it_time = 0, 0\n\n while continue_training:\n valid_mm(args)\n dist.barrier()\n break\n \"\"\"\n train_logs = []\n model.train()\n\n start_time = time.time()\n for _, (batch,labels) in enumerate(train_loader):\n #print(len(batch))\n #print(batch)\n cur_iter += 1\n #batch = torch.cat([batch[0], batch[1]],dim=0)\n #batch = batch.to(device)#[x.to(device) for x in batch]\n batch = [x.to(device) for x in batch]\n data_time += time.time() - start_time\n logs = {}\n if not args.eval_only:\n # forward pass and compute loss\n logs = model.train_step(batch, cur_iter)\n loss = logs['loss']\n # gradient step\n optimizer.zero_grad()\n loss.backward()\n optimizer.step()\n\n # save logs for the batch\n train_logs.append({k: utils.tonp(v) for k, v in logs.items()})\n\n #if cur_iter % args.save_freq == 0 and args.rank == 0:\n # save_checkpoint(args.root, model, optimizer, cur_iter)\n if cur_iter%args.eval_freq==0 or cur_iter>=args.iters or cur_iter==1:\n model.eval()\n valid_one_epoch(model,args)\n model.train()\n it_time += time.time() - start_time\n\n if (cur_iter % args.log_freq == 0 or cur_iter >= args.iters) and args.rank == 0:\n save_checkpoint(args.root, model, optimizer, cur_iter = cur_iter)\n train_logs = utils.agg_all_metrics(train_logs)\n\n logger.add_logs(cur_iter, train_logs, pref='train_')\n logger.add_scalar(cur_iter, 'lr', optimizer.param_groups[0]['lr'])\n logger.add_scalar(cur_iter, 'data_time', data_time)\n logger.add_scalar(cur_iter, 'it_time', it_time)\n logger.iter_info()\n logger.save()\n data_time, it_time = 0, 0\n train_logs = []\n\n if scheduler is not None:\n scheduler.step()\n\n if cur_iter >= args.iters:\n continue_training = False\n break\n\n start_time = time.time()\n \"\"\"\n # save_checkpoint(args.root, model, optimizer)\n\n if args.dist == 'ddp':\n dist.destroy_process_group()\n\ndef save_checkpoint(path, model, optimizer, cur_iter=None, is_best=False):\n if cur_iter is None:\n fname = os.path.join(path,'checkpoint.pth.tar')\n if is_best :\n fname = os.path.join(path,'checkpoint_best.pth.tar')\n else:\n fname = os.path.join(path, 'checkpoint-%d.pth.tar' % cur_iter)\n ckpt = model.get_ckpt()\n ckpt.update(\n {\n 'opt_state_dict': optimizer.state_dict(),\n 'iter': cur_iter,\n }\n )\n torch.save(ckpt,fname)\n\nif __name__ == '__main__':\n main()\n\n" ]
[ [ "torch.utils.data.DataLoader", "torch.no_grad", "torch.mm", "torch.cat", "torch.distributed.init_process_group", "torch.save", "torch.cuda.device_count", "numpy.expand_dims", "torch.device", "torch.cuda.set_device", "numpy.load", "torch.distributed.all_gather", "torch.multiprocessing.spawn", "numpy.zeros", "torch.distributed.get_world_size", "pandas.read_csv", "torch.nn.functional.normalize", "torch.manual_seed", "torch.utils.data.DistributedSampler", "torch.distributed.get_rank", "torch.tensor", "torch.distributed.barrier", "pandas.DataFrame", "torch.nn.BCEWithLogitsLoss", "numpy.array", "torch.distributed.destroy_process_group" ] ]
qinfeng2011/wltp
[ "317ad38fb96599a29d22e40f69b6aeb4d205611d" ]
[ "tests/test_wltp_db.py" ]
[ "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright 2013-2019 European Commission (JRC);\n# Licensed under the EUPL (the 'Licence');\n# You may not use this work except in compliance with the Licence.\n# You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl\n\"\"\"(DEPRECATED) Compares the results of a batch of wltp_db vehicles against phase-1b-alpha Heinz's tool.\n\n* Run as Test-case to generate results for sample-vehicles.\n* Run it as cmd-line to compare with Heinz's results.\n\"\"\"\n\nfrom collections import OrderedDict\nimport glob\nimport logging\nimport math\nimport os\nimport re\nimport unittest\nfrom unittest.case import skipIf\n\nfrom wltp import utils\nfrom wltp.utils import memoize\n\nimport numpy as np\nimport numpy.testing as npt\nimport pandas as pd\n\nfrom wltp.experiment import Experiment\nfrom .goodvehicle import goodVehicle\n\n\noverwrite_old_results = (\n True\n) # NOTE: Set 'False' to UPDATE sample-results or run main() (assuming they are ok).\nforce_rerun = False\n\nmydir = os.path.dirname(__file__)\nsamples_dir = \"wltp_db\"\nvehs_data_inp_fname = \"wltp_db_vehicles.csv\"\nvehs_data_out_fname = \"wltp_db_vehicles_out.csv\"\ngened_fname_regex = r\".*wltp_db_vehicles-(\\d+).csv\"\nheinz_fname_regex = r\".*heinz-(\\d+).csv\"\ngened_fname_glob = \"wltp_db_vehicles-*.csv\"\ntrans_fname_glob = \"trans-wltp_db_vehicles-*.csv\"\ndriver_weight = 70\n\"For calculating unladen_mass.\"\nencoding = \"UTF-8\"\n# desc_columns_to_print = ['mean', 'std', 'min', 'max']\n\n\ndef _init_logging(loglevel=logging.DEBUG):\n logging.basicConfig(level=loglevel)\n logging.getLogger().setLevel(level=loglevel)\n log = logging.getLogger(__name__)\n\n return log\n\n\nlog = _init_logging()\n\n\n@memoize\ndef _read_vehicles_inp():\n df = pd.read_csv(vehs_data_inp_fname, encoding=encoding, index_col=0)\n\n return df.copy()\n\n\ndef _read_vehicles_out():\n try:\n df = pd.read_csv(vehs_data_out_fname, encoding=encoding, index_col=0)\n return df\n except Exception:\n ## File corrupts if run interrupted.\n return None\n\n return df.copy()\n\n\ndef _write_vehicle_data(df):\n df = df.to_csv(vehs_data_out_fname, encoding=encoding)\n\n\n@memoize\ndef _read_wots():\n df = pd.read_csv(\"wot_samples.csv\", encoding=encoding, index_col=None)\n\n return df.copy()\n\n\ndef _select_wot(wots, isDiesel):\n wots_labels = [\"average Euro 6 Petrol\", \"average Euro 6 Diesel\"]\n wots = wots[[\"n_norm\", wots_labels[isDiesel]]]\n wots.columns = [\"n_norm\", \"p_norm\"]\n\n return wots\n\n\ndef _make_gened_fname(transplant_original_gears, veh_num):\n root, ext = os.path.splitext(vehs_data_inp_fname)\n transplant = \"trans-\" if transplant_original_gears else \"\"\n outfname = \"{}{}-{:04}{}\".format(transplant, root, veh_num, ext)\n\n return outfname\n\n\ndef _make_heinz_fname(veh_num):\n return \"heinz-{:04}.csv\".format(veh_num)\n\n\n@memoize\ndef _read_gened_file(inpfname):\n df = pd.read_csv(inpfname, header=0, index_col=0)\n assert not df.empty\n assert df.index.name == \"time\", df.index.name\n\n return df.copy()\n\n\n@memoize\ndef _read_heinz_file(veh_num):\n vehfpath = _make_heinz_fname(veh_num)\n try:\n inpfname = glob.glob(vehfpath)[0]\n except IndexError:\n raise FileNotFoundError(\n \"Skipped veh_id(%s), no file found: %s\" % (veh_num, vehfpath)\n )\n\n df = pd.read_csv(inpfname, encoding=\"UTF-8\", header=0, index_col=0)\n assert not df.empty\n assert df.index.name == \"t\", df.index.name\n\n return df.copy()\n\n\n_sources_latest_date = None\n\n\ndef _is_file_up_to_date(result_file, other_dependency_files=()):\n\n result_fnames = [result_file, vehs_data_out_fname]\n if force_rerun or not all(os.path.exists(f) for f in result_fnames):\n return False\n results_date = max([os.path.getmtime(file) for file in result_fnames])\n\n if _sources_latest_date is None:\n source_fnames = [\n __file__,\n \"../../datamodel.py\",\n \"../../experiment.py\",\n vehs_data_inp_fname,\n ]\n _sources_latest_dep_date = max(\n [os.path.getmtime(file) for file in source_fnames]\n )\n\n latest_dep_date = max([os.path.getmtime(file) for file in other_dependency_files])\n latest_dep_date = max(latest_dep_date, _sources_latest_dep_date)\n\n return results_date > latest_dep_date\n\n\ndef _file_pairs(fname_glob):\n \"\"\"\n Generates pairs of files to compare, skipping non-existent and those with mismatching #_of_rows.\n\n Example:\n\n >>> for (veh_num, df_g, df_h) in _file_pairs('wltp_db_vehicles-00*.csv'):\n ... pass\n \"\"\"\n\n all_gened = sorted(glob.glob(fname_glob))\n for g_fname in all_gened:\n m = re.match(gened_fname_regex, g_fname)\n veh_num = int(m.groups()[0])\n\n df_g = _read_gened_file(g_fname)\n df_h = _read_heinz_file(veh_num)\n\n if df_g.shape[0] != df_h.shape[0]:\n log.warning(\n \"Class-mismatched(%s): gened(%s) !+ heinz(%s)!\",\n g_fname,\n df_g.shape,\n df_h.shape,\n )\n continue\n if abs(df_g.v_class.sum() - df_h.v_orig.sum()) > 1:\n log.warning(\n \"Cycle-mismatched(%s): gened(%s) !+ heinz(%s)!\",\n g_fname,\n df_g.v_class.sum(),\n df_h.v_orig.sum(),\n )\n continue\n\n yield (veh_num, df_g, df_h)\n\n\ndef vehicles_applicator(fname_glob, pair_func):\n \"\"\"\n Applies the fun onto a pair of (generated, heinz) files for each tested-vehicle in the glob and\n appends results to list, preffixed by veh_num.\n\n :param pair_func: signature: func(veh_no, gened_df, heinz_df)-->sequence_of_numbers\n :return: a dataframe with the columns returned from the pair_func, row_indexed by veh_num\n \"\"\"\n\n res = []\n for (veh_num, df_g, df_h) in _file_pairs(fname_glob):\n row = pair_func(veh_num, df_g, df_h)\n res.append([int(veh_num)] + list(row))\n assert len(res) > 0\n\n ares = np.array(res)\n df = pd.DataFrame(ares[:, 1:], index=ares[:, 0])\n\n return df\n\n\ndef aggregate_single_columns_means(gened_column, heinz_column):\n \"\"\"\n Runs experiments and aggregates mean-values from one column of each (gened, heinz) file-sets.\n \"\"\"\n vehdata = _run_the_experiments(\n transplant_original_gears=False, compare_results=False\n )\n\n res = vehicles_applicator(\n gened_fname_glob,\n lambda _, df_g, df_h: (df_g[gened_column].mean(), df_h[heinz_column].mean()),\n )\n res.columns = [\"gened\", \"heinz\"]\n vehdata = vehdata.merge(res, how=\"inner\", left_index=True, right_index=True).sort()\n return vehdata\n\n\nclass WltpDbTests(unittest.TestCase):\n \"\"\"Compares a batch of vehicles with results obtained from \"official\" implementation.\"\"\"\n\n # @classmethod\n # def setUpClass(cls):\n\n def setUp(self):\n self.run_comparison = overwrite_old_results\n os.chdir(os.path.join(mydir, samples_dir))\n\n # @skip\n def test0_runExperiment(self, plot_results=False, encoding=\"UTF-8\"):\n _run_the_experiments(\n transplant_original_gears=False,\n compare_results=self.run_comparison,\n encoding=encoding,\n )\n\n # @skip\n def test0_runExperimentTransplant(self, plot_results=False, encoding=\"UTF-8\"):\n _run_the_experiments(\n transplant_original_gears=True,\n compare_results=self.run_comparison,\n encoding=encoding,\n )\n\n def test1_Downscale(self):\n \"\"\"Check mean-downscaled-velocity diff with Heinz within some percent.\n\n ### Comparison history ###\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n python heinz diff_prcnt\n count 378.000000 378.000000 0.000000e+00\n mean 45.973545 46.189082 4.688300e-01\n std 1.642335 1.126555 -4.578377e+01\n min 35.866421 36.659117 2.210133e+00\n 25% 46.506718 46.504909 -3.892020e-03\n 50% 46.506718 46.506504 -4.620879e-04\n 75% 46.506718 46.506719 4.116024e-08\n max 46.506718 46.506719 4.116024e-08\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n python heinz diff_prcnt\n count 382.000000 382.000000 0.000000e+00\n mean 44.821337 44.846671 5.652189e-02\n std 5.054214 5.050208 -7.933394e-02\n min 28.091672 28.388418 1.056347e+00\n 25% 46.506718 46.504868 -3.978244e-03\n 50% 46.506718 46.506478 -5.162230e-04\n 75% 46.506718 46.506719 4.116033e-08\n max 46.506718 46.506719 4.116033e-08\n \"\"\"\n\n pcrnt_limit = 0.09\n\n res = vehicles_applicator(\n gened_fname_glob,\n lambda _, df_g, df_h: (df_g[\"v_target\"].mean(), df_h[\"v\"].mean()),\n )\n res.columns = [\"python\", \"heinz\"]\n\n df = res.describe()\n\n df[\"diff_prcnt\"] = 100 * (df.heinz - df.python) / df.min(axis=1)\n print(df)\n\n diff_prcnt = df.loc[\"mean\", \"diff_prcnt\"]\n self.assertLess(np.abs(diff_prcnt), pcrnt_limit)\n\n def _check_gear_diffs(self, fname_glob):\n def read_and_compare_experiment(veh_num, df_my, df_hz):\n ## Count base-calc errors (before dirveability).\n ndiff_gears_orig = np.count_nonzero(df_my[\"gears_orig\"] != df_hz[\"g_max\"])\n\n ## Count all errors.\n #\n my_gears = df_my[\"gears\"]\n gears_hz = df_hz[\"gear\"]\n diff_gears = my_gears != gears_hz\n ndiff_gears = np.count_nonzero(diff_gears)\n\n ## Count Acceleration-only errors.\n #\n accel = np.gradient(df_my[\"v_class\"])\n diff_gears_accel = diff_gears[accel >= 0]\n ndiff_gears_accel = np.count_nonzero(diff_gears_accel)\n\n return (ndiff_gears, ndiff_gears_accel, ndiff_gears_orig)\n\n res = vehicles_applicator(fname_glob, read_and_compare_experiment)\n res.columns = [\"diff_gears\", \"diff_accel\", \"diff_orig\"]\n\n res_totals = res.describe()\n res_totals.loc[\"sum\", :] = res.sum(axis=0)\n res_totals.loc[\"mean%\", :] = (\n 100 * res_totals.loc[\"mean\", :] / 1800\n ) # class3-duration\n\n return res_totals\n\n def test2a_gear_diffs(self):\n \"\"\"Check diff-gears with Heinz stays within some percent.\n\n ### Comparison history ###\n\n Class3b-Vehicles, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n count MEAN STD min max\n gears 23387 75.931818 56.921729 6 279\n accell 19146 62.162338 48.831155 4 238\n senza rules 16133 52.379870 35.858415 11 170\n\n Separated test/unladen masses::\n\n diff_gears diff_accel diff_orig\n count 378.000000 378.000000 378.000000\n mean 104.965608 86.171958 90.235450\n std 100.439783 82.613475 109.283901\n min 6.000000 4.000000 11.000000\n 25% 36.250000 25.250000 23.000000\n 50% 69.000000 57.500000 51.000000\n 75% 142.000000 119.750000 104.750000\n max 524.000000 404.000000 600.000000\n sum 39677.000000 32573.000000 34109.000000\n mean% 5.831423 4.787331 5.013081\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n diff_gears diff_accel diff_orig\n count 382.000000 382.000000 382.000000\n mean 75.994764 63.633508 54.083770\n std 58.290971 51.885162 38.762326\n min 2.000000 2.000000 6.000000\n 25% 29.000000 22.000000 19.000000\n 50% 57.000000 48.500000 45.000000\n 75% 111.000000 97.000000 78.750000\n max 279.000000 243.000000 173.000000\n sum 29030.000000 24308.000000 20660.000000\n mean% 4.221931 3.535195 3.004654\n \"\"\"\n\n pcrnt_limit = 4.5 # mean%%(!)\n\n res_totals = self._check_gear_diffs(gened_fname_glob)\n print(res_totals)\n\n diff_prcnt = res_totals.loc[\"mean%\", [\"diff_gears\", \"diff_accel\"]]\n np.testing.assert_array_less(np.abs(diff_prcnt.fillna(0)), pcrnt_limit)\n\n def test2b_gear_diffs_transplanted(self):\n \"\"\"Check driveability-only diff-gears with Heinz stays within some percent.\n\n ### Comparison history ###\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n diff_gears diff_accel diff_orig\n count 378.000000 378.000000 378\n mean 15.566138 5.634921 0\n std 16.554295 8.136700 0\n min 0.000000 0.000000 0\n 25% 5.000000 1.000000 0\n 50% 11.000000 3.000000 0\n 75% 19.750000 7.000000 0\n max 123.000000 78.000000 0\n sum 5884.000000 2130.000000 0\n mean% 0.864785 0.313051 0\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n diff_gears diff_accel diff_orig\n count 382.000000 382.000000 382\n mean 12.599476 4.651832 0\n std 15.375930 7.566103 0\n min 0.000000 0.000000 0\n 25% 4.000000 0.000000 0\n 50% 9.000000 2.000000 0\n 75% 15.000000 6.000000 0\n max 123.000000 78.000000 0\n sum 4813.000000 1777.000000 0\n mean% 0.699971 0.258435 0\n \"\"\"\n\n pcrnt_limit = 0.75 # mean%(!)\n\n res_totals = self._check_gear_diffs(trans_fname_glob)\n print(res_totals)\n\n diff_prcnt = res_totals.loc[\"mean%\", [\"diff_gears\", \"diff_accel\"]]\n np.testing.assert_array_less(np.abs(diff_prcnt.fillna(0)), pcrnt_limit)\n\n def _check_n_mean(self, fname_glob):\n res = vehicles_applicator(\n fname_glob, lambda _, df_g, df_h: (df_g[\"rpm\"].mean(), df_h[\"n\"].mean())\n )\n res.columns = [\"python\", \"heinz\"]\n\n res_totals = res.describe()\n res_totals[\"diff_prcnt\"] = (\n 100 * (res_totals.heinz - res_totals.python) / res_totals.min(axis=1)\n )\n\n return res_totals\n\n def test3a_n_mean(self):\n \"\"\"Check mean-rpm diff with Heinz stays within some percent.\n\n ### Comparison history ###\n\n Class3b-Vehicles, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n mean std min max\n python 1766.707825 410.762478 1135.458463 3217.428423\n heinz 1759.851498 397.343498 1185.905053 3171.826208\n diff_prcnt -0.3896 -3.3772 4.4428 -1.4377\n\n Separated test/unladen masses::\n\n python heinz diff_prcnt\n count 378.000000 378.000000 0.000000\n mean 1923.908119 1899.366431 -1.292099\n std 628.998854 593.126296 -6.048047\n min 1135.458463 1185.905053 4.442839\n 25% 1497.544940 1495.699889 -0.123357\n 50% 1740.927971 1752.668517 0.674384\n 75% 2121.459309 2111.876041 -0.453780\n max 4965.206982 4897.154914 -1.389625\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n python heinz diff_prcnt\n count 382.000000 382.000000 0.000000\n mean 1835.393402 1827.572965 -0.427914\n std 476.687485 464.264779 -2.675781\n min 1135.458463 1185.905053 4.442839\n 25% 1486.886555 1482.789006 -0.276341\n 50% 1731.983662 1739.781233 0.450210\n 75% 2024.534101 2018.716963 -0.288160\n max 3741.849187 3750.927263 0.242609\n \n Keeping idle engine revs::\n \n python heinz diff_prcnt\n count 382.000000 382.000000 0.000000\n mean 1852.183403 1827.572965 -1.346619\n std 473.142045 464.264779 -1.912113\n min 1168.757027 1185.905053 1.467202\n 25% 1507.030779 1482.789006 -1.634877\n 50% 1749.246014 1739.781233 -0.544021\n 75% 2043.861777 2018.716963 -1.245584\n max 3747.026551 3750.927263 0.104102\n \"\"\"\n\n pcrnt_limit = 1.5\n\n res_totals = self._check_n_mean(gened_fname_glob)\n print(res_totals)\n\n diff_prcnt = res_totals.loc[\"mean\", \"diff_prcnt\"]\n self.assertLess(np.abs(diff_prcnt), pcrnt_limit)\n\n def test3b_n_mean_transplanted(self):\n \"\"\"Check driveability-only mean-rpm diff with Heinz stays within some percent.\n\n ### Comparison history ###\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n python heinz diff_prcnt\n count 378.000000 378.000000 0.000000\n mean 1880.045112 1899.366431 1.027705\n std 572.842493 593.126296 3.540904\n min 1150.940393 1185.905053 3.037921\n 25% 1477.913404 1495.699889 1.203486\n 50% 1739.882957 1752.668517 0.734852\n 75% 2073.715015 2111.876041 1.840225\n max 4647.136063 4897.154914 5.380063\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n python heinz diff_prcnt\n count 382.000000 382.000000 0.000000\n mean 1818.519842 1827.572965 0.497829\n std 469.276397 464.264779 -1.079474\n min 1150.940393 1185.905053 3.037921\n 25% 1467.153958 1482.789006 1.065672\n 50% 1730.051632 1739.781233 0.562388\n 75% 2010.264758 2018.716963 0.420452\n max 3704.999890 3750.927263 1.239605\n \"\"\"\n\n pcrnt_limit = 0.55\n\n res_totals = self._check_n_mean(trans_fname_glob)\n print(res_totals)\n\n diff_prcnt = res_totals.loc[\"mean\", \"diff_prcnt\"]\n self.assertLess(np.abs(diff_prcnt), pcrnt_limit)\n\n def _check_n_mean__pmr(self, fname_glob):\n vehdata = _read_vehicles_inp()\n vehdata[\"pmr\"] = 1000.0 * vehdata[\"rated_power\"] / vehdata[\"kerb_mass\"]\n np.testing.assert_allclose(vehdata.pmr_km, vehdata.pmr)\n\n res = vehicles_applicator(\n fname_glob, lambda _, df_g, df_h: (df_g[\"rpm\"].mean(), df_h[\"n\"].mean())\n )\n\n res.columns = [\"gened_mean_rpm\", \"heinz_mean_rpm\"]\n vehdata = vehdata.merge(\n res, how=\"inner\", left_index=True, right_index=True\n ).sort()\n self.assertEqual(vehdata.shape[0], res.shape[0])\n\n df = vehdata.sort(\"pmr\")[[\"gened_mean_rpm\", \"heinz_mean_rpm\"]]\n dfg = df.groupby(pd.cut(vehdata.pmr, 12))\n pmr_histogram = dfg.mean()\n\n dif = (\n pmr_histogram[\"heinz_mean_rpm\"] - pmr_histogram[\"gened_mean_rpm\"]\n ) / pmr_histogram.min(axis=1)\n pmr_histogram[\"diff_prcnt\"] = 100 * dif\n pmr_histogram[\"count\"] = dfg.count().iloc[:, -1]\n\n return pmr_histogram\n\n @skipIf(\n utils.is_travis(),\n \"GroupBy probably fails in old pandas, and cannot upgrade it.\",\n )\n def test4a_n_mean__PMR(self):\n \"\"\"Check mean-rpm diff with Heinz stays within some percent for all PMRs.\n\n ### Comparison history ###\n\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n gened_mean_rpm heinz_mean_rpm diff_ratio count\n pmr\n (9.973, 24.823] 1566.018469 1568.360963 0.001496 32\n (24.823, 39.496] 1701.176128 1702.739797 0.000919 32\n (39.496, 54.17] 1731.541637 1724.959671 -0.003816 106\n (54.17, 68.843] 1894.477475 1877.786294 -0.008889 61\n (68.843, 83.517] 1828.518522 1818.720627 -0.005387 40\n (83.517, 98.191] 1824.060716 1830.482140 0.003520 3\n (98.191, 112.864] 1794.673461 1792.693611 -0.001104 31\n (112.864, 127.538] 3217.428423 3171.826208 -0.014377 1\n (127.538, 142.211] 1627.952896 1597.571904 -0.019017 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1396.061758 1385.176569 -0.007858 1\n\n Separated test/unladen masses::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr\n (11.504, 26.225] 1579.612698 1585.721306 0.386716 28\n (26.225, 40.771] 1706.865069 1700.689983 -0.363093 41\n (40.771, 55.317] 1866.150857 1841.779091 -1.323273 119\n (55.317, 69.863] 2122.662626 2085.262950 -1.793523 122\n (69.863, 84.409] 2228.282795 2171.952804 -2.593518 29\n (84.409, 98.955] 1783.316413 1787.378401 0.227777 4\n (98.955, 113.501] 1718.157828 1718.516147 0.020855 31\n (113.501, 128.0475] 2005.415058 1954.763742 -2.591173 2\n (128.0475, 142.594] 1566.601860 1553.383676 -0.850928 1\n (142.594, 157.14] NaN NaN NaN 0\n (157.14, 171.686] NaN NaN NaN 0\n (171.686, 186.232] 1396.061758 1385.176569 -0.785834 1\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr\n (9.973, 24.823] 1560.010258 1563.836656 0.245280 33\n (24.823, 39.496] 1725.209986 1725.004638 -0.011904 34\n (39.496, 54.17] 1737.811065 1730.770088 -0.406812 123\n (54.17, 68.843] 1996.999520 1983.753219 -0.667739 94\n (68.843, 83.517] 2051.088434 2034.594136 -0.810692 59\n (83.517, 98.191] 1964.832555 1958.081066 -0.344801 4\n (98.191, 112.864] 1682.122484 1684.443875 0.138004 31\n (112.864, 127.538] 2718.877009 2687.055802 -1.184241 2\n (127.538, 142.211] 1660.925042 1668.155469 0.435325 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1396.061758 1385.176569 -0.785834 1\n Mean: 0.419219429398\n\n pandas 0.15.1::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr \n (9.973, 24.823] 2037.027221 2038.842442 0.089111 33\n (24.823, 39.496] 2257.302959 2229.999526 -1.224369 34\n (39.496, 54.17] 1912.075914 1885.792807 -1.393743 123\n (54.17, 68.843] 1716.720028 1717.808457 0.063402 94\n (68.843, 83.517] 1677.882399 1683.916224 0.359610 59\n (83.517, 98.191] 1535.881170 1551.609661 1.024070 4\n (98.191, 112.864] 1571.290286 1589.997331 1.190553 31\n (112.864, 127.538] 1409.308426 1425.965019 1.181898 2\n (127.538, 142.211] 1975.481368 1967.808440 -0.389923 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1950.377512 1937.426430 -0.668468 1\n Mean diff_prcnt: 0.632095580562\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n\n Keeping idle engine revs::\n pmr \n (9.973, 24.823] 2058.624153 2038.842442 -0.970242 33\n (24.823, 39.496] 2271.419763 2229.999526 -1.857410 34\n (39.496, 54.17] 1927.898841 1885.792807 -2.232803 123\n (54.17, 68.843] 1733.545963 1717.808457 -0.916139 94\n (68.843, 83.517] 1694.461857 1683.916224 -0.626256 59\n (83.517, 98.191] 1553.854990 1551.609661 -0.144710 4\n (98.191, 112.864] 1590.081566 1589.997331 -0.005298 31\n (112.864, 127.538] 1427.367629 1425.965019 -0.098362 2\n (127.538, 142.211] 1989.461646 1967.808440 -1.100372 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1960.918157 1937.426430 -1.212522 1\n Mean diff_prcnt: 0.76367613389\n \"\"\"\n\n pcrnt_limit = 0.8\n\n pmr_histogram = self._check_n_mean__pmr(gened_fname_glob)\n\n print(pmr_histogram)\n\n diff_prcnt = pmr_histogram[\"diff_prcnt\"].fillna(0).abs().mean()\n print(\"Mean diff_prcnt: %s\" % diff_prcnt)\n self.assertLess(diff_prcnt, pcrnt_limit)\n\n @skipIf(\n utils.is_travis(),\n \"GroupBy probably fails in old pandas, and cannot upgrade it.\",\n )\n def test4b_n_mean__PMR_transplanted(self):\n \"\"\"Check driveability-only mean-rpm diff with Heinz stays within some percent for all PMRs.\n\n ### Comparison history ###\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr\n (9.973, 24.823] 1557.225037 1568.360963 0.715113 32\n (24.823, 39.496] 1686.859826 1696.482640 0.570457 34\n (39.496, 54.17] 1771.670097 1789.409819 1.001299 120\n (54.17, 68.843] 2133.400050 2165.214662 1.491263 94\n (68.843, 83.517] 2020.903728 2043.741660 1.130085 59\n (83.517, 98.191] 1886.836446 1890.040533 0.169813 4\n (98.191, 112.864] 1788.434592 1792.693611 0.238142 31\n (112.864, 127.538] 2580.884314 2568.011660 -0.501269 2\n (127.538, 142.211] 1581.625191 1597.571904 1.008249 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1367.068837 1385.176569 1.324566 1\n\n Separated test/unladen masses::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr\n (11.504, 26.225] 1572.733597 1585.721306 0.825805 28\n (26.225, 40.771] 1690.081663 1700.689983 0.627681 41\n (40.771, 55.317] 1821.319706 1841.779091 1.123327 119\n (55.317, 69.863] 2060.507029 2085.262950 1.201448 122\n (69.863, 84.409] 2142.964427 2171.952804 1.352723 29\n (84.409, 98.955] 1783.214173 1787.378401 0.233524 4\n (98.955, 113.501] 1713.473617 1718.516147 0.294287 31\n (113.501, 128.0475] 1950.373771 1954.763742 0.225084 2\n (128.0475, 142.594] 1543.937285 1553.383676 0.611838 1\n (142.594, 157.14] NaN NaN NaN 0\n (157.14, 171.686] NaN NaN NaN 0\n (171.686, 186.232] 1367.068837 1385.176569 1.324566 1\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr\n (9.973, 24.823] 1551.901645 1563.836656 0.769057 33\n (24.823, 39.496] 1713.382835 1725.004638 0.678296 34\n (39.496, 54.17] 1722.174466 1730.770088 0.499114 123\n (54.17, 68.843] 1974.768859 1983.753219 0.454958 94\n (68.843, 83.517] 2026.630271 2034.594136 0.392961 59\n (83.517, 98.191] 1954.817179 1958.081066 0.166966 4\n (98.191, 112.864] 1676.678357 1684.443875 0.463149 31\n (112.864, 127.538] 2678.973439 2687.055802 0.301696 2\n (127.538, 142.211] 1658.577318 1668.155469 0.577492 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1367.068837 1385.176569 1.324566 1\n Mean diff_prcnt: 0.469021296461\n \n pandas 0.15.1::\n \n gened_mean_rpm heinz_mean_rpm diff_prcnt count\n pmr \n (9.973, 24.823] 2021.882193 2038.842442 0.838835 33\n (24.823, 39.496] 2204.136804 2229.999526 1.173372 34\n (39.496, 54.17] 1880.733341 1885.792807 0.269016 123\n (54.17, 68.843] 1710.819917 1717.808457 0.408491 94\n (68.843, 83.517] 1677.846860 1683.916224 0.361735 59\n (83.517, 98.191] 1541.587174 1551.609661 0.650141 4\n (98.191, 112.864] 1579.049392 1589.997331 0.693325 31\n (112.864, 127.538] 1411.921405 1425.965019 0.994646 2\n (127.538, 142.211] 1976.193317 1967.808440 -0.426102 1\n (142.211, 156.885] NaN NaN NaN 0\n (156.885, 171.558] NaN NaN NaN 0\n (171.558, 186.232] 1954.662077 1937.426430 -0.889616 1\n Mean diff_prcnt: 0.558773102894\n \"\"\"\n\n pcrnt_limit = 0.6\n\n pmr_histogram = self._check_n_mean__pmr(trans_fname_glob)\n\n print(pmr_histogram)\n\n diff_prcnt = pmr_histogram[\"diff_prcnt\"].fillna(0).abs().mean()\n print(\"Mean diff_prcnt: %s\" % diff_prcnt)\n self.assertLess(diff_prcnt, pcrnt_limit)\n\n def _check_n_mean__gear(self, fname_glob):\n def avg_by_column(group_column, aggregate_column, df):\n sr = df.groupby(group_column)[aggregate_column].describe()\n\n ## Ensure 6-gears for all vehicles\n #\n index = [range(7), [\"mean\", \"std\", \"min\", \"max\"]]\n index = pd.MultiIndex.from_product(index, names=[\"gear\", \"aggregate\"])\n sr = sr.reindex(index)\n\n return sr\n\n vehdata = OrderedDict()\n\n for (veh_num, df_g, df_h) in _file_pairs(fname_glob):\n df = pd.concat(\n (avg_by_column(\"gears\", \"rpm\", df_g), avg_by_column(\"gear\", \"n\", df_h)),\n axis=1,\n )\n df.columns = [\"python\", \"heinz\"]\n df[\"diff%\"] = (\n 100 * (df.python - df.heinz) / df.iloc[:, :2].abs().min(axis=1)\n )\n\n vehdata[veh_num] = df\n\n vehdata = pd.Panel(vehdata).to_frame(filter_observations=False)\n\n diff_prcnt_by_gears = vehdata.xs(\"mean\", level=1).mean(axis=1)\n diff_prcnt_by_gears = pd.DataFrame(diff_prcnt_by_gears).unstack()\n diff_prcnt_by_gears.name = \"diff_prcnt_by_gears\"\n\n diff_prcnt_by_gears = diff_prcnt_by_gears[0]\n diff_prcnt_by_gears.columns.name = \"n_mean\"\n\n return diff_prcnt_by_gears\n\n def test5a_n_mean__gear(self):\n \"\"\"Check mean-rpm diff% with Heinz stays within some percent for all gears.\n\n ### Comparison history ###\n\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n n_mean python heinz diff%\n gear\n 0 732.358286 804.656085 -9.925769\n 1 870.080494 1177.547512 -44.450903\n 2 1789.787609 1650.383967 6.520319\n 3 1921.271483 1761.172027 7.804359\n 4 1990.286402 1886.563262 5.401895\n 5 2138.445024 2112.552162 1.892950\n 6 2030.970322 1987.865039 2.228276\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n gear\n 0 735.143823 808.795812 -10.052865\n 1 799.834530 1139.979330 -47.027383\n 2 1598.773915 1582.431975 1.119054\n 3 1793.617644 1691.589756 5.768020\n 4 1883.863510 1796.957457 5.024360\n 5 2095.211754 2052.059948 2.430360\n 6 2033.663975 1990.344346 2.238421\n \"\"\"\n pcrnt_limit = 48\n\n histogram = self._check_n_mean__gear(gened_fname_glob)\n\n print(histogram)\n\n diff_prcnt = histogram[\"diff%\"]\n np.testing.assert_array_less(np.abs(diff_prcnt.fillna(0)), pcrnt_limit)\n\n def test5b_n_mean__gear_transplanted(self):\n \"\"\"Check mean-rpm diff% with Heinz stays within some percent for all gears.\n\n ### Comparison history ###\n\n\n Force class3b, Phase-1b-beta(ver <= 0.0.8, Aug-2014) with Heinz maxt gear-time=2sec::\n\n n_mean python heinz diff%\n gear\n 0 732.357001 804.656085 -9.926855\n 1 966.022039 1177.547512 -24.409425\n 2 1678.578373 1650.383967 1.616768\n 3 1791.644768 1761.172027 1.700642\n 4 1883.504933 1886.563262 0.119165\n 5 2099.218160 2112.552162 -0.320293\n 6 1985.732086 1987.865039 -0.096754\n\n Not forcing class3b, honoring declared v_max & unladen_mass::\n\n n_mean python heinz diff%\n gear\n 0 735.077116 808.795812 -10.065886\n 1 932.586982 1139.979330 -24.285307\n 2 1606.040896 1582.431975 1.379144\n 3 1721.141364 1691.589756 1.686708\n 4 1803.212699 1796.957457 0.370703\n 5 2053.822313 2052.059948 0.142138\n 6 1988.195381 1990.344346 -0.097482\n \"\"\"\n pcrnt_limit = 25\n\n histogram = self._check_n_mean__gear(trans_fname_glob)\n\n print(histogram)\n\n diff_prcnt = histogram[\"diff%\"]\n np.testing.assert_array_less(np.abs(diff_prcnt.fillna(0)), pcrnt_limit)\n\n\n###################\n# RUN EXPERIMENTS #\n###################\n\n\ndef _run_the_experiments(\n transplant_original_gears=False,\n plot_results=False,\n compare_results=False,\n encoding=\"UTF-8\",\n):\n\n ## If file existent, it contains also calculated fields\n # from the previous experiment run.\n #\n out_df = _read_vehicles_out()\n\n inp_df = _read_vehicles_inp()\n ## Reconstruct the columns only presetn in the out_df.\n #\n inp_df[\"pmr\"] = np.NAN\n inp_df[\"wltc_class\"] = \"\"\n inp_df[\"f_downscale\"] = np.NAN\n\n wots = _read_wots()\n\n failed_vehicles = 0\n for (ix, row) in inp_df.iterrows():\n veh_num = ix\n heinz_fname = _make_heinz_fname(veh_num)\n outfname = _make_gened_fname(transplant_original_gears, veh_num)\n\n if not out_df is None and _is_file_up_to_date(outfname, [heinz_fname]):\n inp_df.loc[ix] = out_df.loc[ix]\n continue\n\n mdl = goodVehicle()\n veh = mdl[\"vehicle\"]\n\n veh[\"test_mass\"] = row[\"test_mass\"]\n veh[\"unladen_mass\"] = row[\"kerb_mass\"]\n veh[\"f0\"] = row[\"f0_real\"]\n veh[\"f1\"] = row[\"f1_real\"]\n veh[\"f2\"] = row[\"f2_real\"]\n veh[\"p_rated\"] = row[\"rated_power\"]\n veh[\"n_rated\"] = row[\"rated_speed\"]\n veh[\"n_idle\"] = int(row[\"idling_speed\"])\n veh[\"v_max\"] = row[\"v_max\"]\n ngears = int(row[\"no_of_gears\"])\n veh[\"gear_ratios\"] = list(row[\"ndv_1\" : \"ndv_%s\" % ngears]) #'ndv_1'\n veh[\"wot\"] = _select_wot(wots, row[\"IDcat\"] == 2)\n\n if transplant_original_gears:\n log.warning(\">>> Transplanting gears from Heinz's!\")\n df_h = _read_heinz_file(veh_num)\n\n mdl[\"cycle\"] = {\"gears_orig\": df_h[\"g_max\"].values}\n\n try:\n experiment = Experiment(mdl)\n mdl = experiment.run()\n except Exception as ex:\n log.warning(\"VEHICLE_FAILED(%s): %s\", veh_num, str(ex))\n failed_vehicles += 1\n continue\n else:\n params = mdl[\"params\"]\n veh = mdl[\"vehicle\"]\n\n inp_df.loc[ix, \"pmr\"] = veh[\"pmr\"]\n inp_df.loc[ix, \"wltc_class\"] = veh[\"wltc_class\"]\n inp_df.loc[ix, \"f_downscale\"] = params[\"f_downscale\"]\n\n # ankostis_mdb: 't', \"v in km/h\",\"v_orig\",\"a in m/s²\",\"gear\",\"g_min\",\"g_max\",\"gear_modification\",\"error_description\"\n # heinz: 't', 'km_h', 'stg', 'gear'\n cycle_df = pd.DataFrame(mdl[\"cycle\"])\n\n _compare_exp_results(cycle_df, outfname, compare_results)\n\n cycle_df.to_csv(outfname, index_label=\"time\")\n fail_limit_prcnt = 0.1\n assert failed_vehicles < fail_limit_prcnt * inp_df.shape[0], (\n \"TOO MANY(>%f) vehicles have Failed(%i out of %i)!\"\n % (fail_limit_prcnt, failed_vehicles, inp_df.shape[0])\n )\n\n if not transplant_original_gears:\n _write_vehicle_data(inp_df)\n\n return inp_df\n\n\n# vehfpath = os.path.join(samples_dir, 'heinz_Petrol_veh{:05}.dri'.format(veh_num))\n# inpfname = glob.glob(vehfpath)[0]\n# out_df = pd.read_csv(inpfname, encoding='latin-1')\n\n\n###################\n# COMPARE RESULTS #\n###################\n\n\ndef _compare_exp_results(tabular, outfname, run_comparison):\n if run_comparison:\n try:\n data_prev = _read_gened_file(outfname)\n ## Compare changed-tabular\n #\n npt.assert_array_equal(tabular[\"gears\"], data_prev[\"gears\"], outfname)\n # Unreached code in case of assertion.\n # cmp = tabular['gears'] != data_prev['gears']\n # if (cmp.any()):\n # self._plotResults(data_prev)\n # print('>> COMPARING(%s): %s'%(fname, cmp.nonzero()))\n # else:\n # print('>> COMPARING(%s): OK'%fname)\n except FileNotFoundError:\n print(\">> COMPARING(%s): No old-tabular found, 1st time to run\" % outfname)\n run_comparison = False\n\n\n## TODO: Move into wltp/plots\n#\ndef _plotResults(\n veh_fname,\n df_g,\n df_h,\n res,\n ax,\n plot_diffs_gears_only=True,\n plot_original_gears=False,\n):\n if plot_original_gears:\n my_gear_col = \"gears_orig\"\n hz_gear_col = \"g_max\"\n else:\n my_gear_col = \"gears\"\n hz_gear_col = \"gear\"\n\n ax.grid(True)\n\n ax2 = ax.twinx()\n\n tlen = len(df_g.index)\n # ax.set_xticks(np.arange(0.0, tlen, 50.0)) NO! looses auto when zooming.\n\n clutch = df_g[\"clutch\"]\n clutch = clutch.nonzero()[0]\n ax.vlines(clutch, 0, 0.2)\n\n ## Add pickers on driveability lines showing the specific msg.\n #\n driveability = df_g[\"driveability\"]\n driveability_true = driveability.apply(lambda s: isinstance(s, str))\n lines = ax2.vlines(driveability_true.nonzero()[0], 2, 4, \"c\", picker=5)\n lines.set_urls(driveability[driveability_true])\n\n v_max = df_g[\"v_class\"].max()\n ax.hlines(1 / v_max, 0, tlen, color=\"0.75\")\n\n ax.plot(df_g[\"v_class\"] / v_max)\n ax.plot(df_g[\"v_target\"] / v_max, \"-.\")\n\n # ax.plot(df_g['rpm'] / df_g['rpm'].max())\n # p_req = df_g['p_required'] / df_g['p_required'].max()\n # p_req[p_req < 0] = 0\n # ax.plot(p_req)\n\n ## Plot gear diffs.\n #\n my_gears = df_g[my_gear_col]\n hz_v_real = df_h[\"v\"]\n hz_v_target = df_h[\"v_downscale\"]\n hz_gears = df_h[hz_gear_col]\n\n orig_gears = df_g[\"gears_orig\"]\n if plot_diffs_gears_only:\n diff_gears = my_gears != hz_gears\n difft = diff_gears.nonzero()[0]\n difft = set().union(\n difft,\n difft + 1,\n difft + 2,\n difft + 3,\n difft + 4,\n difft + 5,\n difft + 6,\n difft - 1,\n difft - 2,\n difft - 3,\n difft - 4,\n difft - 5,\n difft - 6,\n )\n difft = list(difft)\n my_gears = my_gears[difft]\n hz_gears = hz_gears[difft]\n ax2.plot(difft, my_gears.tolist(), \"o\", color=\"red\")\n ax2.plot(difft, orig_gears[difft].tolist(), \"v\", color=\"green\")\n ax2.plot(difft, hz_gears.tolist(), \"*\", color=\"blue\")\n else:\n ax2.plot(my_gears.tolist(), \"o\", color=\"red\")\n ax2.plot(hz_gears.tolist(), \"*\", color=\"blue\")\n\n ax.plot(df_g[\"v_real\"] / v_max)\n\n ## Add pickers on driveability lines showing the specific msg.\n #\n hz_driveability = df_h[\"gear_modification\"]\n hz_driveability_true = ~hz_driveability.apply(np.isreal)\n lines = ax2.vlines(hz_driveability_true.nonzero()[0], 0, 2, \"m\", picker=5)\n lines.set_urls(hz_driveability[hz_driveability_true])\n\n ax.plot(hz_v_target / v_max, \"--\")\n ax.plot(hz_v_real / v_max, \":\")\n\n ax.text(\n 0.7,\n 0,\n \"Diffs: %.4f\" % res,\n transform=ax.transAxes,\n bbox={\"facecolor\": \"red\", \"alpha\": 0.5, \"pad\": 10},\n )\n\n\ndef plot_diffs_with_heinz(diff_results, res, transplant_original_gears=False):\n from matplotlib import pyplot as plt\n\n def fig_onpick(event):\n pickline = event.artist\n urls = pickline.get_urls()\n rule = urls.iloc[event.ind]\n print(rule)\n text_infos.set_text(\"Rule: %s\" % rule)\n\n fig.canvas.draw()\n\n fig = plt.figure()\n text_infos = fig.text(\n 0.5,\n 0.5,\n \"\",\n transform=fig.transFigure,\n bbox={\"facecolor\": \"grey\", \"alpha\": 0.4, \"pad\": 10},\n horizontalalignment=\"center\",\n verticalalignment=\"center\",\n color=\"blue\",\n )\n fig.canvas.mpl_connect(\"pick_event\", fig_onpick)\n orig = \"Driveability\" if transplant_original_gears else \"Pre-Driveability\"\n fig.suptitle(\n \"%s: ±DIFFs: count(%i), min(%i), MEAN(%.2f±%.2f), max(%i).\"\n % (orig, res[0].sum(), res[0].min(), res[0].mean(), res[0].std(), res[0].max())\n )\n\n ## NOTE: Limit subplots to facilitate research.\n #\n # i_to_plot = paths\n # i_to_plot = paths[0:9]\n # i_to_plot = paths[5:6] + paths[7:9] + paths[14:16] + paths[23:24]\n i_to_plot = range[5:8] + range[17:18] + range[22:24]\n\n ## Decide subplot-grid dimensions.\n #\n npaths_to_plot = len(i_to_plot)\n w = math.ceil(math.sqrt(npaths_to_plot))\n h = w - 1 if ((w - 1) * w >= npaths_to_plot) else w\n\n nplotted = 0\n\n for (i, diff) in enumerate(diff_results):\n (\n inpfname,\n df_my,\n df_hz,\n ndiff_gears,\n ndiff_gears_accel,\n ndiff_gears_orig,\n ) = diff\n if i in i_to_plot:\n nplotted += 1\n ax = fig.add_subplot(w, h, nplotted)\n veh_name = os.path.basename(inpfname)\n ax.set_title(\"%i: %s\" % (i, veh_name), fontdict={\"fontsize\": 8})\n _plotResults(\n veh_name,\n df_my,\n df_hz,\n ndiff_gears,\n ax,\n plot_original_gears=not transplant_original_gears,\n )\n\n fig.tight_layout()\n plt.show()\n\n\nif __name__ == \"__main__\":\n unittest.main()\n" ]
[ [ "numpy.gradient", "pandas.Panel", "pandas.read_csv", "matplotlib.pyplot.figure", "pandas.MultiIndex.from_product", "pandas.DataFrame", "numpy.abs", "numpy.testing.assert_array_equal", "numpy.count_nonzero", "matplotlib.pyplot.show", "pandas.cut", "numpy.testing.assert_allclose", "numpy.array" ] ]
Eitan177/StructuralMapping
[ "c20ce43de2698902d606b718c9a9fdf2296b0a52" ]
[ "code/Alignment/FREAD/prosci/loops/fread.py" ]
[ "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n\nimport sys\nimport os\nimport time\nimport itertools\n\nfrom heapq import heappush, heappop\n\n# Compatibility with Python < 2.6\n#\ntry:\n from heapq import heappushpop\nexcept ImportError:\n def heappushpop(heap, item):\n heappush(heap, item)\n return heappop(heap)\n\nimport numpy\n\nfrom prosci.util.protein import Pdb, ResidueList, Protein\nfrom prosci.util.pdb3d import dist\nfrom prosci.util.geohash import GeometricHash\n\nfrom prosci.loops import ccd\nfrom prosci.loops.loopmodel import ANCHOR_LENGTH, get_max_loop_length, describe_anchors, iterate_database, get_loop_structure, add_oxygens, is_loop_length_in_database, get_min_loop_length_in_database, relabel_loop, is_clash, calculate_rmsd, is_consecutive, make_contact_gh, find_contacts, get_contact_class, find_contacts_simple, get_native_contacts\nfrom prosci.loops.esst import load_esst\n\n\n# Define consecutive residues very loosely, for purposes of checking the framework\ndef is_framework_consecutive(a, b):\n # Set max distance for consecutive C and N atoms to 2.8 Angstroms\n # ... which is very loose\n #\n return is_consecutive(a, b, maxlen_sq=7.84)\n\n\n\ndef _heapadd(heap, item, maxitems):\n if len(heap) < maxitems:\n heappush(heap, item)\n else:\n heappushpop(heap, item)\n\n\n\nclass FreadError(RuntimeError):\n pass\n\nclass BadInputError(FreadError):\n pass\n\nclass UnsupportedLoopLengthError(BadInputError):\n pass\n\nclass LoopStretchError(BadInputError):\n pass\n\nclass NonConsecutiveAnchorError(BadInputError):\n pass\n\n\nclass Fread(object):\n def __init__(self, db=None, subst_table_path=None, score_cutoff=25, open_rmsd_cutoff=1.0, closed_rmsd_cutoff=0.3, vdw_factor=0.7, close=True, verbose=False, errstream=sys.stderr, meld=True, max_melding_rmsd=1.0, nostruc=False, mutate=False, ccd_target_rmsd=0.15, ccd_iterations=5000, max_decoys=100, first_decoys=0, extension_size=sys.maxint, extension_minimum=0, calculate_contacts=False, contact_distance=6.0, contact_identity=0.8):\n self.score_cutoff = score_cutoff\n self.open_rmsd_cutoff = open_rmsd_cutoff\n self.closed_rmsd_cutoff = closed_rmsd_cutoff\n self.vdw_factor = vdw_factor\n self.close = close\n self.verbose = verbose\n self.errstream = errstream\n self.meld = meld\n self.max_melding_rmsd = max_melding_rmsd\n self.min_database_loop_length = 1\n self.no_structure_output = nostruc\n self.mutate = mutate\n self.ccd_target_rmsd = ccd_target_rmsd\n self.ccd_iterations = ccd_iterations\n self.max_decoys = max_decoys\n self.first_decoys = first_decoys\n self.extension_size = extension_size\n self.extension_minimum = extension_minimum\n self.min_b_factor = 0.0\n\n self.calculate_contacts = calculate_contacts\n self.contact_distance = contact_distance\n self.contact_identity = contact_identity\n \n self.warnings = []\n \n if db:\n self.set_db(db)\n \n self.set_subst_tables(subst_table_path)\n \n self.results = []\n self.counter = itertools.count(1)\n self.idecoy = 0\n \n def set_options(self, opts):\n for o in opts:\n if o in self.__dict__ and not o.startswith('_'):\n self.__dict__[o] = opts[o]\n \n def set_silent(self):\n #self.verbose = False\n self.errstream = open(os.devnull, 'w')\n \n def note(self, message, force=False):\n if force or self.verbose:\n self.errstream.write(str(message)+\"\\n\")\n \n def warn(self, exception, force=False):\n if isinstance(exception, str):\n exception = FreadError(exception)\n self.note(\"WARNING: \"+str(exception), force)\n self.warnings.append(exception)\n return exception\n \n def die(self, exception, force=True):\n raise self.warn(exception, force)\n \n \n def set_subst_tables(self, subst_table_path):\n self.subst_tables = load_esst(subst_table_path)\n \n def set_db(self, db):\n self.min_database_loop_length = get_min_loop_length_in_database(db)\n self.db = db\n \n def set_structure(self, structure):\n self.clear_results()\n p = Pdb(structure)\n self.ligand_atoms = p.ligands\n self.residues = ResidueList(p)\n add_oxygens(self.residues)\n \n \n def get_loop_index(self, residue_number, inscode=\"\", chain=None):\n if inscode:\n inscode = inscode.strip()\n if chain:\n chain = chain.strip()\n \n if isinstance(residue_number, str) and residue_number[-1].isalpha():\n ires = int(residue_number[:-1])\n inscode = residue_number[-1]\n else:\n ires = int(residue_number)\n \n start_of_loop=None\n for i,r in enumerate(self.residues):\n if r.ires == ires and r.inscode == inscode and (not chain or chain == r.chain):\n start_of_loop = i+1\n break\n \n return start_of_loop\n \n \n def get_structure_sequence(self, startindex=0, endindex=None):\n return self.residues[startindex:endindex].get_seq()\n \n \n def set_loop(self, start_of_loop, loop_sequence, resnum=False, chain=None, structure_known=None):\n self.clear_results()\n if chain is not None:\n chain = chain.strip()\n \n # We've been given a residue number instead of a start index\n if resnum:\n if isinstance(start_of_loop, str):\n if start_of_loop[-1].isalpha():\n ires = int(start_of_loop[:-1])\n inscode = start_of_loop[-1]\n else:\n ires = int(start_of_loop)\n inscode = \"\"\n else:\n ires = int(start_of_loop)\n inscode = \"\"\n \n found=False\n for i,r in enumerate(self.residues):\n if r.ires == ires and r.inscode == inscode and (not chain or chain == r.chain):\n start_of_loop = i+1\n found = True\n self.note(\"Located residue left of loop to be modelled: %d%s. Loop starting index: %d. Sequence surrounding start of loop: %s|%s\" % (r.ires, r.inscode, start_of_loop, self.residues[max(0,start_of_loop-8):start_of_loop].get_seq(), self.residues[start_of_loop:start_of_loop+8].get_seq()))\n break\n \n if not found:\n msg = \"Residue before start of loop (residue number %d%s\" % (ires, inscode)\n if chain:\n msg += \", chain '%s'\" % chain\n msg += \") not found in query structure\"\n self.die(BadInputError(msg))\n \n if start_of_loop < ANCHOR_LENGTH or start_of_loop > len(self.residues) - ANCHOR_LENGTH:\n self.die(BadInputError(\"Cannot model loop closer than %d residues to the terminus (need a complete anchor on both sides).\" % (ANCHOR_LENGTH)))\n \n # Assuming no coordinates are present in file, start and end of the loop are the same\n end_of_loop = start_of_loop\n \n if structure_known is not None:\n # We were told if loop coordinates are present in input file.\n # Skip auto-detection.\n #\n self.loop_structure_is_known = structure_known\n if structure_known:\n self.note(\"User tells me loop structure is present.\")\n else:\n self.note(\"User tells me loop structure is not present.\")\n # Auto-detect if loop coordinates are present in input file.\n else:\n # Are the coordinates are in the file?\n self.loop_structure_is_known = is_framework_consecutive(self.residues[start_of_loop-1],\n self.residues[start_of_loop])\n self.note(\"Is loop structure present: \"+str(self.loop_structure_is_known))\n \n # If we have the native loop structure, adjust end_of_loop.\n # Also do some additional sanity checks\n #\n if self.loop_structure_is_known:\n end_of_loop += len(loop_sequence)\n strucseq = \"\"\n for i,r in enumerate(self.residues[start_of_loop:end_of_loop]):\n strucseq += r.get_seq()\n if (not self.mutate) and (loop_sequence != strucseq):\n self.die(BadInputError(\"Residues differ between sequence and structure input: %s, %s\" % (loop_sequence, strucseq)))\n self.note(\"Loop sequence of given ATOM co-ordinates: %s\" % (strucseq))\n del strucseq\n \n if end_of_loop != start_of_loop:\n if len(loop_sequence) != end_of_loop - start_of_loop:\n self.die(BadInputError(\"Loop co-ordinates present in input, but number of residues (%d) does not match length of input loop sequence (%d).\" % (end_of_loop - start_of_loop, len(loop_sequence))))\n if end_of_loop > len(self.residues) - ANCHOR_LENGTH:\n self.die(BadInputError(\"Cannot model loop closer than %d residues to the terminus (need a complete anchor on both sides).\" % (ANCHOR_LENGTH)))\n \n \n # Set ourselves up for loop extension\n #\n self.seq = self.loop_sequence = loop_sequence\n self.s = self.start_of_loop = start_of_loop\n self.e = self.end_of_loop = end_of_loop\n \n self.verify_anchors()\n \n while len(self.seq) < self.min_database_loop_length:\n try:\n self.verify_stretch()\n except LoopStretchError:\n pass\n self.extend_loop()\n \n while True:\n try:\n self.verify_stretch()\n break\n except LoopStretchError as e:\n try:\n self.extend_loop()\n except BadInputError:\n self.die(LoopStretchError(str(e)+\" Cannot extend gap further.\"))\n \n def extend_loop(self):\n \"\"\"Extend loop by one residue on each side. Raises NonConsecutiveAnchorError() or UnsupportedLoopLengthError() upon failure.\n \"\"\"\n start_of_loop = self.start_of_loop\n end_of_loop = self.end_of_loop\n s = self.s - 1\n e = self.e + 1\n \n length = (start_of_loop - s) + (e - end_of_loop) + len(self.loop_sequence)\n\n if s<ANCHOR_LENGTH or not is_framework_consecutive(self.residues[s-ANCHOR_LENGTH], self.residues[s-ANCHOR_LENGTH+1]) or e>len(self.residues)-ANCHOR_LENGTH or not is_framework_consecutive(self.residues[e+ANCHOR_LENGTH-2], self.residues[e+ANCHOR_LENGTH-1]):\n self.die(NonConsecutiveAnchorError(\"Cannot extend loop to length %d, due to gaps in the query structure or proximity to the termini\"%(length)))\n \n \n if not is_loop_length_in_database(self.db, length):\n self.die(UnsupportedLoopLengthError(\"Cannot extend loop to length %d, due to database limitations\"%(length)))\n \n seq = \"\"\n for i in xrange(s, start_of_loop):\n seq += self.residues[i].get_seq()\n seq += self.loop_sequence\n for i in xrange(end_of_loop, e):\n seq += self.residues[i].get_seq()\n \n assert len(seq) == length, str([s, start_of_loop, end_of_loop, e, length, len(seq), seq, dbdir, pdb_file, start_of_loop, loop_sequence])\n \n self.s = s\n self.e = e\n self.seq = seq\n \n self.note(\"Extending loop to length %d\" % (length))\n \n \n def verify_anchors(self):\n # Ensure anchors are consecutive stretches of amino acids\n for x in xrange(self.s-ANCHOR_LENGTH+1, self.s):\n if not is_framework_consecutive(self.residues[x-1], self.residues[x]):\n self.die(NonConsecutiveAnchorError(\"Anchor residues not consecutive in framework structure: residue index %s, %s\"%(self.s, self.residues.code)))\n for x in xrange(self.e, self.e+ANCHOR_LENGTH-1):\n if not is_framework_consecutive(self.residues[x], self.residues[x+1]):\n self.die(NonConsecutiveAnchorError(\"Anchor residues not consecutive in framework structure: residue index %s, %s\"%(self.s, self.residues.code)))\n \n def verify_stretch(self):\n # Ensure anchors are not too far apart for loop to stretch gap\n anchor_distance = dist(self.residues[self.s-1].C, self.residues[self.e].N)\n if anchor_distance > get_max_loop_length(len(self.seq)) * 1.05:\n self.die(LoopStretchError(\"Loop span (%.2f Angstrom) too large to be closed using %d residues. Trying to extend gap.\" % (anchor_distance, len(self.seq))))\n \n def verify_length(self):\n # Ensure loop is not too long for the database \n if not is_loop_length_in_database(self.db, len(self.seq)):\n self.die(UnsupportedLoopLengthError(\"Cannot model loop of length %d, due to database limitations\"%(len(self.seq))))\n \n def verify(self):\n self.verify_anchors()\n self.verify_stretch()\n self.verify_length()\n \n \n def model(self, top=None, stop_after=None, f_rank_decoy=None, f_stop_search=None, f_filter=None):\n \"\"\"Model loop using the FREAD algorithm. This is the method handling most of the work. Raises UnsupportedLoopLengthError if loop length is not supported.\n \"\"\"\n if top is None:\n top = self.max_decoys\n \n if stop_after is None:\n stop_after = self.first_decoys\n \n if top <= 0:\n top = sys.maxint\n \n if stop_after > 0 and self.idecoy >= stop_after:\n return self.results\n \n if not f_rank_decoy:\n f_rank_decoy = FREAD_RANKING\n if not f_stop_search:\n f_stop_search = lambda x: False\n if not f_filter:\n f_filter = lambda x: True\n \n while len(self.seq) < self.min_database_loop_length:\n self.extend_loop()\n \n self.verify()\n \n meld_anchors = self.meld # and (self.open_rmsd_cutoff <= self.max_melding_rmsd)\n close_loop = self.close\n \n verbose = self.verbose\n residues = self.residues\n start_of_loop = self.s\n end_of_loop = self.e\n loop_sequence = self.seq\n \n start = start_of_loop - ANCHOR_LENGTH\n end = end_of_loop + ANCHOR_LENGTH\n loop_length = len(loop_sequence)\n total_length = loop_length + 2*ANCHOR_LENGTH\n \n self.note(\"Loop region: N(%4d, %4d) C(%4d,%4d) Loop(%3d,'%s')\" % (start, start_of_loop, end_of_loop, end, loop_length, loop_sequence))\n \n ############################################################################\n \n # Get query anchors and prepare for clash checking #\n \n # Get anchor coordinates\n #\n anchor_N = residues[start:start_of_loop]\n anchor_C = residues[end_of_loop:end]\n \n \n # Describe anchors in query structure\n #\n anchor_description, query_transform = describe_anchors(anchor_N, anchor_C, loop_length)\n \n # Build a GeometricHash of the query structure (without the loop region), for\n # clash checking\n #\n coords = []\n gh_atoms = []\n for r in residues[:start]+residues[end:]:\n for a in r:\n #if a.atom in (\"N\", \"CA\", \"C\", \"O\", \"CB\"):\n coords.append(a.xyz)\n gh_atoms.append(a)\n gh = GeometricHash(numpy.array(coords))\n del coords\n \n # Inter-residue contacts\n if self.calculate_contacts:\n p = Protein((residues[:start]+residues[end:]).split_chains())\n p.ligands = Protein(ResidueList(self.ligand_atoms).split_chains())\n contact_gh, contact_gh_atoms = make_contact_gh(p)\n \n \n ############################################################################\n \n # Search the database #\n \n results = [] # Heap Queue of the top-ranking decoys\n for decoy in self.results:\n \n _heapadd(results, (f_rank_decoy(decoy), decoy.idecoy, decoy), top)\n \n \n for decoy in iterate_database(self.db, loop_length, self.subst_tables, anchor_description, loop_sequence, self.open_rmsd_cutoff, self.score_cutoff):\n \n if len(results) >= top:\n if decoy.internal_rmsd > results[0][-1].anchor_rmsd_open:\n continue\n \n # Retrieve loop structure from database\n decoy_residues = get_loop_structure(self.db, decoy.struc, decoy.start, total_length)\n \n assert len(decoy_residues) == total_length\n \n \n # Superimpose anchors and check anchor RMSD before starting\n anchor_rmsd_open = ccd.superimpose(decoy_residues[:ANCHOR_LENGTH]+decoy_residues[-ANCHOR_LENGTH:], anchor_N+anchor_C, decoy_residues)\n \n if anchor_rmsd_open > self.open_rmsd_cutoff:\n self.note(\"%s_%d_%d : Anchor RMSD too large: %.3f\"%(decoy.struc, decoy.start, loop_length, anchor_rmsd_open))\n continue\n \n if len(results) >= top:\n if anchor_rmsd_open > results[0][-1].anchor_rmsd_open:\n continue\n \n \n # Save start residue number of loop, in database structure\n decoy.startres = decoy_residues[ANCHOR_LENGTH].ires\n decoy.startinscode = decoy_residues[ANCHOR_LENGTH].inscode\n \n # Relabel residues and discard non-matching atoms\n relabel_loop(decoy_residues[ANCHOR_LENGTH:-ANCHOR_LENGTH], loop_sequence, prevatom=anchor_N[-1].CA, nextatom=anchor_C[0].CA)\n \n \n if self.loop_structure_is_known:\n loop_rmsd_open = calculate_rmsd(decoy_residues[ANCHOR_LENGTH:-ANCHOR_LENGTH], residues[start_of_loop:end_of_loop])\n \n \n \n # Are we allowed to meld this decoy?\n #\n meld_this_decoy = meld_anchors and (anchor_rmsd_open <= self.max_melding_rmsd) #and not self.no_structure_output\n close_this_decoy = close_loop and anchor_rmsd_open > self.ccd_target_rmsd and (not meld_anchors or anchor_rmsd_open > self.max_melding_rmsd)\n \n #if not self.no_structure_output or close_this_decoy:\n decoy.nanchor = anchor_N.deep_copy()\n decoy.canchor = anchor_C.deep_copy()\n \n if meld_this_decoy:\n meld(decoy.nanchor, decoy_residues[:ANCHOR_LENGTH])\n meld(decoy.canchor, decoy_residues[-ANCHOR_LENGTH:], invertWeights=True)\n \n if not close_this_decoy:\n anchor_rmsd_closed = anchor_rmsd_open\n iterations = 0\n else:\n # Loop closure\n anchor_rmsd_closed, iterations = ccd.close_loop(decoy.nanchor, decoy_residues, decoy.canchor, target_rmsd=self.ccd_target_rmsd, iterations=self.ccd_iterations)\n if anchor_rmsd_closed > self.closed_rmsd_cutoff:\n self.note(\"Failed to close loop: %s_%d_%d\"%(decoy.struc, decoy.start, loop_length))\n continue\n \n # Cut off the decoy loop's anchors\n decoy_residues = decoy_residues[ANCHOR_LENGTH:-ANCHOR_LENGTH]\n \n \n #if not self.no_structure_output:\n # Restore main chain oxygens, which got lost during the melding/closing procedure\n decoy_residues[-1].O = None # This oxygen is wrong anyway, so delete it\n add_oxygens(decoy.nanchor+decoy_residues[:1], force=True)\n add_oxygens(decoy_residues[-1:]+decoy.canchor+residues[end:end+1], force=True)\n \n \n # Clash check\n is_clashing = is_clash(gh, gh_atoms, decoy_residues, self.vdw_factor)\n if is_clashing:\n self.note(\"Clash detected in decoy: %s_%d_%d\"%(decoy.struc, decoy.start, loop_length))\n continue\n \n \n if self.loop_structure_is_known:\n decoy.loop_rmsd_open = loop_rmsd_open\n if not iterations:\n decoy.loop_rmsd_closed = loop_rmsd_open\n else:\n decoy.loop_rmsd_closed = calculate_rmsd(decoy_residues, residues[start_of_loop:end_of_loop])\n \n decoy.length = loop_length\n decoy.anchor_rmsd_open = anchor_rmsd_open\n decoy.iterations = iterations\n \n #if not self.no_structure_output:\n decoy.loop = decoy_residues\n \n \n if self.calculate_contacts:\n contacts = \"\"\n for i, r in enumerate(decoy_residues):\n contact_atoms = find_contacts(contact_gh, r, maxdist=self.contact_distance)\n contact_atoms.extend(find_contacts_simple(decoy.nanchor+decoy_residues+decoy.canchor, i, maxdist=self.contact_distance))\n contacts += get_contact_class(r.chain, contact_atoms)\n \n decoy.native_contacts = get_native_contacts(self.db, decoy.struc, decoy.start, total_length)\n \n id = 0\n for x,y in zip(decoy.native_contacts, contacts):\n if x == y:\n id += 1\n decoy.contact_identity = float(id) / len(contacts)\n decoy.contacts = contacts\n \n if (self.contact_identity > 0) and (decoy.contact_identity < self.contact_identity):\n continue\n \n \n # Get per-residue substitution scores\n #\n tables = self.subst_tables.tables\n ascii2index = self.subst_tables.ascii2index\n seqmap = tuple([ascii2index[ord(s)] for s in loop_sequence])\n dihed = decoy.dihedrals\n score_list = []\n for i,x in enumerate(decoy.seq):\n score_list.append(tables[int(dihed[i])][seqmap[i]][ascii2index[ord(x)]])\n assert sum(score_list) == decoy.score\n decoy.residue_scores = score_list\n \n # Save per-residue scores in the occupancy column and\n # the total score in the B factor column\n #\n #\n for i,r in enumerate(decoy.loop):\n for a in r:\n a.b = self.min_b_factor + max(0, 30 - decoy.score)\n # Average score over a 3 residue window...\n # because that's how MEDELLER does it\n sc = score_list[max(0,i-1):i+2]\n a.occup = float(sum(sc))/len(sc)\n \n \n \n \n if not f_filter(decoy):\n self.note(\"User-defined filter is excluding decoy: %s_%d_%d\"%(decoy.struc, decoy.start, loop_length))\n continue\n \n self.idecoy = decoy.idecoy = next(self.counter)\n # Save result\n _heapadd(results, (f_rank_decoy(decoy), decoy.idecoy, decoy), top)\n \n if stop_after > 0 and self.idecoy >= stop_after:\n break\n \n if f_stop_search(decoy):\n break\n \n self.note(\"%d decoys found\"%(len(results)))\n \n self.results = []\n while results:\n r = heappop(results)\n self.results.append(r[-1])\n self.results.reverse()\n return self.results\n \n \n def clear_results(self):\n self.results = []\n self.warnings = []\n self.counter = itertools.count(1)\n self.idecoy = 0\n \n \n def write_summary(self, outstream=sys.stdout, write_decoy_sequence=False):\n \"\"\"Write summary information to specified output stream.\n \"\"\"\n for decoy in self.results:\n outstream.write(\"%s_%d%s_%d\\t%d\\t%.3f\\t%.3f\\t%d\\t%s\" % (decoy.struc, decoy.startres, decoy.startinscode, decoy.length, decoy.score, decoy.internal_rmsd, decoy.anchor_rmsd_open, decoy.iterations,decoy.seq))\n if write_decoy_sequence:\n outstream.write(\"\\t%s\" % (decoy.seq))\n if self.calculate_contacts:\n outstream.write(\"\\t%s\" % (decoy.contacts))\n outstream.write(\"\\t%s\" % (decoy.native_contacts))\n outstream.write(\"\\t%.3f\" % (decoy.contact_identity))\n if self.loop_structure_is_known:\n outstream.write(\"\\t%.3f\\t%.3f\" % (decoy.loop_rmsd_open, decoy.loop_rmsd_closed))\n outstream.write(\"\\n\")\n \n \n def write_decoy_structures(self, out_dir, top=0, suffix=\".loop.atm\", idfilter=[]):\n \"\"\"Write decoy structure files (PDB format) to specified directory.\n \"\"\"\n if self.no_structure_output:\n self.die(\"Cannot write decoy structures as structure output is disabled\")\n\n if out_dir:\n if not os.path.exists(out_dir):\n os.mkdir(out_dir)\n \n for i, decoy in enumerate(self.results):\n if top > 0 and i >= top:\n break\n \n decoyname = \"%s_%d%s_%d\"%(decoy.struc, decoy.startres, decoy.startinscode, decoy.length)\n \n if idfilter and (decoyname not in idfilter):\n continue\n \n decoyfile = os.path.join(out_dir, decoyname+suffix)\n f = open(decoyfile, \"w\")\n try:\n for r in decoy.nanchor:\n f.write(str(r))\n for r in decoy.loop:\n f.write(str(r))\n for r in decoy.canchor:\n f.write(str(r))\n finally:\n f.close()\n \n \n def assemble_model(self, decoy):\n # Start and end of loop region in model\n start = self.s - ANCHOR_LENGTH\n end = self.e + ANCHOR_LENGTH\n \n if isinstance(decoy, int):\n decoy = self.results[decoy]\n if self.no_structure_output:\n self.die(\"Cannot write decoy structures as structure output is disabled\")\n \n model = ResidueList([])\n for i in xrange(start):\n model.append(self.residues[i].copy())\n model.extend(decoy.nanchor)\n model.extend(decoy.loop)\n model.extend(decoy.canchor)\n for i in xrange(end, len(self.residues)):\n model.append(self.residues[i].copy())\n \n # Replace main chain oxygen before the loop, to ensure the peptide bond there is planar\n model[start-1].O = None\n add_oxygens(model, start=start-1, end=start, force=True)\n \n return model\n \n \n def write_model_structures(self, out_dir, top=0, suffix=\".model.atm\", idfilter=[]):\n \"\"\"Write model structure files (PDB format), including decoy residues, to specified directory.\n \"\"\"\n \n if self.no_structure_output:\n self.die(\"Cannot write model structures as structure output is disabled\")\n \n if out_dir:\n if not os.path.exists(out_dir):\n os.mkdir(out_dir)\n else:\n out_dir = \"\"\n \n for i, decoy in enumerate(self.results):\n if top > 0 and i >= top:\n break\n \n decoyname = \"%s_%d%s_%d\"%(decoy.struc, decoy.startres, decoy.startinscode, decoy.length)\n \n if idfilter and (decoyname not in idfilter):\n continue\n \n decoyfile = os.path.join(out_dir, decoyname+suffix)\n \n model = self.assemble_model(decoy)\n \n f = open(decoyfile, \"w\")\n try:\n f.write(str(model))\n finally:\n f.close()\n\n\n def open_errstream(self, filename):\n if filename == \"-\":\n self.errstream = sys.stderr\n else:\n self.errstream = open(filename, \"w\")\n\n\n def close_errstream(self):\n if self.errstream != sys.stderr:\n self.errstream.close()\n self.set_silent()\n \n \n def automodel_loop(self, start_of_loop, loop_sequence,\n loopsuffix = \"\",\n dbdir = '.',\n strucdir = 'decoys',\n summary_file_name = 'summary',\n write_decoys = True,\n write_models = True,\n write_decoy_sequence = False,\n resnum = False,\n chain = None,\n structure_known = None,\n f_rank_decoy = None,\n f_stop_search = None,\n f_filter = None,\n idfilter = [],\n **kwargs):\n \"\"\"Search the given database and return a list of decoys for the given loop.\n \"\"\"\n \n # Set options defined in the Fread constructor\n self.set_options(kwargs)\n \n if self.extension_size < 0:\n max_extension = sys.maxint\n else:\n max_extension = self.extension_size\n \n if strucdir is not None:\n strucdir = strucdir.strip()\n if (not strucdir) or (not write_decoys and not write_models):\n strucdir = None\n self.no_structure_output = True\n \n for db_group in dbdir.split(\":\"): # try these databases until we find something\n databases = db_group.split(\"|\") # combine results from all these databases\n self.set_db(databases[0])\n \n try:\n\t \n self.set_loop(start_of_loop, loop_sequence.upper(), resnum=resnum, chain=chain, structure_known=structure_known)\n except FreadError:\n break\n \n # Extend loop if extension_minimum is set\n #\n for i in xrange(self.extension_minimum):\n try:\n self.extend_loop()\n except FreadError:\n break\n \n # Try to model, and extend loop if nothing was found\n anyresults = False\n for db in databases:\n\t \n self.set_db(db)\n try:\n\t \n anyresults |= bool(self.model(f_rank_decoy=f_rank_decoy, f_stop_search=f_stop_search, f_filter=f_filter))\n except FreadError:\n break\n \n for i in xrange(max_extension):\n if anyresults:\n break\n try:\n self.extend_loop()\n except FreadError:\n break\n for db in databases:\n self.set_db(db)\n\t \n anyresults |= bool(self.model(f_rank_decoy=f_rank_decoy, f_stop_search=f_stop_search, f_filter=f_filter))\n \n if anyresults:\n break\n \n # Write results to STDOUT or a file, if one was specified\n if summary_file_name:\n if summary_file_name == \"-\":\n self.write_summary(sys.stdout, write_decoy_sequence=write_decoy_sequence)\n else:\n root, ext = os.path.splitext(summary_file_name)\n outstream = open(root + loopsuffix + ext, \"w\")\n self.write_summary(outstream, write_decoy_sequence=write_decoy_sequence)\n outstream.close()\n \n if not self.no_structure_output:\n if write_decoys:\n self.write_decoy_structures(strucdir + loopsuffix, suffix=\".loop.pdb\", idfilter = idfilter)\n if write_models:\n self.write_model_structures(strucdir + loopsuffix, suffix=\".model.pdb\", idfilter = idfilter)\n \n return self.results\n\n\ndef meld(fragScaffold, fragPrediction, invertWeights=False):\n \"\"\"Averages the coordinates of the two Pdb arguments. Move the first object's co-ordinates onto the averaged position.\n \n By default, the first object is assumed to be part of the N-terminal fragment, the second is part of the C-terminal fragment. This can be reversed by setting invertWeights=True.\"\"\"\n \n resS = fragScaffold\n resP = fragPrediction\n L = len(resS)\n \n assert len(resS) == len(resP)\n \n def averageCoord(P, S):\n # P = coordinate of prediction\n # S = coordinate of scaffold\n # D = distance (in residues) from (loop+anchor)-fragment end\n # L = anchor length\n return 1.0/(L+1) * (D*P + (L+1-D)*S)\n \n for i, (rS, rP) in enumerate(zip(resS, resP)):\n if invertWeights:\n D = len(resS)-i\n else:\n D = i+1\n \n newN = averageCoord(rP.N.xyz, rS.N.xyz)\n newCA = averageCoord(rP.CA.xyz, rS.CA.xyz)\n newC = averageCoord(rP.C.xyz, rS.C.xyz)\n \n T_from, T_to, rotmat = ccd.get_rotmat([rS.N.xyz, rS.CA.xyz, rS.C.xyz], [newN, newCA, newC])\n \n rS.O = None # Remove main chain oxygen - can regenerate this later\n \n for a in rS:\n a.xyz = numpy.dot(a.xyz - T_from, rotmat) + T_to\n \n\ndef FREAD_RANKING(decoy):\n \"\"\"Returns a comparable object (a tuple of scores) used for ranking a given decoy.\n \n Bigger values will be ranked higher.\"\"\"\n return (-decoy.anchor_rmsd_open, decoy.score, -decoy.internal_rmsd, -decoy.iterations)\n\n\ndef FREAD_RANKING_BY_ESSS(decoy):\n \"\"\"Returns a comparable object (a tuple of scores) used for ranking a given decoy.\n \n Bigger values will be ranked higher.\"\"\"\n return (decoy.score, -decoy.anchor_rmsd_open, -decoy.internal_rmsd, -decoy.iterations)\n" ]
[ [ "numpy.array", "numpy.dot" ] ]
xudong-sun/mxnet
[ "fc9e70bf2d349ad4c6cb65ff3f0958e23a7410bf" ]
[ "example/reinforcement-learning/a3c/a3c.py" ]
[ "# Licensed to the Apache Software Foundation (ASF) under one\n# or more contributor license agreements. See the NOTICE file\n# distributed with this work for additional information\n# regarding copyright ownership. The ASF licenses this file\n# to you under the Apache License, Version 2.0 (the\n# \"License\"); you may not use this file except in compliance\n# with the License. You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.\n\nfrom __future__ import print_function\nimport mxnet as mx\nimport numpy as np\nimport rl_data\nimport sym\nimport argparse\nimport logging\nimport os\nimport gym\nfrom datetime import datetime\nimport time\nimport sys\ntry:\n from importlib import reload\nexcept ImportError:\n pass\n\nparser = argparse.ArgumentParser(description='Traing A3C with OpenAI Gym')\nparser.add_argument('--test', action='store_true', help='run testing', default=False)\nparser.add_argument('--log-file', type=str, help='the name of log file')\nparser.add_argument('--log-dir', type=str, default=\"./log\", help='directory of the log file')\nparser.add_argument('--model-prefix', type=str, help='the prefix of the model to load')\nparser.add_argument('--save-model-prefix', type=str, help='the prefix of the model to save')\nparser.add_argument('--load-epoch', type=int, help=\"load the model on an epoch using the model-prefix\")\n\nparser.add_argument('--kv-store', type=str, default='device', help='the kvstore type')\nparser.add_argument('--gpus', type=str, help='the gpus will be used, e.g \"0,1,2,3\"')\n\nparser.add_argument('--num-epochs', type=int, default=120, help='the number of training epochs')\nparser.add_argument('--num-examples', type=int, default=1000000, help='the number of training examples')\nparser.add_argument('--batch-size', type=int, default=32)\nparser.add_argument('--input-length', type=int, default=4)\n\nparser.add_argument('--lr', type=float, default=0.0001)\nparser.add_argument('--wd', type=float, default=0)\nparser.add_argument('--t-max', type=int, default=4)\nparser.add_argument('--gamma', type=float, default=0.99)\nparser.add_argument('--beta', type=float, default=0.08)\n\nargs = parser.parse_args()\n\ndef log_config(log_dir=None, log_file=None, prefix=None, rank=0):\n reload(logging)\n head = '%(asctime)-15s Node[' + str(rank) + '] %(message)s'\n if log_dir:\n logging.basicConfig(level=logging.DEBUG, format=head)\n if not os.path.exists(log_dir):\n os.makedirs(log_dir)\n if not log_file:\n log_file = (prefix if prefix else '') + datetime.now().strftime('_%Y_%m_%d-%H_%M.log')\n log_file = log_file.replace('/', '-')\n else:\n log_file = log_file\n log_file_full_name = os.path.join(log_dir, log_file)\n handler = logging.FileHandler(log_file_full_name, mode='w')\n formatter = logging.Formatter(head)\n handler.setFormatter(formatter)\n logging.getLogger().addHandler(handler)\n logging.info('start with arguments %s', args)\n else:\n logging.basicConfig(level=logging.DEBUG, format=head)\n logging.info('start with arguments %s', args)\n\ndef train():\n # kvstore\n kv = mx.kvstore.create(args.kv_store)\n\n model_prefix = args.model_prefix\n if model_prefix is not None:\n model_prefix += \"-%d\" % (kv.rank)\n save_model_prefix = args.save_model_prefix\n if save_model_prefix is None:\n save_model_prefix = model_prefix\n\n log_config(args.log_dir, args.log_file, save_model_prefix, kv.rank)\n\n devs = mx.cpu() if args.gpus is None else [\n mx.gpu(int(i)) for i in args.gpus.split(',')]\n\n epoch_size = args.num_examples / args.batch_size\n\n if args.kv_store == 'dist_sync':\n epoch_size /= kv.num_workers\n\n # disable kvstore for single device\n if 'local' in kv.type and (\n args.gpus is None or len(args.gpus.split(',')) is 1):\n kv = None\n\n # module\n dataiter = rl_data.GymDataIter('Breakout-v0', args.batch_size, args.input_length, web_viz=True)\n net = sym.get_symbol_atari(dataiter.act_dim)\n module = mx.mod.Module(net, data_names=[d[0] for d in dataiter.provide_data], label_names=('policy_label', 'value_label'), context=devs)\n module.bind(data_shapes=dataiter.provide_data,\n label_shapes=[('policy_label', (args.batch_size,)), ('value_label', (args.batch_size, 1))],\n grad_req='add')\n\n # load model\n\n if args.load_epoch is not None:\n assert model_prefix is not None\n _, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, args.load_epoch)\n else:\n arg_params = aux_params = None\n\n # save model\n checkpoint = None if save_model_prefix is None else mx.callback.do_checkpoint(save_model_prefix)\n\n init = mx.init.Mixed(['fc_value_weight|fc_policy_weight', '.*'],\n [mx.init.Uniform(0.001), mx.init.Xavier(rnd_type='gaussian', factor_type=\"in\", magnitude=2)])\n module.init_params(initializer=init,\n arg_params=arg_params, aux_params=aux_params)\n\n # optimizer\n module.init_optimizer(kvstore=kv, optimizer='adam',\n optimizer_params={'learning_rate': args.lr, 'wd': args.wd, 'epsilon': 1e-3})\n\n # logging\n np.set_printoptions(precision=3, suppress=True)\n\n T = 0\n dataiter.reset()\n score = np.zeros((args.batch_size, 1))\n final_score = np.zeros((args.batch_size, 1))\n for epoch in range(args.num_epochs):\n if save_model_prefix:\n module.save_params('%s-%04d.params'%(save_model_prefix, epoch))\n\n\n for _ in range(int(epoch_size/args.t_max)):\n tic = time.time()\n # clear gradients\n for exe in module._exec_group.grad_arrays:\n for g in exe:\n g[:] = 0\n\n S, A, V, r, D = [], [], [], [], []\n for t in range(args.t_max + 1):\n data = dataiter.data()\n module.forward(mx.io.DataBatch(data=data, label=None), is_train=False)\n act, _, val = module.get_outputs()\n V.append(val.asnumpy())\n if t < args.t_max:\n act = act.asnumpy()\n act = [np.random.choice(dataiter.act_dim, p=act[i]) for i in range(act.shape[0])]\n reward, done = dataiter.act(act)\n S.append(data)\n A.append(act)\n r.append(reward.reshape((-1, 1)))\n D.append(done.reshape((-1, 1)))\n\n err = 0\n R = V[args.t_max]\n for i in reversed(range(args.t_max)):\n R = r[i] + args.gamma * (1 - D[i]) * R\n adv = np.tile(R - V[i], (1, dataiter.act_dim))\n\n batch = mx.io.DataBatch(data=S[i], label=[mx.nd.array(A[i]), mx.nd.array(R)])\n module.forward(batch, is_train=True)\n\n pi = module.get_outputs()[1]\n h = -args.beta*(mx.nd.log(pi+1e-7)*pi)\n out_acts = np.amax(pi.asnumpy(), 1)\n out_acts=np.reshape(out_acts,(-1,1))\n out_acts_tile=np.tile(-np.log(out_acts + 1e-7),(1, dataiter.act_dim))\n module.backward([mx.nd.array(out_acts_tile*adv), h])\n\n print('pi', pi[0].asnumpy())\n print('h', h[0].asnumpy())\n err += (adv**2).mean()\n score += r[i]\n final_score *= (1-D[i])\n final_score += score * D[i]\n score *= 1-D[i]\n T += D[i].sum()\n\n module.update()\n logging.info('fps: %f err: %f score: %f final: %f T: %f'%(args.batch_size/(time.time()-tic), err/args.t_max, score.mean(), final_score.mean(), T))\n print(score.squeeze())\n print(final_score.squeeze())\n\ndef test():\n log_config()\n\n devs = mx.cpu() if args.gpus is None else [\n mx.gpu(int(i)) for i in args.gpus.split(',')]\n\n # module\n dataiter = robo_data.RobosimsDataIter('scenes', args.batch_size, args.input_length, web_viz=True)\n print(dataiter.provide_data)\n net = sym.get_symbol_thor(dataiter.act_dim)\n module = mx.mod.Module(net, data_names=[d[0] for d in dataiter.provide_data], label_names=('policy_label', 'value_label'), context=devs)\n module.bind(data_shapes=dataiter.provide_data,\n label_shapes=[('policy_label', (args.batch_size,)), ('value_label', (args.batch_size, 1))],\n for_training=False)\n\n # load model\n assert args.load_epoch is not None\n assert args.model_prefix is not None\n module.load_params('%s-%04d.params'%(args.model_prefix, args.load_epoch))\n\n N = args.num_epochs * args.num_examples / args.batch_size\n\n R = 0\n T = 1e-20\n score = np.zeros((args.batch_size,))\n for t in range(N):\n dataiter.clear_history()\n data = dataiter.next()\n module.forward(data, is_train=False)\n act = module.get_outputs()[0].asnumpy()\n act = [np.random.choice(dataiter.act_dim, p=act[i]) for i in range(act.shape[0])]\n dataiter.act(act)\n time.sleep(0.05)\n _, reward, _, done = dataiter.history[0]\n T += done.sum()\n score += reward\n R += (done*score).sum()\n score *= (1-done)\n\n if t % 100 == 0:\n logging.info('n %d score: %f T: %f'%(t, R/T, T))\n\n\nif __name__ == '__main__':\n if args.test:\n test()\n else:\n train()\n\n\n" ]
[ [ "numpy.tile", "numpy.zeros", "numpy.reshape", "numpy.set_printoptions", "numpy.random.choice", "numpy.log" ] ]
guoshuhong/Classification-networks
[ "0ca20964e64cf9001e2a770b2b44d88dcca04775" ]
[ "net/layers.py" ]
[ "from tensorflow import keras\nimport tensorflow as tf\n\n\nclass BatchNormalization(keras.layers.BatchNormalization):\n \"\"\"\n Identical to keras.layers.BatchNormalization, but adds the option to freeze parameters.\n \"\"\"\n def __init__(self, freeze, *args, **kwargs):\n self.freeze = freeze\n super(BatchNormalization, self).__init__(*args, **kwargs)\n\n # set to non-trainable if freeze is true\n self.trainable = not self.freeze\n\n def call(self, inputs, training=None, **kwargs):\n # return super.call, but set training\n if not training:\n return super(BatchNormalization, self).call(inputs, training=False)\n else:\n return super(BatchNormalization, self).call(inputs, training=(not self.freeze))\n\n def get_config(self):\n config = super(BatchNormalization, self).get_config()\n config.update({'freeze': self.freeze})\n return config\n\n\nclass wBiFPNAdd(keras.layers.Layer):\n def __init__(self, epsilon=1e-4, **kwargs):\n super(wBiFPNAdd, self).__init__(**kwargs)\n self.epsilon = epsilon\n\n def build(self, input_shape):\n num_in = len(input_shape)\n self.w = self.add_weight(name=self.name,\n shape=(num_in,),\n initializer=keras.initializers.constant(1 / num_in),\n trainable=True,\n dtype=tf.float32)\n\n def call(self, inputs, **kwargs):\n w = keras.activations.relu(self.w)\n x = tf.reduce_sum([w[i] * inputs[i] for i in range(len(inputs))], axis=0)\n x = x / (tf.reduce_sum(w) + self.epsilon)\n return x\n\n def compute_output_shape(self, input_shape):\n return input_shape[0]\n\n def get_config(self):\n config = super(wBiFPNAdd, self).get_config()\n config.update({\n 'epsilon': self.epsilon\n })\n return config\n" ]
[ [ "tensorflow.reduce_sum", "tensorflow.keras.initializers.constant", "tensorflow.keras.activations.relu" ] ]
aist9/autoencoder
[ "b3b1ccbfff61883db06a6ca4506ca890eff08568" ]
[ "vae_torch/sample.py" ]
[ "# VAEのサンプルコード, MNIST使用\r\n\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nif __name__ == '__main__':\r\n import sys\r\n \r\n # コマンドライン引数を読み込み\r\n # 引数が'-1'なら学習しない\r\n args = sys.argv\r\n train_mode = ['train', 'retrain', 'load']\r\n mode = 0 if len(args) < 2 else int(args[1])\r\n train_mode = train_mode[mode]\r\n \r\n # torchのMNISTの呼び出し方がよくわからんかったのでchainerで代用\r\n # MNISTデータの読み込み\r\n import chainer\r\n train, test = chainer.datasets.get_mnist()\r\n # データとラベルに分割\r\n train_data, train_label = train._datasets\r\n test_data, test_label = test._datasets\r\n\r\n # 標準化\r\n # tr_std = train_data.std()\r\n # tr_avg = train_data.mean()\r\n # train_data = (train_data - tr_avg) / tr_std\r\n # test_data = (test_data - tr_avg) / tr_std\r\n\r\n # 学習条件\r\n # エポックとミニバッチサイズ\r\n epoch = 100\r\n batchsize = 128\r\n # 隠れ層のユニット数\r\n hidden = [128,2]\r\n\r\n act_func = 'tanh' # 活性化関数\r\n out_func = 'sigmoid' # 出力層の活性化関数 (デコーダの出力層, 無印版は必ずsigmoid)\r\n use_BN = True # Batch Normalization を使うか否か\r\n\r\n fd = './model/torch/'\r\n\r\n # modelのセットアップ\r\n from variational_autoencoder import VAE\r\n vae = VAE( int(train_data.shape[1]) ,hidden, act_func=act_func, out_func=out_func ,use_BN=use_BN, folder=fd, device='cuda', is_gauss_dist=True)\r\n\r\n # ed版はこちら. 無印版とは名前が違うだけ.\r\n # from variational_autoencoder_ed import VAE_ED\r\n # vae = VAE_ED( int(train_data.shape[1]) ,hidden, act_func=act_func, out_func=out_func ,use_BN=True, folder=fd, device='cuda', is_gauss_dist=True)\r\n\r\n # VAEの学習\r\n if train_mode == 'train':\r\n # vae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=None)\r\n vae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=test_data, is_plot_weight=True)\r\n if train_mode == 'retrain':\r\n # もしかしたらoptimizerも保存・ロードしたほうがいいかもしれない\r\n vae.load_model()\r\n vae.train(train_data, epoch, batchsize,C=1.0, k=1, valid=None)\r\n else:\r\n vae.load_model()\r\n\r\n # 評価モードに切り替え. batch normalizationの無効化などに必須\r\n vae.model_to_eval()\r\n\r\n\r\n\r\n\r\n # テストデータを絞る, なおこのコードではテストデータを見ていない\r\n # test_data = test_data[:20]\r\n # test_label = test_label[:20]\r\n\r\n # 再構成\r\n feat_train, reconst_train, err_train = vae.reconst(train_data)\r\n feat_test, reconst_test, err_test = vae.reconst(test_data)\r\n\r\n # 再構成データとオリジナルを1次元で比較\r\n plt.plot(reconst_train[0])\r\n plt.plot(train_data[0])\r\n plt.show()\r\n \r\n # 潜在空間の可視化\r\n col = ['r','g','b','c','m','y','orange','black','gray','violet']\r\n for i in range(3000):\r\n plt.scatter(x=feat_train[i,0],y=feat_train[i,1],marker='.',color=col[train_label[i]])\r\n plt.show()\r\n\r\n\r\n # 自作の潜在空間から出力を画像を確認. VAEで調べるとよく見る数字がシームレスに変化するやつ.\r\n split_num = 20 # 分割数. 出力画像のサイズは split_num*29 × split_num*29. (MNISTの縦横28+分割線1).\r\n rn = np.linspace(-3,3,split_num)\r\n x,y = np.meshgrid(rn, rn)\r\n dt = np.hstack( [x.reshape(-1,1), y.reshape(-1,1)] )\r\n # 変換するメソッド\r\n imgs = vae.featuremap_to_image(dt)\r\n plot_image = np.ones( (split_num*29, split_num*29) )\r\n for i in range(split_num):\r\n for j in range(split_num):\r\n plot_image[i*28+i:-~i*28+i,j*28+j:-~j*28+j] = imgs[i*split_num+j].reshape(28,28)\r\n \r\n plt.imshow(plot_image,cmap='gray', vmax=1, vmin=0)\r\n plt.show()\r\n \r\n\r\n\r\n\r\n" ]
[ [ "numpy.ones", "matplotlib.pyplot.imshow", "matplotlib.pyplot.show", "matplotlib.pyplot.plot", "numpy.meshgrid", "numpy.linspace", "matplotlib.pyplot.scatter" ] ]
mk43/machine-learning
[ "1ca1baf797fe6f593a88ad4e0d7ac7e5c24ce139" ]
[ "algorithm/neural-network/XOR.py" ]
[ "# coding: utf-8\n\nimport numpy as np\n\nx1 = np.asarray([0, 0, 1, 1])\nx2 = np.asarray([0, 1, 0, 1])\nX = np.row_stack((np.ones(shape=(1, 4)), x1, x2))\nprint(\"X:\\n%s\" % X)\ny = np.asarray([0, 1, 1, 0])\nW1 = np.asarray([[-1, 2, -2],\n [-1, -2, 2]])\nW2 = np.asarray([-1, 2, 2])\n\n\ndef sigmoid(input):\n return 1 / (1 + np.power(np.e, -10 * (input)))\n\n\nnp.set_printoptions(precision=6, suppress=True)\nz1 = np.matmul(W1, X)\nprint(\"W1*X = z1:\\n%s\" % z1)\na1 = np.row_stack((np.ones(shape=(1, 4)), sigmoid(z1)))\nprint(\"sigmoid(z1) = a1:\\n%s\" % a1)\nz2 = np.matmul(W2, a1)\nprint(\"W2*a1 = z2:\\n%s\" % z2)\na2 = sigmoid(z2)\nprint(\"------------------------\")\nprint(\"prediction: %s\" % a2)\nprint(\"target: %s\" % y)\nprint(\"------------------------\")\n\n# output:\n# X:\n# [[1. 1. 1. 1.]\n# [0. 0. 1. 1.]\n# [0. 1. 0. 1.]]\n# W1*X = z1:\n# [[-1. -3. 1. -1.]\n# [-1. 1. -3. -1.]]\n# sigmoid(z1) = a1:\n# [[1. 1. 1. 1. ]\n# [0.000045 0. 0.999955 0.000045]\n# [0.000045 0.999955 0. 0.000045]]\n# W2*a1 = z2:\n# [-0.999818 0.999909 0.999909 -0.999818]\n# ------------------------\n# prediction: [0.000045 0.999955 0.999955 0.000045]\n# target: [0 1 1 0]\n# ------------------------\n" ]
[ [ "numpy.ones", "numpy.matmul", "numpy.set_printoptions", "numpy.asarray", "numpy.power" ] ]
smartcommunitylab/sco.mobilitycovid
[ "a4af6b3b2d14208d638894a94b2c673397bf77fd" ]
[ "scripts/stop_user_clusters-v6.py" ]
[ "from __future__ import print_function\nfrom sklearn.cluster import DBSCAN\n\nimport argparse\nimport hashlib\nimport os\nimport time\n\nfrom datetime import date, datetime, timedelta\nfrom functools import reduce\nfrom math import degrees\n\nfrom concurrent.futures import ThreadPoolExecutor\nimport concurrent.futures\n\nfrom azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlobBlock\nfrom azure.core.exceptions import ResourceNotFoundError\n\nfrom pyspark import SparkContext, SparkConf, SQLContext\nfrom pyspark.sql import DataFrame, SparkSession\nimport pyspark.sql.functions as F\nfrom pyspark.sql.functions import col, udf\nfrom pyspark.sql.types import *\nfrom pyspark.sql.window import Window\nfrom pyspark import StorageLevel\nfrom pyspark.sql.functions import lag, pandas_udf, PandasUDFType\n\n# import logging\n\nVERSION = 6\n\n#\n# Driver settings\n#\nSHUFFLE_PARTITIONS = 32\nOUT_PARTITIONS = 2\nCORES = \"4\"\nRAM = \"12g\"\nAPP_NAME = \"StopUserClusters\"\n\n# always set overwrite\nWRITE_MODE = \"overwrite\"\nSKIP_EXISTING = False\nTHREADS = 32\n\n\n# templates\nTABLE_PATH = \"wasbs://{}@{}.blob.core.windows.net/{}/\"\nCONN_STRING = \"BlobEndpoint=https://{}.blob.core.windows.net/;SharedAccessSignature={}\"\n\n# need leading slash\nLOCAL_PATH = \"./table/\"\n\n\n#\n# Stop locations parameters\n#\nEVENTS_ROAM_DIST = 70 # meters\nSTOPS_ROAM_DIST = 65\nEARTH_RADIUS = 6372.795 * 1000\nMIN_STAY = 5\n\nUS_STATES = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS',\n 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY']\n\n\ndef read_multiple_df(spark, paths, format=\"parquet\"):\n dfs = None\n dfs_array = []\n for path in paths:\n dfs_load = spark.read.format(format).load(path)\n dfs_array.append(dfs_load)\n dfs = reduce(DataFrame.unionAll, dfs_array)\n return dfs\n\n\n#\n# Stop location lib\n#\n\n\ndef add_distance_column(dfs, order_column='timestamp'):\n # Radians lat/lon\n dfs = dfs.withColumn('latitude2', F.radians('latitude')).withColumn(\n 'longitude2', F.radians('longitude'))\n\n # Groups GPS locations into chucks. A chunk is formed by groups of points that are distant no more than roam_dist\n w = Window.partitionBy(['userID']).orderBy(order_column)\n dfs = dfs.withColumn('next_lat', F.lead('latitude2', 1).over(w))\n dfs = dfs.withColumn('next_lon', F.lead('longitude2', 1).over(w))\n\n # Haversine distance\n dfs = dfs.withColumn('distance_next', EARTH_RADIUS * 2 * F.asin(F.sqrt(\n F.pow(F.sin((col('next_lat') - col('latitude2')) / 2.0), 2) + F.cos('latitude2') * F.cos('next_lat') * F.pow(\n F.sin((col('next_lon') - col('longitude2')) / 2.0), 2))))\n dfs = dfs.withColumn('distance_prev', F.lag('distance_next', default=0).over(w)).drop(\n 'latitude2').drop('longitude2').drop('next_lon').drop('next_lat').drop('distance_next')\n return dfs\n\n\ndef get_destinations(dfs, roam_dist=110, earth_radius=6372.795 * 1000):\n \"\"\"\n Applies DBSCAN to extract the unique stop locations from a pyspark DataFrame\n\n :param x: DataFrame with ['id_client', 'latitude', 'longitude', \"from\", \"to\"]. Coordinates are in degrees.\n :param roam_dist: The stop location size in meters.\n :param earth_radius: The radius of the earth.\n :param group_results: If True, it groups by the cluster's location and id_client.\n :return: (pyspark DataFrame) If group_results=True: ['id_client', 'clatitude', 'clongitude', 'time_spent', 'frequency']\n (pyspark DataFrame) If group_results=False: ['id_client', 'latitude', 'longitude', 'clatitude', 'clongitude', 'from', 'to']\n \"\"\"\n\n @pandas_udf(\"userId string, state string, latitude double, longitude double, begin timestamp, end timestamp, clusterId integer\", PandasUDFType.GROUPED_MAP)\n def get_destinations(df):\n \"\"\"\n Applies DBSCAN to stop locations\n\n :param x: 2D numpy array with latitude and longitude.\n :param from_to_array: 2D numpy array with from and to timestamps.\n :param roam_dist: The stop location size in meters.\n :param earth_radius: The radius of the earth.\n :return: (pandas DataFrame) ['latitude', 'longitude', 'clatitude', 'clongitude', 'from', 'to', 'time_spent']\n \"\"\"\n db = DBSCAN(eps=roam_dist/earth_radius, min_samples=1,\n algorithm='ball_tree', metric='haversine')\n df[\"clusterId\"] = db.fit_predict(df[['latitude', 'longitude']])\n\n return df\n\n dfs = dfs.withColumn('latitude', F.radians('latitude'))\n dfs = dfs.withColumn('longitude', F.radians('longitude'))\n\n stops_dfs = dfs.groupby('userId', 'state').apply(get_destinations)\n\n stops_dfs = stops_dfs.withColumn('latitude', F.degrees('latitude'))\n stops_dfs = stops_dfs.withColumn('longitude', F.degrees('longitude'))\n\n w = Window().partitionBy('userId', 'clusterId')\n\n stops_dfs = stops_dfs.withColumn(\n 'clusterLatitude', F.mean('latitude').over(w))\n stops_dfs = stops_dfs.withColumn(\n 'clusterLongitude', F.mean('longitude').over(w))\n\n stops_dfs = stops_dfs.drop('latitude').drop('longitude')\n\n return stops_dfs\n\n#\n# Spark\n#\n\n\ndef getSparkConfig(cores, ram, partitions, azure_accounts, azure_oauth):\n # Setting enviroment variables and various drivers\n # \"org.apache.hadoop:hadoop-azure:2.10.0\" driver Azure\n # \"io.delta:delta-core_2.12:0.7.0\" driver Delta-lake\n # \"spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension\" configuration Delta\n # \"spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog\" configuration Delta\n # \"spark.delta.logStore.class=org.apache.spark.sql.delta.storage.AzureLogStore\" configuration Delta\n\n # Set spark environments\n os.environ['PYSPARK_PYTHON'] = '/usr/bin/python3'\n os.environ['PYSPARK_DRIVER_PYTHON'] = '/usr/bin/python3'\n # os.environ[\"PYSPARK_SUBMIT_ARGS\"] = \"\"\"--packages \"org.apache.hadoop:hadoop-azure:3.2.1\" pyspark-shell\"\"\"\n os.environ[\"PYSPARK_SUBMIT_ARGS\"] = \"\"\"--packages \"org.apache.hadoop:hadoop-azure:2.10.0\" --jars \"/mnt/batch/tasks/shared/sco-mobilitycovid-udf_2.11-1.0.jar\",\"/mnt/batch/tasks/shared/geo-0.7.7.jar\" pyspark-shell\"\"\"\n conf = (\n SparkConf()\n\n # SQL\n .set(\"spark.sql.shuffle.partitions\", partitions)\n .set(\"spark.sql.csv.filterPushdown.enabled\", \"false\")\n\n # Driver + memory\n .set(\"spark.driver.cores\", cores)\n .set(\"spark.shuffle.file.buffer\", \"1m\")\n # .set(\"spark.memory.offHeap.enabled\",\"true\")\n # .set(\"spark.memory.offHeap.size\",\"3g\")\n .set(\"spark.memory.fraction\", \"0.8\")\n .set(\"spark.memory.storageFraction\", \"0.2\")\n .set(\"spark.io.compression.lz4.blockSize\", \"128k\")\n .set(\"spark.driver.maxResultSize\", \"0\")\n .set(\"spark.driver.memory\", ram)\n\n # Local storage for spilling & storing temp files\n .set(\"spark.local.dir\", \"/mnt/batch/tasks/shared\")\n\n # Set master local\n .setMaster(\"local[*]\")\n\n # App name\n .setAppName(APP_NAME)\n )\n\n # Azure (Keys, Filesystem WASBS)\n conf.set(\"spark.hadoop.fs.wasbs.impl\",\n \"org.apache.hadoop.fs.azure.NativeAzureFileSystem\")\n\n for account in azure_accounts:\n conf.set(\"fs.azure.sas.{}.{}.blob.core.windows.net\".format(account['container'], account['storage']),\n account['sas'])\n\n if azure_oauth:\n conf.set(\"spark.hadoop.fs.azure.account.auth.type\", \"OAuth\")\n conf.set(\"spark.hadoop.fs.azure.account.oauth.provider.type\",\n \"org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider\")\n conf.set(\"spark.hadoop.fs.azure.account.oauth2.client.id\",\n azure_oauth['client-id'])\n conf.set(\"spark.hadoop.fs.azure.account.oauth2.client.secret\",\n azure_oauth['client-secret'])\n conf.set(\"spark.hadoop.fs.azure.account.oauth2.client.endpoint\",\n azure_oauth['endpoint'])\n return conf\n\n#\n# Utils\n#\n\n\ndef enumerate_prefixes(start=0, end=256):\n for i in range(start, end):\n yield '{:02x}'.format(i)\n\n\ndef upload_blob(blob_service_client, container_out, blob_key, file_path):\n blob_client = blob_service_client.get_blob_client(\n container_out, blob_key)\n\n with open(file_path, \"rb\") as data:\n blob_client.upload_blob(data, overwrite=True)\n\n # cleanup\n os.remove(file_path)\n\n return blob_key\n\n#\n# Argparser\n#\n\n\ndef get_args():\n \"\"\"Parse command line arguments.\"\"\"\n\n parser = argparse.ArgumentParser(description=\"Cuebiq data processor\")\n requiredNamed = parser.add_argument_group('required arguments')\n requiredNamed.add_argument(\n \"--storage\", type=str, required=True, help=\"Azure storage\")\n requiredNamed.add_argument(\n \"--sas\", type=str, required=True, help=\"SAS token\")\n requiredNamed.add_argument(\n \"--oauth-login\", type=str, required=True, help=\"Oauth login\")\n requiredNamed.add_argument(\n \"--oauth-client-id\", type=str, required=True, help=\"Oauth client id\")\n requiredNamed.add_argument(\n \"--oauth-client-secret\", type=str, required=True, help=\"Oauth client secret\")\n requiredNamed.add_argument(\n \"--container-in\", type=str, required=True, help=\"Input container\")\n requiredNamed.add_argument(\n \"--container-out\", type=str, required=True, help=\"Output container\")\n requiredNamed.add_argument(\"--country\", type=str,\n help=\"Country. Options: 'US','IT'\")\n requiredNamed.add_argument(\"--prefix\", type=str, help=\"User prefix\")\n\n # optional\n parser.add_argument(\"--vm-cores\", default=CORES,\n type=str, help=\"Azure VM cores\")\n parser.add_argument(\"--vm-ram\", default=RAM,\n type=str, help=\"Azure VM ram\")\n parser.add_argument(\"--shuffle-partitions\", default=SHUFFLE_PARTITIONS,\n type=int, help=\"Spark shuffle partitions\")\n parser.add_argument(\"--roam-dist-stops\", type=int,\n default=STOPS_ROAM_DIST, help=\"Roam dist stops\")\n parser.add_argument(\"--roam-dist-events\", type=int,\n default=EVENTS_ROAM_DIST, help=\"Roam dist events\")\n parsed_args = parser.parse_args()\n\n return parsed_args\n\n\n#\n# Main function\n#\ndef main():\n \"\"\"Main function\"\"\"\n\n # Get args\n args = get_args()\n\n # container\n container_in = args.container_in\n container_out = args.container_out\n\n # Azure credentials\n sas_token = args.sas\n storage_account_name = args.storage\n azure_accounts = list()\n azure_accounts.append({\n \"storage\": storage_account_name,\n \"sas\": sas_token,\n \"container\": container_in\n })\n azure_accounts.append({\n \"storage\": storage_account_name,\n \"sas\": sas_token,\n \"container\": container_out\n })\n\n oauth_login = args.oauth_login\n oauth_client_id = args.oauth_client_id\n oauth_client_secret = args.oauth_client_secret\n\n # requires hadoop 3.2+\n # azure_oauth = {\n # \"endpoint\": oauth_login,\n # \"client-id\": oauth_client_id,\n # \"client-secret\": oauth_client_secret\n # }\n azure_oauth = False\n\n # VM\n cores = args.vm_cores\n ram = args.vm_ram\n shuffle_partitions = args.shuffle_partitions\n\n # Date, prefix\n country = args.country\n prefix = args.prefix\n\n # process config\n roam_dist_stops = args.roam_dist_stops\n roam_dist_events = args.roam_dist_events\n\n # Path in - path out\n blob_in = f\"wasbs://{container_in}@{storage_account_name}.blob.core.windows.net/stoplocation-v8_prefix_r70-s5-a70-h6/{country}/\"\n timezones_in = f\"wasbs://cuebiq-data@{storage_account_name}.blob.core.windows.net/utils_states_timezones/\"\n if azure_oauth:\n # we can leverage abfss\n blob_in = f\"abfss://{container_in}@{storage_account_name}.dfs.core.windows.net/stoplocation-v8_prefix_r70-s5-a70-h6/country={country}/\"\n timezones_in = f\"abfss://cuebiq-data@{storage_account_name}.dfs.core.windows.net/utils_states_timezones/\"\n\n path_out_distinct = f\"distinct_user_clusters-v8_r70-s5-a70-h6_clustered_{roam_dist_stops}m_v{VERSION}/country={country}\"\n path_out_all = f\"all_user_clusters-v8_r70-s5-a70-h6_clustered_{roam_dist_stops}m_v{VERSION}/country={country}\"\n\n # config spark\n conf = getSparkConfig(cores, ram, shuffle_partitions,\n azure_accounts, azure_oauth)\n\n # set prop for handling partition columns as strings (fixes prefixes as int)\n conf.set(\"spark.sql.sources.partitionColumnTypeInference.enabled\", \"false\")\n\n # Create spark session\n sc = SparkContext(conf=conf).getOrCreate()\n sqlContext = SQLContext(sc)\n spark = sqlContext.sparkSession\n # register UDF from jar\n spark.udf.registerJavaFunction(\n \"geohash\", \"it.smartcommunitylab.sco.mobilitycovid.udf.GeohashEncode\")\n\n # Init azure client\n blob_service_client = BlobServiceClient.from_connection_string(\n CONN_STRING.format(storage_account_name, sas_token))\n\n # build keys, date is mandatory, prefix opt\n partition_key = f\"prefix={prefix}\"\n\n print(\"process \"+partition_key)\n start_time = time.time()\n local_dir = LOCAL_PATH+partition_key\n print(\"write temp to \"+local_dir)\n\n # cleanup local if exists\n if (os.path.isdir(local_dir)):\n map(os.unlink, (os.path.join(local_dir, f)\n for f in os.listdir(local_dir)))\n\n # Input dataset\n print(\"read dataset table\")\n read_time = time.time()\n\n # explode days manually\n dates = [\n datetime(2020, 1, 1) + timedelta(days=x) for x in range(0, 258)]\n blobs_in = [\"{}/year={}/month={}/day={}/prefix={}\".format(\n blob_in, d.year, d.month, d.day, prefix) for d in dates]\n\n #dfs = spark.read.format(\"parquet\").load(*blobs_in)\n dfs = read_multiple_df(spark, blobs_in)\n dfs_timezones = spark.read.format(\"parquet\").load(timezones_in)\n\n # manually inject prefix column\n dfs = dfs.withColumn(\"prefix\", F.lit(prefix))\n\n # apply partition filter\n dfs_state = dfs.where(f\"prefix = '{prefix}'\")\n\n print(\"processing with spark\")\n spark_time = time.time()\n\n w = Window().partitionBy('userId').orderBy('begin')\n\n dfs_state = add_distance_column(dfs_state, order_column='begin')\n dfs_state = dfs_state.fillna(0, subset=['next_travelled_distance'])\n dfs_state = dfs_state.withColumn('lag_next_travelled_distance', F.lag(\n col('next_travelled_distance')).over(w))\n dfs_state = dfs_state.withColumn('lag_end', F.lag('end').over(w))\n dfs_state = dfs_state.withColumn('rn', F.when(((col('lag_next_travelled_distance') != col('prev_travelled_distance')) |\n (col('prev_travelled_distance') > 0) |\n (col('lag_next_travelled_distance') > 0) |\n (col('distance_prev') > roam_dist_events) |\n ((F.dayofyear(col('begin')) - F.dayofyear(col('lag_end')) == 1) &\n (F.hour(col('begin')) < 6))\n ) &\n ((col('lag_end').isNull()) | (col('lag_end') < col('begin'))), 1).otherwise(0))\n # Remove prev_travelled distance when rn == 0 (it happens when lag_end and begin overlap)\n dfs_state = dfs_state.withColumn('prev_travelled_distance', F.when(\n col('rn') == 0, 0).otherwise(col('prev_travelled_distance')))\n\n w = Window().partitionBy('userId').orderBy(\n 'begin').rangeBetween(Window.unboundedPreceding, 0)\n\n dfs_state = dfs_state.withColumn('group', F.sum('rn').over(w))\n\n dfs_state = dfs_state.groupBy('userId', 'group').agg(F.mean('latitude').alias('latitude'),\n F.mean('longitude').alias(\n 'longitude'),\n F.min('begin').alias(\n 'begin'),\n F.max('end').alias(\n 'end'),\n F.first('state').alias('state')).drop('group')\n\n # Bug fix: due to the processing we do in the stop events, where we process stops every two days,\n # sometimes stop events overlap but they do not get merged until here. The error is RARE. Here we fix it\n #\n # We divide the two stops making MIN_STAY space between the two, if we can.\n w = Window().partitionBy('userId').orderBy('begin')\n dfs_state = dfs_state.withColumn('next_begin', F.lead('begin').over(w))\n dfs_state = dfs_state.withColumn('next_end', F.lead('end').over(w))\n dfs_state = dfs_state.withColumn('end', F.when(\n (col('next_begin').cast('long') - col('begin').cast('long') > 2 * MIN_STAY * 60) &\n (col('next_begin') < col('end')),\n col('next_begin') - F.expr(\"INTERVAL {} SECONDS\".format(MIN_STAY * 60))\n ).otherwise(col('end')))\n dfs_state = dfs_state.drop('next_begin', 'next_end')\n\n dfs_destinations = get_destinations(dfs_state, roam_dist=roam_dist_stops)\n dfs_destinations = dfs_destinations.withColumn(\n 'prefix', dfs_destinations.userId.substr(1, 2))\n dfs_destinations = dfs_destinations.withColumn(\n 'dayofyear', F.dayofyear('begin'))\n dfs_destinations = dfs_destinations.withColumn('year', F.year('begin'))\n # dfs_destinations = dfs_destinations.withColumn('state', F.lit(state))\n\n # Local time\n dfs_destinations.createOrReplaceTempView(\"dfs_destinations\")\n dfs_destinations = spark.sql(\"\"\"\n SELECT dfs_destinations.*, geohash(clusterLatitude, clusterLongitude, 7) as geohash7\n from dfs_destinations\n \"\"\")\n dfs_destinations = dfs_destinations.withColumn(\n 'geohash5', F.substring(col('geohash7'), 1, 5))\n dfs_destinations = dfs_destinations.join(\n F.broadcast(dfs_timezones), on='geohash5').drop('geohash5')\n dfs_destinations = dfs_destinations.withColumn(\n 'local_begin', F.from_utc_timestamp(col('begin'), col('tzid')))\n dfs_destinations = dfs_destinations.withColumn('offset', (\n (col('local_begin').cast('long') - col('begin').cast('long')) / 3600).cast('int')).drop('local_begin')\n dfs_destinations.persist(StorageLevel.DISK_ONLY)\n\n # Write\n # output as country/prefix/part1..N\n local_dir_all = local_dir + \"/all/\"\n dfs_destinations_all = dfs_destinations.select(\n 'prefix', 'userId', 'clusterId', 'begin', 'end', 'offset', 'year', 'dayofyear')\n dfs_destinations_all.repartition(8, 'dayofyear').write.format('parquet').mode(\n 'overwrite').save(local_dir_all+\"prefix=\"+prefix+\"/\")\n\n # output as country/prefix/state\n local_dir_distinct = local_dir+\"/distinct/\"\n dfs_destinations_distinct = dfs_destinations.select(\n 'prefix', 'userId', 'clusterId', 'clusterLatitude', 'clusterLongitude', 'geohash7', 'state')\n dfs_destinations_distinct = dfs_destinations_distinct.drop_duplicates([\n 'prefix', 'userId', 'clusterId', 'clusterLatitude', 'clusterLongitude', 'geohash7'])\n dfs_destinations_distinct.repartition(\"state\").write.partitionBy(\n \"state\").format('parquet').mode('overwrite').save(local_dir_distinct+\"prefix=\"+prefix+\"/\")\n\n dfs_destinations.unpersist()\n\n print(\"upload local data to azure\")\n upload_time = time.time()\n\n # upload parts 1 \"prefix/state\"\n print(f\"upload files for distinct\")\n # upload with threads\n dfutures = []\n with ThreadPoolExecutor(max_workers=THREADS) as executor:\n fprefix = prefix\n print(f\"upload files for distinct: {fprefix}\")\n prefix_dir = local_dir_distinct+\"prefix=\"+fprefix\n prefix_key = f\"prefix={fprefix}\"\n\n for state in US_STATES:\n s_key = f\"state={state}\"\n f_dir = prefix_dir + \"/\"+s_key\n f_key = prefix_key + \"/\"+s_key\n\n # print(f\"read files for distinct from {f_dir}\")\n\n if (os.path.isdir(f_dir)):\n files = [filename for filename in os.listdir(\n f_dir) if filename.startswith(\"part-\")]\n\n if len(files) > 0:\n\n for file_local in files:\n file_path = f_dir+\"/\"+file_local\n part_num = int(file_local.split('-')[1])\n part_key = '{:05d}'.format(part_num)\n # fix name as static hash to be reproducible\n filename_hash = hashlib.sha1(\n str.encode(f_key+f_key+part_key)).hexdigest()\n\n blob_key = \"{}/{}/part-{}-{}.snappy.parquet\".format(\n path_out_distinct, f_key, part_key, filename_hash)\n\n # print(\"upload \" + file_path + \" to \" + container_out+\":\"+blob_key)\n # upload_blob(blob_service_client,container_out, blob_key, file_path)\n future = executor.submit(\n upload_blob, blob_service_client, container_out, blob_key, file_path)\n dfutures.append(future)\n\n # else:\n # print(f\"no files to upload for {f_key}\")\n\n # else:\n # print(f\"missing partition for {f_key}\")\n\n # end of loop, wait for futures\n for future in dfutures:\n bkey = future.result()\n\n # ensure we wait all tasks\n # TODO check if all done\n ddone = concurrent.futures.wait(dfutures)\n\n # upload parts 2 \"prefix/parts\"\n print(f\"upload files for all\")\n fprefix = prefix\n # upload with threads\n afutures = []\n with ThreadPoolExecutor(max_workers=THREADS) as executor:\n print(f\"upload files for all: {fprefix}\")\n prefix_dir = local_dir_all+\"prefix=\"+fprefix\n prefix_key = f\"prefix={fprefix}\"\n\n if (os.path.isdir(prefix_dir)):\n files = [filename for filename in os.listdir(\n prefix_dir) if filename.startswith(\"part-\")]\n\n if len(files) > 0:\n\n for file_local in files:\n file_path = prefix_dir+\"/\"+file_local\n part_num = int(file_local.split('-')[1])\n part_key = '{:05d}'.format(part_num)\n # fix name as static hash to be reproducible\n filename_hash = hashlib.sha1(\n str.encode(prefix_key+part_key)).hexdigest()\n\n blob_key = \"{}/{}/part-{}-{}.snappy.parquet\".format(\n path_out_all, prefix_key, part_key, filename_hash)\n\n # print(\"upload \" + file_path + \" to \" + container_out+\":\"+blob_key)\n # upload_blob(blob_service_client,container_out, blob_key, file_path)\n future = executor.submit(\n upload_blob, blob_service_client, container_out, blob_key, file_path)\n afutures.append(future)\n # else:\n # print(f\"no files to upload for {d_key}\")\n\n # else:\n # print(f\"missing partition for {d_key}\")\n # end of loop, wait for futures\n for future in afutures:\n bkey = future.result()\n\n # ensure we wait all tasks\n # TODO check if all done\n adone = concurrent.futures.wait(afutures)\n\n print(\"--- {} seconds elapsed ---\".format(int(time.time() - start_time)))\n print()\n shutdown_time = time.time()\n spark.stop()\n\n end_time = time.time()\n print(\"Done in {} seconds (read:{} spark:{} upload:{} shutdown:{})\".format(\n int(end_time - start_time),\n int(spark_time - read_time),\n int(upload_time - spark_time),\n int(shutdown_time - upload_time),\n int(end_time - shutdown_time)\n ))\n print('Done.')\n #\n # END OF CODE\n #\n\n\nif __name__ == \"__main__\":\n main()\n" ]
[ [ "sklearn.cluster.DBSCAN" ] ]
aeverall/tensorflow
[ "7992bf97711919f56f80bff9e5510cead4ab2095" ]
[ "tensorflow/python/framework/func_graph.py" ]
[ "# Copyright 2018 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"FuncGraph and related functionality.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport collections\nimport weakref\n\nfrom tensorflow.core.framework import attr_value_pb2\nfrom tensorflow.python.eager import context\nfrom tensorflow.python.eager import tape\nfrom tensorflow.python.eager.graph_only_ops import graph_placeholder\nfrom tensorflow.python.framework import ops\nfrom tensorflow.python.framework import sparse_tensor\nfrom tensorflow.python.framework import tensor_spec\nfrom tensorflow.python.framework.auto_control_deps import AutomaticControlDependencies\nfrom tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import custom_gradient\nfrom tensorflow.python.ops import resource_variable_ops\nfrom tensorflow.python.ops import tensor_array_ops\nfrom tensorflow.python.ops import variable_scope\nfrom tensorflow.python.util import compat\nfrom tensorflow.python.util import nest\nfrom tensorflow.python.util import tf_decorator\nfrom tensorflow.python.util.lazy_loader import LazyLoader\n\n# This is to avoid a circular dependency:\n# function -> func_graph\nfunction = LazyLoader(\"function\", globals(),\n \"tensorflow.python.eager.function\")\ndef_function = LazyLoader(\n \"def_function\", globals(),\n \"tensorflow.python.eager.def_function\")\n\nWHITELIST_COLLECTIONS = [\n ops.GraphKeys.GLOBAL_VARIABLES,\n ops.GraphKeys.LOCAL_VARIABLES,\n ops.GraphKeys.TRAINABLE_VARIABLES,\n variable_scope._VARSTORE_KEY, # pylint: disable=protected-access\n variable_scope._VARSCOPESTORE_KEY # pylint: disable=protected-access\n]\n\n\nclass FuncGraph(ops.Graph):\n \"\"\"Graph representing a function body.\n\n Attributes:\n name: The name of the function.\n inputs: Placeholder tensors representing the inputs to this function. The\n tensors are in this FuncGraph. This represents \"regular\" inputs as well as\n captured inputs (i.e. the values of self.captures), with the regular\n inputs coming first.\n outputs: Tensors that will be returned by this function. The tensors are in\n this FuncGraph.\n structured_outputs: A possibly-nested python object which will be returned\n by this function. The Tensors in this structure are the same as those of\n self.outputs. Note that this structure might contain Python `None`s.\n variables: Variables that should be watched during function execution.\n outer_graph: The graph this function is defined in. May be another FuncGraph\n or the global default Graph.\n captures: Maps external tensor -> internal tensor (i.e. input placeholder).\n The entries are in the order they were captured.\n seed: The graph-level random seed.\n \"\"\"\n\n def __init__(self, name, read_only_collections=True):\n \"\"\"Construct a new FuncGraph.\n\n The graph will inherit its graph key, collections, seed, and distribution\n strategy stack from the current context or graph.\n\n Args:\n name: the name of the function.\n read_only_collections: whether to not write function graph collections\n back to default graph. Defaults to True.\n \"\"\"\n super(FuncGraph, self).__init__()\n\n self.name = name\n self.inputs = []\n self.outputs = []\n self.structured_outputs = None\n self._read_only_collections = read_only_collections\n self._weak_variables = []\n self.outer_graph = ops.get_default_graph()\n self.captures = collections.OrderedDict()\n\n self._building_function = True\n # Map from resource tensor name to last op (in program order) which uses\n # this tensor. Used to enforce that execution order matches program order\n # for resource tensors.\n self._last_op_using_resource_tensor = {}\n\n graph = self.outer_graph\n\n # pylint: disable=protected-access\n # TODO(b/112906995, nareshmodi): distribution strategy depends on inheriting\n # this stack from the default graph even in eager mode. Maybe it should be\n # part of the eager context? This would also allow us to remove a\n # get_default_graph() call from the function cache lookup.\n self._distribution_strategy_stack = list(graph._distribution_strategy_stack)\n # We ignore device placements from any outer scopes while tracing the\n # function when possible, to avoid hard-coding them in the function\n # graph. \"Default\" placements come from the PartitionedCallOp's placement,\n # so that the same trace of the Python function may be placed on several\n # different devices and saved functions may be placed on new devices when\n # restored.\n if context.executing_eagerly():\n self.seed = context.global_seed()\n self._xla_compile = (context.context().device_spec.device_type == \"TPU\")\n if self._distribution_strategy_stack or self._xla_compile:\n self._add_device_to_stack(context.context().device_name)\n else:\n self.seed = graph.seed\n self._xla_compile = getattr(graph, \"_xla_compile\", False)\n # TODO(allenl): Figure out if we can remove colocation stack\n # specialization (currently used in cond_v2), here and in the cache key.\n self._colocation_stack = graph._colocation_stack.copy()\n if (self._distribution_strategy_stack\n or self._xla_compile\n or device_stack_has_callable(graph._device_function_stack)):\n # Hard-code devices from device functions in the function body\n self._device_function_stack = graph._device_function_stack.copy()\n if not self._read_only_collections:\n self._collections = graph._collections\n else:\n for collection_name in graph.get_all_collection_keys():\n if collection_name not in WHITELIST_COLLECTIONS:\n self._collections[collection_name] = graph.get_collection(\n collection_name)\n for collection_name in WHITELIST_COLLECTIONS:\n self._collections[collection_name] = graph.get_collection_ref(\n collection_name)\n\n self._variable_creator_stack = graph._variable_creator_stack\n # Inherit the graph key, since this is used for matching variables in\n # optimizers.\n self._graph_key = graph._graph_key\n # pylint: enable=protected-access\n\n @property\n def variables(self):\n \"\"\"A list of variables accessed by this FuncGraph.\n\n Note that functions keep only weak references to variables. Calling the\n function after a variable it accesses has been deleted is an error.\n\n Yields:\n Strong references to variables accessed by this FuncGraph.\n \"\"\"\n for weak_v in self._weak_variables:\n v = weak_v()\n if v is None:\n raise AssertionError(\n \"Called a function referencing variables which have been deleted. \"\n \"This likely means that function-local variables were created and \"\n \"not referenced elsewhere in the program. This is generally a \"\n \"mistake; consider storing variables in an object attribute on \"\n \"first call.\")\n yield v\n\n @variables.setter\n def variables(self, var_list):\n self._weak_variables = [weakref.ref(v) for v in var_list]\n\n def create_op(\n self,\n op_type,\n inputs,\n dtypes,\n input_types=None,\n name=None,\n attrs=None,\n op_def=None,\n compute_shapes=True,\n compute_device=True):\n \"\"\"Like Graph.create_op, except handles external input tensors.\n\n This overload adds functionality to create_op to \"capture\" any external\n input tensors, i.e. tensors from the eager context or outer function graphs\n if this is a nested function. See `capture` for more information.\n\n Args:\n op_type: The `Operation` type to create. This corresponds to the\n `OpDef.name` field for the proto that defines the operation.\n inputs: A list of `Tensor` objects that will be inputs to the `Operation`.\n dtypes: A list of `DType` objects that will be the types of the tensors\n that the operation produces.\n input_types: (Optional.) A list of `DType`s that will be the types of\n the tensors that the operation consumes. By default, uses the base\n `DType` of each input in `inputs`. Operations that expect\n reference-typed inputs must specify `input_types` explicitly.\n name: (Optional.) A string name for the operation. If not specified, a\n name is generated based on `op_type`.\n attrs: (Optional.) A dictionary where the key is the attribute name (a\n string) and the value is the respective `attr` attribute of the\n `NodeDef` proto that will represent the operation (an `AttrValue`\n proto).\n op_def: (Optional.) The `OpDef` proto that describes the `op_type` that\n the operation will have.\n compute_shapes: (Optional.) Deprecated. Has no effect (shapes are always\n computed).\n compute_device: (Optional.) If True, device functions will be executed\n to compute the device property of the Operation.\n\n Returns:\n An `Operation` object.\n \"\"\"\n # This capturing logic interacts poorly with control flow contexts which\n # want to replace inputs of ops far too late in the process. This can lead\n # the context to get confused and try to create an Enter for an Enter. We\n # can detect this here and skip the additional Enter which can confuse loop\n # validation logic.\n if op_type == \"Enter\" and inputs[0].op.type == \"Enter\":\n if inputs[0].op.get_attr(\"frame_name\") == attrs[\"frame_name\"].s:\n return inputs[0].op\n # Calling AddValue on the control flow contexts to force creation of the\n # backward accumulators in the original graph before we create placeholders\n # to capture the inputs.\n ctxt = ops.get_default_graph()._control_flow_context # pylint: disable=protected-access\n for i, inp in enumerate(inputs):\n # TPU Estimator defines a control flow context with no AddValue method.\n if ctxt is not None and hasattr(ctxt, \"AddValue\"):\n inp = ctxt.AddValue(inp)\n inp = self.capture(inp)\n inputs[i] = inp\n return super(FuncGraph, self).create_op(\n op_type, inputs, dtypes, input_types, name, attrs, op_def,\n compute_device=compute_device)\n\n def capture(self, tensor, name=None):\n \"\"\"Captures `tensor` if it's external to this graph.\n\n If `tensor` is from a different graph, returns a placeholder for it.\n `tensor` and the placeholder will appear in self.captures, and the\n placeholder will appear in self.inputs. Multiple calls to this method with\n the same `tensor` argument will return the same placeholder. If `tensor` is\n from this graph, returns `tensor`.\n\n Args:\n tensor: Tensor. May be from this FuncGraph or a different graph.\n name: Optional name if a placeholder is created.\n\n Returns:\n Tensor from this FuncGraph.\n \"\"\"\n if isinstance(tensor, ops.EagerTensor):\n if name is None:\n name = str(ops.uid())\n return self._capture_helper(tensor, name)\n if tensor.graph is not self:\n if name is None:\n name = tensor.op.name\n return self._capture_helper(tensor, name)\n return tensor\n\n def _capture_helper(self, tensor, name):\n captured_tensor = self.captures.get(tensor, None)\n if captured_tensor is None:\n captured_tensor = _create_substitute_placeholder(tensor, name=name,\n dtype=tensor.dtype)\n self.captures[tensor] = captured_tensor\n self.inputs.append(captured_tensor)\n tape.record_operation(\"captured_value\", [captured_tensor], [tensor],\n lambda x: [x])\n return captured_tensor\n\n @property\n def external_captures(self):\n \"\"\"External tensors captured by this function.\"\"\"\n return list(self.captures.keys())\n\n @property\n def internal_captures(self):\n \"\"\"Placeholders in this function corresponding captured tensors.\"\"\"\n return list(self.captures.values())\n\n\ndef func_graph_from_py_func(name,\n python_func,\n args,\n kwargs,\n signature=None,\n func_graph=None,\n autograph=False,\n add_control_dependencies=True,\n arg_names=None,\n op_return_value=None):\n \"\"\"Returns a `FuncGraph` generated from `python_func`.\n\n Args:\n name: an identifier for the function.\n python_func: the Python function to trace.\n args: the positional args with which the Python function should be called;\n ignored if a signature is provided.\n kwargs: the keyword args with which the Python function should be called;\n ignored if a signature is provided.\n signature: a possibly nested sequence of `TensorSpecs` specifying the shapes\n and dtypes of the arguments. When a signature is provided, `args` and\n `kwargs` are ignored, and `python_func` is traced with Tensors conforming\n to `signature`. If `None`, the shapes and dtypes are inferred from the\n inputs.\n func_graph: Optional. An instance of FuncGraph. If provided, we will use\n this graph else a new one is built and returned.\n autograph: whether to use autograph to compile `python_func`.\n See https://www.tensorflow.org/guide/autograph for more information.\n add_control_dependencies: If True, automatically adds control dependencies\n to ensure program order matches execution order and stateful ops always\n execute.\n arg_names: Optional list of argument names, used to give input placeholders\n recognizable names.\n op_return_value: Optional. A Tensor. If set and `python_func` returns\n Operations, those return values will be replaced with this value. If not\n set, returning an Operation triggers an error.\n\n Returns:\n A FuncGraph.\n\n Raises:\n TypeError: If any of `python_func`'s return values is neither `None` nor a\n `Tensor`.\n \"\"\"\n if op_return_value is not None:\n assert isinstance(op_return_value, ops.Tensor), op_return_value\n if func_graph is None:\n func_graph = FuncGraph(name)\n assert isinstance(func_graph, FuncGraph)\n if add_control_dependencies:\n control_manager = AutomaticControlDependencies\n else:\n control_manager = ops.NullContextmanager\n with func_graph.as_default(), control_manager() as a:\n current_scope = variable_scope.get_variable_scope()\n default_use_recource = current_scope.use_resource\n current_scope.set_use_resource(True)\n\n if signature is not None:\n args = signature\n kwargs = {}\n\n # Creates and names placeholders for all arguments.\n func_args = _get_defun_inputs_from_args(args, arg_names)\n func_kwargs = _get_defun_inputs_from_kwargs(kwargs)\n\n # Note: `nest.flatten` sorts by keys, as does `_deterministic_dict_values`.\n # Variables to help check whether mutation happens in calling the function\n # Copy the recursive list, tuple and map structure, but not base objects\n func_args_before = nest.pack_sequence_as(func_args, nest.flatten(func_args))\n func_kwargs_before = nest.pack_sequence_as(\n func_kwargs, nest.flatten(func_kwargs))\n\n def convert(x):\n \"\"\"Converts a function output to a Tensor.\"\"\"\n if x is None:\n return None\n if op_return_value is not None and isinstance(x, ops.Operation):\n # TODO(b/79881896): we currently can't capture external control deps, so\n # this won't work if x needs to be captured (i.e. if python_func returns\n # captured Operations).\n with ops.control_dependencies([x]):\n x = array_ops.identity(op_return_value)\n elif not isinstance(x, tensor_array_ops.TensorArray):\n try:\n x = ops.convert_to_tensor_or_indexed_slices(x)\n except (ValueError, TypeError):\n raise TypeError(\n \"To be compatible with tf.contrib.eager.defun, Python functions \"\n \"must return zero or more Tensors; in compilation of %s, found \"\n \"return value of type %s, which is not a Tensor.\" %\n (str(python_func), type(x)))\n if add_control_dependencies:\n x = a.mark_as_return(x)\n return x\n\n this_tape = tape.push_new_tape()\n try:\n if autograph:\n from tensorflow.python import autograph # pylint: disable=g-import-not-at-top\n _, original_func = tf_decorator.unwrap(python_func)\n\n def wrapper(*args, **kwargs):\n return autograph.converted_call(\n original_func, None,\n autograph.ConversionOptions(\n verbose=autograph.Verbosity.BRIEF,\n recursive=True,\n strip_decorators=(def_function.function,),\n optional_features=(),\n ), *args, **kwargs)\n\n # Wrapping around a decorator allows checks like tf_inspect.getargspec\n # to be accurate.\n converted_func = tf_decorator.make_decorator(original_func, wrapper)\n tf_decorator.rewrap(python_func, original_func, converted_func)\n\n func_outputs = python_func(*func_args, **func_kwargs)\n\n # invariant: `func_outputs` contains only Tensors, IndexedSlices,\n # SparseTensors, TensorArrays and `None`s.\n func_outputs = nest.map_structure(convert, func_outputs)\n\n check_mutation(func_args_before, func_args)\n check_mutation(func_kwargs_before, func_kwargs)\n finally:\n tape.pop_tape(this_tape)\n current_scope.set_use_resource(default_use_recource)\n\n # Variables in `func_args`, `func_kwargs` should be explicit inputs\n # to the function, not captured inputs.\n tape_variables = this_tape.watched_variables()\n arg_variables = set()\n inputs = []\n for arg in nest.flatten(func_args) + nest.flatten(func_kwargs):\n if isinstance(arg, resource_variable_ops.ResourceVariable):\n # Even if an argument variable was not used in the function, we've\n # already manually captured the resource Tensor when creating argument\n # placeholders.\n resource_placeholder = func_graph.captures.pop(arg.handle)\n arg_variables.add(arg)\n inputs.append(resource_placeholder)\n elif isinstance(arg, ops.Tensor):\n inputs.append(arg)\n variables = [v for v in tape_variables if v not in arg_variables]\n func_graph.inputs = inputs + list(func_graph.captures.values())\n\n func_graph.structured_outputs = func_outputs\n # Returning a closed-over tensor does not trigger convert_to_tensor.\n func_graph.outputs.extend(\n func_graph.capture(x)\n for x in flatten(func_graph.structured_outputs)\n if x is not None)\n\n func_graph.variables = variables\n\n # Register any other functions defined in the graph.\n with ops.init_scope():\n if context.executing_eagerly():\n for f in func_graph._functions.values(): # pylint: disable=protected-access\n # TODO(ashankar): What about the gradient registry?\n context.add_function(f._c_func.func) # pylint: disable=protected-access\n\n return func_graph\n\n\ndef maybe_captured(tensor):\n \"\"\"If t is a captured value placeholder, returns the original captured value.\n\n Args:\n tensor: Tensor.\n\n Returns:\n A tensor, potentially from a different Graph/FuncGraph.\n \"\"\"\n if (not isinstance(tensor, ops.EagerTensor) and\n tensor.op.graph.building_function and tensor.op.type == \"Placeholder\"):\n for input_t, placeholder_t in tensor.op.graph.captures.items():\n if tensor == placeholder_t:\n return maybe_captured(input_t)\n # pylint: enable=protected-access\n return tensor\n\n\ndef device_stack_has_callable(device_stack):\n \"\"\"Checks whether a device stack contains a callable.\"\"\"\n return any(callable(spec._device_name_or_function) # pylint: disable=protected-access\n for spec in device_stack.peek_objs())\n\n\ndef check_mutation(n1, n2):\n \"\"\"Check if two list of arguments are exactly the same.\"\"\"\n errmsg = (\"Function to be traced should not modify structure of input \"\n \"arguments. Check if your function has list and dictionary \"\n \"operations that alter input arguments, \"\n \"such as `list.pop`, `list.append`\")\n try:\n nest.assert_same_structure(n1, n2)\n except ValueError:\n raise ValueError(errmsg)\n\n for arg1, arg2 in zip(nest.flatten(n1), nest.flatten(n2)):\n if arg1 is not arg2:\n raise ValueError(errmsg)\n\n\ndef flatten(sequence):\n \"\"\"Like `nest.flatten` but also unpacks other Tensor-like objects.\n\n Flattens non-tensor objects into their constituent tensors.\n\n Args:\n sequence: A nested structure of Tensors, IndexedSlices, SparseTensors and\n TensorArrays.\n\n Returns:\n A list of tensors.\n \"\"\"\n # TODO(akshayka): Support `SparseTensor` in a similar fashion.\n flat_sequence = nest.flatten(sequence)\n outputs = []\n for item in flat_sequence:\n if isinstance(item, ops.IndexedSlices):\n if item.dense_shape is not None:\n outputs.extend([item.values, item.indices, item.dense_shape])\n else:\n outputs.extend([item.values, item.indices])\n elif isinstance(item, sparse_tensor.SparseTensor):\n outputs.extend([item.indices, item.values, item.dense_shape])\n elif isinstance(item, tensor_array_ops.TensorArray):\n outputs.append(item.flow)\n else:\n outputs.append(item)\n return outputs\n\n\ndef pack_sequence_as(structure, flat_sequence):\n \"\"\"Like `nest.pack_sequence_as` but also packs other Tensor-like objects.\n\n Args:\n structure: The structure to pack into. May contain Tensors, IndexedSlices,\n TensorArrays or SparseTensors.\n flat_sequence: An iterable containing tensors.\n\n Returns:\n A nested structure.\n\n Raises:\n AssertionError if `structure` and `flat_sequence` are not compatible.\n \"\"\"\n flattened_structure = nest.flatten(structure)\n flat_sequence_with_slices_and_tas = []\n index = 0\n for t in flattened_structure:\n if isinstance(t, ops.IndexedSlices):\n if t.dense_shape is not None:\n flat_sequence_with_slices_and_tas.append(\n ops.IndexedSlices(*flat_sequence[index:index + 3]))\n index += 3\n else:\n flat_sequence_with_slices_and_tas.append(\n ops.IndexedSlices(*flat_sequence[index:index + 2]))\n index += 2\n elif isinstance(t, sparse_tensor.SparseTensor):\n flat_sequence_with_slices_and_tas.append(\n sparse_tensor.SparseTensor(*flat_sequence[index:index + 3]))\n index += 3\n elif isinstance(t, tensor_array_ops.TensorArray):\n flow = flat_sequence[index]\n ta = tensor_array_ops.build_ta_with_new_flow(t, flow)\n flat_sequence_with_slices_and_tas.append(ta)\n index += 1\n else:\n flat_sequence_with_slices_and_tas.append(flat_sequence[index])\n index += 1\n assert len(flattened_structure) == len(flat_sequence_with_slices_and_tas)\n return nest.pack_sequence_as(structure, flat_sequence_with_slices_and_tas)\n\n\ndef _create_substitute_placeholder(value, name=None, dtype=None):\n \"\"\"Creates a placeholder for `value` and propagates shape info to it.\"\"\"\n # Note: setting ops.control_dependencies(None) ensures we always put\n # capturing placeholders outside of any control flow context.\n with ops.control_dependencies(None):\n placeholder = graph_placeholder(\n dtype=dtype or value.dtype, shape=value.shape, name=name)\n custom_gradient.copy_handle_data(value, placeholder)\n return placeholder\n\n\ndef _get_defun_inputs_from_args(args, names):\n \"\"\"Maps Python function positional args to graph-construction inputs.\"\"\"\n return _get_defun_inputs(args, names, structure=args)\n\n\ndef _get_defun_inputs(flat_args, names, structure):\n \"\"\"Maps python function args to graph-construction inputs.\n\n Args:\n flat_args: A flat list of user-specified arguments.\n names: A list of strings with user-specified argument names, same length as\n `flat_args`. May be `None`, in which case a generic name is used.\n structure: The original argument list or dictionary.\n\n Returns:\n Placeholders with the same structure as `structure`.\n \"\"\"\n func_graph = ops.get_default_graph()\n function_inputs = []\n if names is None:\n names = [None] * len(flat_args)\n for arg_value, name in zip(flat_args, names):\n for arg in nest.flatten(arg_value):\n if isinstance(arg, (ops.Tensor, tensor_spec.TensorSpec)):\n if isinstance(arg, tensor_spec.TensorSpec) and arg.name:\n requested_name = arg.name\n else:\n requested_name = name\n placeholder = graph_placeholder(\n arg.dtype, arg.shape,\n name=requested_name)\n if name is not None:\n # Record the requested/user-specified name in case it's different than\n # the uniquified name, for validation when exporting signatures.\n placeholder.op._set_attr( # pylint: disable=protected-access\n \"_user_specified_name\",\n attr_value_pb2.AttrValue(s=compat.as_bytes(requested_name)))\n function_inputs.append(placeholder)\n elif isinstance(arg, resource_variable_ops.ResourceVariable):\n # Capture arg variables to create placeholders for them. These will be\n # removed as captures after the function is traced (since otherwise we'd\n # just add it back with a new placeholder when the variable was\n # referenced).\n placeholder = func_graph.capture(arg.handle, name=name)\n placeholder.op._set_attr( # pylint: disable=protected-access\n \"_user_specified_name\",\n attr_value_pb2.AttrValue(s=compat.as_bytes(name)))\n function_inputs.append(arg)\n else:\n function_inputs.append(arg)\n return nest.pack_sequence_as(structure, function_inputs)\n\n\ndef _get_defun_inputs_from_kwargs(kwargs):\n \"\"\"Maps Python function keyword args to graph-construction inputs.\"\"\"\n if kwargs:\n names, flat_args = zip(*sorted(kwargs.items()))\n else:\n names = []\n flat_args = []\n return _get_defun_inputs(flat_args, names, structure=kwargs)\n" ]
[ [ "tensorflow.python.eager.tape.push_new_tape", "tensorflow.python.framework.ops.convert_to_tensor_or_indexed_slices", "tensorflow.python.eager.tape.pop_tape", "tensorflow.python.util.tf_decorator.make_decorator", "tensorflow.python.util.nest.flatten", "tensorflow.python.eager.graph_only_ops.graph_placeholder", "tensorflow.python.util.nest.assert_same_structure", "tensorflow.python.framework.ops.uid", "tensorflow.python.framework.ops.IndexedSlices", "tensorflow.python.ops.custom_gradient.copy_handle_data", "tensorflow.python.autograph.ConversionOptions", "tensorflow.python.eager.context.global_seed", "tensorflow.python.framework.sparse_tensor.SparseTensor", "tensorflow.python.eager.tape.record_operation", "tensorflow.python.ops.tensor_array_ops.build_ta_with_new_flow", "tensorflow.python.util.tf_decorator.unwrap", "tensorflow.python.ops.array_ops.identity", "tensorflow.python.eager.context.context", "tensorflow.python.framework.ops.control_dependencies", "tensorflow.python.util.compat.as_bytes", "tensorflow.python.util.tf_decorator.rewrap", "tensorflow.python.framework.ops.init_scope", "tensorflow.python.ops.variable_scope.get_variable_scope", "tensorflow.python.framework.ops.get_default_graph", "tensorflow.python.eager.context.add_function", "tensorflow.python.eager.context.executing_eagerly", "tensorflow.python.util.nest.map_structure", "tensorflow.python.util.nest.pack_sequence_as" ] ]
qbxlvnf11/graph-neural-networks-for-graph-classification
[ "5d69ead58c786aa8e472ab0433156fe09fe6ca4b" ]
[ "models/GraphUNet.py" ]
[ "import os\nimport sys\nsys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))\n\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n#from layers.graph_convolution_layer import GraphConvolutionLayer\nfrom layers.graph_unet_layer import GraphUNetLayer\nfrom readouts.basic_readout import readout_function\n\n\"\"\"\nBase paper: https://arxiv.org/pdf/1905.05178.pdf\n\"\"\"\n\nclass GraphUNet(nn.Module):\n def __init__(self, n_feat, n_class, n_layer, agg_hidden, fc_hidden, dropout, readout, device):\n super(GraphUNet, self).__init__()\n \n self.n_layer = n_layer\n self.readout = readout\n \n # Pooling_rate\n pooling_rations = [0.8 - (i * 0.1) if i < 3 else 0.5 for i in range(n_layer)]\n \n # Graph unet layer\n self.graph_unet_layers = []\n for i in range(n_layer):\n if i == 0:\n self.graph_unet_layers.append(GraphUNetLayer(n_feat, agg_hidden, pooling_rations[i], device))\n else:\n self.graph_unet_layers.append(GraphUNetLayer(agg_hidden, agg_hidden, pooling_rations[i], device))\n \n # Fully-connected layer\n self.fc1 = nn.Linear(agg_hidden, fc_hidden)\n self.fc2 = nn.Linear(fc_hidden, n_class)\n\n def forward(self, data):\n \n for i in range(self.n_layer):\n # Graph unet layer\n data = self.graph_unet_layers[i](data)\n \n x = data[0]\n \n # Dropout\n if i != self.n_layer - 1:\n x = F.dropout(x, p=self.dropout, training=self.training)\n \n # Readout\n x = readout_function(x, self.readout)\n \n # Fully-connected layer\n x = F.relu(self.fc1(x))\n x = F.softmax(self.fc2(x))\n \n return x\n\n def __repr__(self):\n layers = ''\n\n for i in range(self.n_layer):\n layers += str(self.graph_unet_layers[i]) + '\\n'\n layers += str(self.fc1) + '\\n'\n layers += str(self.fc2) + '\\n'\n return layers" ]
[ [ "torch.nn.Linear", "torch.nn.functional.dropout" ] ]
supertopdev/data-science
[ "9534085236a79e123b97f0771a4641289039d93b" ]
[ "ISR/utils/datahandler.py" ]
[ "import os\nimport imageio\nimport numpy as np\nfrom ISR.utils.logger import get_logger\n\n\nclass DataHandler:\n \"\"\"\n DataHandler generate augmented batches used for training or validation.\n\n Args:\n lr_dir: directory containing the Low Res images.\n hr_dir: directory containing the High Res images.\n patch_size: integer, size of the patches extracted from LR images.\n scale: integer, upscaling factor.\n n_validation_samples: integer, size of the validation set. Only provided if the\n DataHandler is used to generate validation sets.\n T: float in [0,1], is the patch \"flatness\" threshold.\n Determines what level of detail the patches need to meet. 0 means any patch is accepted.\n \"\"\"\n\n def __init__(self, lr_dir, hr_dir, patch_size, scale, n_validation_samples=None, T=0.03):\n self.folders = {'hr': hr_dir, 'lr': lr_dir} # image folders\n self.extensions = ('.png', '.jpeg', '.jpg') # admissible extension\n self.img_list = {} # list of file names\n self.n_validation_samples = n_validation_samples\n self.patch_size = patch_size\n self.scale = scale\n self.T = T\n self.patch_size = {'lr': patch_size, 'hr': patch_size * self.scale}\n self.logger = get_logger(__name__)\n self._make_img_list()\n self._check_dataset()\n\n def _make_img_list(self):\n \"\"\" Creates a dictionary of lists of the acceptable images contained in lr_dir and hr_dir. \"\"\"\n\n for res in ['hr', 'lr']:\n file_names = os.listdir(self.folders[res])\n file_names = [file for file in file_names if file.endswith(self.extensions)]\n self.img_list[res] = np.sort(file_names)\n\n if self.n_validation_samples:\n samples = np.random.choice(\n range(len(self.img_list['hr'])), self.n_validation_samples, replace=False\n )\n for res in ['hr', 'lr']:\n self.img_list[res] = self.img_list[res][samples]\n\n def _check_dataset(self):\n \"\"\" Sanity check for dataset. \"\"\"\n\n # the order of these asserts is important for testing\n assert len(self.img_list['hr']) == self.img_list['hr'].shape[0], 'UnevenDatasets'\n assert self._matching_datasets(), 'Input/LabelsMismatch'\n\n def _matching_datasets(self):\n \"\"\" Rough file name matching between lr and hr directories. \"\"\"\n # LR_name.png = HR_name+x+scale.png\n # or\n # LR_name.png = HR_name.png\n LR_name_root = [x.split('.')[0].split('x')[0] for x in self.img_list['lr']]\n HR_name_root = [x.split('.')[0] for x in self.img_list['hr']]\n return np.all(HR_name_root == LR_name_root)\n\n def _not_flat(self, patch):\n \"\"\"\n Determines whether the patch is complex, or not-flat enough.\n Threshold set by self.T.\n \"\"\"\n\n if max(np.std(patch, axis=0).mean(), np.std(patch, axis=1).mean()) < self.T:\n return False\n else:\n return True\n\n def _crop_imgs(self, imgs, batch_size, idx=0):\n \"\"\"\n Get random top left corners coordinates in LR space, multiply by scale to\n get HR coordinates.\n Gets batch_size + n possible coordinates.\n Accepts the batch only if the standard deviation of pixel intensities is above a given threshold, OR\n no patches can be further discarded (n have been discarded already).\n Square crops of size patch_size are taken from the selected\n top left corners.\n \"\"\"\n\n n = 2 * batch_size\n top_left = {'x': {}, 'y': {}}\n for i, axis in enumerate(['x', 'y']):\n top_left[axis]['lr'] = np.random.randint(\n 0, imgs['lr'].shape[i] - self.patch_size['lr'] + 1, batch_size + n\n )\n top_left[axis]['hr'] = top_left[axis]['lr'] * self.scale\n\n crops = {}\n for res in ['lr', 'hr']:\n slices = [\n [slice(x, x + self.patch_size[res]), slice(y, y + self.patch_size[res])]\n for x, y in zip(top_left['x'][res], top_left['y'][res])\n ]\n crops[res] = []\n for s in slices:\n candidate_crop = imgs[res][s[0], s[1], slice(None)]\n if self._not_flat(candidate_crop) or n == 0:\n crops[res].append(candidate_crop)\n else:\n n -= 1\n if len(crops[res]) == batch_size:\n break\n crops[res] = np.array(crops[res])\n return crops\n\n def _apply_transform(self, img, transform_selection):\n \"\"\" Rotates and flips input image according to transform_selection. \"\"\"\n\n rotate = {\n 0: lambda x: x,\n 1: lambda x: np.rot90(x, k=1, axes=(1, 0)), # rotate right\n 2: lambda x: np.rot90(x, k=1, axes=(0, 1)), # rotate left\n }\n\n flip = {\n 0: lambda x: x,\n 1: lambda x: np.flip(x, 0), # flip along horizontal axis\n 2: lambda x: np.flip(x, 1), # flip along vertical axis\n }\n\n rot_direction = transform_selection[0]\n flip_axis = transform_selection[1]\n\n img = rotate[rot_direction](img)\n img = flip[flip_axis](img)\n\n return img\n\n def _transform_batch(self, batch, transforms):\n \"\"\" Transforms each individual image of the batch independently. \"\"\"\n\n t_batch = np.array(\n [self._apply_transform(img, transforms[i]) for i, img in enumerate(batch)]\n )\n return t_batch\n\n def get_batch(self, batch_size, idx=None):\n \"\"\"\n Returns a dictionary with keys ('lr', 'hr') containing training batches\n of Low Res and High Res image patches.\n \"\"\"\n\n if not idx:\n # randomly select one image. idx is given at validation time.\n idx = np.random.choice(range(len(self.img_list['hr'])))\n img = {}\n for res in ['lr', 'hr']:\n img_path = os.path.join(self.folders[res], self.img_list[res][idx])\n img[res] = imageio.imread(img_path) / 255.0\n batch = self._crop_imgs(img, batch_size)\n transforms = np.random.randint(0, 3, (batch_size, 2))\n batch['lr'] = self._transform_batch(batch['lr'], transforms)\n batch['hr'] = self._transform_batch(batch['hr'], transforms)\n\n return batch\n\n def get_validation_batches(self, batch_size):\n \"\"\" Returns a batch for each image in the validation set. \"\"\"\n\n if self.n_validation_samples:\n batches = []\n for idx in range(self.n_validation_samples):\n batches.append(self.get_batch(batch_size, idx))\n return batches\n else:\n self.logger.error(\n 'No validation set size specified. (not operating in a validation set?)'\n )\n raise ValueError(\n 'No validation set size specified. (not operating in a validation set?)'\n )\n\n def get_validation_set(self, batch_size):\n \"\"\"\n Returns a batch for each image in the validation set.\n Flattens and splits them to feed it to Keras's model.evaluate.\n \"\"\"\n\n if self.n_validation_samples:\n batches = self.get_validation_batches(batch_size)\n valid_set = {'lr': [], 'hr': []}\n for batch in batches:\n for res in ('lr', 'hr'):\n valid_set[res].extend(batch[res])\n for res in ('lr', 'hr'):\n valid_set[res] = np.array(valid_set[res])\n return valid_set\n else:\n self.logger.error(\n 'No validation set size specified. (not operating in a validation set?)'\n )\n raise ValueError(\n 'No validation set size specified. (not operating in a validation set?)'\n )\n" ]
[ [ "numpy.rot90", "numpy.all", "numpy.flip", "numpy.sort", "numpy.std", "numpy.random.randint", "numpy.array" ] ]
vaibhav016/TensorFlowASR
[ "2c90f67f284be6c8a6c182223b9f517a73bc766f" ]
[ "tensorflow_asr/utils/env_util.py" ]
[ "# Copyright 2020 Huy Le Nguyen (@usimarit)\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Union, List\nimport warnings\nimport tensorflow as tf\n\nlogger = tf.get_logger()\n\n\ndef setup_environment(): # Set memory growth and only log ERRORs\n \"\"\" Setting tensorflow running environment \"\"\"\n warnings.simplefilter(\"ignore\")\n logger.setLevel(\"WARN\")\n\n\ndef setup_devices(devices: List[int], cpu: bool = False):\n \"\"\"Setting visible devices\n\n Args:\n devices (list): list of visible devices' indices\n \"\"\"\n if cpu:\n cpus = tf.config.list_physical_devices(\"CPU\")\n tf.config.set_visible_devices(cpus, \"CPU\")\n else:\n gpus = tf.config.list_physical_devices(\"GPU\")\n if gpus:\n visible_gpus = [gpus[i] for i in devices]\n tf.config.set_visible_devices(visible_gpus, \"GPU\")\n print(\"Run on\", len(visible_gpus), \"Physical GPUs\")\n\n\ndef setup_strategy(devices: List[int], tpu_address: str = None):\n \"\"\"Setting mirrored strategy for training\n\n Args:\n devices (list): list of visible devices' indices\n tpu_address (str): an optional custom tpu address\n\n Returns:\n tf.distribute.Strategy: TPUStrategy for training on tpus or MirroredStrategy for training on gpus\n \"\"\"\n try:\n return setup_tpu(tpu_address)\n except (ValueError, tf.errors.NotFoundError) as e:\n logger.warn(e)\n pass\n setup_devices(devices)\n return tf.distribute.MirroredStrategy()\n\n\ndef setup_tpu(tpu_address=None):\n if tpu_address is None:\n resolver = tf.distribute.cluster_resolver.TPUClusterResolver()\n else:\n resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=\"grpc://\" + tpu_address)\n tf.config.experimental_connect_to_cluster(resolver)\n tf.tpu.experimental.initialize_tpu_system(resolver)\n print(\"All TPUs: \", tf.config.list_logical_devices(\"TPU\"))\n return tf.distribute.experimental.TPUStrategy(resolver)\n\n\ndef has_devices(devices: Union[List[str], str]):\n if isinstance(devices, list):\n return all([len(tf.config.list_logical_devices(d)) != 0 for d in devices])\n return len(tf.config.list_logical_devices(devices)) != 0\n" ]
[ [ "tensorflow.config.list_logical_devices", "tensorflow.get_logger", "tensorflow.distribute.experimental.TPUStrategy", "tensorflow.distribute.cluster_resolver.TPUClusterResolver", "tensorflow.distribute.MirroredStrategy", "tensorflow.tpu.experimental.initialize_tpu_system", "tensorflow.config.experimental_connect_to_cluster", "tensorflow.config.list_physical_devices", "tensorflow.config.set_visible_devices" ] ]
yjc9696/biobert-my
[ "ffc11c91f7032cffbcc7d9526159f0ff8e08c1f3" ]
[ "helpData_n2c2.py" ]
[ "import json\n\nimport numpy as np\n\nimport metrics\nimport tokenization\nimport utils\nfrom config import opt\n\n\ndef write2file(data, path):\n with open(path, 'w') as f:\n f.write(json.dumps(data, ensure_ascii=False))\ndef get_BIO(self, tag):\n return self.id2tag[tag]\n # if tag == 0:\n # return 'O'\n # if tag % 2 == 1:\n # return 'B-' + str(tag)\n # else:\n # return 'I-' + str(tag)\n\n\ndef detokenize(self,in_tok,in_lab):\n \"\"\"\n convert suub-word level BioBERT-NER results to full words and labels.\n\n Args:\n pred_token_test_path: path to token_test.txt from output folder. ex) output/token_test.txt\n pred_label_test_path: path to label_test.txt from output folder. ex) output/label_test.txt\n Outs:\n A dictionary that contains full words and predicted labels.\n \"\"\"\n\n # read predicted\n pred = {'toks':[], 'labels':[]} # dictionary for predicted tokens and labels.\n for lineIdx, (lineTok, lineLab) in enumerate(zip(in_tok, in_lab)):\n pred['toks'].append(lineTok)\n if lineLab in ['[CLS]','[SEP]', 'X']: # replace non-text tokens with O. These will not be evaluated.\n pred['labels'].append('O')\n continue\n pred['labels'].append(get_BIO(self,lineLab))\n\n assert (len(pred['toks']) == len(pred['labels'])), \"Error! : len(pred['toks'])(%s) != len(pred['labels'])(%s) : Please report us \"%(len(pred['toks']), len(pred['labels']))\n\n bert_pred = {'toks':[], 'labels':[], 'sentence':[]}\n buf = []\n for t, l in zip(pred['toks'], pred['labels']):\n if t in ['[CLS]','[SEP]']: # non-text tokens will not be evaluated.\n bert_pred['toks'].append(t)\n bert_pred['labels'].append(t) # Tokens and labels should be identical if they are [CLS] or [SEP]\n if t == '[SEP]':\n bert_pred['sentence'].append(buf)\n buf = []\n continue\n elif t[:2] == '##': # if it is a piece of a word (broken by Word Piece tokenizer)\n bert_pred['toks'][-1] += t[2:] # append pieces to make a full-word\n buf[-1]+=t[2:]\n else:\n bert_pred['toks'].append(t)\n bert_pred['labels'].append(l)\n buf.append(t)\n\n assert (len(bert_pred['toks']) == len(bert_pred['labels'])), (\"Error! : len(bert_pred['toks']) != len(bert_pred['labels']) : Please report us\")\n\n return bert_pred\n\n\nclass DataHelper(object):\n def __init__(self, opt):\n self.opt = opt\n self.tokenizer = tokenization.FullTokenizer(\n vocab_file=opt.bert_vocab_unk, do_lower_case=True)\n self.id2r, self.r2id = None, None\n self.id2tag, self.tag2id = None, None\n self.id2type, self.type2id = None, None\n self.type2types = None\n self.get_relations()\n\n self.origin_train_data = utils.load_data(self.opt.train_data_dir)\n self.origin_dev_data = utils.load_data(self.opt.dev_data_dir)\n self.origin_test1_data = utils.load_data(self.opt.test1_data_dir)\n\n def get_relations(self):\n \"\"\"\n 得到所有的xx2xx文件\n \"\"\"\n # origin_50_schema = load_data(self.opt.schema_dir_old, case=1)\n relation_all = utils.load_data(self.opt.schema_dir_new, case=1)\n # self.down2top = {} # 记录类别的上下为关系\n # for old, new in zip(origin_50_schema, new_50_schema):\n # old_sample_obj_type = old['object_type']\n # old_sample_sbj_type = old['subject_type']\n # new_sample_obj_type = new['object_type']\n # new_sample_sbj_type = new['subject_type']\n # top_obj, top_sbj = self.down2top.get(old_sample_obj_type, None), self.down2top.get(old_sample_sbj_type, None)\n # assert (top_obj == None or top_obj == new_sample_obj_type) \\\n # and (top_sbj == None or top_sbj == new_sample_sbj_type)\n # self.down2top[old_sample_obj_type] = new_sample_obj_type\n # self.down2top[old_sample_sbj_type] = new_sample_sbj_type\n # print(\"上下位关系为:{}\".format(self.down2top))\n\n self.r2id = {}\n self.id2r = {}\n self.id2tag = {}\n self.tag2id = {}\n self.id2type = {}\n self.type2id = {}\n exist_ent_type, exist_rel_type = set(), set()\n for sample in relation_all:\n obj, r, sbj = sample['object_type'], sample['predicate'], sample['subject_type']\n if obj not in exist_ent_type:\n self.id2type[len(exist_ent_type) + 1] = obj\n self.id2tag[2 * len(exist_ent_type) + 1] = 'B-' + obj\n self.id2tag[2 * len(exist_ent_type) + 2] = 'I-' + obj\n exist_ent_type.add(obj)\n if sbj not in exist_ent_type:\n self.id2type[len(exist_ent_type) + 1] = sbj\n self.id2tag[2 * len(exist_ent_type) + 1] = 'B-' + sbj\n self.id2tag[2 * len(exist_ent_type) + 2] = 'I-' + sbj\n exist_ent_type.add(sbj)\n if r not in exist_rel_type:\n # 多余给NA\n self.id2r[len(exist_rel_type)] = r\n exist_rel_type.add(r)\n\n self.id2r[len(exist_rel_type)] = 'NA'\n exist_rel_type.add('NA')\n self.id2tag[0] = 'O'\n exist_ent_type.add('O')\n self.id2type[0] = 'O'\n\n print(\"实体类型数目为:{};关系数目为:{}\".format(len(exist_ent_type), len(exist_rel_type)))\n self.r2id = {self.id2r[idx]: idx for idx in self.id2r.keys()}\n self.tag2id = {self.id2tag[idx]: idx for idx in self.id2tag.keys()}\n self.type2id = {self.id2type[idx]: idx for idx in self.id2type.keys()}\n\n self.type2types = {ent: set() for ent in exist_ent_type}\n for sample in relation_all:\n obj, r, sbj = sample['object_type'], sample['predicate'], sample['subject_type']\n self.type2types[obj].add(sbj)\n\n self.type2types = {ent: list(self.type2types[ent]) for ent in self.type2types.keys()}\n\n # 写入文件\n print(\"写入xx2xx数据到目录{}..\".format(self.opt.json_data_root))\n write2file(self.id2r, self.opt.id2r_dir)\n write2file(self.r2id, self.opt.r2id_dir)\n write2file(self.id2tag, self.opt.id2tag_dir)\n write2file(self.tag2id, self.opt.tag2id_dir)\n write2file(self.id2type, self.opt.id2type_dir)\n write2file(self.type2id, self.opt.type2id_dir)\n write2file(self.type2types, self.opt.type2types_dir)\n\n def down2topForDatas(self, datas):\n topDatas = []\n for data in datas:\n text = data['text']\n downSpoList = data['spo_list']\n topSpoList = []\n for spo in downSpoList:\n spo['object_type'] = self.down2top[spo['object_type']]\n spo['subject_type'] = self.down2top[spo['subject_type']]\n topSpoList.append(spo)\n dataUnit = {}\n dataUnit['text'] = text\n dataUnit['spo_list'] = topSpoList\n topDatas.append(dataUnit)\n return topDatas\n\n def get_positions(self, data_list, map_str):\n \"\"\"\n 返回实体在单词列表中的所有位置\n sample:\n >> input: ['球','星','球','星', ...., ], '球星'\n >> return: 【(2, 3),(4,5)】\n \"\"\"\n result = []\n if (opt.use_all_positions == False):\n str, end = self.get_position(data_list, map_str)\n result.append((str, end))\n return result\n map_str = map_str.strip().replace(' ', '$')\n map_str = self.tokenizer.tokenize(map_str)\n map_str = [i.replace('#', '') for i in map_str]\n map_str = ''.join(map_str)\n data_list = [i.replace('#', '') for i in data_list]\n # 如果只由一个词组成\n for word in data_list:\n if map_str.lower() in word.lower():\n start_id = end_id = data_list.index(word)\n result.append((start_id, end_id))\n start_id = -1\n end_id = -1\n for idx, word in enumerate(data_list):\n if map_str.startswith(word):\n start_id = end_id = idx\n while end_id + 1 < len(data_list) and data_list[end_id + 1] in map_str:\n if \"\".join(data_list[start_id:end_id + 2]) == map_str:\n # print(\"\".join(data_list[start_id:end_id+3]))\n result.append((start_id, end_id + 1))\n break\n end_id += 1\n find_str = \"\"\n for idx in range(start_id, end_id + 1):\n find_str = find_str + data_list[idx]\n if find_str != map_str:\n pre_extend = (data_list[start_id - 1] if start_id > 0 else \"\") + find_str\n last_extend = find_str + (data_list[end_id + 1] if end_id < len(data_list) - 1 else \"\")\n pre_last_extend = (data_list[start_id - 1] if start_id > 0 else \"\") + find_str + (data_list[end_id + 1] if end_id < len(data_list) - 1 else \"\")\n if map_str in pre_extend:\n start_id -= 1\n elif map_str in last_extend:\n end_id += 1\n elif map_str in pre_last_extend:\n start_id -= 1\n end_id += 1\n else:\n start_id = -1\n end_id = -1\n if len(result) > 0:\n return result\n for idx, word in enumerate(data_list[:-1]):\n if map_str in (word + data_list[idx + 1]):\n result.append((idx, idx + 1))\n\n if len(result) == 0:\n result.append((-1, -1))\n # print(\"word_list{} map_str {} loss\".format(data_list, map_str))\n return result\n\n def get_position(self, data_list, map_str):\n \"\"\"\n 返回实体在单词列表中的位置,只返回第一次出现的位置\n sample:\n >> input: ['球','星','姚','明', ...., ], '姚明'\n >> return: (2, 3)\n \"\"\"\n map_str = map_str.strip().replace(' ', '$')\n map_str = self.tokenizer.tokenize(map_str)\n map_str = [i.replace('#', '') for i in map_str]\n map_str = ''.join(map_str)\n data_list = [i.replace('#', '') for i in data_list]\n # 如果只由一个词组成\n for word in data_list:\n if map_str.lower() in word.lower():\n start_id = end_id = data_list.index(word)\n return start_id, end_id\n\n start_id = -1\n end_id = -1\n for idx, word in enumerate(data_list):\n if start_id != - 1 and end_id != -1:\n return start_id, end_id\n if map_str.startswith(word):\n start_id = end_id = idx\n while end_id + 1 < len(data_list) and data_list[end_id + 1] in map_str:\n if \"\".join(data_list[start_id:end_id + 2]) == map_str:\n # print(\"\".join(data_list[start_id:end_id+3]))\n return start_id, end_id + 1\n end_id += 1\n find_str = \"\"\n for idx in range(start_id, end_id + 1):\n find_str = find_str + data_list[idx]\n if find_str != map_str:\n pre_extend = (data_list[start_id - 1] if start_id > 0 else \"\") + find_str\n last_extend = find_str + (data_list[end_id + 1] if end_id < len(data_list) - 1 else \"\")\n pre_last_extend = (data_list[start_id - 1] if start_id > 0 else \"\") + find_str + (data_list[end_id + 1] if end_id < len(data_list) - 1 else \"\")\n if map_str in pre_extend:\n start_id -= 1\n elif map_str in last_extend:\n end_id += 1\n elif map_str in pre_last_extend:\n start_id -= 1\n end_id += 1\n else:\n start_id = -1\n end_id = -1\n if start_id != -1 and end_id != -1:\n return start_id, end_id\n for idx, word in enumerate(data_list[:-1]):\n if map_str in (word + data_list[idx + 1]):\n return idx, idx + 1\n # print(\"word_list{} map_str {} loss\".format(data_list, map_str))\n return start_id, end_id\n\n def get_tag(self, word_list, entity_list, type_list):\n '''\n 得到一个句子的tag标签\n sampple:\n >> input: ['球','星', '姚', '明', ...], ['姚明'], ['人物']\n >> return: ['O', 'O', 'id(B-人物), id(I-人物)']\n '''\n word_list = [word.replace('#', '') for word in word_list]\n tag_list = [0] * len(word_list)\n for entity, type_ in zip(entity_list, type_list):\n positions = self.get_positions(word_list, entity)\n for start_id, end_id in positions:\n if start_id == -1 or end_id == -1:\n continue\n # 补充书名号\n # if start_id > 0 and end_id < len(word_list)-1:\n # if word_list[start_id-1] == '《' and word_list[end_id+1] == '》':\n # start_id -= 1\n # end_id += 1\n Bid = 2 * (self.type2id[type_] - 1) + 1\n Iid = 2 * (self.type2id[type_] - 1) + 2\n tag_list[start_id] = Bid\n if start_id < end_id:\n for idx in range(start_id + 1, end_id + 1):\n tag_list[idx] = Iid\n return tag_list\n\n def get_entity_list_and_type_list(self, data_list):\n \"\"\"\n 得到实体和对应的类型列表,一一对应\n sample:\n >> input: [姚明,NBA]\n >> return:[人,组织]\n \"\"\"\n entity_list, type_list = [], []\n for unit in data_list:\n entity_list.append(unit['object'])\n type_list.append(unit['object_type'])\n entity_list.append(unit['subject'])\n type_list.append(unit['subject_type'])\n return entity_list, type_list\n\n def get_sample_exist_entity2rlation(self, word_list, spo_list):\n \"\"\"\n 给定句子的 bert切词列表, 一句话的spo_list\n 返回该句话存在的头实体尾实体位置及对应的关系字典\n {(obj_s, obj_e, sbj_s, sbj_e): r}\n \"\"\"\n golden_map = {}\n word_list = [word.replace('#', '') for word in word_list]\n for spo in spo_list:\n obj = spo['object']\n sbj = spo['subject']\n\n o_po = self.get_positions(word_list, obj)\n s_po = self.get_positions(word_list, sbj)\n for o_s, o_e in o_po:\n for s_s, s_e in s_po:\n r = self.r2id[spo['predicate']]\n golden_map[(o_s, o_e, s_s, s_e)] = r\n return golden_map\n\n def get_sample_all_entity2relation(self, tags_list, golden_map):\n \"\"\"\n 返回一个句子所有可能实体组合极其关系\n [[s1, e1, s2, e2, r],\n [s1, e1, s2, e2, 0]\n ...]]\n \"\"\"\n all_entity = []\n NA_entity = []\n NA_num = 0\n rel_num = 0\n tags_list = [self.id2tag[i] for i in tags_list]\n ent_and_position = metrics.get_entities(tags_list)\n for ent1 in ent_and_position:\n for ent2 in ent_and_position:\n if ent2 == ent1:\n continue\n ent2_for_ent1 = self.type2types.get(ent1[0], [])\n if ent2[0] not in ent2_for_ent1:\n continue\n entity_tuple = (ent1[1], ent1[2], ent2[1], ent2[2])\n # 0代表关系为NA\n re = golden_map.get(entity_tuple, self.r2id['NA'])\n ent_list = [entity_tuple[i] for i in range(4)]\n ent_list.append(re)\n if re == self.r2id['NA']:\n NA_entity.append(ent_list)\n else:\n all_entity.append(ent_list)\n rel_num = len(all_entity)\n if len(NA_entity) > 0:\n all_entity.extend(NA_entity[:min(2, len(NA_entity))])\n NA_num = min(opt.naNum, len(NA_entity))\n return all_entity, rel_num, NA_num\n\n def get_sens_and_tags_and_entsRel(self, datas, case=0):\n rel_max_sen = -1\n exceed_length_num = 0\n NA_num = 0\n max_r_num = 0\n all_rel_num = 0\n sens, tags, ent_rel = [], [], []\n PAD = self.tokenizer.convert_tokens_to_ids(['[PAD]'])\n O_tag = [self.type2id['O']]\n BIO_data = \"\"\n BIO_base =\"\"\n for data in datas:\n text = data['text']\n # 一共修改3处, util中一处 此文件两处去掉首位空格,然后将空格替换为@\n text = text.strip().replace(' ', '$')\n word_list = self.tokenizer.tokenize(text)\n sen = self.tokenizer.convert_tokens_to_ids(word_list)\n rel_max_sen = max(rel_max_sen, len(word_list))\n if len(word_list) > self.opt.seq_length:\n exceed_length_num += 1\n\n if len(word_list) < self.opt.seq_length:\n sen = sen + PAD * (self.opt.seq_length - len(sen))\n else:\n sen = sen[:self.opt.seq_length]\n sens.append(sen)\n\n # if case >= 2:\n # continue\n entity_list, type_list = self.get_entity_list_and_type_list(data['spo_list'])\n # __import__('ipdb').set_trace()\n # '▌1999年:「喜剧之王」前两年的贺岁档其实都有星爷,只不过作品票房一直跟不上'\n tag = self.get_tag(word_list, entity_list, type_list)\n assert len(word_list) == len(tag)\n if len(word_list) < self.opt.seq_length:\n tag = tag + O_tag * (self.opt.seq_length - len(tag))\n else:\n tag = tag[:self.opt.seq_length]\n tags.append(tag)\n de_t=detokenize(self,word_list,tag)\n for i,word in enumerate(word_list):\n if word == '$':\n continue\n BIO_data = BIO_data + word + \"\t\" + get_BIO(self, tag[i]) + \"\\n\"\n for i in range(len(de_t['toks'])):\n word=de_t['toks'][i]\n label=de_t['labels'][i]\n if word == '$':\n continue\n BIO_base = BIO_base + word + \"\t\" + label + \"\\n\"\n BIO_data = BIO_data + '\\n'\n BIO_base = BIO_base + '\\n'\n # __import__('ipdb').set_trace()\n exist_map = self.get_sample_exist_entity2rlation(word_list, data['spo_list'])\n if case == 0:\n all_e2r, rel_num, NAs = self.get_sample_all_entity2relation(tag, exist_map)\n NA_num += NAs\n else:\n all_e2r = []\n for key in exist_map.keys():\n e2r = [key[0], key[1], key[2], key[3], exist_map[key]]\n all_e2r.append(e2r)\n all_rel_num += len(exist_map)\n max_r_num = max(max_r_num, len(all_e2r))\n ent_rel.append(all_e2r)\n sens = np.array(sens)\n tags = np.array(tags)\n ent_rel = np.array(ent_rel)\n\n root_path = self.opt.data_root\n base=\"\"\n if case == 0:\n branch = 'train.tsv'\n base ='train_base.tsv'\n if case == 1:\n branch = 'dev.tsv'\n base ='dev_base.tsv'\n if case == 2:\n branch = 'test.tsv'\n base ='test_base.tsv'\n if case == 3:\n branch = 'test2.tsv'\n data_root = root_path\n print(\"存在关系数:{};NA关系数{}; 每句话中最大关系数(含NA):{}\".format(all_rel_num, NA_num, max_r_num))\n print(\"真实最大长度{}; 设置最大长度{}; 超过长度数{}\".format(rel_max_sen, self.opt.seq_length, exceed_length_num))\n print(\"saving data in {}\".format(data_root))\n\n with open(data_root+branch, 'w') as f:\n f.write(BIO_data)\n with open(data_root+base, 'w') as f:\n f.write(BIO_base)\n # np.save(data_root + 'sens', sens)\n # np.save(data_root + 'tags', tags)\n # np.save(data_root + 'relations', ent_rel)\n\n def process_data(self):\n if self.origin_train_data is not None:\n print(\"process train data\")\n self.get_sens_and_tags_and_entsRel(self.origin_train_data, case=0)\n if self.origin_dev_data is not None:\n print(\"process dev data\")\n self.get_sens_and_tags_and_entsRel(self.origin_dev_data, case=1)\n if self.origin_test1_data is not None:\n print(\"process test1 data\")\n self.get_sens_and_tags_and_entsRel(self.origin_test1_data, case=2)\n print(\"确定数据质量...\")\n # metrics.judge_data_quality(self.opt)\n\n\nif __name__ == '__main__':\n dataHelper = DataHelper(opt)\n # metrics.judge_data_quality(opt)\n dataHelper.process_data()\n" ]
[ [ "numpy.array" ] ]
jdieter31/riemannian-nlp
[ "75ef47608c81ec6e925fe24d16c67985e4b987c6" ]
[ "riemann/data/data_ingredient.py" ]
[ "from math import floor\n\nimport numpy as np\n\nfrom .graph import load_edge_list, load_adjacency_matrix\nfrom .graph_dataset import BatchedDataset\nfrom ..config_loader import get_config\n\n\ndef load_dataset():\n data_config = get_config().data\n\n if graph_data_type == \"edge\":\n idx, objects, weights = load_edge_list(data_config.path, data_config.symmetrize,\n delimiter=data_config.delimiter)\n else:\n idx, objects, weights = load_adjacency_matrix(data_config.path,\n data_config.graph_data_format,\n data_config.symmetrize)\n\n # define a feature function\n if data_config.object_id_to_feature_func == \"conceptnet\":\n features = [' '.join(object_id.split('_')) for object_id in objects]\n elif data_config.object_id_to_feature_func == \"wordnet\":\n # placental_mammal.n.01 -> placental mammal\n features = [' '.join(object_id.split('.')[0].split('_')) for object_id in objects]\n elif data_config.object_id_to_feature_func == \"id\":\n # xyz -> xyz\n features = [object_id for object_id in objects]\n else:\n features = None\n\n if make_eval_split:\n np.random.seed(data_config.split_seed)\n shuffle_order = np.arange(idx.shape[0])\n np.random.shuffle(shuffle_order)\n num_eval = floor(idx.shape[0] * data_config.split_size)\n eval_indices = shuffle_order[:num_eval]\n train_indices = shuffle_order[num_eval:]\n train_idx = idx[train_indices]\n train_weights = weights[train_indices]\n eval_idx = idx[eval_indices]\n eval_weights = weights[eval_indices]\n\n train_data = BatchedDataset(\n train_idx,\n objects,\n train_weights,\n data_config.manifold,\n data_config.n_graph_neighbors,\n data_config.n_manifold_neighbors,\n data_config.n_rand_neighbors,\n data_config.batch_size,\n data_config.num_workers,\n data_config.nn_workers,\n data_config.manifold_nn_k,\n features,\n saved_data_file=data_config.graph_data_file,\n gen_data=data_config.gen_graph_data\n )\n\n eval_data = BatchedDataset.initialize_eval_dataset(\n train_data,\n eval_batch_size,\n data_config.n_eval_neighbors,\n data_config.max_eval_graph_neighbors,\n data_config.eval_workers,\n data_config.eval_nn_workers,\n manifold_neighbors=data_config.eval_manifold_neighbors,\n eval_edges=eval_idx,\n eval_weights=eval_weights)\n\n return train_data, eval_data\n else:\n train_data = BatchedDataset(\n idx,\n objects,\n weights,\n manifold,\n data_config.n_graph_neighbors,\n data_config.n_manifold_neighbors,\n data_config.n_rand_neighbors,\n data_config.batch_size,\n data_config.num_workers,\n data_config.nn_workers,\n data_config.manifold_nn_k,\n features,\n saved_data_file=data_config.graph_data_file,\n gen_data=data_config.gen_graph_data)\n\n eval_data = BatchedDataset.initialize_eval_dataset(\n train_data,\n data_config.eval_batch_size,\n data_config.n_eval_neighbors,\n data_config.max_eval_graph_neighbors,\n data_config.eval_workers,\n data_config.eval_nn_workers,\n manifold_neighbors=data_config.eval_manifold_neighbors,\n saved_data_file=data_config.graph_data_file,\n gen_data=data_config.gen_graph_data)\n\n return train_data, eval_data\n\n\ndef get_adjacency_dict(data):\n adj = {}\n for row in data.idx:\n x = row[0]\n y = row[1]\n if x in adj:\n adj[x].add(y)\n else:\n adj[x] = {y}\n return adj\n" ]
[ [ "numpy.arange", "numpy.random.shuffle", "numpy.random.seed" ] ]
dpopadic/arpmRes
[ "ddcc4de713b46e3e9dcb77cc08c502ce4df54f76", "ddcc4de713b46e3e9dcb77cc08c502ce4df54f76", "ddcc4de713b46e3e9dcb77cc08c502ce4df54f76" ]
[ "scripts/sources/s_checklist_scenariobased_step07.py", "functions_legacy/GraphicalLasso.py", "scripts/sources/S_PricingEquityTaylor.py" ]
[ "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# ---\n# jupyter:\n# jupytext:\n# text_representation:\n# extension: .py\n# format_name: light\n# format_version: '1.4'\n# jupytext_version: 1.2.0\n# kernelspec:\n# display_name: Python 3\n# language: python\n# name: python3\n# ---\n\n# # s_checklist_scenariobased_step07 [<img src=\"https://www.arpm.co/lab/icons/icon_permalink.png\" width=30 height=30 style=\"display: inline;\">](https://www.arpm.co/lab/redirect.php?code=s_checklist_scenariobased_step07&codeLang=Python)\n# For details, see [here](https://www.arpm.co/lab/redirect.php?permalink=ex-vue-7).\n\n# +\nimport numpy as np\nimport pandas as pd\n\nfrom arpym.portfolio import spectral_index\nfrom arpym.statistics import meancov_sp, quantile_sp\n\n# -\n\n# ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-parameters)\n\n# +\n# indicates which projection to continue from\n# True: use copula-marginal projections\n# False: use historical projections\ncopula_marginal = True\n\nlam = 3e-7 # parameter of exponential utility function\nc_quantile = 0.95 # confidence level for the quantile satisfaction measure\nc_es = 0.95 # confidence level for the negative expected shortfall\n# -\n\n# ## [Step 0](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step00): Load data\n\n# +\npath = '../../../databases/temporary-databases/'\n\nif copula_marginal:\n # Projection\n db_projection_tools = pd.read_csv(path + 'db_projection_tools.csv')\n j_ = int(db_projection_tools['j_'][0])\n\n db_scenprob = pd.read_csv(path + 'db_scenario_probs.csv')\n p = db_scenprob['p'].values\n\n # Pricing\n db_pricing = pd.read_csv(path + 'db_pricing.csv')\n pi_tnow_thor = db_pricing.values\n\n # Aggregation\n db_exante_perf = pd.read_csv(path + 'db_exante_perf.csv')\n y_h = db_exante_perf.values.squeeze()\n\nelse:\n # Projection\n db_projection_tools = pd.read_csv(path + 'db_projection_bootstrap_tools.csv')\n j_ = int(db_projection_tools['j_'][0])\n\n db_scenprob = pd.read_csv(path + 'db_scenario_probs_bootstrap.csv')\n p = db_scenprob['p'].values\n\n # Pricing\n db_pricing = pd.read_csv(path + 'db_pricing_historical.csv')\n pi_tnow_thor = db_pricing.values\n\n # Aggregation\n db_exante_perf = pd.read_csv(path + 'db_exante_perf_historical.csv')\n y_h = db_exante_perf.values.squeeze()\n\ndb_holdings = pd.read_csv(path + 'db_holdings.csv')\nh = np.squeeze(db_holdings.values)\n# -\n\n# ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step01): Calculate certainty equivalent satisfaction measure\n\n# +\n# expected utility\nexpected_utility = p @ (-np.exp(-lam * y_h)) # expected utility computation\n\n# certainty equivalent satisfaction measure\ncert_eq_yh = -(1 / lam) * np.log(-expected_utility)\n# -\n\n# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step02): Quantile satisfaction measure\n\n# quantile\nq_yh = quantile_sp(1 - c_quantile, y_h, p, method='kernel_smoothing')\n\n\n# ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step03): Expected shortfall satisfaction measure\n\n# +\n# indicator function\ndef indicator(x):\n return (0 <= x and x <= 1 - c_es)\n\n\n# spectrum function\ndef spectr_es(x):\n return (1 / (1 - c_es)) * indicator(x)\n\n\n# negative expected shortfall\nes_yh, _ = spectral_index(spectr_es, pi_tnow_thor,\n p, h)\n# -\n\n# ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step04): Expectation and variance satisfaction measures\n\n# expectation satisfaction measure\nmean_yh, var_yh = meancov_sp(y_h, p)\n# opposite of variance is satisfaction measure\nneg_var_yh = -var_yh\n\n# ## [Step 5](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step07-implementation-step05): Save database\n\n# +\nout = pd.DataFrame({'cert_eq_yh': pd.Series(cert_eq_yh),\n 'q_yh': pd.Series(q_yh),\n 'es_yh': pd.Series(es_yh),\n 'mean_yh': pd.Series(mean_yh),\n 'neg_var_yh': pd.Series(neg_var_yh),\n 'c_es': pd.Series(c_es),\n 'c_quantile': pd.Series(c_quantile)})\nif copula_marginal:\n out.to_csv(path + 'db_quantile_and_satis.csv',\n index=False)\nelse:\n out.to_csv(path + 'db_quantile_and_satis_historical.csv',\n index=False)\n\ndel out\n", "import matplotlib.pyplot as plt\nfrom numpy import array, ones, zeros, cov\n\nplt.style.use('seaborn')\nfrom fglasso import glasso\n\n\ndef GraphicalLasso(pop, lam, initStruct=None, approximate=0, warmInit=0, verbose=0, penalDiag=1, tolThreshold=1e-4, maxIter=1e4, w=None, theta=None):\n # [w, theta, iter, avgTol, hasError] = GraphicalLasso(pop, lam,\n # initStruct, approximate, warmInit, verbose, penalDiag, tolThreshold,\n # maxIter, w, theta)\n #\n # Computes a regularized estimate of covariance matrix and its inverse\n # Inputs:\n # - pop: the set of samples to be used for covariance estimation in an NxP\n # matrix where N is the number of samples and P is the number of variables\n # - lam: the regularization penalty. Can be a single number or a matrix\n # of penalization values for each entry in the covariance matrix\n # - initStruct(o@): a matrix of size PxP, where zero entries will force\n # the corresponding entries in the inverse covariance matrix to be zero.\n # - approximate([o]): a flag indicating whether to use approximate estimation\n # (Meinhausen-Buhlmann approximation)\n # - warmInit[o]: a flag indicating whether the estimation will start from\n # given initial values of w and theta\n # - verbose([o]): a flag indicating whether to output algorithm process\n # - penalDiag[o]: a flag indicating whether to penalize diagonal elements of\n # the covariance matrix\n # - tolThreshold[o]: the amount of tolerance acceptable for covariance matrix\n # elements before terminating the algorithm\n # - maxIter([o]): maximum number of iteration to perform in the algorithm\n # - w[o]: the initial value of covariance matrix used for warm initialization\n # - theta([o]): the initial value of inverse covariance matrix used for warm\n # initialization\n # @: o indicates optional arguments\n # Outputs:\n # - w: the estimated covariance matrix\n # - theta: the estimated inverse covariance matrix\n # - iter: actual number of iterations performed in the algorithm\n # - avgTol: average tolerance of covariance matrix entries before\n # terminating the algorithm\n # - hasError: a flag indicating whether the algorithm terminated\n # erroneously or not\n #\n # Code by: Hossein Karshenas ([email protected])\n # Date: 10 Feb 2011\n\n numVars = pop.shape[1]\n if isinstance(lam,float):\n lam = array([[lam]])\n m, n = lam.shape\n if m != n:\n raise ValueError('Regularization coefficients matrix should be symmetric matrix')\n elif m > 1 and m != numVars:\n raise ValueError('Regularization coefficients matrix should have a size equal to the number of variables')\n if m == 1:\n lam = lam * ones((numVars,numVars))\n\n if initStruct is not None:\n initStruct = 1 - initStruct\n initStruct = 10e9*initStruct\n lam = lam + initStruct\n\n if w is None:\n if warmInit is False:\n raise ValueError('In warm initialization mode starting values for the covariance and precision matrices should be determined')\n else:\n w = zeros((numVars,numVars),order='F')\n if theta is None:\n if warmInit is False:\n raise ValueError('In warm initialization mode starting values for the precision matrix should be determined')\n else:\n theta = zeros((numVars,numVars),order='F')\n\n niter = 0\n jerr = 0.0\n # glasso(cov(pop.T), lam, 1, 1, 1, penalDiag, tolThreshold, maxIter, w, theta, niter, jerr, 1)\n glasso(cov(pop.T), lam, approximate, 0, verbose, penalDiag, tolThreshold, maxIter, w, theta, niter, jerr, numVars)\n # w, theta, iter, avgTol, hasError = glasso(cov(pop.T), lam, approximate, 0, verbose, penalDiag, tolThreshold, maxIter, w, theta, 0, 0, numVars)\n\n if False:\n raise Warning('The execution of the algorithm caused errors')\n return w, theta, niter, jerr\n\n", "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# ---\n# jupyter:\n# jupytext:\n# text_representation:\n# extension: .py\n# format_name: light\n# format_version: '1.4'\n# jupytext_version: 1.1.4\n# kernelspec:\n# display_name: Python 3\n# language: python\n# name: python3\n# ---\n\n# # S_PricingEquityTaylor [<img src=\"https://www.arpm.co/lab/icons/icon_permalink.png\" width=30 height=30 style=\"display: inline;\">](https://www.arpm.co/lab/redirect.php?code=S_PricingEquityTaylor&codeLang=Python)\n# For details, see [here](https://www.arpm.co/lab/redirect.php?permalink=eb-taylor-equity-pl).\n\n# ## Prepare the environment\n\n# +\nimport os\nimport os.path as path\nimport sys\n\nsys.path.append(path.abspath('../../functions-legacy'))\nfrom collections import namedtuple\n\nfrom numpy import arange, array, ones, zeros, sort, where, diff, linspace, round, log, exp, sqrt\nfrom numpy import sum as npsum, min as npmin, max as npmax\n\nfrom scipy.stats import norm\nfrom scipy.io import loadmat\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import figure, plot, legend, xlim, ylabel, \\\n xlabel, title, xticks, yticks\n\nplt.style.use('seaborn')\n\nfrom CONFIG import GLOBAL_DB, TEMPORARY_DB\nfrom ARPM_utils import save_plot, struct_to_dict\nfrom FPmeancov import FPmeancov\nfrom HistogramFP import HistogramFP\nfrom SaddlePointQuadN import SaddlePointQuadN\nfrom SimulateBrownMot import SimulateBrownMot\n# -\n\n# ## run S_PricingEquityProfitAndLoss\n\nfrom S_PricingEquityProfitAndLoss import *\n\n# ## Compute the first order approximation of the equity P&L, which has normal distribution,\n# ## and the second order approximation of the equity P&L, that has generalized non-central gamma distribution,\n# ## at the selected horizon (120 days). Use function SaddlePointQuadN to compute the cumulative distribution\n# ## function of the generalized non-central gamma distribution.\n\n# +\nn_ = 500\nhor_sel = 120 # selected horizon for the plot (120 days)\ni = where(horiz_u == hor_sel)[0][-1]\nx_hor = zeros((n_, i+1))\nTaylor_first = zeros((n_, i+1))\ncdf_QuadN = zeros((n_, i+1))\nTaylor_second = zeros((n_, i+1))\n\nx_hor[:,i] = linspace(Mu_PL[0, i] - 10*Sigma_PL[0, i], Mu_PL[0, i] + 10 *Sigma_PL[0, i], n_)\n# first order approximation (normal pdf)\nTaylor_first[:,i] = norm.pdf(x_hor[:,i], exp(x[0,-1])*mu*horiz_u[i + 1],exp(x[0,-1])*sig*sqrt(horiz_u[i + 1]))\n# second order approximation (QuadN pdf)\nb, c, mu2, sigma2 = array([[exp(x[0,-1])]]), array([[exp(x[0,-1])*0.5]]), mu*horiz_u[i + 1], sig**2*horiz_u[i + 1]\n_, Taylor_second[:,i] = SaddlePointQuadN(x_hor[:,[i]].T, 0, b, c, mu2, sigma2) # QuadN cumulative density function\n# Taylor_second(:,i) = diff(cdf_QuadN(:,i))/diff(x_hor((:,i)))\n# -\n\n# ## Plot a few (say 15) simulated paths of the equity P&L up to the selected horizon (120 days),\n# ## along with the first order approximation, the second order approximation and the analytical\n# ## distribution of the equity P&L. Furthermore, show the mean and the standard deviation of\n# ## the analytical distribution.\n\n# +\nlgrey = [0.8, 0.8, 0.8] # light grey\ndgrey = [0.4, 0.4, 0.4] # dark grey\nlblue = [0.27, 0.4, 0.9] # light blu\norange = [0.94, 0.35, 0] # orange\nj_sel = 15 # selected MC simulations\n\nfigure()\n# simulated path, mean and standard deviation\nplot(horiz_u[:i], PL[:j_sel,:i].T, color=lgrey)\nplt.xticks(arange(0,t_end+20,20))\nxlim([npmin(horiz_u), npmax(horiz_u)+1])\nl1 = plot(horiz_u[:i], Mu_PL[0,:i], color='g', label='mean')\nl2 = plot(horiz_u[:i], Mu_PL[0,:i] + Sigma_PL[0,:i], color='r', label=' + / - st.deviation')\nplot(horiz_u[:i], Mu_PL[0,:i] - Sigma_PL[0,:i], color='r')\n\n# analytical pdf\nflex_probs_scenarios = ones((1, j_)) / j_\noption = namedtuple('option','n_bins')\noption = namedtuple('option', 'n_bins')\noption.n_bins = round(10 * log(j_))\ny_hist, x_hist = HistogramFP(PL[:,[i]].T, flex_probs_scenarios, option)\nscale = 0.15 * Sigma_PL[0, i] / npmax(y_hist)\ny_hist = y_hist * scale\nshift_y_hist = horiz_u[i] + y_hist\nemp_pdf = plt.barh(x_hist[:-1], shift_y_hist[0]-npmin(shift_y_hist[0]), height=x_hist[1]-x_hist[0],\n left=npmin(shift_y_hist[0]), facecolor=lgrey, edgecolor=lgrey,label='horizon pdf') # empirical pdf\nplot(shift_y_hist[0], x_hist[:-1], color=dgrey) # border\n\n# first order approximation\nTaylor_first[:,i] = Taylor_first[:,i]*scale\nshift_T_first = horiz_u[i] + Taylor_first[:,i]\nl3 = plot(shift_T_first, x_hor[:,i], color=orange, label='first order approx')\n\n# second order approximation\nTaylor_second[:,i] = Taylor_second[:,i]*scale\nshift_T_second = horiz_u[i] + Taylor_second[:,i]\nl4 = plot(shift_T_second, x_hor[:,i], color=lblue, label='second order approx')\n\nlegend()\nxlabel('time (days)')\nylabel('P&L')\ntitle('P&L equity Taylor approximation');\n# save_plot(ax=plt.gca(), extension='png', scriptname=os.path.basename('.')[:-3], count=plt.get_fignums()[-1])\n" ]
[ [ "pandas.Series", "numpy.squeeze", "pandas.read_csv", "numpy.exp", "numpy.log" ], [ "numpy.ones", "matplotlib.pyplot.style.use", "numpy.zeros", "numpy.array", "numpy.cov" ], [ "numpy.ones", "matplotlib.pyplot.style.use", "matplotlib.pyplot.legend", "numpy.zeros", "matplotlib.pyplot.figure", "numpy.exp", "matplotlib.pyplot.title", "numpy.arange", "numpy.max", "matplotlib.pyplot.ylabel", "numpy.min", "numpy.log", "numpy.sqrt", "matplotlib.pyplot.plot", "numpy.where", "numpy.linspace", "matplotlib.pyplot.xlabel" ] ]
SayanGhoshBDA/code-backup
[ "8b6135facc0e598e9686b2e8eb2d69dd68198b80" ]
[ "python/data_sutram/scraper/appl_map.py" ]
[ "\"\"\"\nhttps://sat-cdn1.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=84135&y=202065&v=4002&accessKey=1549129912_6641142575737855346_%2F_9%2F4MX0U5yhJDc3LDXazhcQj3xjCJU%2BYsiKcviN%2FnWxE%3D&emphasis=standard&tint=dark\n\nhttps://sat-cdn4.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=84135&y=202061&v=4002&accessKey=1549129912_6641142575737855346_%2F_9%2F4MX0U5yhJDc3LDXazhcQj3xjCJU%2BYsiKcviN%2FnWxE%3D&emphasis=standard&tint=dark\n\nhttps://sat-cdn4.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=84135&y=202064&v=4002&accessKey=1549129912_6641142575737855346_%2F_9%2F4MX0U5yhJDc3LDXazhcQj3xjCJU%2BYsiKcviN%2FnWxE%3D&emphasis=standard&tint=dark\n\nhttps://sat-cdn3.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=84122&y=202064&v=4002&accessKey=1549131702_8398681963599501052_%2F_oQrt5vqvzaVZBN%2FLBc5baLQgg5kEhfpKMYQDRlmZ36Q%3D&emphasis=standard&tint=dark\n\nhttps://sat-cdn3.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=84127&y=202067&v=4002&accessKey=1549131702_8398681963599501052_%2F_oQrt5vqvzaVZBN%2FLBc5baLQgg5kEhfpKMYQDRlmZ36Q%3D&emphasis=standard&tint=dark\nx\n38.089967/84127\n0.0004527674468363308\ny\n-122.236446/202067\n-0.0006049302756016569\n\nx_conv = 2208.8242126933765\n\ny_conv = -1653.078168433501\n\nhttps://sat-cdn1.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=371353&y=184495&v=4002&accessKey=1549132809_7090421196284837684_%2F_gT3s2ghdt72RemReCoMIf13JXH%2BE0rbJKjODBV6pfQc%3D&emphasis=standard&tint=dark\nhttps://sat-cdn1.apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=323660&y=198484&v=4002&accessKey=1549132809_7090421196284837684_/_gT3s2ghdt72RemReCoMIf13JXH+E0rbJKjODBV6pfQc=&emphasis=standard&tint=dark\n\n\n\"\"\"\nimport urllib.request, urllib.parse, urllib.error\nfrom bs4 import BeautifulSoup\nimport ssl\nimport os\nimport wget\nimport imghdr\nimport numpy as np\nimport requests\nimport time\n# Ignore SSL certificate errors\nctx = ssl.create_default_context()\nctx.check_hostname = False\nctx.verify_mode = ssl.CERT_NONE\nimport math\nPI = math.pi\ncdn = [1,2,3,4]\nheaders = {\n 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0'#,\n #'From': '[email protected]' # This is another valid field\n}\n\ndef ret_xy_tiles(lat_deg,lon_deg,zoom):\n n = 2**zoom\n #lon_deg = xtile / n * 360.0 - 180.0\n \n #lat_deg = lat_rad * 180.0 / π\n xtile = n * ((lon_deg + 180) / 360)\n #lat_rad = math.atan(math.asinh(PI * (1 - 2 * ytile / n)))\n lat_rad = lat_deg * PI / 180.0\n ytile = n * (1 - (math.log(math.tan(lat_rad) + 1/math.cos(lat_rad)) / PI)) / 2\n return math.floor(xtile),math.floor(ytile)\n\ndef make_url(lat_deg,lon_deg,zoom):\n x_tyle,y_tyle = ret_xy_tiles(lat_deg,lon_deg,zoom)\n for i in cdn:\n now_time = time.time()\n #url_str = \"https://sat-cdn\"+str(i)+\".apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=\"+str(x_tyle)+\"&y=\"+str(y_tyle)+\"&v=4002&accessKey=\"+str(math.floor(now_time)-10)+\"_8618194673608956327_%2F_AaK0kkbh9QXsy5VX35tXlchP3bjj1%2FkjYGmHywouu0E%3D&emphasis=standard&tint=dark\"\n url_str = \"https://sat-cdn\"+str(i)+\".apple-mapkit.com/tile?style=7&size=1&scale=1&z=19&x=\"+str(x_tyle)+\"&y=\"+str(y_tyle)+\"&v=4002&accessKey=1549197942_115647566105935154_%2F_b3rw2rvOuQvkjh0rILQSaaW3GxphOI%2BXJo48fORdf0Y%3D&emphasis=standard&tint=dark\"\n print(\"Trying :: \",url_str)\n try:\n file_name = str(x_tyle)+\"_\"+str(y_tyle)+\".jpeg\"\n #file_name = wget.download(url_str)\n #file_name.replace(\".tmp\", \"{x_tyle}_{y_tyle}.jpg\")\n r = requests.get(url_str, allow_redirects=True,headers=headers)\n open(file_name, 'wb').write(r.content)\n \n if imghdr.what(file_name) is 'jpeg':\n print(\"JPEG\")\n else:\n os.remove(file_name)\n print(\"NOT JPEG\")\n except:\n print(\"Ops Blown Off!\")\n\nif __name__ == \"__main__\":\n #make_url(38.085668,-122.235644,19)\n for i in np.arange(10,45,0.1):\n for j in np.arange(10,100,0.1):\n print(\"i=\",i,\"j=\",j)\n make_url(i,j,19)" ]
[ [ "numpy.arange" ] ]
gerardrbentley/peak-weather
[ "2880184c99c2d30075665f76a9e8b815906a55e5" ]
[ "streamlit_app/streamlit_app.py" ]
[ "from copy import deepcopy\nimport asyncio\nimport json\n\nimport pandas as pd\nimport streamlit as st\nfrom structlog import get_logger\n\nfrom helpers import (\n fromtimestamp,\n show_weather,\n WeatherItem,\n gather_one_call_weather_data,\n clean_time,\n)\n\nlog = get_logger()\nst.set_page_config(\n layout=\"wide\",\n page_title=\"Peak Weather: 4,000 Footers\",\n page_icon=\":mountain:\",\n)\n\n\[email protected](ttl=60 * 60)\ndef load_data(lat_lon_pairs: list) -> list:\n \"\"\"Function to fetch Open Weather data and cache results\n\n Args:\n lat_lon_pairs (list): Destinations to get data for\n\n Returns:\n list: List of dictionaries which are json responses from open weather\n \"\"\"\n log.info(\"Start Load Data\")\n data = asyncio.run(gather_one_call_weather_data(lat_lon_pairs))\n log.info(\"Returning Load Data\")\n return data\n\n\[email protected]()\ndef load_metadata() -> pd.DataFrame:\n \"\"\"Function to read mountain lat, lon, and other metadata and cache results\n\n Returns:\n pd.DataFrame: df containing information for 48 mountains\n \"\"\"\n df = pd.read_csv(\"./data/mountains.csv\")\n df = df.sort_values(\"name\")\n return df\n\n\ndef get_mtn_anchor(mountain: str) -> str:\n anchor = mountain.lower().replace(\" \", \"-\")\n return f\"[{mountain}](#{anchor})\"\n\n\ndef main():\n \"\"\"Main Streamlit App Entrypoint\"\"\"\n st.title(\n \":sunny::mountain::rainbow: Peak Weather of the 4,000 Footers :rainbow::mountain::sunny:\"\n )\n st.header(\":umbrella: You can't stop the rain, but you can spot it! :umbrella:\")\n with st.expander(\"Expand for Basic App Information:\"):\n st.markdown(\n \"\"\"\\\n# Peak Weather: New Hampshire's 4,000 Footers\n\nBuilt to give you a dashboard view of the next few hours' forecast for New Hampshire's 48 4,000 ft mountains.\nGonna rain on the Kinsmans?\nIs it snowing on Washington?\nShould I hike Owl's Head?\n\nPowered by [Streamlit](https://docs.streamlit.io/) + [Open Weather API](https://openweathermap.org/api).\nSpecifically, Streamlit runs the web interactinos and OpenWeather provides the data.\n\nBuilt with :heart: from [Gar's Bar](https://tech.gerardbentley.com) by Gerard Bentley\n\"\"\"\n )\n with st.spinner(\"Loading Mountain List\"):\n base_mountains = load_metadata()\n\n with st.expander(\"Expand for Basic Mountain Information: \"):\n st.dataframe(base_mountains)\n\n with st.spinner(\"Fetching Weather Data\"):\n lat_lon_pairs = zip(base_mountains.lat, base_mountains.lon)\n cached_responses = load_data(lat_lon_pairs)\n weather_responses = deepcopy(cached_responses)\n\n first_response = weather_responses[0]\n log.info(\"Weather Response\", first_response=first_response)\n if \"current\" not in first_response:\n st.error(\n \"\"\"\\\n### Oof...\n\nOpen Weather API can't be reached for data at the moment.\nApologies, feel free to check back soon.\"\"\"\n )\n st.write(\n f\"## Time: {fromtimestamp(first_response['current']['dt']).strftime('%I:%M:%S %p, %b %d %Y')}\"\n )\n table = []\n\n table.append(\"| Mountains | | |\")\n table.append(\"|---|---|---|\")\n for left, middle, right in zip(\n base_mountains.name[::3], base_mountains.name[1::3], base_mountains.name[2::3]\n ):\n table.append(\n f\"| {get_mtn_anchor(left)} | {get_mtn_anchor(middle)} | {get_mtn_anchor(right)} |\"\n )\n st.markdown(\"\\n\".join(table))\n\n for mountain, response in zip(base_mountains.name, weather_responses):\n st.write(\"-\" * 88)\n st.write(f\"#### {mountain}\")\n st.write(f\"({response['lat']}, {response['lon']})\")\n st.write(f\"Weather {clean_time(response['current']['dt'])}: \")\n current_temperature = round(response[\"current\"][\"temp\"], 1)\n st.metric(\"Temp (F)\", current_temperature, 0.0)\n for weather in response[\"current\"][\"weather\"]:\n weather_item = WeatherItem(**weather)\n show_weather(weather_item)\n\n with st.expander(\"Expand for future forecast:\"):\n for col, entry in zip(st.columns(5), response[\"hourly\"][1:]):\n col.write(f\"{clean_time(entry['dt'])}\")\n temperature = round(entry[\"temp\"], 1)\n col.metric(\n \"Temp (F)\", temperature, round(temperature - current_temperature, 1)\n )\n for weather in entry[\"weather\"]:\n weather_item = WeatherItem(**weather)\n show_weather(weather_item, col)\n current_temperature = temperature\n alerts = response.get(\"alerts\")\n if alerts is not None:\n for alert in alerts:\n body = (\n f\"### Alert From {alert['sender_name']}: {alert['event']}\",\n f\"Duration: {fromtimestamp(alert['start'])} - {fromtimestamp(alert['end'])}\",\n alert[\"description\"],\n f\"Tags: {'; '.join(alert['tags'])}\",\n )\n\n st.warning(\"\\n\".join(body))\n\n\nif __name__ == \"__main__\":\n main()\n" ]
[ [ "pandas.read_csv" ] ]
ErwindeGelder/ScenarioRiskQuantification
[ "ee351b7bd3629af0c1d1d800dcc5ac5426b9b804" ]
[ "simulation/acc_idmplus.py" ]
[ "\"\"\" Model of ACC with FCW and IDM+ to take over from Xiao et al. (2017).\n\nCreation date: 2020 08 12\nAuthor(s): Erwin de Gelder\n\nModifications:\n\"\"\"\n\nimport numpy as np\nfrom .acc import ACC, ACCParameters, ACCState\nfrom .idm import IDMParameters\nfrom .idmplus import IDMPlus\n\n\nclass ACCIDMPlusParameters(ACCParameters):\n \"\"\" Parameters for the ACCIDMPlus. \"\"\"\n fcw_threshold: float = 0.75 # [0-1] If probability is above, give warning.\n fcw_delay: float = 1.0 # [s] After this delay, driver takes over.\n driver_takeover_speed: float = 15.0 # [m/s] Speed difference at which driver takes control.\n driver_takeover_view: float = 150 # [m] Only take over if car is within this distance.\n driver_model: IDMPlus = None # The model of the driver.\n driver_parms: IDMParameters = None # Parameters of the driver model.\n\n def __init__(self, **kwargs):\n self.driver_model = IDMPlus()\n ACCParameters.__init__(self, **kwargs)\n\n\nclass ACCIDMPlusState(ACCState):\n \"\"\" State of the ACCIDMPlus. \"\"\"\n fcw: bool = False\n samples_since_fcw: int = 0\n samples_in_view: int = 0\n driver_takeover: bool = False\n\n\nclass ACCIDMPlus(ACC):\n \"\"\" Class for simulation of the human (IDMplus) + ACC. \"\"\"\n def __init__(self):\n ACC.__init__(self)\n self.parms = ACCIDMPlusParameters()\n self.state = ACCIDMPlusState()\n\n self.nstep = 0\n\n def init_simulation(self, parms: ACCIDMPlusParameters) -> None:\n \"\"\" Initialize the simulation.\n\n See the ACC for the default parameters.\n The following additional parameters can also be set:\n fcw_threshold: float = 0.75 # [0-1] If probability is above, give warning.\n fcw_delay: float = 1.0 # [s] After this delay, driver takes over.\n driver_takeover_speed: float = 15.0 # [m/s] Speed difference at which driver takes control.\n driver_takeover_view: float = 150 # [m] Only take over if car is within this distance.\n driver_model: StandardModel = None # The model of the driver.\n driver_parms: StandardParameters = None # Parameters of the driver model.\n\n :param parms: The parameters listed above.\n \"\"\"\n # Set the parameters of the ACC model.\n ACC.init_simulation(self, parms)\n\n # Initialize the driver model.\n self.parms.driver_model = parms.driver_model\n self.parms.driver_parms = parms.driver_parms\n self.parms.driver_model.init_simulation(parms.driver_parms)\n\n # Initialize parameters of the Forward Collision Warning\n self.parms.fcw_delay, self.parms.fcw_threshold = parms.fcw_delay, parms.fcw_threshold\n\n # Reset the state regarding the takeover.\n self.state.fcw = False\n self.state.samples_since_fcw = 0\n self.state.samples_in_view = 0\n self.state.driver_takeover = False\n\n self.nstep = 0\n\n def step_simulation(self, leader) -> None:\n self.nstep += 1\n self.integration_step()\n\n # Update the driver model.\n self.parms.driver_model.step_simulation(leader)\n\n # If the FCW is active for longer than `fcw_delay`, the driver is active.\n # Note: additional requirement is that the car should be in the viewing range for at least\n # as long as the reactiontime of the driver.\n if leader.state.position-self.state.position < self.parms.driver_parms.max_view:\n self.state.samples_in_view += 1\n if self.state.fcw:\n self.state.samples_since_fcw += 1\n elif not self.state.driver_takeover:\n self.state.fcw = self.fcw_warning(leader)\n if not self.state.driver_takeover and self.state.fcw:\n if self.state.samples_since_fcw*self.parms.timestep >= self.parms.fcw_delay and \\\n self.state.samples_in_view > self.parms.driver_parms.n_reaction+1:\n self.state.driver_takeover = True\n\n # Following Xiao et al. (2017), the driver takes over if approaching speed > 15 m/s and\n # car is within 150 m. The speed (15 m/s) and view (150 m) are parameterized with\n # `driver_take_over_speed`, `driver_take_over_view`.\n # As a additional requirement, the driver should brake.\n if not self.state.driver_takeover:\n if self.state.speed-leader.state.speed > self.parms.driver_takeover_speed and \\\n (leader.state.position-self.state.position) < self.parms.driver_takeover_view and \\\n self.parms.driver_model.state.acceleration < 0:\n self.state.driver_takeover = True\n\n if self.state.driver_takeover:\n # Update our own states with that from the driver model.\n self.state.position = self.parms.driver_model.state.position\n self.state.speed = self.parms.driver_model.state.speed\n self.state.acceleration = self.parms.driver_model.state.acceleration\n else:\n # Update the states of the driver model with that from the ACC.\n self.update(leader.state.position-self.state.position,\n self.state.speed,\n self.state.speed-leader.state.speed)\n\n self.parms.driver_model.state.position = self.state.position\n self.parms.driver_model.state.speed = self.state.speed\n self.parms.driver_model.state.acceleration = self.state.acceleration\n\n def fcw_warning(self, leader) -> bool:\n \"\"\" Issue a FCW based on the model of Kiefer et al.\n\n :param leader: The leading vehicle that contains position and speed.\n :return: Whether an FCW will be issued.\n \"\"\"\n inv_ttc = ((self.state.speed - leader.state.speed) /\n (leader.state.position - self.state.position))\n if leader.state.speed > 0 > leader.state.acceleration:\n tmp = -6.092 + 18.816*inv_ttc + 0.119*self.state.speed\n elif leader.state.speed > 0:\n tmp = -6.092 + 12.584*inv_ttc + 0.119*self.state.speed\n else:\n tmp = -9.073 + 24.225*inv_ttc + 0.119*self.state.speed\n\n probability = 1 / (1 + np.exp(-tmp))\n if probability > self.parms.fcw_threshold:\n return True\n return False\n" ]
[ [ "numpy.exp" ] ]
yqshao/PiNN
[ "464034dda44ce053cf1255a3e7d367c636d6b9f3" ]
[ "pinn/models/base.py" ]
[ "# -*- coding: utf-8 -*-\n\"\"\"Basic functions for PiNN models\"\"\"\nimport tensorflow as tf\nfrom pinn.utils import pi_named\n\ndef export_model(model_fn):\n # default parameters for all models\n from pinn.optimizers import default_adam\n default_params = {'optimizer': default_adam}\n def pinn_model(params, **kwargs):\n model_dir = params['model_dir']\n params_tmp = default_params.copy()\n params_tmp.update(params)\n params = params_tmp\n model = tf.estimator.Estimator(\n model_fn=model_fn, params=params, model_dir=model_dir, **kwargs)\n return model\n return pinn_model\n\nclass MetricsCollector():\n def __init__(self, mode):\n self.mode = mode\n self.LOSS = []\n self.ERROR = []\n self.METRICS = {}\n\n def add_error(self, tag, data, pred, mask=None, weight=1.0,\n use_error=True, log_error=True, log_hist=True):\n \"\"\"Add the error\n\n Args:\n tag (str): name of the error.\n data (tensor): data label tensor.\n pred (tensor): prediction tensor.\n mask (tensor): default to None (no mask, not implemented yet).\n weight (tensor): default to 1.0.\n mode: ModeKeys.TRAIN or ModeKeys.EVAL.\n return_error (bool): return error vector (for usage with Kalman Filter).\n log_loss (bool): log the error and loss function.\n log_hist (bool): add data and predicition histogram to log.\n log_mae (bool): add the mean absolute error to log.\n log_rmse (bool): add the root mean squared error to log.\n \"\"\"\n error = data - pred\n weight = tf.cast(weight, data.dtype)\n if self.mode == tf.estimator.ModeKeys.TRAIN:\n if log_hist:\n tf.compat.v1.summary.histogram(f'{tag}_DATA', data)\n tf.compat.v1.summary.histogram(f'{tag}_PRED', pred)\n tf.compat.v1.summary.histogram(f'{tag}_ERROR', error)\n if log_error:\n mae = tf.reduce_mean(tf.abs(error))\n rmse = tf.sqrt(tf.reduce_mean(error**2))\n tf.compat.v1.summary.scalar(f'{tag}_MAE', mae)\n tf.compat.v1.summary.scalar(f'{tag}_RMSE', rmse)\n if mask is not None:\n error = tf.boolean_mask(error, mask)\n if use_error:\n loss = tf.reduce_mean(error**2 * weight)\n tf.compat.v1.summary.scalar(f'{tag}_LOSS', loss)\n self.ERROR.append(error*tf.math.sqrt(weight))\n self.LOSS.append(loss)\n if self.mode == tf.estimator.ModeKeys.EVAL:\n if log_error:\n self.METRICS[f'METRICS/{tag}_MAE'] = tf.compat.v1.metrics.mean_absolute_error(data, pred)\n self.METRICS[f'METRICS/{tag}_RMSE'] = tf.compat.v1.metrics.root_mean_squared_error(data, pred)\n if mask is not None:\n error = tf.boolean_mask(error, mask)\n if use_error:\n loss = tf.reduce_mean(error**2 * weight)\n self.METRICS[f'METRICS/{tag}_LOSS'] = tf.compat.v1.metrics.mean(loss)\n self.LOSS.append(loss)\n\n\n@pi_named('TRAIN_OP')\ndef get_train_op(optimizer, metrics, tvars, separate_errors=False):\n \"\"\"\n Args:\n optimizer: a PiNN optimizer config.\n params: optimizer parameters.\n loss: scalar loss function.\n error: a list of error vectors (reserved for EKF).\n network: a PiNN network instance.\n sperate_errors (bool): separately update elements in the metrics\n \"\"\"\n from pinn.optimizers import get, EKF, gEKF\n import numpy as np\n\n optimizer = get(optimizer)\n optimizer.iterations = tf.compat.v1.train.get_or_create_global_step()\n nvars = np.sum([np.prod(var.shape) for var in tvars])\n print(f'{nvars} trainable vaiabless, training with {tvars[0].dtype.name} precision.')\n\n if not (isinstance(optimizer, EKF) or isinstance(optimizer, gEKF)):\n loss_list = metrics.LOSS\n if separate_errors:\n selection = tf.random.uniform([], maxval= len(loss_list), dtype=tf.int32)\n loss = tf.stack(loss_list)[selection]\n else:\n loss = tf.reduce_sum(loss_list)\n grads = tf.gradients(loss, tvars)\n return optimizer.apply_gradients(zip(grads, tvars))\n else:\n error_list = metrics.ERROR\n # EKF error vectors are scaled\n if isinstance(optimizer, EKF):\n error = tf.concat([tf.reshape(e, [-1])/tf.math.sqrt(tf.cast(tf.size(e), e.dtype))\n for e in error_list], 0)\n # gEKF should handle this automatically\n if isinstance(optimizer, gEKF):\n error = tf.concat([tf.reshape(e, [-1]) for e in error_list], 0)\n if separate_errors:\n selection = tf.random.uniform([], maxval= len(error_list), dtype=tf.int32)\n mask = tf.concat([tf.fill([tf.size(e)], tf.equal(selection,i))\n for i,e in enumerate(error_list)], 0)\n error = tf.boolean_mask(error, mask)\n return optimizer.get_train_op(error, tvars)\n" ]
[ [ "tensorflow.reshape", "tensorflow.abs", "tensorflow.reduce_sum", "tensorflow.stack", "tensorflow.compat.v1.metrics.mean", "tensorflow.cast", "tensorflow.gradients", "numpy.prod", "tensorflow.boolean_mask", "tensorflow.compat.v1.metrics.mean_absolute_error", "tensorflow.estimator.Estimator", "tensorflow.size", "tensorflow.math.sqrt", "tensorflow.equal", "tensorflow.compat.v1.summary.scalar", "tensorflow.compat.v1.metrics.root_mean_squared_error", "tensorflow.reduce_mean", "tensorflow.compat.v1.summary.histogram", "tensorflow.compat.v1.train.get_or_create_global_step" ] ]
danielabler/glimslib
[ "3d345bf3ed2d364e83a00ad9297dd5f81d7193db" ]
[ "glimslib/simulation_helpers/test_unit_timeSeriesMultiData.py" ]
[ "from unittest import TestCase\nimport os\nimport numpy as np\n\nimport glimslib.utils.file_utils as fu\nfrom glimslib import fenics_local as fenics, config\nfrom glimslib.simulation_helpers.helper_classes import FunctionSpace, TimeSeriesMultiData\n\n\nclass TestTimeSeriesMultiData(TestCase):\n\n def setUp(self):\n # Domain\n nx = ny = nz = 10\n mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny)\n # function spaces\n displacement_element = fenics.VectorElement(\"Lagrange\", mesh.ufl_cell(), 1)\n concentration_element = fenics.FiniteElement(\"Lagrange\", mesh.ufl_cell(), 1)\n element = fenics.MixedElement([displacement_element, concentration_element])\n subspace_names = {0: 'displacement', 1: 'concentration'}\n self.functionspace = FunctionSpace(mesh)\n self.functionspace.init_function_space(element, subspace_names)\n # build a 'solution' function\n u_0_conc_expr = fenics.Expression('sqrt(pow(x[0]-x0,2)+pow(x[1]-y0,2)) < 0.1 ? (1.0) : (0.0)', degree=1,\n x0=0.25,\n y0=0.5)\n u_0_disp_expr = fenics.Constant((1.0, 0.0))\n self.U = self.functionspace.project_over_space(function_expr={0: u_0_disp_expr, 1: u_0_conc_expr})\n\n def test_register_time_series(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n self.assertTrue(hasattr(tsmd, tsmd.time_series_prefix+'solution'))\n tsmd.register_time_series(name='solution2', functionspace=self.functionspace)\n self.assertTrue(hasattr(tsmd, tsmd.time_series_prefix+'solution2'))\n\n def test_get_time_series(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n self.assertEqual(tsmd.get_time_series('solution'), getattr(tsmd, tsmd.time_series_prefix+'solution'))\n\n def test_add_observation(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=1)\n self.assertEqual(tsmd.get_time_series('solution').get_observation(1).get_time_step(), 1)\n\n def test_get_observation(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=1)\n self.assertEqual(tsmd.get_time_series('solution').get_observation(1),\n tsmd.get_observation('solution', 1))\n self.assertEqual(tsmd.get_observation('solution3', 1), None)\n\n def test_get_solution_function(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=1)\n u = tsmd.get_solution_function('solution', subspace_id=None, recording_step=1)\n self.assertEqual(u.function_space(), self.U.function_space())\n self.assertNotEqual(u, self.U)\n\n def test_get_all_time_series(self):\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.register_time_series(name='solution2', functionspace=self.functionspace)\n ts_dict = tsmd.get_all_time_series()\n self.assertEqual(len(ts_dict), 2)\n self.assertTrue('solution' in ts_dict.keys())\n self.assertTrue('solution2' in ts_dict.keys())\n\n def test_save_to_hdf5(self):\n path_to_file = os.path.join(config.output_dir_testing, 'timeseries_to_hdf5.h5')\n fu.ensure_dir_exists(path_to_file)\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=2)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=3)\n tsmd.register_time_series(name='solution2', functionspace=self.functionspace)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=2)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=3)\n tsmd.save_to_hdf5(path_to_file, replace=True)\n # path_to_file2 = os.path.join(config.output_dir_testing, 'timeseries_to_hdf5_manual.h5')\n # hdf = fenics.HDF5File(self.functionspace._mesh.mpi_comm(), path_to_file2, \"w\")\n # function = tsmd.get_solution_function('solution', recording_step=1)\n # hdf.write(function, 'solution', 1)\n # hdf.write(function, 'solution', 2)\n # hdf.close()\n\n def test_load_from_hdf5(self):\n path_to_file = os.path.join(config.output_dir_testing, 'timeseries_to_hdf5_for_reading.h5')\n fu.ensure_dir_exists(path_to_file)\n # create file\n tsmd = TimeSeriesMultiData()\n tsmd.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=2)\n tsmd.add_observation('solution', field=self.U, time=1, time_step=1, recording_step=3)\n tsmd.register_time_series(name='solution2', functionspace=self.functionspace)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=1)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=2)\n tsmd.add_observation('solution2', field=self.U, time=1, time_step=1, recording_step=3)\n tsmd.save_to_hdf5(path_to_file, replace=True)\n # read file\n tsmd2 = TimeSeriesMultiData()\n tsmd2.register_time_series(name='solution', functionspace=self.functionspace)\n tsmd2.register_time_series(name='solution2', functionspace=self.functionspace)\n tsmd2.load_from_hdf5(path_to_file)\n self.assertEqual(len(tsmd2.get_all_time_series()),2)\n self.assertEqual(len(tsmd2.get_time_series('solution').get_all_recording_steps()),3)\n self.assertEqual(len(tsmd2.get_time_series('solution2').get_all_recording_steps()), 3)\n u_reloaded = tsmd2.get_solution_function(name='solution')\n # print(u_reloaded.vector().array())\n # print(self.U.vector().array())\n array_1 = u_reloaded.vector().get_local()\n array_2 = self.U.vector().get_local()\n self.assertTrue(np.allclose(array_1, array_2))\n\n\n\n" ]
[ [ "numpy.allclose" ] ]
linthieda/DeepFEH
[ "273b5c695674e0d22bc7f701a5d64113034b04de" ]
[ "model/a2c.py" ]
[ "import numpy as np\n\nimport matplotlib\nmatplotlib.use('Agg')\nimport matplotlib.pyplot as plt\n\nfrom reinforce import Reinforce\nfrom model import CriticNet\n\nimport feh_simulator.simulator as gym\n\nclass A2C(Reinforce):\n # Implementation of N-step Advantage Actor Critic.\n # This class inherits the Reinforce class, so for example, you can reuse\n # generate_episode() here.\n\n def __init__(self, env, lr, critic_lr, gamma, n, policy_path, critic_path, load=False):\n # Initializes A2C.\n # Args:\n # - model: The actor model.\n # - lr: Learning rate for the actor model.\n # - critic_model: The critic model.\n # - critic_lr: Learning rate for the critic model.\n # - n: The value of N in N-step A2C.\n Reinforce.__init__(self, env, lr, gamma=gamma, save_path=policy_path, load=load)\n self.critic_path = critic_path\n s_len = self.env.observation_space_shape[0]\n self.critic = CriticNet(critic_lr, s_len=s_len)\n self.n = n\n if load:\n self.critic.load(self.critic_path)\n print(\"Hyperparameters:\\nPolicy LR = {} Critic LR = {} Gamma = {} N = {} \\nPolicy Path = {} \\nCritic Path = {} \\nLoad = {}\".format(\n lr, critic_lr, gamma, n, policy_path, critic_path, load\n ))\n return\n\n def train(self):\n # Trains the model on a single episode using A2C.\n K = 500\n print(\"pretrain test:\")\n print('episode 0 ', end='')\n self.test()\n print(\"training\")\n # generate an episode\n gamma_n_1 = self.gamma ** (self.n - 1)\n gamma_n = gamma_n_1 * self.gamma\n for i in range(10000000):\n s, ava, a, r = self.generate_episode()\n s = np.array(s)\n r = np.array(r)\n r /= 100.0\n T = len(r)\n if self.n >= T:\n n = T - 1\n else:\n n = self.n\n sum_r = np.zeros(shape=(T, ), dtype=np.float32)\n sum_r[T - 1] = r[T - 1]\n for p in range(2, n + 1, 1):\n sum_r[T - p] = sum_r[T - p + 1] * self.gamma + r[T - p]\n for q in range(n + 1, T + 1, 1):\n sum_r[T - q] = (sum_r[T - q + 1] - gamma_n_1 * r[T - q + n]) * self.gamma + r[T - q]\n\n V_end = np.zeros(shape=(T,), dtype=np.float32)\n\n for j in range(6):\n V = self.critic.predict(s)\n V_end[0:T-n] = V[n: T]\n R = gamma_n * V_end + sum_r\n G = R - V \n self.model.fit(s, ava, a, G)\n self.critic.fit(s, R)\n \n if (i + 1) % K == 0:\n print('episode {} '.format(i + 1), end='')\n self.test()\n self.model.save(self.save_path)\n self.critic.save(self.critic_path)\n self.model.save(self.save_path)\n return\n\n\ndef main():\n env = gym.make('FEH-v1')\n n = 50\n a2c = A2C(env=env, lr=0.0001, gamma=0.99, critic_lr=0.0001, n=n,\n policy_path=\"./saved_model/a2c_policy-v2-n{}.h5\".format(n),\n critic_path=\"./saved_model/a2c_critic_v2-n{}.h5\".format(n),\n load=False)\n a2c.train()\n return\n\n\nif __name__ == '__main__':\n main()\n" ]
[ [ "matplotlib.use", "numpy.array", "numpy.zeros" ] ]
janosh/mnf-bnn
[ "955b083027d8757716e1cdb6baaf908964546e6d" ]
[ "tf_mnf/flows/rnvp.py" ]
[ "import tensorflow as tf\n\n\nclass RNVP(tf.Module):\n \"\"\"Affine half (aka Real Non-Volume Preserving) flow (x = z * exp(s) + t),\n where a randomly selected half z1 of the dimensions in z are transformed as an\n affine function of the other half z2, i.e. scaled by s(z2) and shifted by t(z2).\n\n From \"Density estimation using Real NVP\", Dinh et al. (May 2016)\n https://arxiv.org/abs/1605.08803\n\n This implementation uses the numerically stable updates introduced by IAF:\n https://arxiv.org/abs/1606.04934\n \"\"\"\n\n def __init__(self, dim, h_sizes=[30], activation=\"tanh\", **kwargs):\n super().__init__(**kwargs)\n layers = [tf.keras.layers.Dense(hs, activation) for hs in h_sizes]\n self.net = tf.keras.Sequential(layers)\n self.t = tf.keras.layers.Dense(dim)\n self.s = tf.keras.layers.Dense(dim)\n\n def forward(self, z): # z -> x\n # Get random Bernoulli mask. This decides which channels will remain\n # unchanged and which will be transformed as functions of the unchanged.\n mask = tf.keras.backend.random_binomial(tf.shape(z), p=0.5)\n z1, z2 = (1 - mask) * z, mask * z\n y = self.net(z2)\n shift = self.t(y)\n scale = self.s(y)\n\n # sigmoid(x) = 1 / (1 + exp(-x)). For x in (-inf, inf) => sigmoid(x) in (0, 1).\n gate = tf.sigmoid(scale)\n log_dets = tf.reduce_sum((1 - mask) * tf.math.log(gate), axis=1)\n x = (z1 * gate + (1 - gate) * shift) + z2\n\n return x, log_dets\n" ]
[ [ "tensorflow.math.log", "tensorflow.shape", "tensorflow.sigmoid", "tensorflow.keras.Sequential", "tensorflow.keras.layers.Dense" ] ]
masasin/spirit
[ "c8366e649eb105a8a579fb7a47dcc5aaeae6a0d8" ]
[ "src/visualization/plot_thesis.py" ]
[ "from functools import partial\nfrom pathlib import Path\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn.apionly as sns\n\nfrom ..analysis.csv_analysis import analyze_data, load_surveys\nfrom ..data.survey_utils import ExperimentType\nfrom .latexify import latexify, figure, fig_size\nfrom .plot_tools import plot_detailed, plot_distribution, plot_overview\n\n\n# Colours\ndefault_cycler = plt.rcParamsDefault[\"axes.prop_cycle\"]\ncolorblind_cmaps = [\"Dark2\", \"Set2\"]\ncmap_main, cmap_complement = colorblind_cmaps\n# cmap_main, cmap_complement = cmap_complement, cmap_main\ncolorblind_cyclers = {cmap: plt.cycler(\"color\", plt.cm.get_cmap(cmap).colors)\n for cmap in colorblind_cmaps}\nplt.rcParams[\"axes.prop_cycle\"] = colorblind_cyclers[cmap_main]\n\n\nFIGURE_DIR = Path(__file__).parent.joinpath(\"../../reports/thesis/img/plots\")\nfigure = partial(figure, folder=FIGURE_DIR, exts=[\"pdf\", \"pgf\"])\n\n\ndef do_drone_dos():\n with figure(\"ardrone_dos\", size=fig_size(0.45)):\n distances = np.array([0, 2, 8, 18, 23, 29, 34, 40,\n 45, 51, 56, 62, 67, 72, 78, 80])\n powers = np.array([90, 90, 86, 60, 50, 62, 35, 26,\n 24, 12, 20, 22, 26, 22, 12, 5])\n\n fig, ax1 = plt.subplots()\n ax1.step(distances, powers, lw=0.5)\n ax1.set_xlabel(\"distance (m)\")\n ax1.set_ylabel(r\"signal (\\%)\")\n ax1.set_ylim(0, 100)\n\n x_range = np.arange(80)\n best_fit = 10 * np.log10(6 / (1e5 * x_range**2.7))\n\n ax2 = ax1.twinx()\n ax2.plot(x_range, best_fit, c=\"C1\", lw=0.5)\n ax2.set_ylim(-100, -50)\n ax2.yaxis.set_tick_params(which=\"both\", labelright=False, right=False)\n\n plt.legend([ax.get_children()[0] for ax in (ax1, ax2)], [\"data\", \"fit\"])\n\n\ndef do_paths():\n with figure(\"paths_overview\", size=fig_size(0.75, 0.8)):\n ax1 = plt.subplot(\"121\")\n plot_overview(results, ExperimentType.Onboard, color=\"C0\", size_point=2,\n drone_width=0.5)\n ax2 = plt.subplot(\"122\", sharex=ax1, sharey=ax1)\n plot_overview(results, ExperimentType.Spirit, color=\"C1\", size_point=2,\n ylabel=\"\", drone_width=0.5)\n plt.setp(ax2.get_yticklabels(), visible=False)\n\n with figure(\"paths_detailed\", size=fig_size(0.75, 0.7)):\n ax1 = plt.subplot(\"121\")\n plot_detailed(results, ExperimentType.Onboard, color=\"C0\",\n size_point=2, crosshair=True, drone_width=0.5)\n ax1.legend_.remove()\n ax2 = plt.subplot(\"122\", sharex=ax1, sharey=ax1)\n plot_detailed(results, ExperimentType.Spirit, color=\"C1\", ylabel=\"\",\n size_point=2, crosshair=True, drone_width=0.5)\n ax2.legend_.remove()\n plt.setp(ax2.get_yticklabels(), visible=False)\n\n\ndef do_distributions():\n with figure(\"distribution_onboard\", size=fig_size(0.44, 1)):\n plot_distribution(results, ExperimentType.Onboard, color=\"C0\",\n crosshair=True, drone_width=0.5)\n\n with figure(\"distribution_spirit\", size=fig_size(0.44, 1)):\n plot_distribution(results, ExperimentType.Spirit, color=\"C1\",\n crosshair=True, drone_width=0.5)\n\n\ndef do_durations():\n with figure(\"duration\", size=fig_size(0.44, 1)):\n sns.factorplot(x=\"experiment\", y=\"duration\", data=analyses, kind=\"box\")\n sns.swarmplot(x=\"experiment\", y=\"duration\", split=True, data=analyses,\n palette=cmap_complement)\n plt.ylim(0, plt.ylim()[1])\n plt.ylabel(\"duration (s)\")\n\n with figure(\"duration_runs\", size=fig_size(0.44, 1)):\n sns.factorplot(x=\"order\", y=\"duration\", hue=\"experiment\", data=analyses,\n capsize=0.2)\n plt.ylim(0, plt.ylim()[1])\n plt.ylabel(\"duration (s)\")\n plt.xlabel(\"run\")\n\n\ndef do_movement():\n with figure(\"movement\", size=fig_size(0.9, 0.4)):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"path_length\", \"move_x\", \"move_y\"])\n g = sns.factorplot(x=\"experiment\", y=\"value\", col=\"variable\",\n data=molten, kind=\"box\")\n g.fig.axes[0].set_title(\"Path length\")\n g.fig.axes[1].set_title(\"Movement in $x$\")\n g.fig.axes[2].set_title(\"Movement in $y$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n plt.ylim(0, plt.ylim()[1])\n\n with figure(\"movement_x\"):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"move_l\", \"move_r\", \"move_x\"])\n g = sns.factorplot(x=\"experiment\", y=\"value\", col=\"variable\",\n data=molten, kind=\"box\")\n g.fig.axes[0].set_title(\"Movement left\")\n g.fig.axes[1].set_title(\"Movement right\")\n g.fig.axes[2].set_title(\"Movement in $x$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n plt.ylim(0, plt.ylim()[1])\n\n with figure(\"movement_y\"):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"move_b\", \"move_f\", \"move_y\"])\n g = sns.factorplot(x=\"experiment\", y=\"value\", col=\"variable\",\n data=molten, kind=\"box\")\n g.fig.axes[0].set_title(\"Movement backwards\")\n g.fig.axes[1].set_title(\"Movement forwards\")\n g.fig.axes[2].set_title(\"Movement in $y$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n plt.ylim(0, plt.ylim()[1])\n\n with figure(\"movement_back\"):\n sns.factorplot(x=\"experiment\", y=\"move_b\", data=analyses, kind=\"box\")\n sns.swarmplot(x=\"experiment\", y=\"move_b\", split=True, data=analyses,\n palette=cmap_complement)\n plt.ylabel(\"distance (m)\")\n plt.title(\"Movement backwards\")\n\n with figure(\"movement_runs\", size=fig_size(0.9, 0.4)):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"path_length\", \"move_x\", \"move_y\"])\n g = sns.factorplot(x=\"order\", y=\"value\", col=\"variable\",\n data=molten, hue=\"experiment\", capsize=0.2)\n g.fig.axes[0].set_title(\"Path length\")\n g.fig.axes[1].set_title(\"Movement in $x$\")\n g.fig.axes[2].set_title(\"Movement in $y$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n g.fig.axes[0].set_xlabel(\"run\")\n g.fig.axes[1].set_xlabel(\"run\")\n g.fig.axes[2].set_xlabel(\"run\")\n plt.ylim(0, plt.ylim()[1])\n\n with figure(\"movement_x_runs\"):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"move_l\", \"move_r\", \"move_x\"])\n g = sns.factorplot(x=\"order\", y=\"value\", col=\"variable\",\n data=molten, hue=\"experiment\")\n g.fig.axes[0].set_title(\"Movement left\")\n g.fig.axes[1].set_title(\"Movement right\")\n g.fig.axes[2].set_title(\"Movement in $x$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n g.fig.axes[0].set_xlabel(\"run\")\n g.fig.axes[1].set_xlabel(\"run\")\n g.fig.axes[2].set_xlabel(\"run\")\n plt.ylim(0, plt.ylim()[1])\n\n with figure(\"movement_y_runs\"):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"move_b\", \"move_f\", \"move_y\"])\n g = sns.factorplot(x=\"order\", y=\"value\", col=\"variable\",\n data=molten, hue=\"experiment\")\n g.fig.axes[0].set_title(\"Movement backwards\")\n g.fig.axes[1].set_title(\"Movement forwards\")\n g.fig.axes[2].set_title(\"Movement in $y$\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n g.fig.axes[0].set_xlabel(\"run\")\n g.fig.axes[1].set_xlabel(\"run\")\n g.fig.axes[2].set_xlabel(\"run\")\n plt.ylim(0, plt.ylim()[1])\n\n\ndef do_errors():\n with figure(\"rms\", size=fig_size(0.9, 0.4)):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"rms\", \"rms_x\", \"rms_y\"])\n g = sns.factorplot(x=\"experiment\", y=\"value\", col=\"variable\",\n data=molten, kind=\"box\")\n g.fig.axes[0].set_title(\"RMS Error*\")\n g.fig.axes[1].set_title(\"RMS Error in $x$*\")\n g.fig.axes[2].set_title(\"RMS Error in $y$*\")\n g.fig.axes[0].set_ylabel(\"error (m)\")\n\n with figure(\"rms_runs\", size=fig_size(0.9, 0.4)):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[\"rms\", \"rms_x\", \"rms_y\"])\n g = sns.factorplot(x=\"order\", y=\"value\", col=\"variable\",\n hue=\"experiment\", data=molten, capsize=0.2)\n g.fig.axes[0].set_title(\"RMS Error\")\n g.fig.axes[1].set_title(\"RMS Error in $x$\")\n g.fig.axes[2].set_title(\"RMS Error in $y$\")\n g.fig.axes[0].set_ylabel(\"error (m)\")\n g.fig.axes[0].set_xlabel(\"run\")\n g.fig.axes[1].set_xlabel(\"run\")\n g.fig.axes[2].set_xlabel(\"run\")\n\n with figure(\"distance\", size=fig_size(0.9, 0.4)):\n molten = pd.melt(analyses,\n id_vars=[\"user\", \"experiment\", \"order\", \"group\"],\n value_vars=[r\"dist_err\", r\"x_err\", r\"y_err\"])\n g = sns.factorplot(x=\"experiment\", y=\"value\", col=\"variable\",\n data=molten, kind=\"box\")\n g.fig.axes[0].set_title(\"Distance from target*\")\n g.fig.axes[1].set_title(\"Distance from target in $x$\")\n g.fig.axes[2].set_title(\"Distance from target in $y$*\")\n g.fig.axes[0].set_ylabel(\"distance (m)\")\n g.axes[0][0].axhline(0, color=\"black\", linewidth=1, zorder=-1)\n g.axes[0][1].axhline(0, color=\"black\", linewidth=1, zorder=-1)\n g.axes[0][2].axhline(0, color=\"black\", linewidth=1, zorder=-1)\n\n\ndef do_surveys():\n with figure(\"tlx_results\", size=fig_size(0.44, 1)):\n sns.factorplot(x=\"experiment\", y=\"tlx\", data=tlx, kind=\"box\")\n sns.swarmplot(x=\"experiment\", y=r\"tlx\",\n data=tlx, palette=cmap_complement, split=True)\n plt.ylim(0, plt.ylim()[1])\n plt.ylabel(\"NASA-TLX weighted score*\")\n\n with figure(\"tlx_components\", size=fig_size(0.44, 1)):\n components = [\"mental\", \"physical\", \"temporal\", \"performance\",\n \"effort\", \"frustration\"]\n molten = pd.melt(tlx, id_vars=[\"user\", \"experiment\", \"order\"],\n value_vars=components,\n var_name=\"component\", value_name=\"score\")\n sns.barplot(x=r\"component\", y=\"score\", hue=\"experiment\", data=molten)\n\n plt.gca().set_xticklabels(\n [\"MD\", \"PD\", \"TD\", \"P\", \"E\", \"F\"])\n\n plt.xlabel(\"NASA-TLX component\")\n plt.ylabel(\"score\")\n\n with figure(\"survey_results\", size=fig_size(0.44, 1)):\n sns.factorplot(x=\"experiment\", y=\"total\", data=surveys, kind=\"box\")\n sns.swarmplot(x=\"experiment\", y=r\"total\", data=surveys,\n palette=cmap_complement, split=True)\n plt.ylim(0, plt.ylim()[1])\n plt.ylabel(\"survey score*\")\n\n with figure(\"survey_components\", size=fig_size(0.44, 1)):\n components = [r\"orientation_understanding\", r\"orientation_control\",\n r\"position_understanding\", r\"position_control\",\n r\"spacial_understanding\", r\"spacial_control\"]\n molten = pd.melt(surveys, id_vars=[\"user\", \"experiment\", \"order\"],\n value_vars=components,\n var_name=\"question\", value_name=\"rating\")\n sns.barplot(x=r\"question\", y=\"rating\", hue=\"experiment\", data=molten)\n\n plt.gca().set_xticklabels(\n [\"OA\", \"OC\", \"PA*\", \"PC*\", \"RA*\", \"RC*\"])\n\n plt.xlabel(\"question\")\n plt.ylabel(\"rating\")\n\n with figure(\"survey_overview\", size=fig_size(0.9, 0.5)):\n molten = pd.melt(surveys, id_vars=[\"user\", \"experiment\", \"order\"],\n value_vars=[r\"orientation_understanding\",\n r\"orientation_control\",\n r\"position_understanding\",\n r\"position_control\",\n r\"spacial_understanding\",\n r\"spacial_control\"],\n var_name=\"question\", value_name=\"rating\")\n g = sns.barplot(x=r\"rating\", y=r\"question\", hue=\"experiment\",\n data=molten)\n sns.stripplot(x=\"rating\", y=r\"question\", data=molten, hue=\"experiment\",\n split=True, palette=cmap_complement, jitter=0.6, size=3)\n\n plt.gca().set_yticklabels(\n [\"angle aware\", \"angle control\",\n \"position aware*\", \"position control*\",\n \"rel. pos. aware*\", \"rel. pos. control*\"])\n\n handles, labels = g.get_legend_handles_labels()\n plt.legend(handles[2:], labels[2:])\n plt.xlabel(\"rating\")\n plt.title(\"Survey results\")\n\n\nif __name__ == \"__main__\":\n latexify()\n\n do_drone_dos()\n\n results, analyses = analyze_data()\n do_paths()\n do_distributions()\n do_durations()\n do_movement()\n do_errors()\n\n users, tlx, surveys = load_surveys()\n do_surveys()\n" ]
[ [ "matplotlib.pyplot.legend", "matplotlib.pyplot.gca", "matplotlib.pyplot.subplots", "numpy.arange", "matplotlib.pyplot.title", "matplotlib.pyplot.subplot", "matplotlib.pyplot.cm.get_cmap", "matplotlib.pyplot.ylabel", "pandas.melt", "numpy.log10", "matplotlib.pyplot.ylim", "numpy.array", "matplotlib.pyplot.xlabel" ] ]
sunandita/ICAPS_Summer_School_RAE_2020
[ "a496b62185bcfdd2c76eb7986ae99cfa85708d28" ]
[ "problems/OF/auto/problem80_OF.py" ]
[ "__author__ = 'mason'\n\nfrom domain_orderFulfillment import *\nfrom timer import DURATION\nfrom state import state\nimport numpy as np\n\n'''\nThis is a randomly generated problem\n'''\n\ndef GetCostOfMove(id, r, loc1, loc2, dist):\n return 1 + dist\n\ndef GetCostOfLookup(id, item):\n return max(1, np.random.beta(2, 2))\n\ndef GetCostOfWrap(id, orderName, m, item):\n return max(1, np.random.normal(5, .5))\n\ndef GetCostOfPickup(id, r, item):\n return max(1, np.random.normal(4, 1))\n\ndef GetCostOfPutdown(id, r, item):\n return max(1, np.random.normal(4, 1))\n\ndef GetCostOfLoad(id, orderName, r, m, item):\n return max(1, np.random.normal(3, .5))\n\nDURATION.TIME = {\n 'lookupDB': GetCostOfLookup,\n 'wrap': GetCostOfWrap,\n 'pickup': GetCostOfPickup,\n 'putdown': GetCostOfPutdown,\n 'loadMachine': GetCostOfLoad,\n 'moveRobot': GetCostOfMove,\n 'acquireRobot': 1,\n 'freeRobot': 1,\n 'wait': 5\n}\n\nDURATION.COUNTER = {\n 'lookupDB': GetCostOfLookup,\n 'wrap': GetCostOfWrap,\n 'pickup': GetCostOfPickup,\n 'putdown': GetCostOfPutdown,\n 'loadMachine': GetCostOfLoad,\n 'moveRobot': GetCostOfMove,\n 'acquireRobot': 1,\n 'freeRobot': 1,\n 'wait': 5\n}\n\nrv.LOCATIONS = [0, 1, 2, 3, 4, 5, 6, 7, 200]\nrv.FACTORY1 = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 200})\nrv.FACTORY_UNION = rv.FACTORY1\nrv.SHIPPING_DOC = {rv.FACTORY1: 2}\n\nrv.GROUND_EDGES = {0: [5, 6, 3], 1: [5, 7], 2: [5, 200, 3, 6], 3: [0, 2, 5], 4: [5], 5: [4, 0, 1, 2, 3, 200], 6: [0, 2, 7, 200], 7: [1, 6], 200: [5, 6, 2]}\nrv.GROUND_WEIGHTS = {(0, 5): 7.9850546018862145, (0, 6): 6.9468710812757815, (0, 3): 12.099718445874334, (1, 5): 7.528748667927151, (1, 7): 6.418070736597217, (2, 5): 2.638611160543803, (2, 200): 7.8118804436721145, (2, 3): 3.9268270102333664, (2, 6): 10.6254313640586, (3, 5): 5.469893763260881, (4, 5): 5.862988480314365, (5, 200): 7.930686919202043, (6, 7): 11.9166581376803, (6, 200): 7.026333797476917}\n\nrv.ROBOTS = { 'r0': rv.FACTORY1, }\nrv.ROBOT_CAPACITY = {'r0': 7.295545079301372}\nrv.MACHINES = { 'm0': rv.FACTORY1, }\nrv.PALLETS = { 'p0', 'p1', }\n\n\ndef ResetState():\n state.OBJECTS = { 'o0': True, 'o1': True, 'o2': True, 'o3': True, 'o4': True, 'o5': True, 'o6': True, 'o7': True, }\n state.OBJ_WEIGHT = {'o0': 6.707214929077857, 'o1': 6.448795041001162, 'o2': 5.54703539784075, 'o3': 7.295545079301372, 'o4': 6.655084061734898, 'o5': 7.295545079301372, 'o6': 7.235373845668021, 'o7': 4.7440365198981445}\n state.OBJ_CLASS = {'type0': ['o0'], 'type1': ['o1', 'o2', 'o3', 'o4', 'o5', 'o6'], 'type2': ['o7']}\n\n state.loc = { 'r0': 4, 'm0': 6, 'p0': 0, 'p1': 3, 'o0': 4, 'o1': 7, 'o2': 5, 'o3': 200, 'o4': 200, 'o5': 4, 'o6': 200, 'o7': 5,}\n state.load = { 'r0': NIL,}\n state.busy = {'r0': False, 'm0': False}\n state.numUses = {'m0': 10}\n state.var1 = {'temp': 'r0', 'temp1': 'r0', 'temp2': 1, 'redoId': 0}\n state.shouldRedo = {}\n\ntasks = {\n 3: [['orderStart', ['type0']]],\n 8: [['orderStart', ['type1']]],\n}\neventsEnv = {\n}" ]
[ [ "numpy.random.beta", "numpy.random.normal" ] ]
ruanchaves/Dual-encoder-Entity-Retrieval-with-BERT
[ "ff8c7933afaf0b2c40a7df0250f4b82a5868dc2a" ]
[ "data_reader.py" ]
[ "import numpy as np\nfrom tqdm import tqdm\nimport torch\nimport pdb\nfrom typing import Iterator\nfrom allennlp.data import Instance\nfrom allennlp.data.dataset_readers import DatasetReader\nfrom allennlp.data.token_indexers import TokenIndexer, SingleIdTokenIndexer, PretrainedTransformerIndexer\nfrom allennlp.data.fields import SpanField, ListField, TextField, MetadataField, ArrayField, SequenceLabelField, LabelField\nfrom allennlp.data.tokenizers import Token\nfrom utils import OnlyFixedDatasetLoader, KBConstructor_fromKGemb, FixedNegativesEntityLoader\nfrom overrides import overrides\nimport random\nimport transformers\nfrom utils import from_original_sentence2left_mention_right_tokens_before_berttokenized\n\n# SEEDS are FIXED\ntorch.backends.cudnn.deterministic = True\nseed = 777\nnp.random.seed(seed)\ntorch.manual_seed(seed)\n\nclass FixedDatasetTokenizedReader(DatasetReader):\n def __init__(self,args, canonical_and_def_connecttoken, token_indexers=None):\n super().__init__(lazy=args.allen_lazyload)\n\n self.args = args\n self.max_context_len = args.max_context_len\n self.max_canonical_len = args.max_canonical_len\n self.max_def_len = args.max_def_len\n\n self.token_indexers = self.token_indexer_returner()\n self.berttokenizer = self.berttokenizer_returner()\n\n linking_dataset_loader = OnlyFixedDatasetLoader(args=args)\n self.id2line, self.train_mention_id, self.dev_mention_id, self.test_mention_id = linking_dataset_loader.id2line_trn_dev_test_loader()\n\n print('loading KB')\n self.kbclass = KBConstructor_fromKGemb(args=self.args)\n self.setting_original_KB()\n print('original KB loaded')\n self.ignored_mention_idxs = self.to_be_ignored_mention_idx_checker()\n self.mention_start_token, self.mention_end_token = '[unused1]', '[unused2]'\n self.canonical_and_def_connecttoken = canonical_and_def_connecttoken\n\n def setting_original_KB(self):\n self.cui2idx, self.idx2cui, self.cui2emb, self.cui2cano, self.cui2def = self.kbclass.return_original_KB()\n\n def currently_stored_KB_dataset_returner(self):\n return self.cui2idx, self.idx2cui, self.cui2emb, self.cui2cano, self.cui2def\n\n def huggingfacename_returner(self):\n 'Return huggingface modelname and do_lower_case parameter'\n if self.args.bert_name == 'bert-base-uncased':\n return 'bert-base-uncased', True\n elif self.args.bert_name == 'biobert':\n return './biobert_transformers/', False\n else:\n print('Currently',self.args.bert_name,'are not supported.')\n exit()\n\n def token_indexer_returner(self):\n huggingface_name, do_lower_case = self.huggingfacename_returner()\n return {'tokens': PretrainedTransformerIndexer(\n model_name=huggingface_name,\n do_lowercase=do_lower_case)\n }\n\n def berttokenizer_returner(self):\n if self.args.bert_name == 'bert-base-uncased':\n vocab_file = './vocab_file/bert-base-uncased-vocab.txt'\n do_lower_case = True\n elif self.args.bert_name == 'biobert':\n vocab_file = './vocab_file/biobert_v1.1_pubmed_vocab.txt'\n do_lower_case = False\n else:\n print('currently not supported:', self.args.bert_name)\n raise NotImplementedError\n return transformers.BertTokenizer(vocab_file=vocab_file,\n do_lower_case=do_lower_case,\n do_basic_tokenize=True,\n never_split=['<target>','</target>'])\n\n def tokenizer_custom(self, txt):\n target_anchors = ['<target>', '</target>']\n original_tokens = txt.split(' ')\n new_tokens = list()\n\n for token in original_tokens:\n if token in target_anchors:\n new_tokens.append(token)\n continue\n else:\n split_to_subwords = self.berttokenizer.tokenize(token) # token is oneword, split_tokens\n if ['[CLS]'] in split_to_subwords:\n split_to_subwords.remove('[CLS]')\n if ['[SEP]'] in split_to_subwords:\n split_to_subwords.remove('[SEP]')\n if split_to_subwords == []:\n new_tokens.append('[UNK]')\n else:\n new_tokens += split_to_subwords\n\n return new_tokens\n\n def mention_and_contexttokenizer_followblinkimplementation(self, txt):\n '''\n Args: sentence with space, including target anchor\n txt:\n\n Returns: [[CLS], split_sub0, ..., [mention_start], mention, [mention_end], ..., [SEP]]\n\n '''\n mention_start = '<target>'\n mention_end = '</target>'\n left, mention, right = from_original_sentence2left_mention_right_tokens_before_berttokenized(txt)\n\n new_tokens = list()\n new_tokens.append('[CLS]')\n\n if len(left) != 0:\n left_tokens = []\n for one_token in left:\n left_tokens += self.berttokenizer.tokenize(one_token)\n new_tokens += left_tokens[:self.args.max_left_context_len]\n\n new_tokens.append(self.mention_start_token)\n if len(mention) != 0:\n mention_tokens = []\n for one_token in mention:\n mention_tokens += self.berttokenizer.tokenize(one_token)\n new_tokens += mention_tokens[:self.args.max_mention_len]\n new_tokens.append(self.mention_end_token)\n\n if len(right) != 0:\n right_tokens = []\n for one_token in right:\n right_tokens += self.berttokenizer.tokenize(one_token)\n new_tokens += right_tokens[:self.args.max_right_context_len]\n new_tokens.append('[SEP]')\n return new_tokens\n\n def find_anchor(self,split_txt,tobefoundtoken):\n for i, word in enumerate(split_txt):\n if word == tobefoundtoken:\n return i\n return -1\n\n def left_right_mention_sentence_from_anchorincludedsentence_returner(self, split_txt):\n i = self.find_anchor(split_txt=split_txt, tobefoundtoken='<target>') # mention start\n j = self.find_anchor(split_txt=split_txt, tobefoundtoken='</target>') # mention end\n\n sfm_mention = split_txt[i+1:j]\n raw_sentence_noanchor = [token for token in split_txt if not token in ['<target>', '</target>']]\n\n left_context_include_mention = split_txt[:j]\n left_context_include_mention.remove('<target>')\n right_context_include_mention = split_txt[i+1:]\n right_context_include_mention.remove('</target>')\n\n return raw_sentence_noanchor, sfm_mention, left_context_include_mention, right_context_include_mention\n\n @overrides\n def _read(self, train_dev_testflag) -> Iterator[Instance]:\n mention_ids = list()\n if train_dev_testflag == 'train':\n mention_ids += self.train_mention_id\n # Because original data is sorted with pmid documents, we have to shuffle data points for in-batch training.\n random.shuffle(mention_ids)\n elif train_dev_testflag == 'dev':\n mention_ids += self.dev_mention_id\n elif train_dev_testflag == 'test':\n mention_ids += self.test_mention_id\n\n for idx, mention_uniq_id in tqdm(enumerate(mention_ids)):\n if mention_uniq_id in self.ignored_mention_idxs:\n continue\n if self.args.model_for_training == 'blink_implementation_inbatchencoder':\n data = self.linesparser_for_blink_implementation(line=self.id2line[mention_uniq_id], mention_uniq_id=mention_uniq_id)\n else:\n data = self.lineparser_for_local_mentions(line=self.id2line[mention_uniq_id], mention_uniq_id=mention_uniq_id)\n yield self.text_to_instance(data=data)\n\n def lineparser_for_local_mentions(self, line, mention_uniq_id):\n '''\n Now this function is going to be depreceated,\n since we gonna follow faithfully with \"Zero-shot entity linking with dense entity retrieval\"\n\n Args:\n line:\n train_dev_testflag:\n mention_uniq_id:\n\n Returns:\n\n '''\n gold_cui, gold_type, gold_surface_mention, targetanchor_included_sentence = line.split('\\t')\n tokenized_context_including_target_anchors = self.tokenizer_custom(txt=targetanchor_included_sentence)\n raw_sentence_noanchor, sfm_mention, left_context_include_mention, right_context_include_mention = self.left_right_mention_sentence_from_anchorincludedsentence_returner(\n split_txt=tokenized_context_including_target_anchors)\n\n data = {}\n\n data['mention_uniq_id'] = mention_uniq_id\n data['gold_ids'] = gold_cui # str\n data['gold_id_idx_with_cui2idx'] = int(self.cui2idx[gold_cui])\n data['mention_raw'] = gold_surface_mention\n data['raw_sentence_without_anchor_str'] = ' '.join(raw_sentence_noanchor)\n\n data['context'] = [Token(word) for word in raw_sentence_noanchor][:self.args.max_context_len]\n data['mention_preprocessed'] = [Token(word) for word in sfm_mention][:self.max_context_len]\n\n if len(left_context_include_mention) <= self.max_context_len:\n data['left_context_include_mention'] = [Token(word) for word in left_context_include_mention]\n else:\n data['left_context_include_mention'] = [Token(word) for word in left_context_include_mention][\n len(left_context_include_mention) - self.max_context_len:]\n\n data['right_context_include_mention'] = [Token(word) for word in right_context_include_mention][:self.max_context_len]\n\n data['context'].insert(0, Token('[CLS]'))\n data['context'].insert(len(data['context']), Token('[SEP]'))\n data['mention_preprocessed'].insert(0, Token('[CLS]'))\n data['mention_preprocessed'].insert(len(data['mention_preprocessed']), Token('[SEP]'))\n data['left_context_include_mention'].insert(0, Token('[CLS]'))\n data['left_context_include_mention'].insert(len(data['left_context_include_mention']), Token('[SEP]'))\n data['right_context_include_mention'].insert(0, Token('[CLS]'))\n data['right_context_include_mention'].insert(len(data['right_context_include_mention']), Token('[SEP]'))\n\n data['gold_cui_cano_and_def_concatenated'] = self.gold_canonical_and_def_concatenated_returner(gold_cui=gold_cui)\n\n return data\n\n def linesparser_for_blink_implementation(self, line, mention_uniq_id):\n gold_cui, gold_type, gold_surface_mention, targetanchor_included_sentence = line.split('\\t')\n gold_cui = gold_cui.replace('UMLS:', '')\n tokenized_context_including_target_anchors = self.mention_and_contexttokenizer_followblinkimplementation(txt=targetanchor_included_sentence)\n tokenized_context_including_target_anchors = [Token(split_token) for split_token in tokenized_context_including_target_anchors]\n data = {}\n data['context'] = tokenized_context_including_target_anchors\n data['gold_cui_cano_and_def_concatenated'] = self.gold_canonical_and_def_concatenated_returner(gold_cui=gold_cui)\n data['gold_cuidx'] = int(self.cui2idx[gold_cui])\n data['mention_uniq_id'] = int(mention_uniq_id)\n return data\n\n def gold_canonical_and_def_concatenated_returner(self, gold_cui):\n canonical = self.tokenizer_custom(txt=self.cui2cano[gold_cui])\n definition = self.tokenizer_custom(txt=self.cui2def[gold_cui])\n\n concatenated = ['[CLS]']\n concatenated += canonical[:self.max_canonical_len]\n concatenated.append(self.canonical_and_def_connecttoken)\n concatenated += definition[:self.max_def_len]\n concatenated.append('[SEP]')\n\n return [Token(tokenized_word) for tokenized_word in concatenated]\n\n def to_be_ignored_mention_idx_checker(self):\n to_be_ignored_mention_idxs = []\n all_mention_idxs = list()\n all_mention_idxs += self.train_mention_id\n all_mention_idxs += self.dev_mention_id\n all_mention_idxs += self.test_mention_id\n for mention_idx in all_mention_idxs:\n gold_cui_or_dui = self.id2line[mention_idx].split('\\t')[0].replace('UMLS:', '')\n if gold_cui_or_dui not in self.cui2idx:\n to_be_ignored_mention_idxs.append(mention_idx)\n return to_be_ignored_mention_idxs\n\n @overrides\n def text_to_instance(self, data=None) -> Instance:\n if self.args.model_for_training == 'blink_implementation_inbatchencoder':\n context_field = TextField(data['context'], self.token_indexers)\n fields = {\"context\": context_field}\n fields['gold_cui_cano_and_def_concatenated'] = TextField(data['gold_cui_cano_and_def_concatenated'], self.token_indexers)\n fields['gold_cuidx'] = ArrayField(np.array(data['gold_cuidx']))\n fields['mention_uniq_id'] = ArrayField(np.array(data['mention_uniq_id']))\n else:\n context_field = TextField(data['context'], self.token_indexers)\n fields = {\"context\": context_field}\n surface_mention_field = TextField(data['mention_preprocessed'], self.token_indexers)\n fields['left_context_include_mention'] = TextField(data['left_context_include_mention'], self.token_indexers)\n fields['right_context_include_mention'] = TextField(data['right_context_include_mention'], self.token_indexers)\n fields['mention_processed'] = surface_mention_field\n fields['gold_cui_cano_and_def_concatenated'] = TextField(data['gold_cui_cano_and_def_concatenated'], self.token_indexers)\n fields['gold_id_for_knn'] = ArrayField(np.array(data['gold_id_idx_with_cui2idx']))\n\n return Instance(fields)\n'''\nFor encoding all entities, we need another datasetreader\n'''\nclass AllEntityCanonical_and_Defs_loader(DatasetReader):\n def __init__(self, args, idx2cui, cui2cano, cui2def,\n textfield_embedder, pretrained_tokenizer, tokenindexer, canonical_and_def_connect_token):\n super().__init__(lazy=args.allen_lazyload)\n\n self.args = args\n self.idx2cui = idx2cui\n self.cui2cano = cui2cano\n self.cui2def = cui2def\n self.textfield_embedder = textfield_embedder\n self.pretrained_tokenizer = pretrained_tokenizer\n self.token_indexers = tokenindexer\n self.canonical_and_def_connect_token = canonical_and_def_connect_token\n\n @overrides\n def _read(self,file_path=None) -> Iterator[Instance]:\n for idx, cui in tqdm(self.idx2cui.items()):\n if self.args.debug_for_entity_encoder and idx==2100:\n break\n data = self.cui2data(cui=cui, idx=idx)\n yield self.text_to_instance(data=data)\n\n @overrides\n def text_to_instance(self, data=None) -> Instance:\n cano_and_def_concatenated = TextField(data['cano_and_def_concatenated'], self.token_indexers)\n fields = {\"cano_and_def_concatenated\": cano_and_def_concatenated, 'cui_idx':ArrayField(np.array(data['cui_idx'], dtype='int32'))}\n\n return Instance(fields)\n\n def tokenizer_custom(self, txt):\n original_tokens = txt.split(' ')\n new_tokens = list()\n\n for token in original_tokens:\n split_to_subwords = self.pretrained_tokenizer.tokenize(token) # token is oneword, split_tokens\n if ['[CLS]'] in split_to_subwords:\n split_to_subwords.remove('[CLS]')\n if ['[SEP]'] in split_to_subwords:\n split_to_subwords.remove('[SEP]')\n if split_to_subwords == []:\n new_tokens.append('[UNK]')\n else:\n new_tokens += split_to_subwords\n\n return new_tokens\n\n def cui2data(self, cui, idx):\n canonical_plus_definition = []\n canonical_plus_definition.append('[CLS]')\n\n canonical = self.cui2cano[cui]\n canonical_tokens = [split_word for split_word in self.tokenizer_custom(txt=canonical)]\n canonical_plus_definition += canonical_tokens[:self.args.max_canonical_len]\n\n canonical_plus_definition.append(self.canonical_and_def_connect_token)\n\n definition = self.cui2def[cui]\n definition_tokens = [split_word for split_word in self.tokenizer_custom(txt=definition)]\n canonical_plus_definition += definition_tokens[:self.args.max_def_len]\n\n canonical_plus_definition.append('[SEP]')\n\n return {'cano_and_def_concatenated':[ Token(split_word_) for split_word_ in canonical_plus_definition],\n 'cui_idx': idx}" ]
[ [ "numpy.array", "torch.manual_seed", "numpy.random.seed" ] ]
sanjitjain2/soundnet_tf
[ "ba8d85246dbf14f2573ad5b46355ae512bb630de" ]
[ "util.py" ]
[ "import numpy as np\nimport librosa\n# import pdb\nimport wget\n\nlocal_config = {\n 'batch_size': 64, \n 'load_size': 22050*20,\n 'phase': 'extract'\n }\n\ndef get_audio(audio_link):\n file_name = audio_link.split('/')[-1]\n save_location = \"/Users/sanjitjain/projects/soundnet_tf/data/\"\n wget.download(audio_link, save_location + file_name + \".mp3\")\n return str(save_location+file_name)\n\n\ndef load_from_link(link, config=local_config):\n audio_path = get_audio(link)\n audio_path = \"/Users/sanjitjain/projects/soundnet_tf/data/tame_impala.mp3\"\n sound_sample, _ = load_audio(audio_path)\n audio = preprocess(sound_sample, config)\n\n return audio\n\ndef load_from_list(name_list, config=local_config):\n assert len(name_list) == config['batch_size'], \\\n \"The length of name_list({})[{}] is not the same as batch_size[{}]\".format(\n name_list[0], len(name_list), config['batch_size'])\n audios = np.zeros([config['batch_size'], config['load_size'], 1, 1])\n for idx, audio_path in enumerate(name_list):\n sound_sample, _ = load_audio(audio_path)\n audios[idx] = preprocess(sound_sample, config)\n \n return audios\n\n\ndef load_from_txt(txt_name, config=local_config):\n with open(txt_name, 'r') as handle:\n txt_list = handle.read().splitlines()\n\n audios = []\n for idx, audio_path in enumerate(txt_list):\n sound_sample, _ = load_audio(audio_path)\n audios.append(preprocess(sound_sample, config))\n \n return audios\n\n\n# NOTE: Load an audio as the same format in soundnet\n# 1. Keep original sample rate (which conflicts their own paper)\n# 2. Use first channel in multiple channels\n# 3. Keep range in [-256, 256]\n\ndef load_audio(audio_path, sr=None):\n # By default, librosa will resample the signal to 22050Hz(sr=None). And range in (-1., 1.)\n sound_sample, sr = librosa.load(audio_path, sr=sr, mono=False)\n\n return sound_sample, sr\n\n\ndef preprocess(raw_audio, config=local_config):\n # Select first channel (mono)\n if len(raw_audio.shape) > 1:\n raw_audio = raw_audio[0]\n\n # Make range [-256, 256]\n raw_audio *= 256.0\n\n # Make minimum length available\n length = config['load_size']\n if length > raw_audio.shape[0]:\n raw_audio = np.tile(raw_audio, length/raw_audio.shape[0] + 1)\n\n # Make equal training length\n if config['phase'] != 'extract':\n raw_audio = raw_audio[:length]\n\n # Check conditions\n assert len(raw_audio.shape) == 1, \"It seems this audio contains two channels, we only need the first channel\"\n assert np.max(raw_audio) <= 256, \"It seems this audio contains signal that exceeds 256\"\n assert np.min(raw_audio) >= -256, \"It seems this audio contains signal that exceeds -256\"\n\n # Shape to 1 x DIM x 1 x 1\n raw_audio = np.reshape(raw_audio, [1, -1, 1, 1])\n\n return raw_audio.copy()\n\n\n" ]
[ [ "numpy.tile", "numpy.zeros", "numpy.reshape", "numpy.max", "numpy.min" ] ]
aaalgo/aardvark
[ "cdd42acdc20e85f4b3070dd1486f3dc9c9a9b905" ]
[ "cxray/predict-cls-vis.py" ]
[ "#!/usr/bin/env python3\nimport os\nimport sys\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\nsys.path.append('..')\nimport numpy as np\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.python.framework import meta_graph\nfrom mold import Scaling\nfrom gallery import Gallery\nfrom chest import *\n\nclass Model:\n def __init__ (self, X, path, name):\n mg = meta_graph.read_meta_graph_file(path + '.meta')\n is_training = tf.constant(False)\n self.probs, self.heatmap = \\\n tf.import_graph_def(mg.graph_def, name=name,\n input_map={'images:0': X, 'is_training:0': is_training},\n return_elements=['probs:0', 'heatmap:0'])\n self.saver = tf.train.Saver(saver_def=mg.saver_def, name=name)\n self.loader = lambda sess: self.saver.restore(sess, path)\n pass\n pass\n\nflags = tf.app.flags\nFLAGS = flags.FLAGS\n\nflags.DEFINE_string('model', None, '')\nflags.DEFINE_integer('stride', 16, '')\nflags.DEFINE_integer('channels', 1, '')\nflags.DEFINE_string('list', 'scratch/val-nz.list', '')\nflags.DEFINE_integer('max', 10, '')\nflags.DEFINE_integer('resize', 256, '')\n\ndef save_prediction_image (gal, image, label, probs, heatmap):\n pred = np.argmax(probs)\n image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR).astype(np.float32) \n orig = np.copy(image)\n\n # ground truth\n cv2.putText(image, 'gt %d: %.3f %s' % (label, probs[label], CATEGORIES[label][1]), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n cv2.putText(image, 'inf %d: %.3f %s' % (pred, probs[pred], CATEGORIES[pred][1]), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)\n image[:, :, 1] += heatmap[:, :, label] * 128\n image[:, :, 2] += heatmap[:, :, pred] * 128\n image = np.concatenate([image, orig], axis=1)\n cv2.imwrite(gal.next(), np.clip(image, 0, 255))\n pass\n\ndef main (_):\n X = tf.placeholder(tf.float32, shape=(None, None, None, FLAGS.channels), name=\"images\")\n model = Model(X, FLAGS.model, 'xxx')\n config = tf.ConfigProto()\n config.gpu_options.allow_growth=True\n\n mold = Scaling(stride = FLAGS.stride)\n with tf.Session(config=config) as sess:\n sess.run(tf.global_variables_initializer())\n sess.run(tf.local_variables_initializer())\n model.loader(sess)\n\n gal = Gallery('output', ext='.png')\n CC = 0\n if FLAGS.list:\n with open(FLAGS.list, 'r') as f:\n for line in f:\n if CC > FLAGS.max:\n break\n path, label = line.strip().split(',')\n label = int(label)\n\n print(path)\n if FLAGS.channels == 3:\n image = cv2.imread(path, cv2.IMREAD_COLOR)\n elif FLAGS.channels == 1:\n image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n else:\n assert False\n\n image = cv2.resize(image, (FLAGS.resize, FLAGS.resize))\n\n probs, heatmap = sess.run([model.probs, model.heatmap], feed_dict={X: mold.batch_image(image)})\n probs = probs[0]\n heatmap = mold.unbatch_prob(image, heatmap)\n '''END INFERENCE'''\n\n save_prediction_image(gal, image, label, probs, heatmap)\n CC += 1\n gal.flush()\n pass\n\nif __name__ == '__main__':\n tf.app.run()\n\n" ]
[ [ "tensorflow.python.framework.meta_graph.read_meta_graph_file", "tensorflow.placeholder", "tensorflow.app.run", "tensorflow.global_variables_initializer", "tensorflow.local_variables_initializer", "numpy.argmax", "numpy.copy", "numpy.clip", "tensorflow.import_graph_def", "tensorflow.train.Saver", "tensorflow.constant", "tensorflow.Session", "numpy.concatenate", "tensorflow.ConfigProto" ] ]
shippingwang/models
[ "a92e212932b764e500a833527e0fb772ac9a491a" ]
[ "PaddleCV/PaddleDetection/tools/face_eval.py" ]
[ "# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\n\nimport paddle.fluid as fluid\nimport numpy as np\nfrom PIL import Image\nfrom collections import OrderedDict\n\nimport ppdet.utils.checkpoint as checkpoint\nfrom ppdet.utils.cli import ArgsParser\nfrom ppdet.utils.check import check_gpu\nfrom ppdet.utils.widerface_eval_utils import get_shrink, bbox_vote, \\\n save_widerface_bboxes, save_fddb_bboxes, to_chw_bgr\nfrom ppdet.core.workspace import load_config, merge_config, create\nfrom ppdet.modeling.model_input import create_feed\n\nimport logging\nFORMAT = '%(asctime)s-%(levelname)s: %(message)s'\nlogging.basicConfig(level=logging.INFO, format=FORMAT)\nlogger = logging.getLogger(__name__)\n\n\ndef face_img_process(image,\n mean=[104., 117., 123.],\n std=[127.502231, 127.502231, 127.502231]):\n img = np.array(image)\n img = to_chw_bgr(img)\n img = img.astype('float32')\n img -= np.array(mean)[:, np.newaxis, np.newaxis].astype('float32')\n img /= np.array(std)[:, np.newaxis, np.newaxis].astype('float32')\n img = [img]\n img = np.array(img)\n return img\n\n\ndef face_eval_run(exe,\n compile_program,\n fetches,\n img_root_dir,\n gt_file,\n pred_dir='output/pred',\n eval_mode='widerface',\n multi_scale=False):\n # load ground truth files\n with open(gt_file, 'r') as f:\n gt_lines = f.readlines()\n imid2path = []\n pos_gt = 0\n while pos_gt < len(gt_lines):\n name_gt = gt_lines[pos_gt].strip('\\n\\t').split()[0]\n imid2path.append(name_gt)\n pos_gt += 1\n n_gt = int(gt_lines[pos_gt].strip('\\n\\t').split()[0])\n pos_gt += 1 + n_gt\n logger.info('The ground truth file load {} images'.format(len(imid2path)))\n\n dets_dist = OrderedDict()\n for iter_id, im_path in enumerate(imid2path):\n image_path = os.path.join(img_root_dir, im_path)\n if eval_mode == 'fddb':\n image_path += '.jpg'\n image = Image.open(image_path).convert('RGB')\n if multi_scale:\n shrink, max_shrink = get_shrink(image.size[1], image.size[0])\n det0 = detect_face(exe, compile_program, fetches, image, shrink)\n det1 = flip_test(exe, compile_program, fetches, image, shrink)\n [det2, det3] = multi_scale_test(exe, compile_program, fetches, image,\n max_shrink)\n det4 = multi_scale_test_pyramid(exe, compile_program, fetches, image,\n max_shrink)\n det = np.row_stack((det0, det1, det2, det3, det4))\n dets = bbox_vote(det)\n else:\n dets = detect_face(exe, compile_program, fetches, image, 1)\n if eval_mode == 'widerface':\n save_widerface_bboxes(image_path, dets, pred_dir)\n else:\n dets_dist[im_path] = dets\n if iter_id % 100 == 0:\n logger.info('Test iter {}'.format(iter_id))\n if eval_mode == 'fddb':\n save_fddb_bboxes(dets_dist, pred_dir)\n logger.info(\"Finish evaluation.\")\n\n\ndef detect_face(exe, compile_program, fetches, image, shrink):\n image_shape = [3, image.size[1], image.size[0]]\n if shrink != 1:\n h, w = int(image_shape[1] * shrink), int(image_shape[2] * shrink)\n image = image.resize((w, h), Image.ANTIALIAS)\n image_shape = [3, h, w]\n\n img = face_img_process(image)\n detection, = exe.run(compile_program,\n feed={'image': img},\n fetch_list=[fetches['bbox']],\n return_numpy=False)\n detection = np.array(detection)\n # layout: xmin, ymin, xmax. ymax, score\n if np.prod(detection.shape) == 1:\n logger.info(\"No face detected\")\n return np.array([[0, 0, 0, 0, 0]])\n det_conf = detection[:, 1]\n det_xmin = image_shape[2] * detection[:, 2] / shrink\n det_ymin = image_shape[1] * detection[:, 3] / shrink\n det_xmax = image_shape[2] * detection[:, 4] / shrink\n det_ymax = image_shape[1] * detection[:, 5] / shrink\n\n det = np.column_stack((det_xmin, det_ymin, det_xmax, det_ymax, det_conf))\n return det\n\n\ndef flip_test(exe, compile_program, fetches, image, shrink):\n img = image.transpose(Image.FLIP_LEFT_RIGHT)\n det_f = detect_face(exe, compile_program, fetches, img, shrink)\n det_t = np.zeros(det_f.shape)\n # image.size: [width, height]\n det_t[:, 0] = image.size[0] - det_f[:, 2]\n det_t[:, 1] = det_f[:, 1]\n det_t[:, 2] = image.size[0] - det_f[:, 0]\n det_t[:, 3] = det_f[:, 3]\n det_t[:, 4] = det_f[:, 4]\n return det_t\n\n\ndef multi_scale_test(exe, compile_program, fetches, image, max_shrink):\n # Shrink detecting is only used to detect big faces\n st = 0.5 if max_shrink >= 0.75 else 0.5 * max_shrink\n det_s = detect_face(exe, compile_program, fetches, image, st)\n index = np.where(\n np.maximum(det_s[:, 2] - det_s[:, 0] + 1, det_s[:, 3] - det_s[:, 1] + 1)\n > 30)[0]\n det_s = det_s[index, :]\n # Enlarge one times\n bt = min(2, max_shrink) if max_shrink > 1 else (st + max_shrink) / 2\n det_b = detect_face(exe, compile_program, fetches, image, bt)\n\n # Enlarge small image x times for small faces\n if max_shrink > 2:\n bt *= 2\n while bt < max_shrink:\n det_b = np.row_stack((det_b, detect_face(exe, compile_program,\n fetches, image, bt)))\n bt *= 2\n det_b = np.row_stack((det_b, detect_face(exe, compile_program, fetches,\n image, max_shrink)))\n\n # Enlarged images are only used to detect small faces.\n if bt > 1:\n index = np.where(\n np.minimum(det_b[:, 2] - det_b[:, 0] + 1,\n det_b[:, 3] - det_b[:, 1] + 1) < 100)[0]\n det_b = det_b[index, :]\n # Shrinked images are only used to detect big faces.\n else:\n index = np.where(\n np.maximum(det_b[:, 2] - det_b[:, 0] + 1,\n det_b[:, 3] - det_b[:, 1] + 1) > 30)[0]\n det_b = det_b[index, :]\n return det_s, det_b\n\n\ndef multi_scale_test_pyramid(exe, compile_program, fetches, image, max_shrink):\n # Use image pyramids to detect faces\n det_b = detect_face(exe, compile_program, fetches, image, 0.25)\n index = np.where(\n np.maximum(det_b[:, 2] - det_b[:, 0] + 1, det_b[:, 3] - det_b[:, 1] + 1)\n > 30)[0]\n det_b = det_b[index, :]\n\n st = [0.75, 1.25, 1.5, 1.75]\n for i in range(len(st)):\n if st[i] <= max_shrink:\n det_temp = detect_face(exe, compile_program, fetches, image, st[i])\n # Enlarged images are only used to detect small faces.\n if st[i] > 1:\n index = np.where(\n np.minimum(det_temp[:, 2] - det_temp[:, 0] + 1,\n det_temp[:, 3] - det_temp[:, 1] + 1) < 100)[0]\n det_temp = det_temp[index, :]\n # Shrinked images are only used to detect big faces.\n else:\n index = np.where(\n np.maximum(det_temp[:, 2] - det_temp[:, 0] + 1,\n det_temp[:, 3] - det_temp[:, 1] + 1) > 30)[0]\n det_temp = det_temp[index, :]\n det_b = np.row_stack((det_b, det_temp))\n return det_b\n\n\ndef main():\n \"\"\"\n Main evaluate function\n \"\"\"\n cfg = load_config(FLAGS.config)\n if 'architecture' in cfg:\n main_arch = cfg.architecture\n else:\n raise ValueError(\"'architecture' not specified in config file.\")\n\n merge_config(FLAGS.opt)\n\n # check if set use_gpu=True in paddlepaddle cpu version\n check_gpu(cfg.use_gpu)\n\n if 'eval_feed' not in cfg:\n eval_feed = create(main_arch + 'EvalFeed')\n else:\n eval_feed = create(cfg.eval_feed)\n\n # define executor\n place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()\n exe = fluid.Executor(place)\n\n # build program\n model = create(main_arch)\n startup_prog = fluid.Program()\n eval_prog = fluid.Program()\n with fluid.program_guard(eval_prog, startup_prog):\n with fluid.unique_name.guard():\n _, feed_vars = create_feed(eval_feed, use_pyreader=False)\n fetches = model.eval(feed_vars)\n\n eval_prog = eval_prog.clone(True)\n\n # load model\n exe.run(startup_prog)\n if 'weights' in cfg:\n checkpoint.load_params(exe, eval_prog, cfg.weights)\n\n assert cfg.metric in ['WIDERFACE'], \\\n \"unknown metric type {}\".format(cfg.metric)\n\n annotation_file = getattr(eval_feed.dataset, 'annotation', None)\n dataset_dir = FLAGS.dataset_dir if FLAGS.dataset_dir else \\\n getattr(eval_feed.dataset, 'dataset_dir', None)\n img_root_dir = dataset_dir\n if FLAGS.eval_mode == \"widerface\":\n image_dir = getattr(eval_feed.dataset, 'image_dir', None)\n img_root_dir = os.path.join(dataset_dir, image_dir)\n gt_file = os.path.join(dataset_dir, annotation_file)\n pred_dir = FLAGS.output_eval if FLAGS.output_eval else 'output/pred'\n face_eval_run(\n exe,\n eval_prog,\n fetches,\n img_root_dir,\n gt_file,\n pred_dir=pred_dir,\n eval_mode=FLAGS.eval_mode,\n multi_scale=FLAGS.multi_scale)\n\n\nif __name__ == '__main__':\n parser = ArgsParser()\n parser.add_argument(\n \"-d\",\n \"--dataset_dir\",\n default=None,\n type=str,\n help=\"Dataset path, same as DataFeed.dataset.dataset_dir\")\n parser.add_argument(\n \"-f\",\n \"--output_eval\",\n default=None,\n type=str,\n help=\"Evaluation file directory, default is current directory.\")\n parser.add_argument(\n \"-e\",\n \"--eval_mode\",\n default=\"widerface\",\n type=str,\n help=\"Evaluation mode, include `widerface` and `fddb`, default is `widerface`.\"\n )\n parser.add_argument(\n \"--multi_scale\",\n action='store_true',\n default=False,\n help=\"If True it will select `multi_scale` evaluation. Default is `False`, it will select `single-scale` evaluation.\")\n FLAGS = parser.parse_args()\n main()\n" ]
[ [ "numpy.zeros", "numpy.maximum", "numpy.row_stack", "numpy.column_stack", "numpy.prod", "numpy.array", "numpy.minimum" ] ]
DequanZhu/facenet
[ "78f32c2fa43217489f5b340826991780b3276fe2" ]
[ "src/train.py" ]
[ "from __future__ import division\r\nimport os\r\nimport sys\r\nimport numpy as np\r\nimport tensorflow as tf\r\nfrom tensorflow.keras.optimizers import schedules, Adam\r\nfrom tensorflow.python.keras.losses import SparseCategoricalCrossentropy\r\nfrom tensorflow.python.keras.metrics import SparseCategoricalAccuracy\r\nfrom facenet import FaceNet\r\nfrom options.train_options import TrainOptions\r\nfrom losses import *\r\nfrom datasets import create_datasets_from_tfrecord\r\nfrom utils import check_folder\r\nfrom progressbar import *\r\n\r\n\r\nclass Trainer(object):\r\n def __init__(self, args):\r\n self.args=args\r\n self.model = FaceNet(args).model\r\n self.train_datasets, self.nrof_train = create_datasets_from_tfrecord(tfrcd_dir=args.datasets,\r\n batch_size = args.batch_size,\r\n phase='train')\r\n\r\n self.val_datasets, self.nrof_val = create_datasets_from_tfrecord(tfrcd_dir=args.datasets,\r\n batch_size = args.batch_size,\r\n phase='val')\r\n self.lr_schedule = schedules.ExponentialDecay(args.learning_rate,\r\n decay_steps=10000,\r\n decay_rate=0.96,\r\n staircase=True)\r\n\r\n self.optimizer = Adam(learning_rate=self.lr_schedule, beta_1=0.9, beta_2=0.999, epsilon=0.1)\r\n self.checkpoint = tf.train.Checkpoint(epoch=tf.Variable(0,dtype=tf.int64),\r\n n_iter=tf.Variable(0,dtype=tf.int64), \r\n best_pred=tf.Variable(0.0,dtype=tf.float32),\r\n optimizer=self.optimizer,\r\n model=self.model)\r\n self.manager = tf.train.CheckpointManager(self.checkpoint, args.checkpoint_dir, max_to_keep=3)\r\n check_folder(args.log_dir)\r\n self.train_summary_writer = tf.summary.create_file_writer(args.log_dir)\r\n\r\n # @tf.function()\r\n def train_one_step(self, train_acc_metric, loss_layer, batch_examples, trainable_variables):\r\n with tf.GradientTape() as tape:\r\n batch_images, batch_labels = batch_examples\r\n features = self.model(batch_images)\r\n embedding = tf.math.l2_normalize(features, axis=1, epsilon=1e-10)\r\n logits = loss_layer(embedding, batch_labels)\r\n loss = SparseCategoricalCrossentropy(from_logits=True)(batch_labels, logits)\r\n train_acc_metric(batch_labels, logits)\r\n gradients = tape.gradient(loss, trainable_variables)\r\n self.optimizer.apply_gradients(zip(gradients, trainable_variables))\r\n return loss\r\n\r\n def training(self, epoch):\r\n opt = self.args\r\n loss_layer = ArcFaceSoftmaxLinear(opt.nrof_classes, opt.embedding_size, opt.margin, opt.feature_scale)\r\n trainable_variables=[]\r\n trainable_variables.extend(loss_layer.trainable_variables)\r\n trainable_variables.extend(self.model.trainable_variables)\r\n train_acc_metric = SparseCategoricalAccuracy()\r\n widgets = ['train :', Percentage(), ' ', Bar('#'), ' ',Timer(), ' ', ETA(), ' ']\r\n pbar = ProgressBar(widgets=widgets, max_value=int(self.nrof_train//opt.batch_size)+1).start()\r\n for batch_id, batch_examples in pbar(enumerate(self.train_datasets)):\r\n loss = self.train_one_step(train_acc_metric, loss_layer, batch_examples, trainable_variables)\r\n with self.train_summary_writer.as_default():\r\n tf.summary.scalar('total_loss', loss, self.checkpoint.n_iter)\r\n self.checkpoint.n_iter.assign_add(1)\r\n pbar.finish() \r\n train_acc = train_acc_metric.result()\r\n print('\\nTraining acc over epoch {}: {:.4f}'.format(epoch, train_acc))\r\n with self.train_summary_writer.as_default():\r\n tf.summary.scalar('train/acc', train_acc_metric.result(), self.checkpoint.epoch)\r\n train_acc_metric.reset_states()\r\n save_path = self.manager.save()\r\n print('save checkpoint to {}'.format(save_path))\r\n\r\n\r\n def validate(self, epoch):\r\n widgets = ['validate :', Percentage(), ' ', Bar('#'), ' ',Timer(), ' ', ETA(), ' ']\r\n pbar = ProgressBar(widgets=widgets, max_value=int(self.nrof_val//self.args.batch_size)+1).start()\r\n val_acc_metric = SparseCategoricalAccuracy()\r\n for batch_id, (batch_images_validate, batch_labels_validate) in pbar(enumerate(self.val_datasets)):\r\n prediction = self.model(batch_images_validate)\r\n val_acc_metric(batch_labels_validate, prediction)\r\n pbar.finish() \r\n val_acc = val_acc_metric.result()\r\n print('\\nvalidate acc over epoch {}: {:.4f}'.format(epoch, val_acc))\r\n with self.train_summary_writer.as_default():\r\n tf.summary.scalar('val/acc', val_acc_metric.result(),self.checkpoint.epoch)\r\n self.checkpoint.epoch.assign_add(1)\r\n \r\n val_acc_metric.reset_states()\r\n\r\n if(val_acc > self.checkpoint.best_pred):\r\n self.checkpoint.best_pred = val_acc\r\n with open(os.path.join(self.checkpoint_dir, 'best_pred.txt'), 'w') as f:\r\n f.write(str(self.best_pred))\r\n self.model.save(os.path.join(self.checkpoint_dir, 'best_model.h5'))\r\n\r\n\r\ndef main(argv):\r\n opt = TrainOptions(argv).parse()\r\n trainer = Trainer(opt)\r\n start_epoch = 0\r\n if opt.restore:\r\n start_epoch = trainer.checkpoint.restore(trainer.manager.latest_checkpoint)\r\n for epoch in range(start_epoch, opt.max_epoch):\r\n # trainer.training(epoch)\r\n if not opt.no_val and epoch % opt.eval_interval == (opt.eval_interval - 1):\r\n trainer.validate(epoch)\r\n\r\n\r\nif __name__ == '__main__':\r\n main(sys.argv[1:])\r\n" ]
[ [ "tensorflow.summary.scalar", "tensorflow.keras.optimizers.Adam", "tensorflow.math.l2_normalize", "tensorflow.python.keras.losses.SparseCategoricalCrossentropy", "tensorflow.python.keras.metrics.SparseCategoricalAccuracy", "tensorflow.GradientTape", "tensorflow.train.CheckpointManager", "tensorflow.Variable", "tensorflow.keras.optimizers.schedules.ExponentialDecay", "tensorflow.summary.create_file_writer" ] ]
huzq/scikit-learn
[ "f862129f36786acbae3d9f2d161bbb72d77b87ec" ]
[ "sklearn/decomposition/tests/test_incremental_pca.py" ]
[ "\"\"\"Tests for Incremental PCA.\"\"\"\nimport numpy as np\nimport pytest\nimport warnings\n\nfrom sklearn.utils._testing import assert_almost_equal\nfrom sklearn.utils._testing import assert_array_almost_equal\nfrom sklearn.utils._testing import assert_allclose_dense_sparse\nfrom numpy.testing import assert_array_equal\n\nfrom sklearn import datasets\nfrom sklearn.decomposition import PCA, IncrementalPCA\n\nfrom scipy import sparse\n\niris = datasets.load_iris()\n\n\ndef test_incremental_pca():\n # Incremental PCA on dense arrays.\n X = iris.data\n batch_size = X.shape[0] // 3\n ipca = IncrementalPCA(n_components=2, batch_size=batch_size)\n pca = PCA(n_components=2)\n pca.fit_transform(X)\n\n X_transformed = ipca.fit_transform(X)\n\n assert X_transformed.shape == (X.shape[0], 2)\n np.testing.assert_allclose(\n ipca.explained_variance_ratio_.sum(),\n pca.explained_variance_ratio_.sum(),\n rtol=1e-3,\n )\n\n for n_components in [1, 2, X.shape[1]]:\n ipca = IncrementalPCA(n_components, batch_size=batch_size)\n ipca.fit(X)\n cov = ipca.get_covariance()\n precision = ipca.get_precision()\n np.testing.assert_allclose(\n np.dot(cov, precision), np.eye(X.shape[1]), atol=1e-13\n )\n\n\[email protected](\n \"matrix_class\", [sparse.csc_matrix, sparse.csr_matrix, sparse.lil_matrix]\n)\ndef test_incremental_pca_sparse(matrix_class):\n # Incremental PCA on sparse arrays.\n X = iris.data\n pca = PCA(n_components=2)\n pca.fit_transform(X)\n X_sparse = matrix_class(X)\n batch_size = X_sparse.shape[0] // 3\n ipca = IncrementalPCA(n_components=2, batch_size=batch_size)\n\n X_transformed = ipca.fit_transform(X_sparse)\n\n assert X_transformed.shape == (X_sparse.shape[0], 2)\n np.testing.assert_allclose(\n ipca.explained_variance_ratio_.sum(),\n pca.explained_variance_ratio_.sum(),\n rtol=1e-3,\n )\n\n for n_components in [1, 2, X.shape[1]]:\n ipca = IncrementalPCA(n_components, batch_size=batch_size)\n ipca.fit(X_sparse)\n cov = ipca.get_covariance()\n precision = ipca.get_precision()\n np.testing.assert_allclose(\n np.dot(cov, precision), np.eye(X_sparse.shape[1]), atol=1e-13\n )\n\n with pytest.raises(\n TypeError,\n match=(\n \"IncrementalPCA.partial_fit does not support \"\n \"sparse input. Either convert data to dense \"\n \"or use IncrementalPCA.fit to do so in batches.\"\n ),\n ):\n ipca.partial_fit(X_sparse)\n\n\ndef test_incremental_pca_check_projection():\n # Test that the projection of data is correct.\n rng = np.random.RandomState(1999)\n n, p = 100, 3\n X = rng.randn(n, p) * 0.1\n X[:10] += np.array([3, 4, 5])\n Xt = 0.1 * rng.randn(1, p) + np.array([3, 4, 5])\n\n # Get the reconstruction of the generated data X\n # Note that Xt has the same \"components\" as X, just separated\n # This is what we want to ensure is recreated correctly\n Yt = IncrementalPCA(n_components=2).fit(X).transform(Xt)\n\n # Normalize\n Yt /= np.sqrt((Yt**2).sum())\n\n # Make sure that the first element of Yt is ~1, this means\n # the reconstruction worked as expected\n assert_almost_equal(np.abs(Yt[0][0]), 1.0, 1)\n\n\ndef test_incremental_pca_inverse():\n # Test that the projection of data can be inverted.\n rng = np.random.RandomState(1999)\n n, p = 50, 3\n X = rng.randn(n, p) # spherical data\n X[:, 1] *= 0.00001 # make middle component relatively small\n X += [5, 4, 3] # make a large mean\n\n # same check that we can find the original data from the transformed\n # signal (since the data is almost of rank n_components)\n ipca = IncrementalPCA(n_components=2, batch_size=10).fit(X)\n Y = ipca.transform(X)\n Y_inverse = ipca.inverse_transform(Y)\n assert_almost_equal(X, Y_inverse, decimal=3)\n\n\ndef test_incremental_pca_validation():\n # Test that n_components is >=1 and <= n_features.\n X = np.array([[0, 1, 0], [1, 0, 0]])\n n_samples, n_features = X.shape\n for n_components in [-1, 0, 0.99, 4]:\n with pytest.raises(\n ValueError,\n match=(\n \"n_components={} invalid\"\n \" for n_features={}, need more rows than\"\n \" columns for IncrementalPCA\"\n \" processing\".format(n_components, n_features)\n ),\n ):\n IncrementalPCA(n_components, batch_size=10).fit(X)\n\n # Tests that n_components is also <= n_samples.\n n_components = 3\n with pytest.raises(\n ValueError,\n match=(\n \"n_components={} must be\"\n \" less or equal to the batch number of\"\n \" samples {}\".format(n_components, n_samples)\n ),\n ):\n IncrementalPCA(n_components=n_components).partial_fit(X)\n\n\ndef test_n_samples_equal_n_components():\n # Ensures no warning is raised when n_samples==n_components\n # Non-regression test for gh-19050\n ipca = IncrementalPCA(n_components=5)\n with warnings.catch_warnings():\n warnings.simplefilter(\"error\", RuntimeWarning)\n ipca.partial_fit(np.random.randn(5, 7))\n with warnings.catch_warnings():\n warnings.simplefilter(\"error\", RuntimeWarning)\n ipca.fit(np.random.randn(5, 7))\n\n\ndef test_n_components_none():\n # Ensures that n_components == None is handled correctly\n rng = np.random.RandomState(1999)\n for n_samples, n_features in [(50, 10), (10, 50)]:\n X = rng.rand(n_samples, n_features)\n ipca = IncrementalPCA(n_components=None)\n\n # First partial_fit call, ipca.n_components_ is inferred from\n # min(X.shape)\n ipca.partial_fit(X)\n assert ipca.n_components_ == min(X.shape)\n\n # Second partial_fit call, ipca.n_components_ is inferred from\n # ipca.components_ computed from the first partial_fit call\n ipca.partial_fit(X)\n assert ipca.n_components_ == ipca.components_.shape[0]\n\n\ndef test_incremental_pca_set_params():\n # Test that components_ sign is stable over batch sizes.\n rng = np.random.RandomState(1999)\n n_samples = 100\n n_features = 20\n X = rng.randn(n_samples, n_features)\n X2 = rng.randn(n_samples, n_features)\n X3 = rng.randn(n_samples, n_features)\n ipca = IncrementalPCA(n_components=20)\n ipca.fit(X)\n # Decreasing number of components\n ipca.set_params(n_components=10)\n with pytest.raises(ValueError):\n ipca.partial_fit(X2)\n # Increasing number of components\n ipca.set_params(n_components=15)\n with pytest.raises(ValueError):\n ipca.partial_fit(X3)\n # Returning to original setting\n ipca.set_params(n_components=20)\n ipca.partial_fit(X)\n\n\ndef test_incremental_pca_num_features_change():\n # Test that changing n_components will raise an error.\n rng = np.random.RandomState(1999)\n n_samples = 100\n X = rng.randn(n_samples, 20)\n X2 = rng.randn(n_samples, 50)\n ipca = IncrementalPCA(n_components=None)\n ipca.fit(X)\n with pytest.raises(ValueError):\n ipca.partial_fit(X2)\n\n\ndef test_incremental_pca_batch_signs():\n # Test that components_ sign is stable over batch sizes.\n rng = np.random.RandomState(1999)\n n_samples = 100\n n_features = 3\n X = rng.randn(n_samples, n_features)\n all_components = []\n batch_sizes = np.arange(10, 20)\n for batch_size in batch_sizes:\n ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X)\n all_components.append(ipca.components_)\n\n for i, j in zip(all_components[:-1], all_components[1:]):\n assert_almost_equal(np.sign(i), np.sign(j), decimal=6)\n\n\ndef test_incremental_pca_batch_values():\n # Test that components_ values are stable over batch sizes.\n rng = np.random.RandomState(1999)\n n_samples = 100\n n_features = 3\n X = rng.randn(n_samples, n_features)\n all_components = []\n batch_sizes = np.arange(20, 40, 3)\n for batch_size in batch_sizes:\n ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X)\n all_components.append(ipca.components_)\n\n for i, j in zip(all_components[:-1], all_components[1:]):\n assert_almost_equal(i, j, decimal=1)\n\n\ndef test_incremental_pca_batch_rank():\n # Test sample size in each batch is always larger or equal to n_components\n rng = np.random.RandomState(1999)\n n_samples = 100\n n_features = 20\n X = rng.randn(n_samples, n_features)\n all_components = []\n batch_sizes = np.arange(20, 90, 3)\n for batch_size in batch_sizes:\n ipca = IncrementalPCA(n_components=20, batch_size=batch_size).fit(X)\n all_components.append(ipca.components_)\n\n for components_i, components_j in zip(all_components[:-1], all_components[1:]):\n assert_allclose_dense_sparse(components_i, components_j)\n\n\ndef test_incremental_pca_partial_fit():\n # Test that fit and partial_fit get equivalent results.\n rng = np.random.RandomState(1999)\n n, p = 50, 3\n X = rng.randn(n, p) # spherical data\n X[:, 1] *= 0.00001 # make middle component relatively small\n X += [5, 4, 3] # make a large mean\n\n # same check that we can find the original data from the transformed\n # signal (since the data is almost of rank n_components)\n batch_size = 10\n ipca = IncrementalPCA(n_components=2, batch_size=batch_size).fit(X)\n pipca = IncrementalPCA(n_components=2, batch_size=batch_size)\n # Add one to make sure endpoint is included\n batch_itr = np.arange(0, n + 1, batch_size)\n for i, j in zip(batch_itr[:-1], batch_itr[1:]):\n pipca.partial_fit(X[i:j, :])\n assert_almost_equal(ipca.components_, pipca.components_, decimal=3)\n\n\ndef test_incremental_pca_against_pca_iris():\n # Test that IncrementalPCA and PCA are approximate (to a sign flip).\n X = iris.data\n\n Y_pca = PCA(n_components=2).fit_transform(X)\n Y_ipca = IncrementalPCA(n_components=2, batch_size=25).fit_transform(X)\n\n assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1)\n\n\ndef test_incremental_pca_against_pca_random_data():\n # Test that IncrementalPCA and PCA are approximate (to a sign flip).\n rng = np.random.RandomState(1999)\n n_samples = 100\n n_features = 3\n X = rng.randn(n_samples, n_features) + 5 * rng.rand(1, n_features)\n\n Y_pca = PCA(n_components=3).fit_transform(X)\n Y_ipca = IncrementalPCA(n_components=3, batch_size=25).fit_transform(X)\n\n assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1)\n\n\ndef test_explained_variances():\n # Test that PCA and IncrementalPCA calculations match\n X = datasets.make_low_rank_matrix(\n 1000, 100, tail_strength=0.0, effective_rank=10, random_state=1999\n )\n prec = 3\n n_samples, n_features = X.shape\n for nc in [None, 99]:\n pca = PCA(n_components=nc).fit(X)\n ipca = IncrementalPCA(n_components=nc, batch_size=100).fit(X)\n assert_almost_equal(\n pca.explained_variance_, ipca.explained_variance_, decimal=prec\n )\n assert_almost_equal(\n pca.explained_variance_ratio_, ipca.explained_variance_ratio_, decimal=prec\n )\n assert_almost_equal(pca.noise_variance_, ipca.noise_variance_, decimal=prec)\n\n\ndef test_singular_values():\n # Check that the IncrementalPCA output has the correct singular values\n\n rng = np.random.RandomState(0)\n n_samples = 1000\n n_features = 100\n\n X = datasets.make_low_rank_matrix(\n n_samples, n_features, tail_strength=0.0, effective_rank=10, random_state=rng\n )\n\n pca = PCA(n_components=10, svd_solver=\"full\", random_state=rng).fit(X)\n ipca = IncrementalPCA(n_components=10, batch_size=100).fit(X)\n assert_array_almost_equal(pca.singular_values_, ipca.singular_values_, 2)\n\n # Compare to the Frobenius norm\n X_pca = pca.transform(X)\n X_ipca = ipca.transform(X)\n assert_array_almost_equal(\n np.sum(pca.singular_values_**2.0), np.linalg.norm(X_pca, \"fro\") ** 2.0, 12\n )\n assert_array_almost_equal(\n np.sum(ipca.singular_values_**2.0), np.linalg.norm(X_ipca, \"fro\") ** 2.0, 2\n )\n\n # Compare to the 2-norms of the score vectors\n assert_array_almost_equal(\n pca.singular_values_, np.sqrt(np.sum(X_pca**2.0, axis=0)), 12\n )\n assert_array_almost_equal(\n ipca.singular_values_, np.sqrt(np.sum(X_ipca**2.0, axis=0)), 2\n )\n\n # Set the singular values and see what we get back\n rng = np.random.RandomState(0)\n n_samples = 100\n n_features = 110\n\n X = datasets.make_low_rank_matrix(\n n_samples, n_features, tail_strength=0.0, effective_rank=3, random_state=rng\n )\n\n pca = PCA(n_components=3, svd_solver=\"full\", random_state=rng)\n ipca = IncrementalPCA(n_components=3, batch_size=100)\n\n X_pca = pca.fit_transform(X)\n X_pca /= np.sqrt(np.sum(X_pca**2.0, axis=0))\n X_pca[:, 0] *= 3.142\n X_pca[:, 1] *= 2.718\n\n X_hat = np.dot(X_pca, pca.components_)\n pca.fit(X_hat)\n ipca.fit(X_hat)\n assert_array_almost_equal(pca.singular_values_, [3.142, 2.718, 1.0], 14)\n assert_array_almost_equal(ipca.singular_values_, [3.142, 2.718, 1.0], 14)\n\n\ndef test_whitening():\n # Test that PCA and IncrementalPCA transforms match to sign flip.\n X = datasets.make_low_rank_matrix(\n 1000, 10, tail_strength=0.0, effective_rank=2, random_state=1999\n )\n prec = 3\n n_samples, n_features = X.shape\n for nc in [None, 9]:\n pca = PCA(whiten=True, n_components=nc).fit(X)\n ipca = IncrementalPCA(whiten=True, n_components=nc, batch_size=250).fit(X)\n\n Xt_pca = pca.transform(X)\n Xt_ipca = ipca.transform(X)\n assert_almost_equal(np.abs(Xt_pca), np.abs(Xt_ipca), decimal=prec)\n Xinv_ipca = ipca.inverse_transform(Xt_ipca)\n Xinv_pca = pca.inverse_transform(Xt_pca)\n assert_almost_equal(X, Xinv_ipca, decimal=prec)\n assert_almost_equal(X, Xinv_pca, decimal=prec)\n assert_almost_equal(Xinv_pca, Xinv_ipca, decimal=prec)\n\n\ndef test_incremental_pca_partial_fit_float_division():\n # Test to ensure float division is used in all versions of Python\n # (non-regression test for issue #9489)\n\n rng = np.random.RandomState(0)\n A = rng.randn(5, 3) + 2\n B = rng.randn(7, 3) + 5\n\n pca = IncrementalPCA(n_components=2)\n pca.partial_fit(A)\n # Set n_samples_seen_ to be a floating point number instead of an int\n pca.n_samples_seen_ = float(pca.n_samples_seen_)\n pca.partial_fit(B)\n singular_vals_float_samples_seen = pca.singular_values_\n\n pca2 = IncrementalPCA(n_components=2)\n pca2.partial_fit(A)\n pca2.partial_fit(B)\n singular_vals_int_samples_seen = pca2.singular_values_\n\n np.testing.assert_allclose(\n singular_vals_float_samples_seen, singular_vals_int_samples_seen\n )\n\n\ndef test_incremental_pca_fit_overflow_error():\n # Test for overflow error on Windows OS\n # (non-regression test for issue #17693)\n rng = np.random.RandomState(0)\n A = rng.rand(500000, 2)\n\n ipca = IncrementalPCA(n_components=2, batch_size=10000)\n ipca.fit(A)\n\n pca = PCA(n_components=2)\n pca.fit(A)\n\n np.testing.assert_allclose(ipca.singular_values_, pca.singular_values_)\n\n\ndef test_incremental_pca_feature_names_out():\n \"\"\"Check feature names out for IncrementalPCA.\"\"\"\n ipca = IncrementalPCA(n_components=2).fit(iris.data)\n\n names = ipca.get_feature_names_out()\n assert_array_equal([f\"incrementalpca{i}\" for i in range(2)], names)\n" ]
[ [ "numpy.sum", "numpy.eye", "numpy.sign", "sklearn.utils._testing.assert_almost_equal", "numpy.abs", "sklearn.utils._testing.assert_array_almost_equal", "sklearn.utils._testing.assert_allclose_dense_sparse", "numpy.random.randn", "numpy.random.RandomState", "numpy.arange", "numpy.testing.assert_allclose", "numpy.array", "numpy.dot", "sklearn.decomposition.IncrementalPCA", "numpy.linalg.norm", "sklearn.datasets.make_low_rank_matrix", "sklearn.decomposition.PCA", "sklearn.datasets.load_iris" ] ]
EVS-ATMOS/precipitation-onset
[ "3c9cf010b5246d17fae2796d271f6a8be892efb1" ]
[ "radar/Amazon Weather Program.py" ]
[ "import boto\nfrom boto.s3.connection import S3Connection\nfrom datetime import timedelta, datetime\nimport os\nimport pyart\nfrom matplotlib import pyplot as plt\nimport tempfile\nimport numpy as np\nimport cartopy\n\n\ndef _nearestDate(dates, pivot):\n return min(dates, key=lambda x: abs(x - pivot))\n\n\ndef get_radar_from_aws(site, datetime_t):\n \"\"\"\n Get the closest volume of NEXRAD data to a particular datetime.\n Parameters\n ----------\n site : string\n four letter radar designation\n datetime_t : datetime\n desired date time\n Returns\n -------\n radar : Py-ART Radar Object\n Radar closest to the queried datetime\n \"\"\"\n\n #First create the query string for the bucket knowing\n #how NOAA and AWS store the data\n\n my_pref = datetime_t.strftime('%Y/%m/%d/') + site\n\n #Connect to the bucket\n\n conn = S3Connection(anon = True)\n bucket = conn.get_bucket('noaa-nexrad-level2')\n\n #Get a list of files\n\n bucket_list = list(bucket.list(prefix = my_pref))\n\n #we are going to create a list of keys and datetimes to allow easy searching\n\n keys = []\n datetimes = []\n\n #populate the list\n\n for i in range(len(bucket_list)):\n this_str = str(bucket_list[i].key)\n if 'gz' in this_str:\n endme = this_str[-22:-4]\n fmt = '%Y%m%d_%H%M%S_V0'\n dt = datetime.strptime(endme, fmt)\n datetimes.append(dt)\n keys.append(bucket_list[i])\n\n if this_str[-3::] == 'V06':\n endme = this_str[-19::]\n fmt = '%Y%m%d_%H%M%S_V06'\n dt = datetime.strptime(endme, fmt)\n datetimes.append(dt)\n keys.append(bucket_list[i])\n\n #find the closest available radar to your datetime\n\n closest_datetime = _nearestDate(datetimes, datetime_t)\n index = datetimes.index(closest_datetime)\n\n localfile = tempfile.NamedTemporaryFile()\n keys[index].get_contents_to_filename(localfile.name)\n radar = pyart.io.read(localfile.name)\n #print (radar.info())\n #my_radar.fields.keys()\n #my_radar.metadata['vcp_pattern']\n return radar\n\nbase_date = \"20150520_190000\"\nfmt = '%Y%m%d_%H%M%S' \nb_d = datetime.strptime(base_date, fmt)\n\nmy_radar = get_radar_from_aws('KHGX',b_d )\nnyq = my_radar.instrument_parameters['nyquist_velocity']['data'].max()\n#print (\"VCP: %s\"%my_radar.metadata['vcp_pattern'])\n#print (\"NYQ: %s\"%nyq)\n\n#Plot Bounds\n\ncenterx = -95.3632700\ncentery = 29.4718835\nzoom = 1.5\n\nxm = 25/18\nmin_lon = centerx - (zoom*xm)\nmin_lat = centery - zoom\nmax_lon = centerx + (zoom*xm)\nmax_lat = centery + zoom\n\nlal = np.arange(min_lat, max_lat, .5)\nlol = np.arange(min_lon, max_lon, .5)\n\n\ndisplay = pyart.graph.RadarMapDisplayCartopy(my_radar)\nlat_0 = display.loc[0]\nlon_0 = display.loc[1]\nproj = cartopy.crs.Mercator(\n central_longitude=lon_0,\n min_latitude=min_lat, max_latitude=max_lat)\n\nsaveloc = '/home/scarani/Desktop/output/'\n\n#Plot Relfectivity\nfig = plt.figure(figsize = [20,8])\ndisplay.plot_ppi_map('reflectivity', sweep = 0, projection=proj, resolution = '10m',\n vmin = -8, vmax = 64, mask_outside = False,\n cmap = pyart.graph.cm.NWSRef,\n min_lat = min_lat, min_lon = min_lon,\n max_lat = max_lat, max_lon = max_lon,\n lat_lines = lal, lon_lines = lol)\ngl = display.ax.gridlines(draw_labels=True,\n linewidth=2, color='gray', alpha=0.5, linestyle='--')\ngl.xlabels_top = False\ngl.ylabels_right = False\nplt.title('Reflectivity: ' + my_radar.time['units'].split()[2])\nplt.savefig(saveloc + my_radar.time['units'].split()[2] +'.png', bbox_inches = 'tight')\n\"\"\"\n#Plot Correlation Coefficient\nfig = plt.figure(figsize = [20,8])\ndisplay.plot_ppi_map('cross_correlation_ratio', sweep = 0, projection=proj, resolution = '10m',\n vmin = .8, vmax = 1, mask_outside = False,\n cmap = pyart.graph.cm.RefDiff,\n min_lat = min_lat, min_lon = min_lon,\n max_lat = max_lat, max_lon = max_lon,\n lat_lines = lal, lon_lines = lol)\ngl = display.ax.gridlines(draw_labels=True,\n linewidth=2, color='gray', alpha=0.5, linestyle='--')\ngl.xlabels_top = False\ngl.ylabels_right = False\nplt.savefig('/home/scarani/Desktop/Output/correlation_coefficent.png', bbox_inches = 'tight')\n\n#Plot Differential Reflectivity\nfig = plt.figure(figsize = [20,8])\ndisplay.plot_ppi_map('differential_reflectivity', sweep = 0, projection=proj, resolution = '10m',\n vmin = -1, vmax = 4, mask_outside = False,\n cmap = pyart.graph.cm.RefDiff,\n min_lat = min_lat, min_lon = min_lon,\n max_lat = max_lat, max_lon = max_lon,\n lat_lines = lal, lon_lines = lol)\ngl = display.ax.gridlines(draw_labels=True,\n linewidth=2, color='gray', alpha=0.5, linestyle='--')\ngl.xlabels_top = False\ngl.ylabels_right = False\nplt.title('Differential Reflectivity: ' + my_radar.time['units'].split()[2])\nplt.savefig('/home/scarani/Desktop/Output/differential_reflectivity.png', bbox_inches = 'tight')\n\n#Plot Velocity\nfig = plt.figure(figsize = [20,8])\ndisplay.plot_ppi_map('velocity', sweep = 1, projection=proj, resolution = '10m',\n vmin = -nyq*1.5, vmax = nyq*1.5, mask_outside = False,\n cmap = pyart.graph.cm.NWSVel,\n min_lat = min_lat, min_lon = min_lon,\n max_lat = max_lat, max_lon = max_lon,\n lat_lines = lal, lon_lines = lol)\ngl = display.ax.gridlines(draw_labels=True,\n linewidth=2, color='gray', alpha=0.5, linestyle='--')\ngl.xlabels_top = False\ngl.ylabels_right = False\nplt.savefig('/home/scarani/Desktop/Output/velocity.png', bbox_inches = 'tight')\n\"\"\"" ]
[ [ "numpy.arange", "matplotlib.pyplot.figure" ] ]
magnusmel/Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda
[ "cc226deb7b46852407900f9fec0caf62638defe2" ]
[ "lesson7.4/tensorflow/contrib/distributions/python/ops/bijectors/affine_impl.py" ]
[ "# Copyright 2016 The TensorFlow Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\n\"\"\"Affine bijector.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom tensorflow.contrib import linalg\nfrom tensorflow.contrib.distributions.python.ops import distribution_util\nfrom tensorflow.contrib.distributions.python.ops.shape import _DistributionShape\nfrom tensorflow.python.framework import dtypes\nfrom tensorflow.python.framework import ops\nfrom tensorflow.python.framework import tensor_util\nfrom tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import check_ops\nfrom tensorflow.python.ops import control_flow_ops\nfrom tensorflow.python.ops import math_ops\nfrom tensorflow.python.ops.distributions import bijector\n\n\n__all__ = [\n \"Affine\",\n]\n\n\ndef _as_tensor(x, name):\n \"\"\"Convenience to convert to `Tensor` or leave as `None`.\"\"\"\n return None if x is None else ops.convert_to_tensor(x, name=name)\n\n\nclass Affine(bijector.Bijector):\n \"\"\"Compute `Y = g(X; shift, scale) = scale @ X + shift`.\n\n Here `scale = c * I + diag(D1) + tril(L) + V @ diag(D2) @ V.T`.\n\n In TF parlance, the `scale` term is logically equivalent to:\n\n ```python\n scale = (\n scale_identity_multiplier * tf.diag(tf.ones(d)) +\n tf.diag(scale_diag) +\n scale_tril +\n scale_perturb_factor @ diag(scale_perturb_diag) @\n tf.transpose([scale_perturb_factor])\n )\n ```\n\n The `scale` term is applied without necessarily materializing constituent\n matrices, i.e., the matmul is [matrix-free](\n https://en.wikipedia.org/wiki/Matrix-free_methods) when possible.\n\n Examples:\n\n ```python\n # Y = X\n b = Affine()\n\n # Y = X + shift\n b = Affine(shift=[1., 2, 3])\n\n # Y = 2 * I @ X.T + shift\n b = Affine(shift=[1., 2, 3],\n scale_identity_multiplier=2.)\n\n # Y = tf.diag(d1) @ X.T + shift\n b = Affine(shift=[1., 2, 3],\n scale_diag=[-1., 2, 1]) # Implicitly 3x3.\n\n # Y = (I + v * v.T) @ X.T + shift\n b = Affine(shift=[1., 2, 3],\n scale_perturb_factor=[[1., 0],\n [0, 1],\n [1, 1]])\n\n # Y = (diag(d1) + v * diag(d2) * v.T) @ X.T + shift\n b = Affine(shift=[1., 2, 3],\n scale_diag=[1., 3, 3], # Implicitly 3x3.\n scale_perturb_diag=[2., 1], # Implicitly 2x2.\n scale_perturb_factor=[[1., 0],\n [0, 1],\n [1, 1]])\n\n ```\n\n \"\"\"\n\n def __init__(self,\n shift=None,\n scale_identity_multiplier=None,\n scale_diag=None,\n scale_tril=None,\n scale_perturb_factor=None,\n scale_perturb_diag=None,\n event_ndims=1,\n validate_args=False,\n name=\"affine\"):\n \"\"\"Instantiates the `Affine` bijector.\n\n This `Bijector` is initialized with `shift` `Tensor` and `scale` arguments,\n giving the forward operation:\n\n ```none\n Y = g(X) = scale @ X + shift\n ```\n\n where the `scale` term is logically equivalent to:\n\n ```python\n scale = (\n scale_identity_multiplier * tf.diag(tf.ones(d)) +\n tf.diag(scale_diag) +\n scale_tril +\n scale_perturb_factor @ diag(scale_perturb_diag) @\n tf.transpose([scale_perturb_factor])\n )\n ```\n\n If none of `scale_identity_multiplier`, `scale_diag`, or `scale_tril` are\n specified then `scale += IdentityMatrix`. Otherwise specifying a\n `scale` argument has the semantics of `scale += Expand(arg)`, i.e.,\n `scale_diag != None` means `scale += tf.diag(scale_diag)`.\n\n Args:\n shift: Floating-point `Tensor`. If this is set to `None`, no shift is\n applied.\n scale_identity_multiplier: floating point rank 0 `Tensor` representing a\n scaling done to the identity matrix.\n When `scale_identity_multiplier = scale_diag = scale_tril = None` then\n `scale += IdentityMatrix`. Otherwise no scaled-identity-matrix is added\n to `scale`.\n scale_diag: Floating-point `Tensor` representing the diagonal matrix.\n `scale_diag` has shape [N1, N2, ... k], which represents a k x k\n diagonal matrix.\n When `None` no diagonal term is added to `scale`.\n scale_tril: Floating-point `Tensor` representing the diagonal matrix.\n `scale_diag` has shape [N1, N2, ... k, k], which represents a k x k\n lower triangular matrix.\n When `None` no `scale_tril` term is added to `scale`.\n The upper triangular elements above the diagonal are ignored.\n scale_perturb_factor: Floating-point `Tensor` representing factor matrix\n with last two dimensions of shape `(k, r)`. When `None`, no rank-r\n update is added to `scale`.\n scale_perturb_diag: Floating-point `Tensor` representing the diagonal\n matrix. `scale_perturb_diag` has shape [N1, N2, ... r], which\n represents an `r x r` diagonal matrix. When `None` low rank updates will\n take the form `scale_perturb_factor * scale_perturb_factor.T`.\n event_ndims: Scalar `int` `Tensor` indicating the number of dimensions\n associated with a particular draw from the distribution. Must be 0 or 1.\n validate_args: Python `bool` indicating whether arguments should be\n checked for correctness.\n name: Python `str` name given to ops managed by this object.\n\n Raises:\n ValueError: if `perturb_diag` is specified but not `perturb_factor`.\n TypeError: if `shift` has different `dtype` from `scale` arguments.\n \"\"\"\n self._graph_parents = []\n self._name = name\n self._validate_args = validate_args\n\n # Ambiguous definition of low rank update.\n if scale_perturb_diag is not None and scale_perturb_factor is None:\n raise ValueError(\"When scale_perturb_diag is specified, \"\n \"scale_perturb_factor must be specified.\")\n\n # Special case, only handling a scaled identity matrix. We don't know its\n # dimensions, so this is special cased.\n # We don't check identity_multiplier, since below we set it to 1. if all\n # other scale args are None.\n self._is_only_identity_multiplier = (scale_tril is None and\n scale_diag is None and\n scale_perturb_factor is None)\n\n with self._name_scope(\"init\", values=[\n shift, scale_identity_multiplier, scale_diag, scale_tril,\n scale_perturb_diag, scale_perturb_factor]):\n event_ndims = ops.convert_to_tensor(event_ndims, name=\"event_ndims\")\n event_ndims_const = tensor_util.constant_value(event_ndims)\n if event_ndims_const is not None and event_ndims_const not in (0, 1):\n raise ValueError(\"event_ndims(%s) was not 0 or 1\" % event_ndims_const)\n else:\n if validate_args:\n # Shape tool will catch if event_ndims is negative.\n event_ndims = control_flow_ops.with_dependencies(\n [check_ops.assert_less(\n event_ndims, 2, message=\"event_ndims must be 0 or 1\")],\n event_ndims)\n\n if event_ndims_const == 0 and not self._is_only_identity_multiplier:\n raise ValueError(\n \"If event_ndims == 0, the only scale argument you can pass is \"\n \"scale_identity_multiplier. All others operate on vectors.\")\n\n # In the absence of `loc` and `scale`, we'll assume `dtype` is `float32`.\n dtype = dtypes.float32\n\n if shift is not None:\n shift = ops.convert_to_tensor(shift, name=\"shift\")\n dtype = shift.dtype.base_dtype\n self._shift = shift\n\n # When no args are specified, pretend the scale matrix is the identity\n # matrix.\n if (self._is_only_identity_multiplier and\n scale_identity_multiplier is None):\n scale_identity_multiplier = ops.convert_to_tensor(1., dtype=dtype)\n\n # self._create_scale_operator returns a LinearOperator in all cases\n # except if self._is_only_identity_multiplier; in which case it\n # returns a scalar Tensor.\n scale = self._create_scale_operator(\n identity_multiplier=scale_identity_multiplier,\n diag=scale_diag,\n tril=scale_tril,\n perturb_diag=scale_perturb_diag,\n perturb_factor=scale_perturb_factor,\n shift=shift,\n validate_args=validate_args)\n\n if scale.dtype is not None:\n dtype = scale.dtype.base_dtype\n\n if scale is not None and not self._is_only_identity_multiplier:\n if (shift is not None and\n shift.dtype.base_dtype != scale.dtype.base_dtype):\n raise TypeError(\n \"shift.dtype({}) is incompatible with scale.dtype({}).\".format(\n shift.dtype, scale.dtype))\n\n if scale.tensor_rank is not None:\n batch_ndims = scale.tensor_rank - 2\n else:\n batch_ndims = scale.tensor_rank_tensor() - 2\n else:\n # We won't need shape inference when scale is None or when scale is a\n # scalar.\n batch_ndims = 0\n self._scale = scale\n self._shaper = _DistributionShape(\n batch_ndims=batch_ndims,\n event_ndims=event_ndims,\n validate_args=validate_args)\n super(Affine, self).__init__(\n event_ndims=event_ndims,\n graph_parents=(\n [event_ndims] +\n [self._scale] if tensor_util.is_tensor(self._scale)\n else self._scale.graph_parents +\n [self._shift] if self._shift is not None else []),\n is_constant_jacobian=True,\n dtype=dtype,\n validate_args=validate_args,\n name=name)\n\n def _create_scale_operator(self, identity_multiplier, diag, tril,\n perturb_diag, perturb_factor, shift,\n validate_args):\n \"\"\"Construct `scale` from various components.\n\n Args:\n identity_multiplier: floating point rank 0 `Tensor` representing a scaling\n done to the identity matrix.\n diag: Floating-point `Tensor` representing the diagonal matrix.\n `scale_diag` has shape [N1, N2, ... k], which represents a k x k\n diagonal matrix.\n tril: Floating-point `Tensor` representing the diagonal matrix.\n `scale_tril` has shape [N1, N2, ... k], which represents a k x k lower\n triangular matrix.\n perturb_diag: Floating-point `Tensor` representing the diagonal matrix of\n the low rank update.\n perturb_factor: Floating-point `Tensor` representing factor matrix.\n shift: Floating-point `Tensor` representing `shift in `scale @ X + shift`.\n validate_args: Python `bool` indicating whether arguments should be\n checked for correctness.\n\n Returns:\n scale. In the case of scaling by a constant, scale is a\n floating point `Tensor`. Otherwise, scale is a `LinearOperator`.\n\n Raises:\n ValueError: if all of `tril`, `diag` and `identity_multiplier` are `None`.\n \"\"\"\n identity_multiplier = _as_tensor(identity_multiplier, \"identity_multiplier\")\n diag = _as_tensor(diag, \"diag\")\n tril = _as_tensor(tril, \"tril\")\n perturb_diag = _as_tensor(perturb_diag, \"perturb_diag\")\n perturb_factor = _as_tensor(perturb_factor, \"perturb_factor\")\n\n # If possible, use the low rank update to infer the shape of\n # the identity matrix, when scale represents a scaled identity matrix\n # with a low rank update.\n shape_hint = None\n if perturb_factor is not None:\n shape_hint = distribution_util.dimension_size(perturb_factor, axis=-2)\n\n if self._is_only_identity_multiplier:\n if validate_args:\n return control_flow_ops.with_dependencies(\n [check_ops.assert_none_equal(\n identity_multiplier,\n array_ops.zeros([], identity_multiplier.dtype),\n [\"identity_multiplier should be non-zero.\"])],\n identity_multiplier)\n return identity_multiplier\n\n scale = distribution_util.make_tril_scale(\n loc=shift,\n scale_tril=tril,\n scale_diag=diag,\n scale_identity_multiplier=identity_multiplier,\n validate_args=validate_args,\n assert_positive=False,\n shape_hint=shape_hint)\n\n if perturb_factor is not None:\n return linalg.LinearOperatorUDVHUpdate(\n scale,\n u=perturb_factor,\n diag_update=perturb_diag,\n is_diag_update_positive=perturb_diag is None,\n is_non_singular=True, # Implied by is_positive_definite=True.\n is_self_adjoint=True,\n is_positive_definite=True,\n is_square=True)\n\n return scale\n\n @property\n def shift(self):\n \"\"\"The `shift` `Tensor` in `Y = scale @ X + shift`.\"\"\"\n return self._shift\n\n @property\n def scale(self):\n \"\"\"The `scale` `LinearOperator` in `Y = scale @ X + shift`.\"\"\"\n return self._scale\n\n def _forward(self, x):\n y = x\n if self._is_only_identity_multiplier:\n y *= self._scale\n if self.shift is not None:\n return y + self.shift\n return y\n y, sample_shape = self._shaper.make_batch_of_event_sample_matrices(\n y, expand_batch_dim=False)\n with ops.control_dependencies(self._maybe_check_scale() if\n self.validate_args else []):\n y = self.scale.matmul(y)\n y = self._shaper.undo_make_batch_of_event_sample_matrices(\n y, sample_shape, expand_batch_dim=False)\n if self.shift is not None:\n y += self.shift\n return y\n\n def _inverse(self, y):\n x = y\n if self.shift is not None:\n x -= self.shift\n if self._is_only_identity_multiplier:\n return x / self._scale\n\n x, sample_shape = self._shaper.make_batch_of_event_sample_matrices(\n x, expand_batch_dim=False)\n # Solve fails if the op is singular so we may safely skip this assertion.\n x = self.scale.solve(x)\n x = self._shaper.undo_make_batch_of_event_sample_matrices(\n x, sample_shape, expand_batch_dim=False)\n return x\n\n def _inverse_log_det_jacobian(self, y):\n return -self._forward_log_det_jacobian(y)\n\n def _forward_log_det_jacobian(self, x):\n if self._is_only_identity_multiplier:\n # We don't pad in this case and instead let the fldj be applied\n # via broadcast.\n event_size = distribution_util.pick_vector(\n math_ops.equal(self._shaper.event_ndims, 0),\n [1], array_ops.shape(x))[-1]\n event_size = math_ops.cast(event_size, dtype=self._scale.dtype)\n return math_ops.log(math_ops.abs(self._scale)) * event_size\n return self.scale.log_abs_determinant()\n\n def _maybe_check_scale(self):\n try:\n return [self.scale.assert_non_singular()]\n except NotImplementedError:\n pass\n return []\n" ]
[ [ "tensorflow.python.ops.math_ops.equal", "tensorflow.python.ops.math_ops.abs", "tensorflow.python.ops.array_ops.zeros", "tensorflow.python.ops.array_ops.shape", "tensorflow.contrib.distributions.python.ops.distribution_util.make_tril_scale", "tensorflow.contrib.distributions.python.ops.shape._DistributionShape", "tensorflow.python.ops.check_ops.assert_less", "tensorflow.python.ops.math_ops.cast", "tensorflow.contrib.distributions.python.ops.distribution_util.dimension_size", "tensorflow.python.framework.ops.convert_to_tensor", "tensorflow.contrib.linalg.LinearOperatorUDVHUpdate", "tensorflow.python.framework.tensor_util.constant_value", "tensorflow.python.framework.tensor_util.is_tensor" ] ]
bryanblackbee/topic__deep-learning-python
[ "6d916cee3457a886f3bffc7a5dd97a4d627b3c23" ]
[ "chap06/weather_modelv4_stacked_rnn_with_dropout.py" ]
[ "import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.backend import clear_session\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing import sequence\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import (Flatten, Dense, SimpleRNN, LSTM, GRU)\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n\ndef ingest():\n # Read from CSV, keep only values\n df = pd.read_csv('jena_climate_2009_2016.csv')\n df = df.iloc[:,1:]\n df_values = df.values\n # Normalisation\n df_mean = df_values[:200000].mean(axis=0)\n df_std = df_values[:200000].std(axis=0)\n df_values-=df_mean\n df_values/=df_std\n return df_values\n\n# Generator\ndef generator(data, lookback=0, delay=0, min_index=0, \n max_index=None, shuffle=False,\n batch_size=128, step=6):\n if max_index == None:\n max_index = len(data) - delay - 1\n i = min_index + lookback\n \n while 1:\n if shuffle:\n rows = np.random.randint(\n min_index + lookback, max_index, size=batch_size)\n else:\n if i + batch_size >= max_index:\n i = min_index + lookback\n rows = np.arange(i, min(i + batch_size, max_index))\n i+= len(rows)\n \n samples = np.zeros((len(rows),\n lookback // step,\n data.shape[-1]))\n targets = np.zeros((len(rows,)))\n for j, row in enumerate(rows):\n indices = range(rows[j] - lookback, rows[j], step)\n samples[j] = data[indices]\n targets[j] = data[rows[j] + delay][1]\n yield samples, targets\n\ndf_values = ingest()\n\nLOOKBACK, STEP, DELAY, BATCH_SIZE = 1440, 6, 144, 128\ntrain_min_i, train_max_i = 0, 200000\nval_min_i, val_max_i = 200001, 300000\ntest_min_i, test_max_i = 300001, None\nval_steps = (val_max_i - val_min_i - LOOKBACK)\ntest_steps = (len(df_values) - test_min_i - LOOKBACK)\ntrain_gen = generator(df_values, \n lookback=LOOKBACK, delay=DELAY, \n min_index=train_min_i, max_index=train_max_i, \n batch_size=BATCH_SIZE, step=STEP,shuffle=True)\nval_gen = generator(df_values, \n lookback=LOOKBACK, delay=DELAY, \n min_index=val_min_i, max_index=val_max_i, \n batch_size=BATCH_SIZE, step=STEP,shuffle=False)\ntest_gen = generator(df_values, \n lookback=LOOKBACK, delay=DELAY, \n min_index=test_min_i, max_index=test_max_i, \n batch_size=BATCH_SIZE, step=STEP,shuffle=False) \n\n# Instantiate Model\n###################\nclear_session()\nmodel4 = Sequential()\nmodel4.add(GRU(32, dropout=0.1, recurrent_dropout=0.5, \n input_shape=(None, df_values.shape[-1]), return_sequences=True))\nmodel4.add(GRU(64, dropout=0.1, recurrent_dropout=0.5, \n activation='relu'))\nmodel4.add(Dense(1))\nmodel4.compile(optimizer=RMSprop(), loss='mae', metrics=['mae'])\nprint(model4.summary())\n\n# Train\n#######\nm2_callbacks = [\n # interrupt training when there is no more improvement.\n # patience=2 means interrupt training when accuracy has stopped improving\n # for more than 2 epochs. mae MUST be in the compile step in the metrics\n EarlyStopping(monitor='mae', patience=2),\n # saves the current weights after every epoch\n # only overwrite the model file when val_loss has improved\n ModelCheckpoint('weather__v4__stacked_rnn_with_dropout.h5', monitor='val_loss', save_best_only=True)]\nhistory4 = model4.fit(train_gen, \n steps_per_epoch=500, \n epochs=40, \n validation_data=val_gen, \n callbacks=m2_callbacks,\n validation_steps=val_steps)\nmetrics_df = pd.DataFrame(history4.history)\nmetrics_df.to_csv('history4.csv', index=False)\n\n# Save\n######\nmodel4.save('weather__v4__stacked_rnn_with_dropout.h5')\n" ]
[ [ "tensorflow.keras.models.Sequential", "pandas.read_csv", "tensorflow.keras.optimizers.RMSprop", "pandas.DataFrame", "tensorflow.keras.backend.clear_session", "tensorflow.keras.callbacks.EarlyStopping", "tensorflow.keras.layers.GRU", "tensorflow.keras.layers.Dense", "tensorflow.keras.callbacks.ModelCheckpoint", "numpy.random.randint" ] ]
Johnny-Wish/fake-news-detection-pipeline
[ "3bdad59d680968375a23d72c80af7d6ef11d7711" ]
[ "model/__main__.py" ]
[ "import os\nimport argparse\nimport heapq\nimport pandas as pd\nimport pickle as pkl\nfrom embedding_utils import EmbeddingLoader\nfrom sklearn.model_selection import RandomizedSearchCV, train_test_split\nfrom sklearn.model_selection._search import BaseSearchCV\n\n\ndef print_cv_result(result, n):\n if isinstance(result, BaseSearchCV):\n result = result.cv_results_\n\n scores = result['mean_test_score']\n params = result['params']\n\n if n < 0:\n n = len(scores)\n\n print(\"Cross Validation result in descending order: (totalling {} trials)\".format(n))\n for rank, candidate, in enumerate(heapq.nlargest(n, zip(scores, params), key=lambda tup: tup[0])):\n print(\"rank {}, score = {}\\n hyperparams = {}\".format(rank + 1, *candidate))\n\n\nif __name__ == '__main__':\n parser = argparse.ArgumentParser()\n parser.add_argument(\"--input\", required=True, help=\"parent dir to load embeddings\")\n parser.add_argument(\"--output\", required=True, help=\"parent dir to dump search results\")\n # uses python reflection to dynamically load model\n parser.add_argument(\"--classifier\", required=True,\n help=\"classifier to use, must be existent under model/, such as model/KNN.py\")\n parser.add_argument(\"--corpus\", default=\"title\", help=\"title, text, or concatenated\")\n parser.add_argument(\"--embedding\", default=\"d2v\",\n help=\"embeddings model to use, must be one of [d2v, nd2v, onehot], default is d2v\")\n parser.add_argument(\"--n_iter\", default=100, type=int, help=\"number of trials to run during cross-validation. \"\n \"default=100. This is NOT epochs to train d2v\")\n parser.add_argument(\"--n_jobs\", default=1, type=int, help=\"number of cpu workers to run in parallel\")\n parser.add_argument(\"--cv\", default=5, type=int, help=\"number of folds for cross-validation, default=5\")\n # hyperparameters for doc2vec\n parser.add_argument(\"--vec_size\", default=300, type=int,\n help=\"size of vectors, default is 300, recommended to be left untouched\")\n parser.add_argument(\"--win_size\", default=13, type=int,\n help=\"window size, used if model is d2v, default = 13\")\n parser.add_argument(\"--min_count\", default=5, type=int,\n help=\"min count for inclusion in dict, used if model is d2v, default = 5\")\n parser.add_argument(\"--dm\", action=\"store_true\",\n help=\"whether to use DM or DBOW, used if model is d2v, default is DBOW\")\n parser.add_argument(\"--epochs\", default=100, type=int,\n help=\"number of epochs to train the model for, used if model is d2v, default = 100. This is \"\n \"NOT the epochs for RandomizedSearch\")\n # hyperparameters for naive doc2vec\n parser.add_argument(\"--normalizer\", default=None,\n help=\"normalizer for naive doc2vec, either l2 or mean, default is None\")\n # hyperparameters for one-hot\n parser.add_argument(\"--scorer\", default=\"count\",\n help=\"scorer function for one-hot, either tfidf or count, default is count\")\n\n opt = parser.parse_args()\n print(opt)\n\n loader = EmbeddingLoader(opt.input)\n\n # filename is saved for dumping CV results later\n if opt.embedding == \"d2v\":\n filename = loader.get_d2v_filename(corpus=opt.corpus, vec_size=opt.vec_size, win_size=opt.win_size,\n min_count=opt.min_count, dm=opt.dm, epochs=opt.epochs)\n embeddings = loader.get_d2v(corpus=opt.corpus, vec_size=opt.vec_size, win_size=opt.win_size,\n min_count=opt.min_count, dm=opt.dm, epochs=opt.epochs)\n elif opt.embedding == \"nd2v\":\n filename = loader.get_nd2v_filename(corpus=opt.corpus, normalizer=opt.normalizer)\n embeddings = loader.get_nd2v(corpus=opt.corpus, normalizer=opt.normalizer)\n elif opt.embedding == \"onehot\":\n filename = loader.get_onehot_filename(corpus=opt.corpus, scorer=opt.scorer, normalize=opt.normalize is not None)\n embeddings = loader.get_onehot(corpus=opt.corpus, scorer=opt.scorer, normalize=opt.normalize is not None)\n else:\n print(\"unrecognized embedding method: {}; proceed with d2v as fall back\".format(opt.embedding))\n filename = loader.get_d2v_filename(corpus=opt.corpus, vec_size=opt.vec_size, win_size=opt.win_size,\n min_count=opt.min_count, dm=opt.dm, epochs=opt.epochs)\n embeddings = loader.get_d2v(corpus=opt.corpus, vec_size=opt.vec_size, win_size=opt.win_size,\n min_count=opt.min_count, dm=opt.dm, epochs=opt.epochs)\n\n labels = loader.get_label()\n\n seed = 0\n embeddings_train, embeddings_test, labels_train, labels_test = \\\n train_test_split(embeddings, labels, test_size=0.25, random_state=seed, stratify=labels)\n\n # import the target file\n try:\n module = __import__(\"model.\" + opt.classifier)\n module = getattr(module, opt.classifier)\n except ModuleNotFoundError as e:\n print(\"There is no such file, double check that you have a `model/{}.py`\".format(opt.classifier))\n print(\"If you have checked and the problem persist, make sure to run this script from ROOTDIR instead of \"\n \"ROOTDIR/model, your command should look like `python -m model ...`\")\n raise e\n print(\"Successfully imported module {}\".format(module))\n\n # get the model from the target file\n try:\n model = getattr(module, \"model\")\n except AttributeError as e:\n print(\"There is no `model` attribute in `model/{}.py`\".format(opt.classifier))\n print(\"Make sure to include a variable named `model` in your file\")\n raise e\n print(\"Successfully obtained model {}\".format(model))\n\n # get the hyperparameters to be trained\n try:\n param_dist = getattr(module, \"param_dist\")\n except AttributeError as e:\n print(\"There is no `param_dist` attribute in `model/{}.py`\".format(opt.classifier))\n print(\"Make sure to include a variable named `param_dist` in your file\")\n raise e\n print(\"Successfully obtained param_dist {}\".format(param_dist))\n\n verbose = opt.cv * opt.n_iter\n searcher = RandomizedSearchCV(model, param_distributions=param_dist, n_iter=opt.n_iter, scoring='f1', cv=opt.cv,\n verbose=verbose, random_state=seed, error_score=0, return_train_score=False,\n n_jobs=opt.n_jobs)\n searcher.fit(embeddings_train, labels_train)\n\n print(\"best: {}\\n{}\\n{}\\n{}\".format(searcher.best_index_, searcher.best_score_, searcher.best_estimator_,\n searcher.best_params_))\n # The following line is meant for floydhub renderer to grep\n print('{\"metric\": \"highest_val\", \"value\": %f}' % searcher.best_score_)\n\n results = pd.DataFrame(searcher.cv_results_)\n\n filename_classifier = opt.classifier\n dump_filename = \"{}-{}\".format(opt.classifier, filename)\n with open(os.path.join(opt.output, dump_filename), \"wb\") as f:\n pkl.dump(results, f)\n\n print_cv_result(results, n=-1)\n\n # uses all training samples to refit the model\n searcher.best_estimator_.fit(embeddings_train, labels_train)\n test_score = searcher.best_estimator_.score(embeddings_test, labels_test)\n print(\"Final test score of the best performing model: {}\".format(test_score))\n\n # The following line is meant for floydhub renderer to grep\n print('{\"metric\": \"test\", \"value\": %f}' % test_score)\n" ]
[ [ "pandas.DataFrame", "sklearn.model_selection.RandomizedSearchCV", "sklearn.model_selection.train_test_split" ] ]
naototachibana/chainer-chemistry
[ "04577ba920b46c7141d4f01e212f7040eb91db19" ]
[ "examples/own_dataset/train_own_dataset.py" ]
[ "#!/usr/bin/env python\n\nfrom __future__ import print_function\n\nimport chainer\nimport numpy\nimport os\n\nfrom argparse import ArgumentParser\nfrom chainer.datasets import split_dataset_random\nfrom chainer import functions as F\nfrom chainer import optimizers\nfrom chainer import training\nfrom chainer.iterators import SerialIterator\nfrom chainer.training import extensions as E\n\nfrom chainer_chemistry.dataset.converters import concat_mols\nfrom chainer_chemistry.dataset.parsers import CSVFileParser\nfrom chainer_chemistry.dataset.preprocessors import preprocess_method_dict\nfrom chainer_chemistry.links.scaler.standard_scaler import StandardScaler\nfrom chainer_chemistry.models import Regressor\nfrom chainer_chemistry.models.prediction import set_up_predictor\nfrom chainer_chemistry.training.extensions.auto_print_report import \\\n AutoPrintReport\nfrom chainer_chemistry.utils import run_train\n\n\ndef rmse(x0, x1):\n return F.sqrt(F.mean_squared_error(x0, x1))\n\n\ndef parse_arguments():\n # Lists of supported preprocessing methods/models.\n method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn', 'relgcn',\n 'relgat', 'mpnn', 'gnnfilm']\n scale_list = ['standardize', 'none']\n\n # Set up the argument parser.\n parser = ArgumentParser(description='Regression on own dataset')\n parser.add_argument('--datafile', '-d', type=str,\n default='dataset_train.csv',\n help='csv file containing the dataset')\n parser.add_argument('--method', '-m', type=str, choices=method_list,\n help='method name', default='nfp')\n parser.add_argument('--label', '-l', nargs='+',\n default=['value1', 'value2'],\n help='target label for regression')\n parser.add_argument('--scale', type=str, choices=scale_list,\n help='label scaling method', default='standardize')\n parser.add_argument('--conv-layers', '-c', type=int, default=4,\n help='number of convolution layers')\n parser.add_argument('--batchsize', '-b', type=int, default=32,\n help='batch size')\n parser.add_argument(\n '--device', type=str, default='-1',\n help='Device specifier. Either ChainerX device specifier or an '\n 'integer. If non-negative integer, CuPy arrays with specified '\n 'device id are used. If negative integer, NumPy arrays are used')\n parser.add_argument('--out', '-o', type=str, default='result',\n help='path to save the computed model to')\n parser.add_argument('--epoch', '-e', type=int, default=10,\n help='number of epochs')\n parser.add_argument('--unit-num', '-u', type=int, default=16,\n help='number of units in one layer of the model')\n parser.add_argument('--seed', '-s', type=int, default=777,\n help='random seed value')\n parser.add_argument('--train-data-ratio', '-r', type=float, default=0.7,\n help='ratio of training data w.r.t the dataset')\n parser.add_argument('--protocol', type=int, default=2,\n help='pickle protocol version')\n parser.add_argument('--model-filename', type=str, default='regressor.pkl',\n help='saved model filename')\n return parser.parse_args()\n\n\ndef main():\n # Parse the arguments.\n args = parse_arguments()\n\n if args.label:\n labels = args.label\n class_num = len(labels) if isinstance(labels, list) else 1\n else:\n raise ValueError('No target label was specified.')\n\n # Dataset preparation. Postprocessing is required for the regression task.\n def postprocess_label(label_list):\n return numpy.asarray(label_list, dtype=numpy.float32)\n\n # Apply a preprocessor to the dataset.\n print('Preprocessing dataset...')\n preprocessor = preprocess_method_dict[args.method]()\n parser = CSVFileParser(preprocessor, postprocess_label=postprocess_label,\n labels=labels, smiles_col='SMILES')\n dataset = parser.parse(args.datafile)['dataset']\n\n # Scale the label values, if necessary.\n if args.scale == 'standardize':\n scaler = StandardScaler()\n scaler.fit(dataset.get_datasets()[-1])\n else:\n scaler = None\n\n # Split the dataset into training and validation.\n train_data_size = int(len(dataset) * args.train_data_ratio)\n train, _ = split_dataset_random(dataset, train_data_size, args.seed)\n\n # Set up the predictor.\n predictor = set_up_predictor(\n args.method, args.unit_num,\n args.conv_layers, class_num, label_scaler=scaler)\n\n # Set up the regressor.\n device = chainer.get_device(args.device)\n metrics_fun = {'mae': F.mean_absolute_error, 'rmse': rmse}\n regressor = Regressor(predictor, lossfun=F.mean_squared_error,\n metrics_fun=metrics_fun, device=device)\n\n print('Training...')\n run_train(regressor, train, valid=None,\n batch_size=args.batchsize, epoch=args.epoch,\n out=args.out, extensions_list=None,\n device=device, converter=concat_mols,\n resume_path=None)\n\n # Save the regressor's parameters.\n model_path = os.path.join(args.out, args.model_filename)\n print('Saving the trained model to {}...'.format(model_path))\n\n # TODO(nakago): ChainerX array cannot be sent to numpy array when internal\n # state has gradients.\n if hasattr(regressor.predictor.graph_conv, 'reset_state'):\n regressor.predictor.graph_conv.reset_state()\n\n regressor.save_pickle(model_path, protocol=args.protocol)\n\n\nif __name__ == '__main__':\n main()\n" ]
[ [ "numpy.asarray" ] ]
Tsunaou/Event-Structure-Enumerator
[ "6b8df19517d73d6c144a4395aa9bf33e24bcd8cd" ]
[ "lemma1.py" ]
[ "import numpy as np\nfrom typing import List\nfrom utils import allSubLists, filterPositionsId, setFilterPositionsId, makeAlphas\n\n\ndef cond1(A: np.ndarray, alpha: np.array) -> List[np.array]:\n \"\"\"\n 生成满足条件的 betas\n :param A: 矩阵 n*n\n :param alpha: 行向量 1*n\n :return: 是否可以返回一个符合条件的beta,若存在则返回所有beta的list\n \"\"\"\n assert A.shape[0] == A.shape[1]\n assert A.shape[0] == alpha.shape[0]\n n = A.shape[0]\n zero = np.zeros((1, n), dtype=bool)[0]\n one = np.ones((1, n), dtype=bool)[0]\n if alpha.__eq__(zero).all():\n return allSubLists(one)\n else:\n id_rows = list()\n for idx, value in enumerate(alpha):\n if value:\n id_rows.append(idx)\n tmp = np.ones((1, n), dtype=bool)[0]\n for i in id_rows:\n tmp = tmp.__and__(A[i])\n\n return allSubLists(tmp)\n\n\ndef cond2(A: np.ndarray, beta: np.array) -> bool:\n \"\"\"\n :param A: 矩阵 n*n\n :param beta: 行向量 1*n\n :return:\n \"\"\"\n assert A.shape[0] == A.shape[1]\n assert A.shape[0] == beta.shape[0]\n n = A.shape[0]\n for i in range(n):\n for j in range(n):\n if not (A[i][j] and beta[i]):\n continue\n if not beta[j]:\n return False\n return True\n\n\ndef cond2v2(A: np.ndarray, beta: np.array) -> bool:\n \"\"\"\n :param A: 矩阵 n*n\n :param beta: 行向量 1*n\n :return:\n \"\"\"\n assert A.shape[0] == A.shape[1]\n assert A.shape[0] == beta.shape[0]\n row_set = setFilterPositionsId(beta)\n rows = [A[i] for i in row_set]\n sub_row_set = set()\n for row in rows:\n sub_row_set = sub_row_set.union(setFilterPositionsId(row))\n return sub_row_set <= row_set\n\n\ndef cond3(A: np.ndarray, alpha: np.array) -> bool:\n \"\"\"\n :param A: 矩阵 n*n\n :param alpha: 行向量 n*1\n :return:\n \"\"\"\n assert A.shape[0] == A.shape[1]\n assert A.shape[0] == alpha.shape[0]\n n = A.shape[0]\n for i in range(n):\n for j in range(n):\n if not (A[i][j] and alpha[j]):\n continue\n if not alpha[i]:\n return False\n return True\n\ndef cond3v2(A: np.ndarray, alpha: np.array) -> bool:\n \"\"\"\n :param A: 矩阵 n*n\n :param alpha: 行向量 n*1\n :return:\n \"\"\"\n assert A.shape[0] == A.shape[1]\n assert A.shape[0] == alpha.shape[0]\n col_set = setFilterPositionsId(alpha)\n AT = A.T\n cols = [AT[i] for i in col_set]\n sub_col_set = set()\n for col in cols:\n sub_col_set = sub_col_set.union(setFilterPositionsId(col))\n return sub_col_set <= col_set\n\ndef concat(A: np.ndarray, alpha: np.array, beta: np.array):\n \"\"\"\n [A a^T\n b 1 ]\n :param A: n*n\n :param alpha: 1*n\n :param beta: 1*n\n :return:\n \"\"\"\n n = A.shape[0]\n assert A.shape[0] == A.shape[1]\n assert alpha.shape[0] == n\n assert beta.shape[0] == n\n res = np.ones((n + 1, n + 1), dtype=A.dtype)\n res[:n, :n] = A\n res[:n, n] = alpha.T\n res[n, :n] = beta\n return res\n\n\nif __name__ == '__main__':\n A = np.ones((1, 1), dtype=bool)\n alphas = makeAlphas(1)\n result = list()\n for alpha in alphas:\n betas = cond1(A, alpha)\n for beta in betas:\n assert cond2(A, beta) == cond2v2(A, beta)\n if cond2(A, beta) and cond3(A, alpha):\n result.append(concat(A, alpha, beta))\n" ]
[ [ "numpy.ones", "numpy.zeros" ] ]
juancruzgassoloncan/Udacity-Robo-nanodegree
[ "7621360ce05faf90660989e9d28f56da083246c9" ]
[ "src/rover/ex_4/extra_functions.py" ]
[ "import numpy as np\nimport cv2\nimport matplotlib.image as mpimg\n\n\ndef perspect_transform(img, src, dst):\n\n # Get transform matrix using cv2.getPerspectivTransform()\n M = cv2.getPerspectiveTransform(src, dst)\n # Warp image using cv2.warpPerspective()\n # keep same size as input image\n warped = cv2.warpPerspective(img, M, (img.shape[1], img.shape[0]))\n # Return the result\n return warped\n\n\ndef color_thresh(img, rgb_thresh=(160, 160, 160)):\n # Create an array of zeros same xy size as img, but single channel\n color_select = np.zeros_like(img[:, :, 0])\n # Require that each pixel be above all thre threshold values in RGB\n # above_thresh will now contain a boolean array with \"True\"\n # where threshold was met\n above_thresh = (img[:, :, 0] > rgb_thresh[0]) \\\n & (img[:, :, 1] > rgb_thresh[1]) \\\n & (img[:, :, 2] > rgb_thresh[2])\n # Index the array of zeros with the boolean array and set to 1\n color_select[above_thresh] = 1\n # Return the binary image\n return color_select\n\n\nimage_name = '../data/IMG/robocam_2017_10_03_15_35_32_475.jpg'\nimage = mpimg.imread(image_name)\n# Define calibration box in source (actual) and destination (desired) coordinates\n# These source and destination points are defined to warp the image\n# to a grid where each 10x10 pixel square represents 1 square meter\ndst_size = 5\n# Set a bottom offset to account for the fact that the bottom of the image\n# is not the position of the rover but a bit in front of it\nbottom_offset = 6\nsource = np.float32([[35, 135], [120, 97], [202, 97], [300, 135]])\ndestination = np.float32([[image.shape[1] / 2 - dst_size, image.shape[0] - bottom_offset],\n [image.shape[1] / 2 - dst_size, image.shape[0] -\n bottom_offset - 2 * dst_size],\n [image.shape[1] / 2 + dst_size, image.shape[0] -\n bottom_offset - 2 * dst_size],\n [image.shape[1] / 2 + dst_size, image.shape[0] - bottom_offset]])\n" ]
[ [ "numpy.zeros_like", "numpy.float32", "matplotlib.image.imread" ] ]
thomas-brth/sentinel
[ "747bd0b9a4a9356be69aae6d6ebbfa500e845218" ]
[ "sentIA/utils/figure/__init__.py" ]
[ "# Plotting tools and utility functions\n# Nested GridSpec : https://matplotlib.org/stable/gallery/subplots_axes_and_figures/gridspec_nested.html#sphx-glr-gallery-subplots-axes-and-figures-gridspec-nested-py\n# GridSpec : https://matplotlib.org/stable/gallery/subplots_axes_and_figures/gridspec_multicolumn.html#sphx-glr-gallery-subplots-axes-and-figures-gridspec-multicolumn-py\n# colorbar : https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html#sphx-glr-gallery-subplots-axes-and-figures-colorbar-placement-py\n\n#############\n## Imports ##\n#############\n\n## General imports ##\nfrom matplotlib import pyplot as plt\nfrom matplotlib import colors\nimport numpy as np\nimport os\n\n###############\n## Constants ##\n###############\n\n#############\n## Classes ##\n#############\n\nclass MidpointNormalize(colors.Normalize):\n\t\"\"\"\n\tUseful object enbling to normalize colorbar with a chosen midpoint.\n\t\"\"\"\n\tdef __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):\n\t\tsuper(MidpointNormalize, self).__init__(vmin, vmax, clip)\n\t\tself.midpoint = midpoint\n\t\n\tdef __call__(self, value, clip=None):\n\t\tx, y = [self.vmin, self.midpoint, self.vmax], [0,0.5,1]\n\t\treturn np.ma.masked_array(np.interp(value, x, y))\n\nclass FigBase():\n\t\"\"\"\n\t\"\"\"\n\n\tCREDITS = \"Credit : EU, contains modified Copernicus Sentinel data, processed with custom script.\"\n\t\n\tdef __init__(self, title : str, dim : tuple):\n\t\tself.title = title\n\t\tself.fig = plt.figure(figsize=dim)\n\n\tdef _format(self):\n\t\tpass\n\n\tdef show(self):\n\t\tpass\n\n###############\n## Functions ##\n###############\n\ndef main():\n\tpass\n\nif __name__ == '__main__':\n\tmain()\nelse:\n\tprint(f\"Module {__name__} imported.\", flush=True)" ]
[ [ "matplotlib.pyplot.figure", "numpy.interp" ] ]
Demonliquid/cars-python-cleaning
[ "91c516a33c4522114dc024cfaf04f1c1d594f973" ]
[ "Bike cleaning/motocicleta_p6.py" ]
[ "# %%\nimport os\nimport pandas as pd\nimport numpy as np\nimport datetime\nfrom googletrans import Translator\nfrom vininfo import Vin\n\n\n# %%\nmotocicleta_p6 = pd.read_excel(r'D:\\Basededatos\\Origen\\MOTOCICLETAS-COLOMBIA\\MOTOCICLETA_P6.xlsx', engine='openpyxl')\n\n\n# %%\nmotocicleta_p6.rename(columns={'MODELO': 'AÑO', 'ORIGEN': 'IMPORTACION'}, inplace=True)\nmotocicleta_p6.drop_duplicates(inplace=True)\nmotocicleta_p6convin = motocicleta_p6[motocicleta_p6[\"VIN\"].str.len() == 17]\nmotocicleta_p6convin = motocicleta_p6convin[motocicleta_p6convin[\"VIN\"].str.contains('Q|O|I', regex=True) == False]\nmotocicleta_p6sinvin = pd.concat([motocicleta_p6, motocicleta_p6convin, motocicleta_p6convin]).drop_duplicates(keep=False)\nmotocicleta_p6sinvin[\"VIN\"] = None\nmotocicleta_p6sinvin[\"ORIGEN\"] = None\n\n\n\n# %%\nmotocicleta_p6convin[\"ORIGEN\"] = motocicleta_p6convin[\"VIN\"].map(lambda x: Vin(x).country)\nmotocicleta_p6convin['ORIGEN'].replace('China (Mainland)', 'China', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace('Taiwan, China', 'China', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Cote d'Ivoire\", 'Costa de Marfil', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Germany/West Germany\", 'Alemania', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Korea (South)\", 'Corea del Sur', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Saudi Arabia\", 'Arabia Saudita', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"United Kingdom\", 'Reino Unido', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Italy\", 'Italia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Greece\", 'Grecia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Belgium\", 'Belgica', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Luxembourg\", 'Luxemburgo', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"United States\", 'Estados Unidos', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Japan\", 'Japon', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Czech Republic\", 'Republica Checa', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"United Arab Emirates\", 'Emiratos Arabes Unidos', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Ethiopia\", 'Etiopia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Hungary\", 'Hungria', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Brazil\", 'Brasil', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Spain\", 'España', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"France\", 'Francia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Switzerland\", 'Suiza', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Thailand\", 'Tailandia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Denmark\", 'Dinamarca', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Finland\", 'Finlandia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Poland\", 'Polonia', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Myanmar\", 'Birmania', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Ireland\", 'Irlanda', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"Netherlands\", 'Paises Bajos', inplace=True)\nmotocicleta_p6convin['ORIGEN'].replace(r\"South Africa\", 'Sudafrica', inplace=True)\n\n\n# %%\nmotocicleta_p6 = pd.concat([motocicleta_p6convin, motocicleta_p6sinvin])\n\n\n\n# %%\ndict(motocicleta_p6[\"ORIGEN\"].value_counts())\n\n# %%\nmotocicleta_p6.to_csv(r'D:\\Basededatos\\Limpioparaentregar\\MOTOCICLETAS-COLOMBIA\\motocicleta_p6.csv', index=False)\n\n\n# %%" ]
[ [ "pandas.read_excel", "pandas.concat" ] ]
Taerbit/EXP
[ "b7468ae77bfccd7283cdfc0215af9d1032e472ce" ]
[ "src/run2.py" ]
[ "import Controller\nimport tensorflow as tf\nimport time\nimport efficientnet.tfkeras\n\n# Run pre-loaded pipelines\nstart_time= time.time()\n\n#E0 - G\nmodel = tf.keras.models.load_model(\"..\\\\models\\\\200324_EfficientNetB0NoisyStudent_001.h5\", compile=False)\n\nimage_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Lesions\\\\\", \".jpg\", \"ISIC_\", \"_downsampled\", 0]\nseg_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Segmentations\\\\\", \".png\", \"ISIC_\", \"_segmentation\", 0]\nnumpy_tags = [\"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200324_EfficientNetB0NoisyStudent_001\\\\grad_cam\\\\\", \"_sorted.npy\", \"ISIC_\", \"_downsampled\", 0]\nlabel_tag = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Dual Classifier\\\\\", \".csv\", \"ISIC_\", \"_downsampled\", 0]\n\ninput_tags = [image_tags, numpy_tags, seg_tags, label_tag]\n\noutput_dir = \"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200324_EfficientNetB0NoisyStudent_001\\\\grad_cam\\\\\"\n\nController.pre_loaded_shap(model, input_tags, output=output_dir, save_csv=True)\n\n#E0 - S\nmodel = tf.keras.models.load_model(\"..\\\\models\\\\200324_EfficientNetB0NoisyStudent_001.h5\", compile=False)\n\nimage_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Lesions\\\\\", \".jpg\", \"ISIC_\", \"_downsampled\", 0]\nseg_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Segmentations\\\\\", \".png\", \"ISIC_\", \"_segmentation\", 0]\nnumpy_tags = [\"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200324_EfficientNetB0NoisyStudent_001\\\\shap\\\\\", \"_sorted.npy\", \"ISIC_\", \"_downsampled\", 0]\nlabel_tag = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Dual Classifier\\\\\", \".csv\", \"ISIC_\", \"_downsampled\", 0]\n\ninput_tags = [image_tags, numpy_tags, seg_tags, label_tag]\n\noutput_dir = \"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200324_EfficientNetB0NoisyStudent_001\\\\shap\\\\\"\n\nController.pre_loaded_shap(model, input_tags, output=output_dir, save_csv=True)\n\n#E7 - G\nmodel = tf.keras.models.load_model(\"..\\\\models\\\\200411_EfficientNetB7NoisyStudent_001.h5\", compile=False)\n\nimage_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Lesions\\\\\", \".jpg\", \"ISIC_\", \"_downsampled\", 0]\nseg_tags = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Segmentations\\\\\", \".png\", \"ISIC_\", \"_segmentation\", 0]\nnumpy_tags = [\"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200411_EfficientNetB7NoisyStudent_001\\\\grad_cam\\\\\", \"_sorted.npy\", \"ISIC_\", \"_downsampled\", 0]\nlabel_tag = [\"C:\\\\Users\\\\finnt\\\\OneDrive\\\\Documents\\\\Uni\\\\Year 4\\\\Honours\\\\Project\\\\Dual Classifier\\\\\", \".csv\", \"ISIC_\", \"_downsampled\", 0]\n\ninput_tags = [image_tags, numpy_tags, seg_tags, label_tag]\n\noutput_dir = \"C:\\\\Users\\\\finnt\\\\Documents\\\\Honours Results\\\\200411_EfficientNetB7NoisyStudent_001\\\\grad_cam\\\\\"\n\nController.pre_loaded_shap(model, input_tags, output=output_dir, save_csv=True)\n\nprint(\"Finished: \" + str((time.time()-start_time)/60) + \" mins\")" ]
[ [ "tensorflow.keras.models.load_model" ] ]
noe/iterative_expansion_lms
[ "a5533a60f6f749673dae2329eeae0646ee2b740d" ]
[ "src/syntaxd/fairseq/criterion.py" ]
[ "# Copyright (c) Facebook, Inc. and its affiliates.\n#\n# This source code is licensed under the MIT license found in the\n# LICENSE file in the root directory of this source tree.\n\nfrom argparse import ArgumentParser\nimport math\nimport torch.nn.functional as F\n\nfrom fairseq import utils\n\nfrom fairseq.criterions import FairseqCriterion, register_criterion\n\nfrom syntaxd.data.dependency.binarize_data import KEY_PREV_LEVEL_TOKENS\n\n\n@register_criterion('token_expansion_cross_entropy')\nclass DoubleCrossEntropyCriterion(FairseqCriterion):\n\n def __init__(self, args, task):\n super().__init__(args, task)\n self.scale_loss_with_padding = args.scale_loss_with_padding\n\n @staticmethod\n def add_args(parser: ArgumentParser):\n parser.add_argument('--scale-loss-with-padding', action='store_true')\n\n def forward(self, model, sample, reduce=True):\n \"\"\"Compute the loss for the given sample.\n\n Returns a tuple with three elements:\n 1) the loss\n 2) the sample size, which is used as the denominator for the gradient\n 3) logging outputs to display while training\n \"\"\"\n net_output = model(**sample['net_input'])\n token_loss, expansion_loss = self.compute_loss(model, net_output, sample, reduce=reduce)\n sample_size = sample['target'].size(0) if self.args.sentence_avg else sample['ntokens']\n num_batch_tokens = sample['num_batch_tokens']\n num_non_pad_tokens = num_batch_tokens - sample['num_pad_tokens']\n batch_density = float(num_non_pad_tokens) / num_batch_tokens\n loss = (token_loss + expansion_loss)\n if self.scale_loss_with_padding:\n loss = loss * batch_density\n logging_output = {\n 'loss': utils.item(loss.data) if reduce else loss.data,\n 'token_loss': utils.item(token_loss.data) if reduce else token_loss.data,\n 'expansion_loss': utils.item(expansion_loss.data) if reduce else expansion_loss.data,\n 'ntokens': sample['ntokens'],\n 'nsentences': sample['net_input'][KEY_PREV_LEVEL_TOKENS].size(0),\n 'sample_size': sample_size,\n 'target_num_pad_tokens': sample['num_pad_tokens'],\n 'target_num_batch_tokens': sample['num_batch_tokens'],\n 'target_pad_ratio': sample['pad_ratio'],\n }\n return loss, sample_size, logging_output\n\n def compute_loss(self, model, net_output, sample, reduce=True):\n tokens_lprobs = model.get_normalized_probs_tokens(net_output, log_probs=True)\n tokens_lprobs = tokens_lprobs.view(-1, tokens_lprobs.size(-1))\n tokens_target = model.get_targets_tokens(sample, net_output).view(-1)\n token_loss = F.nll_loss(\n tokens_lprobs,\n tokens_target,\n ignore_index=self.padding_idx,\n reduction='sum' if reduce else 'none',\n )\n\n expansions_lprobs = model.get_normalized_probs_expansions(net_output, log_probs=True)\n expansions_lprobs = expansions_lprobs.view(-1, expansions_lprobs.size(-1))\n expansions_target = model.get_targets_expansions(sample, net_output).view(-1)\n expansions_loss = F.nll_loss(\n expansions_lprobs,\n expansions_target,\n ignore_index=self.padding_idx,\n reduction='sum' if reduce else 'none',\n )\n\n return token_loss, expansions_loss\n\n @staticmethod\n def aggregate_logging_outputs(logging_outputs):\n \"\"\"Aggregate logging outputs from data parallel training.\"\"\"\n loss_sum = sum(log.get('loss', 0) for log in logging_outputs)\n token_loss_sum = sum(log.get('token_loss', 0) for log in logging_outputs)\n expansion_loss_sum = sum(log.get('expansion_loss', 0) for log in logging_outputs)\n ntokens = sum(log.get('ntokens', 0) for log in logging_outputs)\n nsentences = sum(log.get('nsentences', 0) for log in logging_outputs)\n sample_size = sum(log.get('sample_size', 0) for log in logging_outputs)\n num_batch_tokens = sum(log.get('target_num_batch_tokens', 0) for log in logging_outputs)\n num_pad_tokens = sum(log.get('target_num_pad_tokens', 0) for log in logging_outputs)\n pad_ratio = float(num_pad_tokens) / num_batch_tokens\n agg_output = {\n 'loss': loss_sum / sample_size / math.log(2) if sample_size > 0 else 0.,\n 'token_loss': token_loss_sum / sample_size / math.log(2) if sample_size > 0 else 0.,\n 'expansion_loss': expansion_loss_sum / sample_size / math.log(2) if sample_size > 0 else 0.,\n 'ntokens': ntokens,\n 'nsentences': nsentences,\n 'sample_size': sample_size,\n 'target_num_pad_tokens': num_pad_tokens,\n 'target_num_non_pad_tokens': num_batch_tokens - num_pad_tokens,\n 'target_num_batch_tokens': num_batch_tokens,\n 'target_pad_ratio': pad_ratio,\n }\n if sample_size != ntokens:\n agg_output['nll_loss'] = loss_sum / ntokens / math.log(2)\n return agg_output\n" ]
[ [ "torch.nn.functional.nll_loss" ] ]
1054/a3cosmos-gas-evolution
[ "66027338602ed2830e289cfbb4db6200739b39d6" ]
[ "a3cosmos_gas_evolution/Common_Python_Code/calc_galaxy_luminosity_function.py" ]
[ "#!/usr/bin/env python\n# \n# 20190222\n# copied from \"calc_stellar_mass_function.py\", this code will superceed \"calc_stellar_mass_function.py\". \n# \n\nfrom __future__ import print_function\n\nimport os, sys, re, json, time, astropy\nimport numpy as np\nfrom astropy.table import Table, Column, hstack\nfrom copy import copy\nfrom numpy import log, log10, power, sum, sqrt, pi, exp\npow = power\nlg = log10\nln = log\nfrom scipy.interpolate import InterpolatedUnivariateSpline, interp1d\n\nif not (os.path.dirname(os.path.abspath(__file__)) in sys.path): sys.path.append(os.path.dirname(os.path.abspath(__file__)))\nimport apply_cosmology\ncosmo = apply_cosmology.cosmo\n\nif sys.version_info.major >= 3:\n long = int\nelse:\n pass\n\n\n\n\n# \n# def \n# \ndef Schechter_Function_for_LF(L, L_character, Phi_character, alpha):\n # \n # Schechter (1976)\n # \n # Phi(L) dL = (Phi_*) * (L/L_*)**(alpha) * exp(-L/L_*) dL/L_*\n # = (Phi_*) * x**(alpha) * exp(-x) dx\n # = (Phi_*) * 10**(lgx * alpha) * exp(-10**lgx) dx\n # = (Phi_*) * 10**(lgx * alpha) * exp(-10**lgx) dlnx\n # = (Phi_*) * 10**(lgx * alpha) * exp(-10**lgx) dlgx * ln(10)\n # = (Phi_*) * 10**((lgL-lgL_*)*(alpha+1)) * exp(-10**(lgL-lgL_*)) * ln(10) dlgx\n # = (Our_Phi_Phi_Schechter) dlgx\n # \n #lgx = lgL-lg_L0\n #Phi_Schechter = phi * (10**(lgx*(alpha+1))) * (np.exp(-10**lgx)) * ln(10) # per dex and already multiplied ln(10), so that its integral directly equals \\int Phi(L) / L dL\n # \n Phi_Schechter = Phi_character * (L/L_character)**(alpha) * np.exp(-(L/L_character)) # Mpc-3 dex-1\n #Phi_Schechter = Phi_Schechter * ln(10)\n return Phi_Schechter\n\n\ndef Saunders_Function_for_LF(L, L_character, Phi_character, alpha, sigma):\n # Saunders et al. (1990)\n Phi_Saunders = Phi_character * (L/L_character)**(1-alpha) * np.exp(-1.0/(2.0*sigma**2) * (np.log10(1.0+(L/L_character)))**2 )\n #print('Phi_character', Phi_character)\n #print('(L/L_character)**(1-alpha)', (L/L_character)**(1-alpha))\n #print('np.exp(-1.0/(2.0*sigma**2) * (np.log10(1.0+(L/L_character)))**2 )', np.exp(-1.0/(2.0*sigma**2) * (np.log10(1.0+(L/L_character)))**2 ))\n #print('Phi_Saunders', Phi_Saunders)\n return Phi_Saunders\n\n\n\n\n\n# \n# def \n# \ndef calc_radio_LF_Novak2017(z, lgL=None, galaxy_type = 'SFG'):\n # \n # Novak 2017 bibcode:2017A&A...602A...5N\n # IMF: Chabrier 2003\n # Saunders et al. (1990)\n # Outputs: lgL_grid, lgPhi_grid\n # \n # check z\n if not np.isscalar(z):\n raise ValueError('Please input a float number as the redshift!')\n # \n # check galaxy_type\n if not (type(galaxy_type) is str):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n else:\n if not (galaxy_type in ['ALL', 'SFG', 'QG']):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n # \n # make lgL_grid\n if lgL is None:\n lgL_grid = np.linspace(18.0, 25.0, num=1000, endpoint=True)\n else:\n lgL_grid = lgL\n # \n L_grid = 10**lgL_grid\n # \n # read LF parameters\n L_character = 1.85e21 # * 1.4e9 / 3.839e25 # vLv(1.4GHz,rest) = W Hz-1 --> Lsun\n Phi_character = 3.55e-3 # Mpc-3 dex-1\n alpha = 1.22\n sigma = 0.63\n # \n #Phi_z0 = Saunders_Function(L_grid, L_character, Phi_character, alpha, sigma)\n # \n # check z\n LF_zmin = 0.0\n LF_zmax = +np.inf\n if z < LF_zmin or z > LF_zmax:\n raise ValueError('calc_radio_LF_Novak2017: The input redshift is out of the allowed range of %s -- %s!'%(LF_zmin, LF_zmax))\n # \n # scale to z via pure luminosity evolution\n alphaL = 3.16\n betaL = -0.32\n L_grid_z = (L_grid / ((1.0+z)**(alphaL+(z*betaL))))\n Phi = Saunders_Function_for_LF(L_grid_z, L_character, Phi_character, alpha, sigma)\n lgPhi = np.log10(Phi)\n # \n if lgL is None:\n return lgL_grid, lgPhi\n else:\n return lgPhi\n\n\n\ndef calc_IR_250um_LF_Koprowski2017(z, lgL=None, galaxy_type = 'SFG'):\n # \n # Koprowski 2017 bibcode:2017MNRAS.471.4155K\n # IMF: Chabrier 2003\n # Saunders et al. (1990)\n # Outputs: lgL_grid, lgPhi_grid\n # \n # check z\n if not np.isscalar(z):\n raise ValueError('Please input a float number as the redshift!')\n # \n # check galaxy_type\n if not (type(galaxy_type) is str):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n else:\n if not (galaxy_type in ['ALL', 'SFG', 'QG']):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n # \n # make lgL_grid\n if lgL is None:\n lgL_grid = np.linspace(24.0, 27.0, num=1000, endpoint=True)\n else:\n lgL_grid = lgL\n # \n L_grid = 10**lgL_grid\n # \n # read LF parameters\n table_z_lower = [0.5, 1.5, 2.5, 3.5]\n table_z_upper = [1.5, 2.5, 3.5, 4.5]\n table_lgL_character = [25.20, 25.40, 25.63, 25.84] # W Hz-1\n table_lgPhi_character = [-2.88, -3.03, -3.73, -4.59] # Mpc-3 dex-1\n alpha = -0.4\n # \n # check z\n LF_zmin = table_z_lower[0]\n LF_zmax = table_z_upper[-1]\n if z < LF_zmin or z > LF_zmax:\n raise ValueError('calc_IR_250um_LF_Koprowski2017: The input redshift is out of the allowed range of %s -- %s!'%(LF_zmin, LF_zmax))\n # \n # scale to z (using step function... <TODO>)\n Phi = None\n lgPhi = None\n for i in range(len(table_z_upper)):\n if z >= table_z_lower[i] and z <= table_z_upper[i]:\n L_character = 10**(table_lgL_character[i])\n Phi_character = 10**(table_lgPhi_character[i])\n Phi = Schechter_Function_for_LF(L_grid, L_character, Phi_character, alpha)\n lgPhi = np.log10(Phi)\n break\n # \n if lgL is None:\n return lgL_grid, lgPhi\n else:\n return lgPhi\n\n\n\ndef calc_IR_LF_Gruppioni2013(z, lgL=None, galaxy_type = 'SFG'):\n # \n # Gruppioni 2013 bibcode:\n # IMF: Chabrier 2003\n # H0 = 71 km s−1 Mpc−1, Ωm = 0.27, and ΩΛ = 0.73.\n # Saunders et al. (1990)\n # Outputs: lgL_grid, lgPhi_grid\n # \n # check z\n if not np.isscalar(z):\n raise ValueError('Please input a float number as the redshift!')\n # \n # check galaxy_type\n if not (type(galaxy_type) is str):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n else:\n if not (galaxy_type in ['ALL', 'SFG', 'QG']):\n raise ValueError('Please input either \"ALL\", \"SFG\" or \"QG\" as the galaxy_type!')\n # \n # make lgL_grid\n if lgL is None:\n lgL_grid = np.linspace(8.0, 14.0, num=1000, endpoint=True)\n else:\n lgL_grid = lgL\n # \n L_grid = 10**lgL_grid\n # \n # read LF parameters (their Table 7)\n table_data = [ [0.0 , 0.3 , 1.15, 0.52, 10.12, -2.29], \n [0.3 , 0.45, 1.2 , 0.5 , 10.41, -2.31], \n [0.45, 0.6 , 1.2 , 0.5 , 10.55, -2.35], \n [0.6 , 0.8 , 1.2 , 0.5 , 10.71, -2.35], \n [0.8 , 1.0 , 1.2 , 0.5 , 10.97, -2.40], \n [1.0 , 1.2 , 1.2 , 0.5 , 11.13, -2.43], \n [1.2 , 1.7 , 1.2 , 0.5 , 11.37, -2.70], \n [1.7 , 2.0 , 1.2 , 0.5 , 11.50, -3.00], \n [2.0 , 2.5 , 1.2 , 0.5 , 11.60, -3.01], \n [2.5 , 3.0 , 1.2 , 0.5 , 11.92, -3.27], \n [3.0 , 4.2 , 1.2 , 0.5 , 11.90, -3.74] ]\n table_data = np.array(table_data).T\n table_z_lower = table_data[0]\n table_z_upper = table_data[1]\n table_alpha = table_data[2]\n table_sigma = table_data[3]\n table_lgL_character = table_data[4] # Lsun\n table_lgPhi_character = table_data[5] # Mpc-3 dex-1\n # \n # check z\n LF_zmin = table_z_lower[0]\n LF_zmax = table_z_upper[-1]\n if z < LF_zmin or z > LF_zmax:\n raise ValueError('calc_IR_LF_Gruppioni2013: The input redshift is out of the allowed range of %s -- %s!'%(LF_zmin, LF_zmax))\n # \n # scale to z (using step function... <TODO>)\n Phi = None\n lgPhi = None\n for i in range(len(table_z_upper)):\n if z >= table_z_lower[i] and z <= table_z_upper[i]:\n L_character = 10**(table_lgL_character[i])\n Phi_character = 10**(table_lgPhi_character[i])\n alpha = table_alpha[i]\n sigma = table_sigma[i]\n Phi = Saunders_Function_for_LF(L_grid, L_character, Phi_character, alpha, sigma)\n lgPhi = np.log10(Phi)\n break\n # \n if lgL is None:\n return lgL_grid, lgPhi\n else:\n return lgPhi\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ]
[ [ "numpy.exp", "numpy.log10", "numpy.array", "numpy.isscalar", "numpy.linspace" ] ]
lindsey98/lightly
[ "6a66a38ccd9fc4422f5ebb0a13009abe8266d8e2" ]
[ "mytest/test.py" ]
[ "import torch\nimport torchvision\nimport lightly.models as models\nimport lightly.loss as loss\nimport lightly.data as data\nimport pytorch_lightning as pl\nimport math\nimport os\nimport shutil\nfrom tqdm import tqdm\nimport logging\nos.environ[\"CUDA_VISIBLE_DEVICES\"]=\"1,0\"\n\nexp_name = 'CIFAR10'\nstart_epoch = 0\navg_loss = 0.\navg_output_std = 0.\nepochs = 800\nout_dim = 2048\ninput_size = 32\n\nlogging.basicConfig(filename='mytest_{}.log'.format(exp_name), level=logging.INFO)\nlogger = logging.getLogger('trace')\n\n\n# the collate function applies random transforms to the input images\ncollate_fn = data.ImageCollateFunction(input_size=input_size, \n # require invariance to flips and rotations?\n hf_prob=0.0, # horizontal flip prob\n vf_prob=0.0, # vertical flip prob\n rr_prob=0.0, # (+90 degree) rotation is applied prob\n min_scale=0.0,\n cj_prob=0.7, # color jitter prob\n )\n\n# create a dataset from your image folder\ndataset = data.LightlyDataset(input_dir='../datasets/{}/train/'.format(exp_name))\nprint('Dataset is loaded')\nlogger.info('Dataset is loaded')\n\n# build a PyTorch dataloader\ndataloader = torch.utils.data.DataLoader(\n dataset, # pass the dataset to the dataloader\n batch_size=64, # a large batch size helps with the learning\n shuffle=True, # shuffling is important!\n collate_fn=collate_fn, # apply transformations to the input images\n drop_last=True) # FIXME: drop_last for distributed training, single-gpu training does not need this to be True\n\nlogger.info('Length of data {}'.format(len(dataloader.dataset)))\n\n# use a resnet50 backbone\nresnet = torchvision.models.resnet.resnet18()\nresnet = torch.nn.Sequential(*list(resnet.children())[:-1])\n\n# build the simsiam model\nmodel = models.SimSiam(resnet, num_ftrs=512)\n\n# use a criterion for self-supervised learning\ncriterion = loss.SymNegCosineSimilarityLoss()\n\n# get a PyTorch optimizer\noptimizer = torch.optim.SGD(model.parameters(), lr=1e-0, weight_decay=1e-5)\n\n# push to device\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel = torch.nn.DataParallel(model)\n\n# if resume\nif os.path.exists('../output/{}.pt'.format(exp_name)):\n model.load_state_dict(torch.load('../output/{}.pt'.format(exp_name), map_location=\"cpu\"))\n logger.info('Resume model {}'.format(exp_name))\n \nmodel.to(device)\nprint('Model is initialized and pushed to device')\nlogger.info('Model is initialized and pushed to device')\n\n# Train!\nfor e in range(start_epoch, epochs):\n \n print('Epoch {}'.format(str(e)))\n logger.info('Epoch {}'.format(str(e)))\n \n for (x0, x1), _, _ in tqdm(dataloader):\n\n # move images to the gpu\n x0 = x0.to(device)\n x1 = x1.to(device)\n\n # run the model on both transforms of the images\n # the output of the simsiam model is a y containing the predictions\n # and projections for each input x\n y0, y1 = model(x0, x1)\n\n # backpropagation\n loss = criterion(y0, y1)\n loss.backward()\n\n optimizer.step()\n optimizer.zero_grad()\n\n # calculate the per-dimension standard deviation of the outputs\n # we can use this later to check whether the embeddings are collapsing\n output, _ = y0\n output = output.detach()\n output = torch.nn.functional.normalize(output, dim=1)\n\n output_std = torch.std(output, 0)\n output_std = output_std.mean()\n\n # use moving averages to track the loss and standard deviation\n w = 0.9\n avg_loss = w * avg_loss + (1 - w) * loss.item()\n avg_output_std = w * avg_output_std + (1 - w) * output_std.item()\n \n torch.save(model.state_dict(), '../output/{}.pt'.format(exp_name))\n # the level of collapse is large if the standard deviation of the l2\n # normalized output is much smaller than 1 / sqrt(dim)\n collapse_level = max(0., 1 - math.sqrt(out_dim) * avg_output_std)\n # print intermediate results\n print(f'[Epoch {e:3d}] '\n f'Loss = {avg_loss:.2f} | '\n f'Collapse Level: {collapse_level:.2f} / 1.00')\n \n logger.info(f'[Epoch {e:3d}] '\n f'Loss = {avg_loss:.2f} | '\n f'Collapse Level: {collapse_level:.2f} / 1.00')\n \n \n " ]
[ [ "torch.utils.data.DataLoader", "torch.std", "torch.nn.functional.normalize", "torch.cuda.is_available", "torch.nn.DataParallel" ] ]
bartolkaruza/pytorch-lightning-bolts
[ "2e903c333c37ea83394c7da2ce826de1b82fb356" ]
[ "pl_bolts/models/self_supervised/simclr/transforms.py" ]
[ "import numpy as np\n\nfrom pl_bolts.utils.warnings import warn_missing_pkg\n\ntry:\n import torchvision.transforms as transforms\nexcept ModuleNotFoundError:\n warn_missing_pkg('torchvision') # pragma: no-cover\n _TORCHVISION_AVAILABLE = False\nelse:\n _TORCHVISION_AVAILABLE = True\n\ntry:\n import cv2\nexcept ModuleNotFoundError:\n warn_missing_pkg('cv2', pypi_name='opencv-python') # pragma: no-cover\n\n\nclass SimCLRTrainDataTransform(object):\n \"\"\"\n Transforms for SimCLR\n\n Transform::\n\n RandomResizedCrop(size=self.input_height)\n RandomHorizontalFlip()\n RandomApply([color_jitter], p=0.8)\n RandomGrayscale(p=0.2)\n GaussianBlur(kernel_size=int(0.1 * self.input_height))\n transforms.ToTensor()\n\n Example::\n\n from pl_bolts.models.self_supervised.simclr.transforms import SimCLRTrainDataTransform\n\n transform = SimCLRTrainDataTransform(input_height=32)\n x = sample()\n (xi, xj) = transform(x)\n \"\"\"\n def __init__(\n self,\n input_height: int = 224,\n gaussian_blur: bool = True,\n jitter_strength: float = 1.,\n normalize=None\n ) -> None:\n\n if not _TORCHVISION_AVAILABLE:\n raise ModuleNotFoundError( # pragma: no-cover\n 'You want to use `transforms` from `torchvision` which is not installed yet.'\n )\n\n self.jitter_strength = jitter_strength\n self.input_height = input_height\n self.gaussian_blur = gaussian_blur\n self.normalize = normalize\n\n self.color_jitter = transforms.ColorJitter(\n 0.8 * self.jitter_strength,\n 0.8 * self.jitter_strength,\n 0.8 * self.jitter_strength,\n 0.2 * self.jitter_strength\n )\n\n data_transforms = [\n transforms.RandomResizedCrop(size=self.input_height),\n transforms.RandomHorizontalFlip(p=0.5),\n transforms.RandomApply([self.color_jitter], p=0.8),\n transforms.RandomGrayscale(p=0.2)\n ]\n\n if self.gaussian_blur:\n kernel_size = int(0.1 * self.input_height)\n if kernel_size % 2 == 0:\n kernel_size += 1\n\n data_transforms.append(GaussianBlur(kernel_size=kernel_size, p=0.5))\n\n data_transforms = transforms.Compose(data_transforms)\n\n if normalize is None:\n self.final_transform = transforms.ToTensor()\n else:\n self.final_transform = transforms.Compose([transforms.ToTensor(), normalize])\n\n self.train_transform = transforms.Compose([data_transforms, self.final_transform])\n\n # add online train transform of the size of global view\n self.online_transform = transforms.Compose([\n transforms.RandomResizedCrop(self.input_height),\n transforms.RandomHorizontalFlip(),\n self.final_transform\n ])\n\n def __call__(self, sample):\n transform = self.train_transform\n\n xi = transform(sample)\n xj = transform(sample)\n\n return xi, xj, self.online_transform(sample)\n\n\nclass SimCLREvalDataTransform(SimCLRTrainDataTransform):\n \"\"\"\n Transforms for SimCLR\n\n Transform::\n\n Resize(input_height + 10, interpolation=3)\n transforms.CenterCrop(input_height),\n transforms.ToTensor()\n\n Example::\n\n from pl_bolts.models.self_supervised.simclr.transforms import SimCLREvalDataTransform\n\n transform = SimCLREvalDataTransform(input_height=32)\n x = sample()\n (xi, xj) = transform(x)\n \"\"\"\n def __init__(\n self,\n input_height: int = 224,\n gaussian_blur: bool = True,\n jitter_strength: float = 1.,\n normalize=None\n ):\n super().__init__(\n normalize=normalize,\n input_height=input_height,\n gaussian_blur=gaussian_blur,\n jitter_strength=jitter_strength\n )\n\n # replace online transform with eval time transform\n self.online_transform = transforms.Compose([\n transforms.Resize(int(self.input_height + 0.1 * self.input_height)),\n transforms.CenterCrop(self.input_height),\n self.final_transform,\n ])\n\n\nclass SimCLRFinetuneTransform(object):\n def __init__(\n self,\n input_height: int = 224,\n jitter_strength: float = 1.,\n normalize=None,\n eval_transform: bool = False\n ) -> None:\n\n self.jitter_strength = jitter_strength\n self.input_height = input_height\n self.normalize = normalize\n\n self.color_jitter = transforms.ColorJitter(\n 0.8 * self.jitter_strength,\n 0.8 * self.jitter_strength,\n 0.8 * self.jitter_strength,\n 0.2 * self.jitter_strength\n )\n\n if not eval_transform:\n data_transforms = [\n transforms.RandomResizedCrop(size=self.input_height),\n transforms.RandomHorizontalFlip(p=0.5),\n transforms.RandomApply([self.color_jitter], p=0.8),\n transforms.RandomGrayscale(p=0.2)\n ]\n else:\n data_transforms = [\n transforms.Resize(int(self.input_height + 0.1 * self.input_height)),\n transforms.CenterCrop(self.input_height)\n ]\n\n if normalize is None:\n final_transform = transforms.ToTensor()\n else:\n final_transform = transforms.Compose([transforms.ToTensor(), normalize])\n\n data_transforms.append(final_transform)\n self.transform = transforms.Compose(data_transforms)\n\n def __call__(self, sample):\n return self.transform(sample)\n\n\nclass GaussianBlur(object):\n # Implements Gaussian blur as described in the SimCLR paper\n def __init__(self, kernel_size, p=0.5, min=0.1, max=2.0):\n self.min = min\n self.max = max\n\n # kernel size is set to be 10% of the image height/width\n self.kernel_size = kernel_size\n self.p = p\n\n def __call__(self, sample):\n sample = np.array(sample)\n\n # blur the image with a 50% chance\n prob = np.random.random_sample()\n\n if prob < self.p:\n sigma = (self.max - self.min) * np.random.random_sample() + self.min\n sample = cv2.GaussianBlur(sample, (self.kernel_size, self.kernel_size), sigma)\n\n return sample\n" ]
[ [ "numpy.array", "numpy.random.random_sample" ] ]
gibbbone/ohmnet
[ "1b4ee4c146f526ea6e2f4f8607df7e9687204a9e" ]
[ "ohmnet/gensimmod/utils.py" ]
[ "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# This OhmNet code is adapted from:\n# Copyright (C) 2010 Radim Rehurek <[email protected]>\n# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html\n\nfrom __future__ import with_statement\n\nimport logging\nimport itertools\n\nlogger = logging.getLogger(__name__)\n\ntry:\n from html.entities import name2codepoint as n2cp\nexcept ImportError:\n from htmlentitydefs import name2codepoint as n2cp\ntry:\n import cPickle as _pickle\nexcept ImportError:\n import pickle as _pickle\n\nimport sys\nimport os\n\nimport numpy\nimport scipy.sparse\n\nif sys.version_info[0] >= 3:\n unicode = str\n\nfrom six import iteritems\n\ntry:\n from smart_open import smart_open\nexcept ImportError:\n logger.info(\"smart_open library not found; falling back to local-filesystem-only\")\n\n def make_closing(base, **attrs):\n \"\"\"\n Add support for `with Base(attrs) as fout:` to the base class if it's missing.\n The base class' `close()` method will be called on context exit, to always close the file properly.\n\n This is needed for gzip.GzipFile, bz2.BZ2File etc in older Pythons (<=2.6), which otherwise\n raise \"AttributeError: GzipFile instance has no attribute '__exit__'\".\n\n \"\"\"\n if not hasattr(base, '__enter__'):\n attrs['__enter__'] = lambda self: self\n if not hasattr(base, '__exit__'):\n attrs['__exit__'] = lambda self, type, value, traceback: self.close()\n return type('Closing' + base.__name__, (base, object), attrs)\n\n def smart_open(fname, mode='rb'):\n _, ext = os.path.splitext(fname)\n if ext == '.bz2':\n from bz2 import BZ2File\n return make_closing(BZ2File)(fname, mode)\n if ext == '.gz':\n from gzip import GzipFile\n return make_closing(GzipFile)(fname, mode)\n return open(fname, mode)\n\n\ndef any2utf8(text, errors='strict', encoding='utf8'):\n \"\"\"Convert a string (unicode or bytestring in `encoding`), to bytestring in utf8.\"\"\"\n if isinstance(text, unicode):\n return text.encode('utf8')\n # do bytestring -> unicode -> utf8 full circle, to ensure valid utf8\n return unicode(text, encoding, errors=errors).encode('utf8')\nto_utf8 = any2utf8\n\n\ndef any2unicode(text, encoding='utf8', errors='strict'):\n \"\"\"Convert a string (bytestring in `encoding` or unicode), to unicode.\"\"\"\n if isinstance(text, unicode):\n return text\n return unicode(text, encoding, errors=errors)\nto_unicode = any2unicode\n\n\nclass SaveLoad(object):\n \"\"\"\n Objects which inherit from this class have save/load functions, which un/pickle\n them to disk.\n\n This uses pickle for de/serializing, so objects must not contain\n unpicklable attributes, such as lambda functions etc.\n\n \"\"\"\n @classmethod\n def load(cls, fname, mmap=None):\n \"\"\"\n Load a previously saved object from file (also see `save`).\n\n If the object was saved with large arrays stored separately, you can load\n these arrays via mmap (shared memory) using `mmap='r'`. Default: don't use\n mmap, load large arrays as normal objects.\n\n If the file being loaded is compressed (either '.gz' or '.bz2'), then\n `mmap=None` must be set. Load will raise an `IOError` if this condition\n is encountered.\n\n \"\"\"\n logger.info(\"loading %s object from %s\" % (cls.__name__, fname))\n\n compress, subname = SaveLoad._adapt_by_suffix(fname)\n\n obj = unpickle(fname)\n obj._load_specials(fname, mmap, compress, subname)\n return obj\n\n\n def _load_specials(self, fname, mmap, compress, subname):\n \"\"\"\n Loads any attributes that were stored specially, and gives the same\n opportunity to recursively included SaveLoad instances.\n\n \"\"\"\n\n mmap_error = lambda x, y: IOError(\n 'Cannot mmap compressed object %s in file %s. ' % (x, y) +\n 'Use `load(fname, mmap=None)` or uncompress files manually.')\n\n for attrib in getattr(self, '__recursive_saveloads', []):\n cfname = '.'.join((fname, attrib))\n logger.info(\"loading %s recursively from %s.* with mmap=%s\" % (\n attrib, cfname, mmap))\n getattr(self, attrib)._load_specials(cfname, mmap, compress, subname)\n\n for attrib in getattr(self, '__numpys', []):\n logger.info(\"loading %s from %s with mmap=%s\" % (\n attrib, subname(fname, attrib), mmap))\n\n if compress:\n if mmap:\n raise mmap_error(attrib, subname(fname, attrib))\n\n val = numpy.load(subname(fname, attrib))['val']\n else:\n val = numpy.load(subname(fname, attrib), mmap_mode=mmap)\n\n setattr(self, attrib, val)\n\n for attrib in getattr(self, '__scipys', []):\n logger.info(\"loading %s from %s with mmap=%s\" % (\n attrib, subname(fname, attrib), mmap))\n sparse = unpickle(subname(fname, attrib))\n if compress:\n if mmap:\n raise mmap_error(attrib, subname(fname, attrib))\n\n with numpy.load(subname(fname, attrib, 'sparse')) as f:\n sparse.data = f['data']\n sparse.indptr = f['indptr']\n sparse.indices = f['indices']\n else:\n sparse.data = numpy.load(subname(fname, attrib, 'data'), mmap_mode=mmap)\n sparse.indptr = numpy.load(subname(fname, attrib, 'indptr'), mmap_mode=mmap)\n sparse.indices = numpy.load(subname(fname, attrib, 'indices'), mmap_mode=mmap)\n\n setattr(self, attrib, sparse)\n\n for attrib in getattr(self, '__ignoreds', []):\n logger.info(\"setting ignored attribute %s to None\" % (attrib))\n setattr(self, attrib, None)\n\n\n @staticmethod\n def _adapt_by_suffix(fname):\n \"\"\"Give appropriate compress setting and filename formula\"\"\"\n if fname.endswith('.gz') or fname.endswith('.bz2'):\n compress = True\n subname = lambda *args: '.'.join(list(args) + ['npz'])\n else:\n compress = False\n subname = lambda *args: '.'.join(list(args) + ['npy'])\n return (compress, subname)\n\n\n def _smart_save(self, fname, separately=None, sep_limit=10 * 1024**2,\n ignore=frozenset(), pickle_protocol=2):\n \"\"\"\n Save the object to file (also see `load`).\n\n If `separately` is None, automatically detect large\n numpy/scipy.sparse arrays in the object being stored, and store\n them into separate files. This avoids pickle memory errors and\n allows mmap'ing large arrays back on load efficiently.\n\n You can also set `separately` manually, in which case it must be\n a list of attribute names to be stored in separate files. The\n automatic check is not performed in this case.\n\n `ignore` is a set of attribute names to *not* serialize (file\n handles, caches etc). On subsequent load() these attributes will\n be set to None.\n\n `pickle_protocol` defaults to 2 so the pickled object can be imported\n in both Python 2 and 3.\n\n \"\"\"\n logger.info(\n \"saving %s object under %s, separately %s\" % (\n self.__class__.__name__, fname, separately))\n\n compress, subname = SaveLoad._adapt_by_suffix(fname)\n\n restores = self._save_specials(fname, separately, sep_limit, ignore, pickle_protocol,\n compress, subname)\n try:\n pickle(self, fname, protocol=pickle_protocol)\n finally:\n # restore attribs handled specially\n for obj, asides in restores:\n for attrib, val in iteritems(asides):\n setattr(obj, attrib, val)\n\n\n def _save_specials(self, fname, separately, sep_limit, ignore, pickle_protocol, compress, subname):\n \"\"\"\n Save aside any attributes that need to be handled separately, including\n by recursion any attributes that are themselves SaveLoad instances.\n\n Returns a list of (obj, {attrib: value, ...}) settings that the caller\n should use to restore each object's attributes that were set aside\n during the default pickle().\n\n \"\"\"\n asides = {}\n sparse_matrices = (scipy.sparse.csr_matrix, scipy.sparse.csc_matrix)\n if separately is None:\n separately = []\n for attrib, val in iteritems(self.__dict__):\n if isinstance(val, numpy.ndarray) and val.size >= sep_limit:\n separately.append(attrib)\n elif isinstance(val, sparse_matrices) and val.nnz >= sep_limit:\n separately.append(attrib)\n\n # whatever's in `separately` or `ignore` at this point won't get pickled\n for attrib in separately + list(ignore):\n if hasattr(self, attrib):\n asides[attrib] = getattr(self, attrib)\n delattr(self, attrib)\n\n recursive_saveloads = []\n restores = []\n for attrib, val in iteritems(self.__dict__):\n if hasattr(val, '_save_specials'): # better than 'isinstance(val, SaveLoad)' if IPython reloading\n recursive_saveloads.append(attrib)\n cfname = '.'.join((fname,attrib))\n restores.extend(val._save_specials(cfname, None, sep_limit, ignore,\n pickle_protocol, compress, subname))\n\n try:\n numpys, scipys, ignoreds = [], [], []\n for attrib, val in iteritems(asides):\n if isinstance(val, numpy.ndarray) and attrib not in ignore:\n numpys.append(attrib)\n logger.info(\"storing numpy array '%s' to %s\" % (\n attrib, subname(fname, attrib)))\n\n if compress:\n numpy.savez_compressed(subname(fname, attrib), val=numpy.ascontiguousarray(val))\n else:\n numpy.save(subname(fname, attrib), numpy.ascontiguousarray(val))\n\n elif isinstance(val, (scipy.sparse.csr_matrix, scipy.sparse.csc_matrix)) and attrib not in ignore:\n scipys.append(attrib)\n logger.info(\"storing scipy.sparse array '%s' under %s\" % (\n attrib, subname(fname, attrib)))\n\n if compress:\n numpy.savez_compressed(subname(fname, attrib, 'sparse'),\n data=val.data,\n indptr=val.indptr,\n indices=val.indices)\n else:\n numpy.save(subname(fname, attrib, 'data'), val.data)\n numpy.save(subname(fname, attrib, 'indptr'), val.indptr)\n numpy.save(subname(fname, attrib, 'indices'), val.indices)\n\n data, indptr, indices = val.data, val.indptr, val.indices\n val.data, val.indptr, val.indices = None, None, None\n\n try:\n # store array-less object\n pickle(val, subname(fname, attrib), protocol=pickle_protocol)\n finally:\n val.data, val.indptr, val.indices = data, indptr, indices\n else:\n logger.info(\"not storing attribute %s\" % (attrib))\n ignoreds.append(attrib)\n\n self.__dict__['__numpys'] = numpys\n self.__dict__['__scipys'] = scipys\n self.__dict__['__ignoreds'] = ignoreds\n self.__dict__['__recursive_saveloads'] = recursive_saveloads\n except:\n # restore the attributes if exception-interrupted\n for attrib, val in iteritems(asides):\n setattr(self, attrib, val)\n raise\n return restores + [(self, asides)]\n\n\n def save(self, fname_or_handle, separately=None, sep_limit=10 * 1024**2,\n ignore=frozenset(), pickle_protocol=2):\n \"\"\"\n Save the object to file (also see `load`).\n\n `fname_or_handle` is either a string specifying the file name to\n save to, or an open file-like object which can be written to. If\n the object is a file handle, no special array handling will be\n performed; all attributes will be saved to the same file.\n\n If `separately` is None, automatically detect large\n numpy/scipy.sparse arrays in the object being stored, and store\n them into separate files. This avoids pickle memory errors and\n allows mmap'ing large arrays back on load efficiently.\n\n You can also set `separately` manually, in which case it must be\n a list of attribute names to be stored in separate files. The\n automatic check is not performed in this case.\n\n `ignore` is a set of attribute names to *not* serialize (file\n handles, caches etc). On subsequent load() these attributes will\n be set to None.\n\n `pickle_protocol` defaults to 2 so the pickled object can be imported\n in both Python 2 and 3.\n\n \"\"\"\n try:\n _pickle.dump(self, fname_or_handle, protocol=pickle_protocol)\n logger.info(\"saved %s object\" % self.__class__.__name__)\n except TypeError: # `fname_or_handle` does not have write attribute\n self._smart_save(fname_or_handle, separately, sep_limit, ignore,\n pickle_protocol=pickle_protocol)\n#endclass SaveLoad\n\ndef pickle(obj, fname, protocol=2):\n \"\"\"Pickle object `obj` to file `fname`.\n `protocol` defaults to 2 so pickled objects are compatible across\n Python 2.x and 3.x.\n \"\"\"\n with open(fname, 'wb') as fout: # 'b' for binary, needed on Windows\n _pickle.dump(obj, fout, protocol=protocol)\n\n\ndef unpickle(fname):\n \"\"\"Load pickled object from `fname`\"\"\"\n with open(fname) as f:\n return _pickle.loads(f.read())\n\n\ndef prune_vocab(vocab, min_reduce, trim_rule=None):\n \"\"\"\n Remove all entries from the `vocab` dictionary with count smaller than `min_reduce`.\n\n Modifies `vocab` in place, returns the sum of all counts that were pruned.\n\n \"\"\"\n result = 0\n old_len = len(vocab)\n for w in list(vocab): # make a copy of dict's keys\n if not keep_vocab_item(w, vocab[w], min_reduce, trim_rule): # vocab[w] <= min_reduce:\n result += vocab[w]\n del vocab[w]\n logger.info(\"pruned out %i tokens with count <=%i (before %i, after %i)\",\n old_len - len(vocab), min_reduce, old_len, len(vocab))\n return result\n\n\ndef qsize(queue):\n \"\"\"Return the (approximate) queue size where available; -1 where not (OS X).\"\"\"\n try:\n return queue.qsize()\n except NotImplementedError:\n # OS X doesn't support qsize\n return -1\n\n\nRULE_DEFAULT = 0\nRULE_DISCARD = 1\nRULE_KEEP = 2\n\n\ndef keep_vocab_item(word, count, min_count, trim_rule=None):\n default_res = count >= min_count\n\n if trim_rule is None:\n return default_res\n else:\n rule_res = trim_rule(word, count, min_count)\n if rule_res == RULE_KEEP:\n return True\n elif rule_res == RULE_DISCARD:\n return False\n else:\n return default_res\n\n\ndef chunkize_serial(iterable, chunksize, as_numpy=False):\n \"\"\"\n Return elements from the iterable in `chunksize`-ed lists. The last returned\n element may be smaller (if length of collection is not divisible by `chunksize`).\n\n >>> print(list(grouper(range(10), 3)))\n [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]\n\n \"\"\"\n import numpy\n it = iter(iterable)\n while True:\n if as_numpy:\n # convert each document to a 2d numpy array (~6x faster when transmitting\n # chunk data over the wire, in Pyro)\n wrapped_chunk = [[numpy.array(doc) for doc in itertools.islice(it, int(chunksize))]]\n else:\n wrapped_chunk = [list(itertools.islice(it, int(chunksize)))]\n if not wrapped_chunk[0]:\n break\n # memory opt: wrap the chunk and then pop(), to avoid leaving behind a dangling reference\n yield wrapped_chunk.pop()\n\ngrouper = chunkize_serial\n\n\nclass RepeatCorpusNTimes(SaveLoad):\n\n def __init__(self, corpus, n):\n \"\"\"\n Repeat a `corpus` `n` times.\n\n >>> corpus = [[(1, 0.5)], []]\n >>> list(RepeatCorpusNTimes(corpus, 3)) # repeat 3 times\n [[(1, 0.5)], [], [(1, 0.5)], [], [(1, 0.5)], []]\n \"\"\"\n self.corpus = corpus\n self.n = n\n\n def __iter__(self):\n for _ in xrange(self.n):\n for document in self.corpus:\n yield document" ]
[ [ "numpy.array", "numpy.ascontiguousarray" ] ]
Levi-Armstrong/point_cloud_segmentation
[ "ed0db4f49aa378901860dd2a81897b79e0fe1a66" ]
[ "pcs_detection/src_python/pcs_detection/utils.py" ]
[ "'''\n * @file utils.py\n * @brief Helper functions for viewing images and reading/writing config and label files \n *\n * @author Jake Janssen\n * @date Oct 24, 2019\n * @version TODO\n * @bug No known bugs\n *\n * @copyright Copyright (c) 2019, Southwest Research Institute\n *\n * @par License\n * Software License Agreement (Apache License)\n * @par\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * @par\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n '''\n\nimport numpy as np\nimport pandas as pd\nfrom lxml import etree\nimport cv2\nimport os\nimport json\n\ndef get_labels_from_xml(label_path):\n '''\n Reads in labels from an xml file with CVATs format\n and returns a dictionary where the keys are whatever is specified as image name (outer directory and filename)\n and the values are the contours for that image\n '''\n anno = dict()\n root = etree.parse(label_path).getroot()\n \n # name of the outer directory this label corresponds to\n # allows for images in different directories to have the same filename \n dataset_name = label_path.split('/')[-2]\n\n for image_tag in root.iter('image'):\n image = {}\n # initialize all types of labels\n for label_name in root.iter('label'):\n image[label_name.find('name').text] = list()\n\n #extract the meta info from the image\n for key, value in image_tag.items():\n image[key] = value\n\n #keys will be collection folder combined with each images\n #this allows for images in different folders to have the same name\n image['name'] = dataset_name + '/' + image['name']\n\n image['contour'] = list()\n\n # loop through the poly elements in the image\n for poly_tag in image_tag:\n # get the type of poly element - eg polyline, polygon\n poly_elem = {}\n poly_type = poly_tag.tag\n poly_elem[poly_type] = []\n\n for key, value in poly_tag.items():\n poly_elem[key] = value\n \n #Get the contour points\n contour_points = []\n shape = str(poly_elem['points']).split(\";\") #get the shape from the polyline \n for pair in shape:\n x, y = pair.split(\",\")\n contour_points.append([int(float(x)), int(float(y))])\n\n #create a contour set of points in the proper order from the points\n contour = np.array(pd.DataFrame(contour_points),\n np.int32\n )\n image[poly_elem['label']].append([contour,poly_type])\n \n anno[image['name']] = image\n \n return anno\n\ndef dump_validation_config(config):\n '''\n Save the config used to train in the same folder in the model weights.\n This config can later be used to apply the same preprocessing, and model selection\n that was used in training \n '''\n\n config_dump = {}\n for key in config.__dict__:\n if (not key.startswith('__')) and (key != 'logger') :\n try:\n config_dump[key] = config.__getattribute__(key)\n except:\n pass\n save_path = os.path.join(os.path.split(config.WEIGHT_SAVE_PATH)[0],'full_config.json')\n with open(save_path, 'w') as outfile:\n json.dump(config_dump, outfile, indent=4) \n\ndef dump_inference_config(config):\n '''\n Save a config used for inference in the same folder as the weights.\n '''\n wanted_keys = ['MODEL', 'VAL_WEIGHT_PATH', 'BATCH_SIZE', 'MODE', 'DISPLAY_SCALE_FACTOR', 'CHANNEL', 'PRE_PROCESS', 'CONFIDENCE_THRESHOLD', 'CLASS_NAMES', 'ORIG_DIMS']\n config_dump = {}\n for key in config.__dict__:\n if key in wanted_keys:\n try:\n config_dump[key] = config.__getattribute__(key)\n except:\n pass\n save_path = os.path.join(os.path.split(config.WEIGHT_SAVE_PATH)[0],'inference_config.json')\n print('_____CONFIG______')\n print(config_dump)\n print('_________________')\n with open(save_path, 'w') as outfile:\n json.dump(config_dump, outfile, indent=4) \n\n\ndef resize(image, scale_factor):\n '''\n Used to resize a display image.\n '''\n new_height = int(image.shape[0] * scale_factor)\n new_width = int(image.shape[1] * scale_factor)\n resized_img = cv2.resize(image, (new_width, new_height))\n return resized_img\n\n\ndef minMaxNormalize(chnls):\n '''\n Normalizes images to have pixel values between 0-255\n This function should only be used for displaying \n '''\n #loop over all of the channels\n for i in range(chnls.shape[-1]):\n # calculate min and ptp of each channel. \n min_arr = np.min(chnls[:, :, i][chnls[:, :, i] != 0]) \n ptp_arr = chnls[:, :, i].ptp()\n\n chnls[:, :, i] = (((chnls[:, :, i] - min_arr) / ptp_arr) * 255)\n\n return chnls.astype(np.uint8)\n\ndef histogram(original_image):\n '''\n Displays a histogram showing the pixel value distributions per channel of the image.\n This function should be used to check the being fed into the network.\n Each channel should have a mean of zero and all standard deviations should be within the same magnitude. \n '''\n img_cv = original_image.copy()\n\n org_means = []\n org_stds = []\n\n for i in range(img_cv.shape[-1]):\n org_means.append(round(img_cv[:,:,i].mean(),2))\n org_stds.append(round(img_cv[:,:,i].std(),2))\n\n img_cv = minMaxNormalize(img_cv)\n\n # Histograms of data distribution \n # split channels \n hist_chnls = cv2.split(img_cv)\n\n # histogram parameters\n histSize = 256\n histRange = (0,256)\n accumulate = False\n hist_w = 512\n hist_h = 400\n bin_w = int(round( hist_w/histSize ))\n histImage = np.zeros((hist_h, hist_w, 4), dtype=np.uint8)\n\n hists = [] \n \n # text colors and colors for plot\n t_colors = [(210,0,0), (0,210,0), (0,0,210)]\n colors = [(255,0,0), (0,255,0), (0,0,255) ]\n\n # starting vertical location of text\n text_h = 30\n\n # get data into histogram format\n for ii in range(len(hist_chnls)):\n temp_hist = cv2.calcHist(hist_chnls, [ii], None, [histSize], histRange, accumulate=accumulate)\n cv2.normalize(temp_hist, temp_hist, alpha = 0, beta=hist_h, norm_type=cv2.NORM_MINMAX)\n hists.append(temp_hist)\n\n # add histogram to image \n for jj in range(1, histSize):\n for curr_hist, color in zip(hists,colors):\n cv2.line(histImage, ( bin_w*(jj-1), hist_h - int((curr_hist[jj-1]).round()) ),\n ( bin_w*(jj), hist_h - int((curr_hist[jj]).round()) ),\n color, thickness=1)\n\n # add text and mean/normal distribution lines \n for ii, color, t_color in zip(range(len(hist_chnls)), colors, t_colors):\n hist_std = int(round(hist_chnls[ii].std()))\n hist_mean = int(round(hist_chnls[ii].mean()))\n cv2.circle(histImage, (2*hist_mean, 400 ), 2*hist_std, color, thickness=4)\n display_str = 'Mean: ' + str(org_means[ii]) + ', Std: ' + str(org_stds[ii])\n cv2.putText(histImage , display_str, (10,text_h), cv2.FONT_HERSHEY_SIMPLEX, .4, t_color, 1,cv2.LINE_AA)\n text_h += 20\n\n # display histogram \n cv2.imshow('RGBT Data Distribution', histImage)\n\ndef colorTriLabel(label, colors):\n display_label = np.zeros((label.shape[0], label.shape[1], 3))\n display_label[:,:,0] = label[:,:,0] * 255\n display_label[:,:,-1] = label[:,:,-1] * 255\n for ii, color in enumerate(colors):\n display_label[label[:,:,ii+1] == 1] = color\n return display_label.astype(np.uint8)\n\ndef colorPrediction(prediction, orig_img, colors):\n '''\n Creates a three channel bgr image and colors it with the prediciton.\n '''\n prediction_display = orig_img.copy() \n prediction = np.argmax(prediction, axis=-1)\n for jj, color in enumerate(colors):\n prediction_display[:,:][prediction==jj+1] = color\n prediction_display = prediction_display.astype(np.uint8)\n return prediction_display\n\ndef LABtoBGR(image, config):\n '''\n Used to convert the LAB color space back to BGR\n '''\n image += config.PRE_PROCESS['lab']\n image[image==0] = 1e-4\n image = cv2.cvtColor(image.astype(np.float32), cv2.COLOR_LAB2BGR)\n image *= 255\n return image\n\ndef get_colors(n):\n '''\n Generates a list of colors\n '''\n colors = [[102,255,153], [255, 102, 204], [102, 204, 255], [51, 102, 153]]\n return colors[0:n]\n\n" ]
[ [ "pandas.DataFrame", "numpy.min", "numpy.argmax", "numpy.zeros" ] ]
izumiya-keisuke/mmle
[ "43dbe281ee591a2d7f7cc1e5386ed04651930205" ]
[ "examples/distributed_data_parallel.py" ]
[ "\"\"\"\nCopyright 2021 Keisuke Izumiya\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\"\"\"\n\nimport torch\nimport torch.distributed as distd\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.distributed import DistributedSampler\n\nimport mmle.distributed as mdistd\nimport mmle.nn as mnn\nfrom mmle.utils.manager import Manager\n\n\nBATCH_SIZE = 400\nDATA_NUM = 40000\nDATA_DIM = 10\nEPOCH_NUM = 50\nLABEL_DIM = 5\nLOG_DIR = \"log\"\nMIDDLE_DIM = 16\n\n\nclass Dataset(torch.utils.data.Dataset):\n def __init__(self):\n super().__init__()\n\n self.data = torch.randn(DATA_NUM, DATA_DIM)\n self.label = torch.randn(DATA_NUM, LABEL_DIM)\n\n def __len__(self):\n return DATA_NUM\n\n def __getitem__(self, idx):\n return self.data[idx], self.label[idx]\n\n\ndef main():\n world_size = distd.get_world_size()\n rank = distd.get_rank()\n\n model = nn.Sequential(\n mnn.FC(DATA_DIM, MIDDLE_DIM), mnn.FC(MIDDLE_DIM, LABEL_DIM, bn=False, activ=\"id\")\n )\n model = mdistd.distribute_module(model)\n\n optimizer = optim.Adam(model.parameters())\n\n dataset = Dataset()\n sampler = DistributedSampler(dataset, world_size, rank)\n loader = DataLoader(dataset, BATCH_SIZE, sampler=sampler, drop_last=True)\n\n if rank == 0:\n manager = Manager(LOG_DIR, use_tensorboard=True)\n manager.tensorboard_writer.add_graph(model, dataset[0][0].repeat(BATCH_SIZE, 1))\n step = 0\n\n for epoch in range(EPOCH_NUM):\n model.train()\n for data, label in loader:\n loss = F.mse_loss(model(data), label.to(rank))\n\n mnn.zero_grad(model)\n loss.backward()\n optimizer.step()\n distd.barrier()\n\n if rank == 0:\n step += world_size\n manager.tensorboard_writer.plot(\"loss\", \"train\", loss.item(), step)\n\n if rank == 0:\n print(f\"Finish epoch {epoch}: loss={loss.item():.3f}\")\n distd.barrier()\n\n if rank == 0:\n manager.save({\"model\": model.state_dict()}, \"model\")\n manager.close()\n\n\nif __name__ == \"__main__\":\n mdistd.spawn(main, nprocs=torch.cuda.device_count())\n" ]
[ [ "torch.utils.data.DataLoader", "torch.distributed.get_rank", "torch.distributed.get_world_size", "torch.utils.data.distributed.DistributedSampler", "torch.randn", "torch.cuda.device_count", "torch.distributed.barrier" ] ]
AlphaJia/faster-rcnn.pytorch
[ "b79a83c84f083495e2edb1a55a970946cb59add2" ]
[ "lib/setup.py" ]
[ "# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.\n# !/usr/bin/env python\n\nimport glob\nimport os\n\nimport torch\nfrom setuptools import find_packages\nfrom setuptools import setup\nfrom torch.utils.cpp_extension import CUDAExtension\nfrom torch.utils.cpp_extension import CUDA_HOME\nfrom torch.utils.cpp_extension import CppExtension\n\nrequirements = [\"torch\", \"torchvision\"]\n\n\ndef get_extensions():\n this_dir = os.path.dirname(os.path.abspath(__file__))\n extensions_dir = os.path.join(this_dir, \"model\", \"csrc\")\n\n main_file = glob.glob(os.path.join(extensions_dir, \"*.cpp\"))\n source_cpu = glob.glob(os.path.join(extensions_dir, \"cpu\", \"*.cpp\"))\n source_cuda = glob.glob(os.path.join(extensions_dir, \"cuda\", \"*.cu\"))\n\n sources = main_file + source_cpu\n extension = CppExtension\n\n extra_compile_args = {\"cxx\": []}\n define_macros = []\n\n if torch.cuda.is_available() and CUDA_HOME is not None:\n extension = CUDAExtension\n sources += source_cuda\n define_macros += [(\"WITH_CUDA\", None)]\n extra_compile_args[\"nvcc\"] = [\n \"-DCUDA_HAS_FP16=1\",\n \"-D__CUDA_NO_HALF_OPERATORS__\",\n \"-D__CUDA_NO_HALF_CONVERSIONS__\",\n \"-D__CUDA_NO_HALF2_OPERATORS__\",\n ]\n\n sources = [os.path.join(extensions_dir, s) for s in sources]\n\n include_dirs = [extensions_dir]\n\n ext_modules = [\n extension(\n \"model._C\",\n sources,\n include_dirs=include_dirs,\n define_macros=define_macros,\n extra_compile_args=extra_compile_args,\n )\n ]\n\n return ext_modules\n\n\nsetup(\n name=\"faster_rcnn\",\n version=\"0.1\",\n description=\"object detection in pytorch\",\n packages=find_packages(exclude=(\"configs\", \"tests\",)),\n # install_requires=requirements,\n ext_modules=get_extensions(),\n cmdclass={\"build_ext\": torch.utils.cpp_extension.BuildExtension},\n)\n" ]
[ [ "torch.cuda.is_available" ] ]
danielaeblancoj/Design-of-experiment-Python
[ "541c00a96fad4a9ef074c4fb4dde9c65a5deb20d" ]
[ "DOE_functions.py" ]
[ "#====================\n# Essential imports\n#====================\nfrom pyDOE import *\nfrom pyDOE_corrected import *\nfrom diversipy import *\nimport pandas as pd\nimport numpy as np\n\n# ===========================================================================================================\n# Function for constructing a DataFrame from a numpy array generated by PyDOE function and individual lists\n# ===========================================================================================================\n\ndef construct_df(x,r):\n df=pd.DataFrame(data=x,dtype='float32')\n for i in df.index:\n for j in range(len(list(df.iloc[i]))):\n df.iloc[i][j]=r[j][int(df.iloc[i][j])]\n return df\n\n# ===================================================================================================\n# Function for constructing a DataFrame from a matrix with floating point numbers between -1 and +1\n# ===================================================================================================\n\ndef construct_df_from_matrix(x,factor_array):\n \"\"\"\n This function constructs a DataFrame out of x and factor_array, both of which are assumed to be numpy arrays.\n It projects the numbers in the x (which is output of a design-of-experiment build) to the factor array ranges.\n Here factor_array is assumed to have only min and max ranges.\n Matrix x is assumed to have numbers ranging from -1 to 1.\n \"\"\"\n \n row_num=x.shape[0] # Number of rows in the matrix x\n col_num=x.shape[1] # Number of columns in the matrix x\n \n empty=np.zeros((row_num,col_num)) \n \n def simple_substitution(idx,factor_list):\n if idx==-1:\n return factor_list[0]\n elif idx==0:\n return factor_list[1]\n elif idx==1:\n return factor_list[2]\n else:\n alpha=np.abs(factor_list[2]-factor_list[0])/2\n if idx<0:\n beta=np.abs(idx)-1\n return factor_list[0]-(beta*alpha)\n else:\n beta=idx-1\n return factor_list[2]+(beta*alpha)\n \n for i in range(row_num):\n for j in range(col_num):\n empty[i,j] = simple_substitution(x[i,j],factor_array[j])\n \n return pd.DataFrame(data=empty)\n\n# =================================================================================================\n# Function for constructing a DataFrame from a matrix with floating point numbers between 0 and 1\n# =================================================================================================\n\ndef construct_df_from_random_matrix(x,factor_array):\n \"\"\"\n This function constructs a DataFrame out of matrix x and factor_array, both of which are assumed to be numpy arrays.\n It projects the numbers in the x (which is output of a design-of-experiment build) to the factor array ranges.\n Here factor_array is assumed to have only min and max ranges.\n Matrix x is assumed to have numbers ranging from 0 to 1 only.\n \"\"\"\n \n row_num=x.shape[0] # Number of rows in the matrix x\n col_num=x.shape[1] # Number of columns in the matrix x\n \n empty=np.zeros((row_num,col_num)) \n \n def simple_substitution(idx,factor_list):\n alpha=np.abs(factor_list[1]-factor_list[0])\n beta=idx\n return factor_list[0]+(beta*alpha)\n \n for i in range(row_num):\n for j in range(col_num):\n empty[i,j] = simple_substitution(x[i,j],factor_array[j])\n \n return pd.DataFrame(data=empty)\n\n# ======================================================================================\n# Function for building full factorial DataFrame from a dictionary of process variables\n# ======================================================================================\n\ndef build_full_fact(factor_level_ranges):\n \"\"\"\n Builds a full factorial design dataframe from a dictionary of factor/level ranges\n Example of the process variable dictionary:\n {'Pressure':[50,60,70],'Temperature':[290, 320, 350],'Flow rate':[0.9,1.0]}\n \"\"\"\n \n factor_lvl_count=[]\n factor_lists=[]\n \n for key in factor_level_ranges:\n factor_lvl_count.append(len(factor_level_ranges[key]))\n factor_lists.append(factor_level_ranges[key])\n \n x = fullfact_corrected(factor_lvl_count)\n df=construct_df(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n \n return df\n\n# ==================================================================================================================================================\n# Function for building 2-level fractional factorial DataFrame from a dictionary and a generator string\n# ================================================================================================================================================================\n\ndef build_frac_fact(factor_level_ranges,gen_string):\n \"\"\"\n Builds a full factorial design dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n\tExample of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n\t\n\tThis function requires a little more knowledge of how the confounding will be allowed. \n\tThis means that some factor effects get muddled with other interaction effects, so it’s harder to distinguish between them).\n\t\n\tLet’s assume that we just can’t afford (for whatever reason) the number of runs in a full-factorial design. We can systematically decide on a fraction of the full-factorial by allowing some of the factor main effects to be confounded with other factor interaction effects. \n\tThis is done by defining an alias structure that defines, symbolically, these interactions. These alias structures are written like “C = AB” or “I = ABC”, or “AB = CD”, etc. \n\tThese define how one column is related to the others.\n\t\n\tEXAMPLE\n ------------\n For example, the alias “C = AB” or “I = ABC” indicate that there are three factors (A, B, and C) and that the main effect of factor C is confounded with the interaction effect of the product AB, and by extension, A is confounded with BC and B is confounded with AC. \n\tA full- factorial design with these three factors results in a design matrix with 8 runs, but we will assume that we can only afford 4 of those runs. \n\tTo create this fractional design, we need a matrix with three columns, one for A, B, and C, only now where the levels in the C column is created by the product of the A and B columns.\n \"\"\"\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n if factor_count!=len(gen_string.split(' ')):\n print(\"Length of the generator string for the fractional factorial build does not match the length of the process variables dictionary\")\n return None\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = fracfact(gen_string)\n \n def index_change(x):\n if x==-1:\n return 0\n else:\n return x\n vfunc=np.vectorize(index_change)\n x=vfunc(x)\n \n df=construct_df(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n \n return df\n\n# =====================================================================================\n# Function for building Plackett-Burman designs from a dictionary of process variables\n# =====================================================================================\n\ndef build_plackett_burman(factor_level_ranges):\n \"\"\"\n Builds a Plackett-Burman dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n\t\n\tPlackett–Burman designs are experimental designs presented in 1946 by Robin L. Plackett and J. P. Burman while working in the British Ministry of Supply.(Their goal was to find experimental designs for investigating the dependence of some measured quantity on a number of independent variables (factors), each taking L levels, in such a way as to minimize the variance of the estimates of these dependencies using a limited number of experiments. \n\t\n Interactions between the factors were considered negligible. The solution to this problem is to find an experimental design where each combination of levels for any pair of factors appears the same number of times, throughout all the experimental runs (refer to table). \n\tA complete factorial design would satisfy this criterion, but the idea was to find smaller designs.\n\t\n\tThese designs are unique in that the number of trial conditions (rows) expands by multiples of four (e.g. 4, 8, 12, etc.). \n\tThe max number of columns allowed before a design increases the number of rows is always one less than the next higher multiple of four.\n \"\"\"\n \n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = pbdesign(factor_count)\n \n def index_change(x):\n if x==-1:\n return 0\n else:\n return x\n vfunc=np.vectorize(index_change)\n x=vfunc(x)\n \n df=construct_df(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n \n return df\n\n# ===================================================================================\n# Function for building Sukharev Grid designs from a dictionary of process variables\n# ===================================================================================\n\ndef build_sukharev(factor_level_ranges,num_samples=None):\n \"\"\"\n Builds a Sukharev-grid hypercube design dataframe from a dictionary of factor/level ranges.\n Number of samples raised to the power of (1/dimension), where dimension is the number of variables, must be an integer.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n\t\n\tSpecial property of this grid is that points are not placed on the boundaries of the hypercube, but at centroids of the subcells constituted by individual samples. \n\tThis design offers optimal results for the covering radius regarding distances based on the max-norm.\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n check=num_samples**((1/factor_count))\n if (check-int(check)>1e-5):\n num_samples=(int(check)+1)**(factor_count)\n print(\"\\nNumber of samples not adequate to fill a Sukharev grid. Increasing sample size to: \",num_samples)\n \n x = sukharev_grid(num_points=num_samples,dimension=factor_count)\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# ===================================================================================\n# Function for building Box-Behnken designs from a dictionary of process variables\n# ===================================================================================\n\ndef build_box_behnken(factor_level_ranges,center=1):\n \"\"\"\n Builds a Box-Behnken design dataframe from a dictionary of factor/level ranges.\n Note 3 levels of factors are necessary. If not given, the function will automatically create 3 levels by linear mid-section method.\n Example of the dictionary:\n {'Pressure':[50,60,70],'Temperature':[290, 320, 350],'Flow rate':[0.9,1.0,1.1]}\n\t\n\tIn statistics, Box–Behnken designs are experimental designs for response surface methodology, devised by George E. P. Box and Donald Behnken in 1960, to achieve the following goals:\n\t\t* Each factor, or independent variable, is placed at one of three equally spaced values, usually coded as −1, 0, +1. (At least three levels are needed for the following goal.)\n\t\t* The design should be sufficient to fit a quadratic model, that is, one containing squared terms, products of two factors, linear terms and an intercept.\n\t\t* The ratio of the number of experimental points to the number of coefficients in the quadratic model should be reasonable (in fact, their designs kept it in the range of 1.5 to 2.6).*estimation variance should more or less depend only on the distance from the centre (this is achieved exactly for the designs with 4 and 7 factors), and should not vary too much inside the smallest (hyper)cube containing the experimental points.\n\t\"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])==2:\n factor_level_ranges[key].append((factor_level_ranges[key][0]+factor_level_ranges[key][1])/2)\n factor_level_ranges[key].sort()\n print(f\"{key} had only two end points. Creating a mid-point by averaging them\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = bbdesign_corrected(factor_count,center=center)\n x=x+1 #Adjusting the index up by 1\n\n df=construct_df(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n \n return df\n\n# =====================================================================================================\n# Function for building central-composite (Box-Wilson) designs from a dictionary of process variables\n# ===================================================================================================== \n\ndef build_central_composite(factor_level_ranges,center=(2,2),alpha='o',face='ccc'):\n \"\"\"\n Builds a central-composite design dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n\t\n\tIn statistics, a central composite design is an experimental design, useful in response surface methodology, for building a second order (quadratic) model for the response variable without needing to use a complete three-level factorial experiment.\n\tThe design consists of three distinct sets of experimental runs:\n\t\t* A factorial (perhaps fractional) design in the factors studied, each having two levels;\n\t\t* A set of center points, experimental runs whose values of each factor are the medians of the values used in the factorial portion. This point is often replicated in order to improve the precision of the experiment;\n\t\t* A set of axial points, experimental runs identical to the centre points except for one factor, which will take on values both below and above the median of the two factorial levels, and typically both outside their range. All factors are varied in this way.\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n \n # Creates the mid-points by averaging the low and high levels\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])==2:\n factor_level_ranges[key].append((factor_level_ranges[key][0]+factor_level_ranges[key][1])/2)\n factor_level_ranges[key].sort()\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = ccdesign(factor_count,center=center,alpha=alpha,face=face)\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# ====================================================================================\n# Function for building simple Latin Hypercube from a dictionary of process variables\n# ====================================================================================\n\ndef build_lhs(factor_level_ranges, num_samples=None, prob_distribution=None):\n \"\"\"\n Builds a Latin Hypercube design dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n prob_distribution: Analytical probability distribution to be applied over the randomized sampling. \n\tTakes strings like: 'Normal', 'Poisson', 'Exponential', 'Beta', 'Gamma'\n\n\tLatin hypercube sampling (LHS) is a form of stratified sampling that can be applied to multiple variables. The method commonly used to reduce the number or runs necessary for a Monte Carlo simulation to achieve a reasonably accurate random distribution. LHS can be incorporated into an existing Monte Carlo model fairly easily, and work with variables following any analytical probability distribution.\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = lhs(n=factor_count,samples=num_samples)\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# ============================================================================================\n# Function for building space-filling Latin Hypercube from a dictionary of process variables\n# ============================================================================================\n\ndef build_space_filling_lhs(factor_level_ranges, num_samples=None):\n \"\"\"\n Builds a space-filling Latin Hypercube design dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = transform_spread_out(lhd_matrix(num_points=num_samples,dimension=factor_count)) # create latin hypercube design\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# =====================================================================================================\n# Function for building designs with random _k-means_ clusters from a dictionary of process variables\n# =====================================================================================================\n\ndef build_random_k_means(factor_level_ranges, num_samples=None):\n \"\"\"\n This function aims to produce a centroidal Voronoi tesselation of the unit random hypercube and generate k-means clusters.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = random_k_means(num_points=num_samples,dimension=factor_count) # create latin hypercube design\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# =============================================================================================\n# Function for building maximin reconstruction matrix from a dictionary of process variables\n# =============================================================================================\n\ndef build_maximin(factor_level_ranges, num_samples=None):\n \"\"\"\n Builds a maximin reconstructed design dataframe from a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n\t\n\tThis algorithm carries out a user-specified number of iterations to maximize the minimal distance of a point in the set to \n\t\t* other points in the set, \n\t\t* existing (fixed) points, \n\t\t* the boundary of the hypercube.\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = maximin_reconstruction(num_points=num_samples,dimension=factor_count) # create latin hypercube design\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# ========================================================================================\n# Function for building Halton matrix based design from a dictionary of process variables\n# ========================================================================================\n\ndef build_halton(factor_level_ranges, num_samples=None):\n \"\"\"\n Builds a quasirandom dataframe from a dictionary of factor/level ranges using prime numbers as seed.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n\n Quasirandom sequence using the default initialization with first n prime numbers equal to the number of factors/variables.\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = halton(num_points=num_samples,dimension=factor_count) # create Halton matrix design\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n\n# ==========================================================================================\n# Function for building uniform random design matrix from a dictionary of process variables\n# ==========================================================================================\n\ndef build_uniform_random (factor_level_ranges, num_samples=None):\n \"\"\"\n Builds a design dataframe with samples drawn from uniform random distribution based on a dictionary of factor/level ranges.\n Only min and max values of the range are required.\n Example of the dictionary:\n {'Pressure':[50,70],'Temperature':[290, 350],'Flow rate':[0.9,1.0]}\n num_samples: Number of samples to be generated\n \"\"\"\n for key in factor_level_ranges:\n if len(factor_level_ranges[key])!=2:\n factor_level_ranges[key][1]=factor_level_ranges[key][-1]\n factor_level_ranges[key]=factor_level_ranges[key][:2]\n print(f\"{key} had more than two levels. Assigning the end point to the high level.\")\n \n factor_count=len(factor_level_ranges)\n factor_lists=[]\n \n if num_samples==None:\n num_samples=factor_count\n \n for key in factor_level_ranges:\n factor_lists.append(factor_level_ranges[key])\n \n x = random_uniform(num_points=num_samples,dimension=factor_count) # create Halton matrix design\n factor_lists=np.array(factor_lists)\n \n df = construct_df_from_random_matrix(x,factor_lists)\n df.columns=factor_level_ranges.keys()\n return df\n" ]
[ [ "numpy.vectorize", "numpy.zeros", "pandas.DataFrame", "numpy.abs", "numpy.array" ] ]
kidakoji/ImgRecogRecipe
[ "14b9dec4d581c0109539f96072d883ec4f9d64a1" ]
[ "docker-python3-flask-ml-app/app/keras_mnist/kerasPredict.py" ]
[ "# -*- coding: utf-8 -*-\n# -----------------------------------------------------------------------------\n#\n#\nimport os\n\nimport numpy as np\nfrom keras import backend as Keras\nfrom keras.models import load_model\n\n# -----------------------------------------------------------------------------\n#\nKeras.clear_session()\n# 学習済みモデル\nkeras_mnist_model = load_model(\n os.path.abspath(os.path.dirname(__file__)) + '/keras-mnist-model.h5')\n\nkeras_mnist_model._make_predict_function()\nkeras_mnist_model.summary()\nprint('Keras MNIST model is loaded.')\n\n\ndef result(input_data):\n input_data = np.expand_dims(input_data, axis=0)\n input_data = input_data.reshape(input_data.shape[0], 28, 28, 1)\n result = np.argmax(keras_mnist_model.predict(input_data))\n return int(result)\n" ]
[ [ "numpy.expand_dims" ] ]
avielfedida/DecisiveML
[ "cf0feceeda0fc4abd4af6cc766c7c14ee655b576" ]
[ "decisiveml/montecarlo.py" ]
[ "import pandas as pd\nimport random\nimport statistics\nimport logging\n\nlogger = logging.getLogger(__name__)\n\n# MonkeyPatch Python 3.6 choices into random 3.5.5\nimport bisect as _bisect\nimport itertools as _itertools\n\n\ndef choices(population, weights=None, cum_weights=None, k=1):\n \"\"\"Return a k sized list of population elements chosen with\nreplacement.\n If the relative weights or cumulative weights are not specified,\n the selections are made with equal probability.\n \"\"\"\n if cum_weights is None:\n if weights is None:\n total = len(population)\n return [population[int(random.random() * total)] for i in range(k)]\n cum_weights = list(_itertools.accumulate(weights))\n elif weights is not None:\n raise TypeError(\"Cannot specify both weights and cumulative weights\")\n if len(cum_weights) != len(population):\n raise ValueError(\"The number of weights does not match the population\")\n bisect = _bisect.bisect\n total = cum_weights[-1]\n hi = len(cum_weights) - 1\n return [population[bisect(cum_weights, random() * total, 0, hi)] for i in range(k)]\n\n\nrandom.choices = choices\n\n\nclass ExcessiveBaseEquity(Exception):\n pass\n\n\nclass MonteCarlo(object):\n def __init__(self, trades_list):\n self.trades_list = trades_list\n self.num_trades_total = len(self.trades_list)\n self.num_trades_per_year = None\n self.ruin_equity = None\n self.runs = None\n\n self._MONTECARLO_RUNS = 2500\n logger.info(\n \"Initialize \\t| Trades: {} \\t| MC Runs: {}\".format(\n self.num_trades_total, self._MONTECARLO_RUNS\n )\n )\n\n def settings(self, ruin_equity, start_date, end_date):\n self._set_ruin_equity(ruin_equity)\n self._set_trades_per_year(start_date, end_date)\n logger.info(\n \"Settings \\t| Ruin: {} \\t| Trades Per Year: {}\".format(\n self.ruin_equity, self.num_trades_per_year\n )\n )\n\n def _set_ruin_equity(self, ruin_equity):\n self.ruin_equity = ruin_equity\n\n def _set_trades_per_year(self, start_date, end_date):\n td = end_date - start_date\n self.num_trades_per_year = int(self.num_trades_total * 365 / td.days)\n logger.debug(\"TimeDelta: {} {} trades/yr\".format(td, self.num_trades_per_year))\n\n def _random_trade(self, starting_equity):\n assert self.num_trades_per_year\n assert self.ruin_equity\n\n trades = random.choices(self.trades_list, k=self.num_trades_per_year)\n # logger.debug(\"{} {}\".format(len(trades), trades))\n\n # Check for ruin at any point in the trades list\n is_ruined = 0\n equity = starting_equity\n for trade in trades:\n equity = equity + trade\n if equity < self.ruin_equity:\n is_ruined = 1\n break\n\n stats = {\n \"profit\": sum(trades),\n \"returns_pct\": int(\n 100 * ((starting_equity + sum(trades)) / starting_equity - 1)\n ),\n \"drawdown_pct\": self._drawdown(starting_equity, trades),\n \"is_ruined\": is_ruined,\n \"is_profitable\": 1 if sum(trades) >= 0 else 0,\n }\n try:\n stats[\"returns_per_drawdown\"] = stats[\"returns_pct\"] / stats[\"drawdown_pct\"]\n except ZeroDivisionError:\n stats[\"returns_per_drawdown\"] = 0\n logger.debug(stats)\n return stats\n\n def _drawdown(self, starting_equity, trades):\n \"\"\"Returns the maximum drawdown in a set of trades\"\"\"\n equity = starting_equity\n hwm = starting_equity\n max_drawdown_pct = 0\n drawdown_pct = 0\n\n for trade in trades:\n equity = equity + trade\n\n # Set High Water Mark\n if equity > hwm:\n hwm = equity\n\n # Look for drawdown\n if equity < hwm:\n drawdown_pct = 100 * (1 - (equity / hwm))\n if drawdown_pct > max_drawdown_pct:\n max_drawdown_pct = drawdown_pct\n\n # logger.debug(\"{} eq:{} \\t hwm:{} \\t dd:{} mdd:{}\".format(trade, equity, hwm, drawdown_pct, max_drawdown_pct))\n return max_drawdown_pct\n\n def _median_stats_run(self, starting_equity):\n montecarlo = {}\n median_montecarlo = {}\n\n # runs\n for _ in range(self._MONTECARLO_RUNS):\n\n # make the list for every key\n stats = self._random_trade(starting_equity)\n for k, v in stats.items():\n if k not in montecarlo.keys():\n montecarlo[k] = []\n montecarlo[k].append(v)\n\n # run statistics on all the lists of every key\n for k, v in montecarlo.items():\n # ignore non-median stats\n if k != \"is_ruined\" or k != \"is_profitable\":\n median_montecarlo[k] = statistics.median(montecarlo[k])\n\n logger.debug((montecarlo[\"profit\"]))\n logger.debug((montecarlo[\"is_ruined\"]))\n logger.debug(sum(montecarlo[\"is_ruined\"]))\n median_montecarlo[\"is_ruined\"] = (\n 100 * sum(montecarlo[\"is_ruined\"]) / self._MONTECARLO_RUNS\n )\n median_montecarlo[\"is_profitable\"] = (\n 100 * sum(montecarlo[\"is_profitable\"]) / self._MONTECARLO_RUNS\n )\n median_montecarlo[\"equity\"] = starting_equity\n\n # calculate risk of ruin\n logger.debug(\"Median {}: {}\".format(starting_equity, median_montecarlo))\n\n return median_montecarlo\n\n def run(self, base_equity, steps=11):\n \"\"\"Create the results for the MonteCarlo, adding equity to the\n base_equity\n\n Args:\n base_equity (int): starting equity to add to\n steps (:obj:`int`, optional). Default is 11 runs.\n\n Returns:\n pd.DataFrame: results for each run with various starting equities\n\n Example:\n >>> mc = dvm.MonteCarlo(trade_list)\n >>> start_date = trade_list.index[0].to_pydatetime()\n >>> end_date = trade_list.index[-1].to_pydatetime()\n >>> mc.settings(ruin_equity=5000, start_date=start_date, end_date=end_date)\n >>> results = mc.run(base_equity=starting_equity)\n\n \"\"\"\n step_size = int(base_equity / 4)\n end_eq = base_equity + step_size * steps\n starting_equities_list = range(base_equity, end_eq, step_size)\n results = self._run_equity_list(starting_equities_list)\n df = pd.DataFrame(results)\n return df\n\n def _run_equity_list(self, starting_equities_list):\n runs = []\n for starting_equity in starting_equities_list:\n runs.append(self._median_stats_run(starting_equity))\n self.runs = runs\n return runs\n\n def best_run(self, target_risk_of_ruin_pct=10):\n assert self.runs\n for run in self.runs:\n if run[\"is_ruined\"] < target_risk_of_ruin_pct:\n return run\n return None\n\n def recommendation(self, start_date, end_date):\n \"\"\"Create a recommendation from the best run\n\n Args:\n start_date (datetime): trade start date\n end_date (datetime): trade end date\n\n Returns:\n pd.Series: recommendation categories\n\n Example:\n >>> mc = dvm.MonteCarlo(trade_list)\n >>> start_date = trade_list.index[0].to_pydatetime()\n >>> end_date = trade_list.index[-1].to_pydatetime()\n >>> mc.settings(ruin_equity=5000, start_date=start_date, end_date=end_date)\n >>> results = mc.run(base_equity=starting_equity)\n >>> my_rec = mc.recommendation(start_date, end_date)\n \"\"\"\n\n # Get the recommended values\n best = self.best_run()\n my_rec = pd.Series(best)\n\n if my_rec.empty:\n raise ExcessiveBaseEquity(f\"No best run found\")\n\n # Determine result\n if my_rec[\"is_ruined\"] > 10 or my_rec[\"returns_per_drawdown\"] < 2.0:\n logger.info(\"MonteCarlo Risk Assessment: FAILED\")\n my_rec[\"is_pass\"] = False\n else:\n logger.info(\"MonteCarlo Risk Assessment: PASSED\")\n my_rec[\"is_pass\"] = True\n\n # Add additional calculations\n my_rec[\"start_date\"] = start_date\n my_rec[\"end_date\"] = end_date\n my_rec[\"months\"] = (end_date - start_date).days / 30\n my_rec[\"avg_monthly_profit\"] = my_rec[\"profit\"] / my_rec[\"months\"]\n\n return my_rec\n" ]
[ [ "pandas.Series", "pandas.DataFrame" ] ]
deeplearningunb/NextValue
[ "50c04ea55fa3141009d1f018197e02344fdda327" ]
[ "src/App.py" ]
[ "import tkinter as tk\nfrom os import listdir\nfrom os.path import isfile, join\nfrom pages.StartPage import StartPage\nfrom pages.ConfigurationPage import ConfigurationPage\nfrom pages.TrainingPage import TrainingPage\nfrom pages.ResultPage import ResultPage\nfrom pages.ChooseDatePage import ChooseDatePage\nfrom pages.ChooseIntervalPage import ChooseIntervalPage\nfrom Layer import (\n Layer,\n DEFAULT_DAYS,\n DEFAULT_OPTIMIZER,\n DEFAULT_LOSS,\n DEFAULT_EPOCHS,\n DEFAULT_BATCH_SIZE,\n DEFAULT_LAYERS\n)\nimport pandas as pd\nimport numpy as np\nimport math\nimport page_list\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import LSTM\nfrom keras.layers import Dropout\nimport datetime\n\nDATA_PATH = \"../Prices\"\n\nclass App(tk.Tk):\n def __init__(self, master=None):\n tk.Tk.__init__(self)\n\n self.shared_data = {\n \"days\": tk.StringVar(),\n \"optimizer\": tk.StringVar(),\n \"loss\": tk.StringVar(),\n \"epochs\": tk.StringVar(),\n \"batch\": tk.StringVar(),\n \"layers\": DEFAULT_LAYERS,\n \"cryptocurrency\": \"Bitcoin\",\n \"cryptocurrency_list\": [f for f in listdir(DATA_PATH) if (isfile(join(DATA_PATH, f)) and f.endswith(\".csv\"))],\n }\n self.shared_data[\"days\"].set(DEFAULT_DAYS)\n self.shared_data[\"optimizer\"].set(DEFAULT_OPTIMIZER)\n self.shared_data[\"loss\"].set(DEFAULT_LOSS)\n self.shared_data[\"epochs\"].set(DEFAULT_EPOCHS)\n self.shared_data[\"batch\"].set(DEFAULT_BATCH_SIZE)\n\n self.NUMBER_OF_CRYPTOCURRENCIES = len(self.shared_data[\"cryptocurrency_list\"])\n self.shared_data[\"cryptocurrency_list\"].sort()\n\n self.process_data(self.shared_data[\"cryptocurrency_list\"])\n\n container = tk.Frame(self)\n container.pack(side=\"top\", fill=\"both\", expand=True)\n container.grid_rowconfigure(0, weight = 1)\n container.grid_columnconfigure(0, weight = 1)\n\n self.frames = {}\n for F in (StartPage, ConfigurationPage, TrainingPage, ResultPage, ChooseDatePage, ChooseIntervalPage):\n frame = F(container, self)\n self.frames[F] = frame\n frame.grid(row = 0, column = 0, sticky =\"nsew\")\n\n self.show_frame(page_list.START_PAGE)\n\n def show_frame(self, c):\n if c == page_list.START_PAGE:\n frame = self.frames[StartPage]\n elif c == page_list.CONFIGURATION_PAGE:\n frame = self.frames[ConfigurationPage]\n elif c == page_list.TRAINING_PAGE:\n frame = self.frames[TrainingPage]\n elif c == page_list.RESULT_PAGE:\n frame = self.frames[ResultPage]\n elif c == page_list.CHOOSE_DATE_PAGE:\n frame = self.frames[ChooseDatePage]\n elif c == page_list.CHOOSE_INTERVAL_PAGE:\n frame = self.frames[ChooseIntervalPage]\n else:\n return\n\n frame.tkraise()\n \n def generate_initial_values(self):\n initial_values = {}\n\n for i in range(self.dataset.shape[0]):\n initial_values[self.dataset.iloc[i, 0]] = self.dataset.iloc[i, 2:].values\n\n return initial_values\n\n def process_data(self, cryptocurrency_list):\n cryptocurrency_list.sort()\n TRAINING_RATE = 0.95\n \n self.dataset = pd.read_csv(DATA_PATH + \"/Bitcoin.csv\")\n self.dataset = self.dataset.iloc[:, 1:3]\n\n for i in range(0, self.NUMBER_OF_CRYPTOCURRENCIES):\n file = pd.read_csv(DATA_PATH + \"/\" + cryptocurrency_list[i])\n file = file.iloc[:, 1:3]\n self.dataset = pd.merge(self.dataset, file, on='Date', how='outer')\n\n self.dataset = self.dataset.replace(np.nan, 0)\n\n self.initial_values = self.generate_initial_values()\n\n self.dataset = self.dataset.iloc[:, 2:self.NUMBER_OF_CRYPTOCURRENCIES+2]\n\n NUMBER_OF_ROWS = self.dataset.shape[0]\n self.TRAINING_SET_SIZE = math.ceil(NUMBER_OF_ROWS*TRAINING_RATE)\n self.TEST_SET_SIZE = NUMBER_OF_ROWS - self.TRAINING_SET_SIZE\n\n self.test_set = self.dataset.iloc[self.TRAINING_SET_SIZE:, : ].values\n\n self.sc = MinMaxScaler(feature_range = (0, 1))\n\n def build_rnn(self):\n self.PREVIOUS_DAYS = int(self.shared_data[\"days\"].get())\n self.values = self.initial_values.copy()\n\n training_set = self.dataset.iloc[:self.TRAINING_SET_SIZE, : ].values\n training_set_scaled = self.sc.fit_transform(training_set)\n\n self.X_train = []\n self.y_train = []\n for i in range(self.PREVIOUS_DAYS, self.TRAINING_SET_SIZE):\n self.X_train.append(training_set_scaled[i-self.PREVIOUS_DAYS:i, 0:self.NUMBER_OF_CRYPTOCURRENCIES])\n self.y_train.append(training_set_scaled[i, 0:self.NUMBER_OF_CRYPTOCURRENCIES])\n\n self.X_train, self.y_train = np.array(self.X_train), np.array(self.y_train)\n\n self.X_train = self.X_train.reshape((self.X_train.shape[0], self.X_train.shape[1], self.NUMBER_OF_CRYPTOCURRENCIES))\n\n layer_list = self.shared_data[\"layers\"]\n NUMBER_OF_LAYERS = len(layer_list)\n\n regressor = Sequential()\n\n if NUMBER_OF_LAYERS == 1:\n layer = layer_list[0]\n units = layer.units\n dropout = layer.dropout\n\n regressor.add(LSTM(units = units, input_shape = (self.X_train.shape[1], self.NUMBER_OF_CRYPTOCURRENCIES)))\n\n if dropout > 0:\n regressor.add(Dropout(dropout))\n else:\n first_layer = layer_list[0]\n units = first_layer.units\n dropout = first_layer.dropout\n\n regressor.add(LSTM(units = units, return_sequences = True, input_shape = (self.X_train.shape[1], self.NUMBER_OF_CRYPTOCURRENCIES)))\n\n if dropout > 0:\n regressor.add(Dropout(dropout))\n\n for i in range(1, NUMBER_OF_LAYERS-1):\n layer = layer_list[i]\n units = layer.units\n dropout = layer.dropout\n\n regressor.add(LSTM(units = units, return_sequences = True))\n\n if dropout > 0:\n regressor.add(Dropout(dropout))\n\n last_layer = layer_list[-1]\n units = last_layer.units\n dropout = last_layer.dropout\n\n regressor.add(LSTM(units = units))\n\n if dropout > 0:\n regressor.add(Dropout(dropout))\n\n regressor.add(Dense(units = self.NUMBER_OF_CRYPTOCURRENCIES))\n regressor.compile(optimizer = self.shared_data[\"optimizer\"].get(), loss = self.shared_data[\"loss\"].get())\n\n self.rnn = regressor\n \n def get_test_predict(self):\n inputs = self.dataset[self.TRAINING_SET_SIZE-self.PREVIOUS_DAYS:].values\n inputs = inputs.reshape((inputs.shape[0], inputs.shape[1]))\n inputs = self.sc.transform(inputs)\n\n self.X_test = []\n for i in range(self.PREVIOUS_DAYS, self.PREVIOUS_DAYS+self.TEST_SET_SIZE):\n self.X_test.append(inputs[i-self.PREVIOUS_DAYS:i, 0:self.NUMBER_OF_CRYPTOCURRENCIES])\n\n self.X_test = np.array(self.X_test)\n self.X_test = np.reshape(self.X_test, (self.X_test.shape[0], self.X_test.shape[1], self.NUMBER_OF_CRYPTOCURRENCIES))\n\n predict = self.rnn.predict(self.X_test)\n predict = self.sc.inverse_transform(predict)\n\n return predict\n \n def predict_day(self, date):\n if date in self.values:\n return self.values[date]\n \n delta = datetime.timedelta(days=self.PREVIOUS_DAYS)\n end_date = datetime.date.fromisoformat(date)\n start_date = end_date - delta\n delta = datetime.timedelta(days=1)\n\n values = []\n while start_date < end_date:\n d = start_date.strftime('%Y-%m-%d')\n inputs = self.predict_day(d)\n inputs = inputs.reshape(1, -1)\n inputs = self.sc.transform(inputs)\n values.append(inputs)\n start_date += delta\n \n values = [values]\n values = np.array(values)\n values = np.reshape(values, (values.shape[0], values.shape[1], self.NUMBER_OF_CRYPTOCURRENCIES))\n\n predict = self.rnn.predict(values)\n predict = self.sc.inverse_transform(predict)\n\n self.values[date] = predict[0]\n return predict[0]\n\n def predict(self, cryptocurrency, date, end=None):\n cryptocurrency_list = [c[:-4] for c in self.shared_data[\"cryptocurrency_list\"]]\n cryptocurrency_index = cryptocurrency_list.index(cryptocurrency)\n\n if end == None:\n predict = self.predict_day(date)\n return predict[cryptocurrency_index]\n else:\n start_date = datetime.date.fromisoformat(date)\n end_date = datetime.date.fromisoformat(end)\n delta = datetime.timedelta(days=1)\n predict_list = []\n\n while start_date <= end_date:\n d = start_date.strftime('%Y-%m-%d')\n predict = self.predict_day(d)\n predict_list.append(predict[cryptocurrency_index])\n start_date += delta\n\n return predict_list" ]
[ [ "pandas.read_csv", "sklearn.preprocessing.MinMaxScaler", "numpy.reshape", "pandas.merge", "numpy.array" ] ]
brian220/Sketch2PointCloud
[ "17e8657ffc6605804ab4f1da89f446ea4d37665c" ]
[ "models/networks_graphx_refine_no_img_encoder.py" ]
[ "import torch.nn as nn\nimport torch\nimport torch.nn.functional as F\nimport torchvision.models\nimport os\n\nimport utils.network_utils\nfrom utils.pointnet2_utils import PointNetSetAbstraction,PointNetFeaturePropagation\n\nimport cuda.emd.emd_module as emd\n\n# Set the path for pretrain weight\nos.environ['TORCH_HOME'] = '/media/caig/FECA2C89CA2C406F/sketch3D/pretrain_models'\n\nConv = nn.Conv2d\n\ndef wrapper(func, *args, **kwargs):\n class Wrapper(nn.Module):\n def __init__(self):\n super().__init__()\n self.func = func\n\n def forward(self, input):\n return self.func(input, *args, **kwargs)\n\n return Wrapper()\n\n\nclass TransformPC(nn.Module):\n \"\"\"\n Transform point cloud to camera coordinate\n\n Input:\n xyz: float tensor, (BS,N_PTS,3); input point cloud\n values assumed to be in (-1,1)\n az: float tensor, (BS); azimuthal angle of camera in radians\n el: float tensor, (BS); elevation of camera in radians\n \n Output:\n xyz_out: float tensor, (BS,N_PTS,3); output point cloud in camera\n co-ordinates\n \"\"\"\n def __init__(self, cfg):\n super().__init__()\n self.cfg = cfg\n self.n_pts = cfg.CONST.NUM_POINTS\n \n def forward(self, xyz, az, el):\n batch_size = xyz.size(0)\n cam_xyz = self.world2cam(xyz, az, el, batch_size, N_PTS=self.n_pts)\n return cam_xyz\n\n def world2cam(self, xyz, az, el, batch_size, N_PTS=1024):\n # y ---> x\n rotmat_az=[\n [torch.cos(az),torch.sin(az),torch.zeros_like(az)],\n [-torch.sin(az),torch.cos(az),torch.zeros_like(az)],\n [torch.zeros_like(az),torch.zeros_like(az), torch.ones_like(az)]\n ]\n rotmat_az = [ torch.stack(x) for x in rotmat_az ]\n \n # z ---> x, in dataloader, az = original az - 90 degree, which means here is actually x ----> -z \n rotmat_el=[\n [torch.cos(el),torch.zeros_like(az), torch.sin(el)],\n [torch.zeros_like(az),torch.ones_like(az),torch.zeros_like(az)],\n [-torch.sin(el),torch.zeros_like(az), torch.cos(el)]\n ]\n rotmat_el = [ torch.stack(x) for x in rotmat_el ]\n \n rotmat_az = torch.stack(rotmat_az, 0) # [3,3,B]\n rotmat_el = torch.stack(rotmat_el, 0) # [3,3,B]\n rotmat_az = rotmat_az.permute(2, 0, 1) # [B,3,3]\n rotmat_el = rotmat_el.permute(2, 0, 1) # [B,3,3]\n rotmat = torch.matmul(rotmat_el, rotmat_az)\n\n # Transformation(t)\n # Distance of object from camera - fixed to 2\n d = 2.\n # Calculate translation params\n tx, ty, tz = [0, 0, d]\n \n tr_mat = torch.unsqueeze(torch.tensor([tx, ty, tz]), 0).repeat(batch_size, 1) # [B,3]\n tr_mat = torch.unsqueeze(tr_mat,2) # [B,3,1]\n tr_mat = tr_mat.permute(0, 2, 1) # [B,1,3]\n tr_mat = tr_mat.repeat(1, N_PTS, 1) # [B,N_PTS,3]\n tr_mat = utils.network_utils.var_or_cuda(tr_mat) # [B,N_PTS,3]\n\n xyz_out = torch.matmul(rotmat, xyz.permute(0, 2, 1)) - tr_mat.permute(0, 2, 1)\n\n return xyz_out.permute(0, 2, 1)\n\n\nclass FeatureProjection(nn.Module):\n \"\"\"\n Project the pointcloud to 2d image and get the corresponding image features at\n the project location\n \n Input:\n img_feats: multi-scale image features \n pc: input point clouds (in camera coordinate) [B, N, 3]\n\n Output:\n pc_feats_trans: pointcloud xyz + multi-view image features (by feature ptojection)\n\n \"\"\"\n def __init__(self, cfg):\n super().__init__()\n self.cfg = cfg\n self.concat = wrapper(torch.cat, dim=-1)\n\n def forward(self, img_feats, pc):\n pc_feats = []\n pc_feats += [self.get_projection(img_feat, pc) for img_feat in img_feats]\n pc_feats_trans = self.concat(pc_feats)\n return pc_feats_trans\n\n def _project(self, img_feats, xs, ys):\n x, y = xs.flatten(), ys.flatten()\n idb = torch.arange(img_feats.shape[0], device=img_feats.device)\n idb = idb[None].repeat(xs.shape[1], 1).t().flatten().long()\n\n x1, y1 = torch.floor(x), torch.floor(y)\n x2, y2 = torch.ceil(x), torch.ceil(y)\n q11 = img_feats[idb, :, x1.long(), y1.long()].to(img_feats.device)\n q12 = img_feats[idb, :, x1.long(), y2.long()].to(img_feats.device)\n q21 = img_feats[idb, :, x2.long(), y1.long()].to(img_feats.device)\n q22 = img_feats[idb, :, x2.long(), y2.long()].to(img_feats.device)\n\n weights = ((x2 - x) * (y2 - y)).unsqueeze(1)\n q11 *= weights\n\n weights = ((x - x1) * (y2 - y)).unsqueeze(1)\n q21 *= weights\n\n weights = ((x2 - x) * (y - y1)).unsqueeze(1)\n q12 *= weights\n\n weights = ((x - x1) * (y - y1)).unsqueeze(1)\n q22 *= weights\n out = q11 + q12 + q21 + q22\n return out.view(img_feats.shape[0], -1, img_feats.shape[1])\n\n def get_projection(self, img_feat, pc):\n _, _, h_, w_ = tuple(img_feat.shape)\n X, Y, Z = pc[..., 0], pc[..., 1], pc[..., 2]\n w = (420.*X/abs(Z) + (111.5))\n h = (420.*Y/abs(Z) + (111.5))\n w = torch.clamp(w, 0., 223.)\n h = torch.clamp(h, 0., 223.)\n \n x = w / (223. / (w_ - 1.))\n y = h / (223. / (h_ - 1.))\n feats = self._project(img_feat, x, y)\n return feats\n\n\nclass PointNet2(nn.Module):\n \"\"\"\n Point cloud segmentation (set abstraction + feature propagation) in pointnet++\n \n Input:\n xyz: input points position [B, N, 3]\n\n output:\n point_feature: per-point features encode by pointnet [B, 128, N]\n \"\"\"\n def __init__(self, cfg):\n super().__init__()\n self.cfg = cfg\n \n self.sa1 = PointNetSetAbstraction(npoint=1024, radius=0.1, nsample=64, in_channel=3, mlp=[64, 64, 128], group_all=False)\n self.sa2 = PointNetSetAbstraction(npoint=384, radius=0.2, nsample=64, in_channel=128 + 3, mlp=[128, 128, 256], group_all=False)\n self.sa3 = PointNetSetAbstraction(npoint=128, radius=0.4, nsample=64, in_channel=256 + 3, mlp=[256, 256, 512], group_all=False)\n self.sa4 = PointNetSetAbstraction(npoint=None, radius=None, nsample=None, in_channel=512 + 3, mlp=[512, 512, 1024], group_all=True)\n \n self.fp4 = PointNetFeaturePropagation(in_channel=512 + 1024, mlp=[512, 512])\n self.fp3 = PointNetFeaturePropagation(in_channel=256 + 512 , mlp=[512, 256])\n self.fp2 = PointNetFeaturePropagation(in_channel=128 + 256 , mlp=[256, 128])\n self.fp1 = PointNetFeaturePropagation(in_channel=0 + 128 , mlp=[128, 128, 128])\n \n def forward(self, xyz):\n xyz = xyz.transpose(2, 1) # [B, C, N]\n \n l0_xyz = xyz\n l0_points = None\n\n l1_xyz, l1_points = self.sa1(l0_xyz, l0_points)\n l2_xyz, l2_points = self.sa2(l1_xyz, l1_points)\n l3_xyz, l3_points = self.sa3(l2_xyz, l2_points)\n l4_xyz, l4_points = self.sa4(l3_xyz, l3_points)\n\n l3_points = self.fp4(l3_xyz, l4_xyz, l3_points, l4_points)\n l2_points = self.fp3(l2_xyz, l3_xyz, l2_points, l3_points)\n l1_points = self.fp2(l1_xyz, l2_xyz, l1_points, l2_points)\n l0_points = self.fp1(l0_xyz, l1_xyz, l0_points, l1_points)\n \n return l0_points\n\n\nclass LinearDisplacementNet(nn.Module):\n \"\"\"\n Predict the displacement from pointcloud features and image features\n\n Input:\n pc_features: poincloud features from pointnet2 [B, D, N]\n proj_features: image features from feature projection [B, N, D']\n noises: noises vector [B, N, n_length]\n\n Output:\n displacement: perpoint displacement [B, C, N]\n\n \"\"\"\n\n def __init__(self, cfg):\n super().__init__()\n self.cfg = cfg\n\n self.conv1 = nn.Conv1d(1120, 960, 1)\n self.bn1 = nn.BatchNorm1d(960)\n self.conv2 = nn.Conv1d(960, 512, 1)\n self.bn2 = nn.BatchNorm1d(512)\n self.conv3 = nn.Conv1d(512, 256, 1)\n self.bn3 = nn.BatchNorm1d(256)\n self.conv4 = nn.Conv1d(256, 128, 1)\n self.bn4 = nn.BatchNorm1d(128)\n self.conv5 = nn.Conv1d(128, 64, 1)\n self.bn5 = nn.BatchNorm1d(64)\n self.conv6 = nn.Conv1d(64, 3, 1)\n\n def forward(self, transform_xyz, proj_features, pc_features, noises):\n noises = noises.transpose(2, 1) # [B, n_length, N]\n noises = utils.network_utils.var_or_cuda(noises)\n \n proj_features = proj_features.transpose(2, 1) # [B, D', N]\n proj_features = utils.network_utils.var_or_cuda(proj_features)\n \n # concat the img features after each point features\n refine_features = torch.cat((pc_features, proj_features, noises), 1) # [B, D+D'+n_length, N]\n \n refine_features = F.relu(self.bn1(self.conv1(refine_features)))\n refine_features = F.relu(self.bn2(self.conv2(refine_features)))\n refine_features = F.relu(self.bn3(self.conv3(refine_features)))\n refine_features = F.relu(self.bn4(self.conv4(refine_features)))\n refine_features = F.relu(self.bn5(self.conv5(refine_features)))\n displacements = self.conv6(refine_features)\n\n displacements = F.sigmoid(displacements) * self.cfg.REFINE.RANGE_MAX * 2 - self.cfg.REFINE.RANGE_MAX\n \n return displacements\n\n\nclass GRAPHX_REFINE_MODEL(nn.Module):\n \"\"\"\n Refine the point cloud based on the input image\n\n Input:\n xyz: point cloud from reconstruction model\n\n Ouput:\n update_pc: updated point cloud\n \"\"\"\n\n def __init__(self, cfg, in_channels, optimizer=None):\n super().__init__()\n self.cfg = cfg\n \n # Refinement\n self.transform_pc = TransformPC(cfg)\n self.feature_projection = FeatureProjection(cfg)\n self.pc_encode = PointNet2(cfg)\n self.displacement_net = LinearDisplacementNet(cfg)\n \n self.optimizer = None if optimizer is None else optimizer(self.parameters())\n \n # emd loss\n self.emd_dist = emd.emdModule()\n\n if torch.cuda.is_available():\n self.transform_pc = torch.nn.DataParallel(self.transform_pc, device_ids=cfg.CONST.DEVICE).cuda()\n self.feature_projection = torch.nn.DataParallel(self.feature_projection, device_ids=cfg.CONST.DEVICE).cuda()\n self.pc_encode = torch.nn.DataParallel(self.pc_encode, device_ids=cfg.CONST.DEVICE).cuda()\n self.displacement_net = torch.nn.DataParallel(self.displacement_net, device_ids=cfg.CONST.DEVICE).cuda()\n self.emd_dist = torch.nn.DataParallel(self.emd_dist, device_ids=cfg.CONST.DEVICE).cuda()\n self.cuda()\n \n def train_step(self, img_features, xyz, gt_pc, view_az, view_el):\n '''\n Input:\n img_features\n init pc: [B, N, 3]\n gt pc: [B, N, 3]\n view_az: [B]\n view_el: [B]\n\n Output:\n loss\n pred_pc: [B, N, 3]\n '''\n refine_pc = self.refine(img_features, xyz, view_az, view_el)\n # compute reconstruction loss\n emd_loss, _ = self.emd_dist(\n refine_pc, gt_pc, eps=0.005, iters=50\n )\n rec_loss = torch.sqrt(emd_loss).mean(1).mean()\n\n self.refiner_backward(rec_loss)\n\n rec_loss_np = rec_loss.detach().item()\n\n return rec_loss_np*1000\n\n def valid_step(self, img_features, xyz, gt_pc, view_az, view_el):\n # refine the point cloud\n refine_pc = self.refine(img_features, xyz, view_az, view_el)\n # compute reconstruction loss\n emd_loss, _ = self.emd_dist(\n refine_pc, gt_pc, eps=0.005, iters=50\n )\n rec_loss = torch.sqrt(emd_loss).mean(1).mean()\n\n return rec_loss*1000, pred_pc\n\n def refine(self, img_features, xyz, view_az, view_el):\n # img_features = self.img_enc(img)\n transform_xyz = self.transform_pc(xyz, view_az, view_el)\n proj_features = self.feature_projection(img_features, transform_xyz)\n pc_features = self.pc_encode(transform_xyz)\n noises = torch.normal(mean=0.0, std=1, size=(self.cfg.CONST.BATCH_SIZE, self.cfg.CONST.NUM_POINTS, self.cfg.REFINE.NOISE_LENGTH))\n displacements = self.displacement_net(transform_xyz, proj_features, pc_features, noises)\n displacements = displacements.transpose(2, 1)\n refine_pc = xyz + displacements\n\n return refine_pc\n\n def refiner_backward(self, rec_loss):\n self.train(True)\n self.optimizer.zero_grad()\n rec_loss.backward()\n self.optimizer.step()\n\n" ]
[ [ "torch.stack", "torch.sqrt", "torch.cuda.is_available", "torch.cat", "torch.floor", "torch.nn.functional.sigmoid", "torch.cos", "torch.nn.BatchNorm1d", "torch.sin", "torch.arange", "torch.nn.DataParallel", "torch.unsqueeze", "torch.ceil", "torch.ones_like", "torch.tensor", "torch.normal", "torch.nn.Conv1d", "torch.clamp", "torch.zeros_like", "torch.matmul" ] ]
escribano89/cartpole-REINFORCE
[ "e2d79ce721a50feea783c368a66c6f80631cdf26" ]
[ "policy.py" ]
[ "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.distributions import Categorical\n\nclass Policy(nn.Module):\n def __init__(self, s_size=4, h_size=8, a_size=2):\n super(Policy, self).__init__()\n \n self.fc1 = nn.Linear(s_size, h_size)\n self.fc2 = nn.Linear(h_size, a_size)\n\n def forward(self, x):\n x = F.selu(self.fc1(x))\n x = self.fc2(x)\n return F.softmax(x, dim=1)\n \n def act(self, state, device):\n state = torch.from_numpy(state).float().unsqueeze(0).to(device)\n probs = self.forward(state).cpu()\n # >>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))\n # >>> m.sample() # equal probability of 0, 1, 2, 3\n m = Categorical(probs)\n action = m.sample()\n return action.item(), m.log_prob(action)" ]
[ [ "torch.nn.functional.softmax", "torch.from_numpy", "torch.nn.Linear", "torch.distributions.Categorical" ] ]
dpton/tensorflow-attention-rnn
[ "a1b8884640ca49d6fdebc7df2167e4353364a243" ]
[ "utils/reprocessing.py" ]
[ "# _*_ coding:utf-8 _*_\n# !/usr/bin/env python\nfrom __future__ import absolute_import, division, print_function, unicode_literals\nimport numpy as np\nimport logging\nimport nltk\nfrom nltk.corpus import stopwords\nfrom gensim.models.wrappers import FastText\nfrom gensim.models import Word2Vec\nimport random\nimport threading\nimport tensorflow as tf\n\n\n\nimport re\nimport unicodedata\n\nfrom ftfy import fix_text\nfrom unidecode import unidecode\n\nfrom textacy.compat import unicode_\nfrom textacy.constants import (CURRENCIES, URL_REGEX, SHORT_URL_REGEX, EMAIL_REGEX,\n PHONE_REGEX, NUMBERS_REGEX, CURRENCY_REGEX,\n LINEBREAK_REGEX, NONBREAKING_SPACE_REGEX,\n PUNCT_TRANSLATE_UNICODE,\n PUNCT_TRANSLATE_BYTES)\n\n\nUNK = u'_UNK'\nGO = u'_GO'\nEOS = u'_EOS'\nPAD = u'_PAD'\nPAD_ID = 0\nGO_ID = 1\nEOS_ID = 2\nUNK_ID = 3\nSPECIAL_WORDS = [PAD, GO, EOS, UNK]\ndecode_max_length = 25\n\ndef fix_bad_unicode(text, normalization='NFC'):\n \"\"\"\n Fix unicode text that's \"broken\" using `ftfy <http://ftfy.readthedocs.org/>`_;\n this includes mojibake, HTML entities and other code cruft,\n and non-standard forms for display purposes.\n\n Args:\n text (str): raw text\n normalization ({'NFC', 'NFKC', 'NFD', 'NFKD'}): if 'NFC',\n combines characters and diacritics written using separate code points,\n e.g. converting \"e\" plus an acute accent modifier into \"é\"; unicode\n can be converted to NFC form without any change in its meaning!\n if 'NFKC', additional normalizations are applied that can change\n the meanings of characters, e.g. ellipsis characters will be replaced\n with three periods\n\n Returns:\n str\n \"\"\"\n return fix_text(text, normalization=normalization)\n\n\ndef transliterate_unicode(text):\n \"\"\"\n Try to represent unicode data in ascii characters similar to what a human\n with a US keyboard would choose using unidecode <https://pypi.python.org/pypi/Unidecode>\n \"\"\"\n return unidecode(text)\n\n\ndef normalize_whitespace(text):\n \"\"\"\n Given ``text`` str, replace one or more spacings with a single space, and one\n or more linebreaks with a single newline. Also strip leading/trailing whitespace.\n \"\"\"\n return NONBREAKING_SPACE_REGEX.sub(' ', LINEBREAK_REGEX.sub(r'\\n', text)).strip()\n\n\ndef unpack_contractions(text):\n \"\"\"\n Replace *English* contractions in ``text`` str with their unshortened forms.\n N.B. The \"'d\" and \"'s\" forms are ambiguous (had/would, is/has/possessive),\n so are left as-is.\n \"\"\"\n # standard\n text = re.sub(\n r\"(\\b)([Aa]re|[Cc]ould|[Dd]id|[Dd]oes|[Dd]o|[Hh]ad|[Hh]as|[Hh]ave|[Ii]s|[Mm]ight|[Mm]ust|[Ss]hould|[Ww]ere|[Ww]ould)n't\", r\"\\1\\2 not\", text)\n text = re.sub(\n r\"(\\b)([Hh]e|[Ii]|[Ss]he|[Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Yy]ou)'ll\", r\"\\1\\2 will\", text)\n text = re.sub(\n r\"(\\b)([Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Yy]ou)'re\", r\"\\1\\2 are\", text)\n text = re.sub(\n r\"(\\b)([Ii]|[Ss]hould|[Tt]hey|[Ww]e|[Ww]hat|[Ww]ho|[Ww]ould|[Yy]ou)'ve\", r\"\\1\\2 have\", text)\n # non-standard\n text = re.sub(r\"(\\b)([Cc]a)n't\", r\"\\1\\2n not\", text)\n text = re.sub(r\"(\\b)([Ii])'m\", r\"\\1\\2 am\", text)\n text = re.sub(r\"(\\b)([Ll]et)'s\", r\"\\1\\2 us\", text)\n text = re.sub(r\"(\\b)([Ww])on't\", r\"\\1\\2ill not\", text)\n text = re.sub(r\"(\\b)([Ss])han't\", r\"\\1\\2hall not\", text)\n text = re.sub(r\"(\\b)([Yy])(?:'all|a'll)\", r\"\\1\\2ou all\", text)\n return text\n\n\ndef replace_urls(text, replace_with='<url>'):\n \"\"\"Replace all URLs in ``text`` str with ``replace_with`` str.\"\"\"\n return URL_REGEX.sub(replace_with, SHORT_URL_REGEX.sub(replace_with, text))\n\n\ndef replace_emails(text, replace_with='<email>'):\n \"\"\"Replace all emails in ``text`` str with ``replace_with`` str.\"\"\"\n return EMAIL_REGEX.sub(replace_with, text)\n\n\ndef replace_phone_numbers(text, replace_with='<phone>'):\n \"\"\"Replace all phone numbers in ``text`` str with ``replace_with`` str.\"\"\"\n return PHONE_REGEX.sub(replace_with, text)\n\n\ndef replace_numbers(text, replace_with='<number>'):\n \"\"\"Replace all numbers in ``text`` str with ``replace_with`` str.\"\"\"\n return NUMBERS_REGEX.sub(replace_with, text)\n\n\ndef replace_currency_symbols(text, replace_with='<currency>'):\n \"\"\"\n Replace all currency symbols in ``text`` str with string specified by ``replace_with`` str.\n\n Args:\n text (str): raw text\n replace_with (str): if None (default), replace symbols with\n their standard 3-letter abbreviations (e.g. '$' with 'USD', '£' with 'GBP');\n otherwise, pass in a string with which to replace all symbols\n (e.g. \"*CURRENCY*\")\n\n Returns:\n str\n \"\"\"\n if replace_with is None:\n for k, v in CURRENCIES.items():\n text = text.replace(k, v)\n return text\n else:\n return CURRENCY_REGEX.sub(replace_with, text)\n\n\ndef remove_punct(text, marks=None):\n \"\"\"\n Remove punctuation from ``text`` by replacing all instances of ``marks``\n with an empty string.\n\n Args:\n text (str): raw text\n marks (str): If specified, remove only the characters in this string,\n e.g. ``marks=',;:'`` removes commas, semi-colons, and colons.\n Otherwise, all punctuation marks are removed.\n\n Returns:\n str\n\n .. note:: When ``marks=None``, Python's built-in :meth:`str.translate()` is\n used to remove punctuation; otherwise,, a regular expression is used\n instead. The former's performance is about 5-10x faster.\n \"\"\"\n if marks:\n return re.sub('[{}]+'.format(re.escape(marks)), '', text, flags=re.UNICODE)\n else:\n if isinstance(text, unicode_):\n return text.translate(PUNCT_TRANSLATE_UNICODE)\n else:\n return text.translate(None, PUNCT_TRANSLATE_BYTES)\n\n\ndef remove_accents(text, method='unicode'):\n \"\"\"\n Remove accents from any accented unicode characters in ``text`` str, either by\n transforming them into ascii equivalents or removing them entirely.\n\n Args:\n text (str): raw text\n method ({'unicode', 'ascii'}): if 'unicode', remove accented\n char for any unicode symbol with a direct ASCII equivalent; if 'ascii',\n remove accented char for any unicode symbol\n\n NB: the 'ascii' method is notably faster than 'unicode', but less good\n\n Returns:\n str\n\n Raises:\n ValueError: if ``method`` is not in {'unicode', 'ascii'}\n \"\"\"\n if method == 'unicode':\n return ''.join(c for c in unicodedata.normalize('NFKD', text)\n if not unicodedata.combining(c))\n elif method == 'ascii':\n return unicodedata.normalize('NFKD', text).encode('ascii', errors='ignore').decode('ascii')\n else:\n msg = '`method` must be either \"unicode\" and \"ascii\", not {}'.format(\n method)\n raise ValueError(msg)\n\n\ndef replace_hashtag(text, replace_with_hashtag='<hashtag>'):\n text = ' '.join(\n re.sub(\"(#[A-Za-z0-9]+)\", replace_with_hashtag, text).split())\n return text\n\n\ndef replace_nametag(text, replace_with_nametag='<nametag>'):\n text = ' '.join(\n re.sub(\"(@[A-Za-z0-9]+)\", replace_with_nametag, text).split())\n return text\n\n\ndef preprocess_text(text, fix_unicode=False, lowercase=False, transliterate=False,\n no_urls=False, no_emails=False, no_phone_numbers=False,\n no_numbers=False, no_currency_symbols=False, no_punct=False,\n no_contractions=False, no_accents=False, no_hashtag=False, no_nametag=False):\n \"\"\"\n This is a modified version of the function \"textacy.preprocess_text\".\n Normalize various aspects of a raw text doc before parsing it with Spacy.\n A convenience function for applying all other preprocessing functions in one go.\n\n Args:\n text (str): raw text to preprocess\n fix_unicode (bool): if True, fix \"broken\" unicode such as\n mojibake and garbled HTML entities\n lowercase (bool): if True, all text is lower-cased\n transliterate (bool): if True, convert non-ascii characters\n into their closest ascii equivalents\n no_urls (bool): if True, replace all URL strings with '*URL*'\n no_emails (bool): if True, replace all email strings with '*EMAIL*'\n no_phone_numbers (bool): if True, replace all phone number strings\n with '<phone>'\n no_numbers (bool): if True, replace all number-like strings\n with '<number>'\n no_currency_symbols (bool): if True, replace all currency symbols\n with their standard 3-letter abbreviations\n no_punct (bool): if True, remove all punctuation (replace with\n empty string)\n no_contractions (bool): if True, replace *English* contractions\n with their unshortened forms\n no_accents (bool): if True, replace all accented characters\n with unaccented versions; NB: if `transliterate` is True, this option\n is redundant\n not_hashtag (bool): if True, replace all hashtag (twitter, facebook)\n\n Returns:\n str: input ``text`` processed according to function args\n\n .. warning:: These changes may negatively affect subsequent NLP analysis\n performed on the text, so choose carefully, and preprocess at your own\n risk!\n \"\"\"\n if fix_unicode is True:\n text = fix_bad_unicode(text, normalization='NFC')\n if transliterate is True:\n text = transliterate_unicode(text)\n if no_urls is True:\n text = replace_urls(text)\n if no_emails is True:\n text = replace_emails(text)\n if no_phone_numbers is True:\n text = replace_phone_numbers(text)\n if no_numbers is True:\n text = replace_numbers(text)\n if no_currency_symbols is True:\n text = replace_currency_symbols(text)\n if no_contractions is True:\n text = unpack_contractions(text)\n if no_accents is True:\n text = remove_accents(text, method='unicode')\n if no_punct is True:\n text = remove_punct(text)\n if lowercase is True:\n text = text.lower()\n if no_hashtag is True:\n text = replace_hashtag(text)\n if no_nametag is True:\n text = replace_nametag(text)\n # always normalize whitespace; treat linebreaks separately from spacing\n text = normalize_whitespace(text)\n\n return text\n\n\ndef text_normalize(string, convert2digit=True):\n text = preprocess_text(text=string, fix_unicode=False, lowercase=True, transliterate=False,\n no_urls=True, no_emails=True, no_phone_numbers=True,\n no_numbers=True, no_currency_symbols=True, no_punct=False,\n no_contractions=True, no_accents=True, not_hashtag=True)\n if convert2digit:\n return text2num(text)\n else:\n return text\n\n\ndef remove_stopwords(word_list):\n filtered_words = [\n word for word in word_list if word not in stopwords.words('english')]\n return filtered_words\n\n\ndef tokenize(string):\n text = text_normalize(string)\n return nltk.word_tokenize(text, language='english')\n\n\nclass wordEmbedding(object):\n '''\n This class wraps the two popular models using for word embedding, FastText and Word2Vec\n '''\n\n def __init__(self, model_path, model_type='fasttext', **kwarg):\n if model_type == \"fasttext\":\n self._model = FastText.load_fasttext_format(model_path)\n elif model_type == \"word2vec\":\n self._model = Word2Vec.load_word2vec_format(model_path)\n else:\n raise NotImplementedError(\"other model is not supported\")\n\n def sentence_to_index(self, sentence):\n list_of_index = [self._model.wv.vocab[\n word].index for word in tokenize(sentence)]\n return list_of_index\n\n def get_embedding_matrix(self):\n return self._model.syn0\n\ndef create_queue(sess = None, coord = None, encode_data = None,\n decode_data = None, capacity = 1024, batch_size = 32, scope = None):\n\n encode = tf.placeholder(tf.int32, shape=[None], name=\"encode\")\n decode = tf.placeholder(tf.int32, shape=[decode_max_length + 2], name=\"decode\")\n weight = tf.placeholder(tf.float32, shape=[decode_max_length + 1], name=\"weight\")\n queue = tf.PaddingFIFOQueue(capacity = capacity,\n dtypes = [tf.int32, tf.int32, tf.float32],\n shapes = [[None], [decode_max_length + 2], [decode_max_length + 1]],\n name = 'FIFOQueue')\n enqueue_op = queue.enqueue([encode, decode, weight])\n\n\n def _iterator():\n assert len(encode_data) == len(decode_data)\n data = list(zip(encode_data, decode_data))\n random.shuffle(data)\n encode, decode = [list(t) for t in zip(*data)]\n\n for i in range(len(data)):\n# if len(encode[i]) > encode_max_length - 1 or len(decode[i]) > decode_max_length - 1:\n# raise 'the sentence is longer than max_length'\n #_encode = encode[i][:encode_max_length]\n #_encode = _encode + [PAD_ID] * (encode_max_length - len(_encode))\n _encode = encode[i]\n _decode = decode[i][:decode_max_length]\n \n \n \n _decode_padding_size = decode_max_length - len(_decode)\n _weight = [1.0] * (len(_decode) + 1) + [0.0] * _decode_padding_size\n _decode = [GO_ID] + _decode + [EOS_ID] + [PAD_ID] * _decode_padding_size\n \n yield _encode, _decode, _weight#, _encode_length, _decode_length\n def basic_enqueue(sess, encode_input, decode_input = None):\n# if len(encode_input) > encode_max_length:\n# encode_input = encode_input[:encode_max_length]\n# _encode = encode_input + [PAD_ID] * (encode_max_length - len(encode_input))\n _encode = encode_input\n if decode_input is None:\n _decode = [GO_ID] + [PAD_ID] * (decode_max_length + 1)\n _weight = [1.0] * (decode_max_length + 1)\n else:\n _decode_padding_size = decode_max_length - len(decode_input)\n _decode = [GO_ID] + decode_input + [EOS_ID] + [PAD_ID] * _decode_padding_size\n _weight = [1.0] * (len(decode_input) + 1) + [0.0] * _decode_padding_size\n feed_dict = {\n encode: _encode,\n decode: _decode,\n weight: _weight\n }\n # Push all the training examples to the queue\n sess.run(enqueue_op, feed_dict=feed_dict)\n def _enqueue(sess, coord):\n try:\n while not coord.should_stop():\n for _encode, _decode, _weight in _iterator():\n feed_dict = {\n encode: _encode,\n decode: _decode,\n weight: _weight,\n }\n # Push all the training examples to the queue\n sess.run(enqueue_op, feed_dict=feed_dict)\n except tf.errors.CancelledError:\n coord.request_stop()\n #Start thread enqueue data\n # if encode_data is None or decode_data is None:\n # return queue, None, basic_enqueue\n enqueue_threads = []\n ## enqueue asynchronously\n for i in range(num_threads):\n enqueue_thread = threading.Thread(target=_enqueue, args=(sess, coord))\n enqueue_thread.setDaemon(True)\n enqueue_threads.append(enqueue_thread)\n return queue, enqueue_threads, basic_enqueue\n\nif __name__ == '__main__':\n print(tokenize(\"http://google.com.vn I love the cat @Peter with 69USD\"))\n" ]
[ [ "tensorflow.placeholder", "tensorflow.PaddingFIFOQueue" ] ]
zzm422/automl
[ "38d6623e0983d6ee1d16bb5b1db5c6b2ccbb3ecf" ]
[ "efficientdet/inference.py" ]
[ "# Copyright 2020 Google Research. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================\nr\"\"\"Inference related utilities.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\n# gtype import\nfrom __future__ import print_function\n\nimport copy\nimport functools\nimport os\nimport time\n\nfrom absl import logging\nimport numpy as np\nfrom PIL import Image\nimport tensorflow.compat.v1 as tf\nfrom typing import Text, Dict, Any, List, Tuple, Union\nimport yaml\n\nimport anchors\nimport dataloader\nimport det_model_fn\nimport hparams_config\nimport utils\nfrom visualize import vis_utils\n\ncoco_id_mapping = {\n 1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane',\n 6: 'bus', 7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light',\n 11: 'fire hydrant', 13: 'stop sign', 14: 'parking meter', 15: 'bench',\n 16: 'bird', 17: 'cat', 18: 'dog', 19: 'horse', 20: 'sheep', 21: 'cow',\n 22: 'elephant', 23: 'bear', 24: 'zebra', 25: 'giraffe', 27: 'backpack',\n 28: 'umbrella', 31: 'handbag', 32: 'tie', 33: 'suitcase', 34: 'frisbee',\n 35: 'skis', 36: 'snowboard', 37: 'sports ball', 38: 'kite',\n 39: 'baseball bat', 40: 'baseball glove', 41: 'skateboard', 42: 'surfboard',\n 43: 'tennis racket', 44: 'bottle', 46: 'wine glass', 47: 'cup', 48: 'fork',\n 49: 'knife', 50: 'spoon', 51: 'bowl', 52: 'banana', 53: 'apple',\n 54: 'sandwich', 55: 'orange', 56: 'broccoli', 57: 'carrot', 58: 'hot dog',\n 59: 'pizza', 60: 'donut', 61: 'cake', 62: 'chair', 63: 'couch',\n 64: 'potted plant', 65: 'bed', 67: 'dining table', 70: 'toilet', 72: 'tv',\n 73: 'laptop', 74: 'mouse', 75: 'remote', 76: 'keyboard', 77: 'cell phone',\n 78: 'microwave', 79: 'oven', 80: 'toaster', 81: 'sink', 82: 'refrigerator',\n 84: 'book', 85: 'clock', 86: 'vase', 87: 'scissors', 88: 'teddy bear',\n 89: 'hair drier', 90: 'toothbrush',\n} # pyformat: disable\n\n\ndef image_preprocess(image, image_size: Union[int, Tuple[int, int]]):\n \"\"\"Preprocess image for inference.\n\n Args:\n image: input image, can be a tensor or a numpy arary.\n image_size: single integer of image size for square image or tuple of two\n integers, in the format of (image_height, image_width).\n\n Returns:\n (image, scale): a tuple of processed image and its scale.\n \"\"\"\n input_processor = dataloader.DetectionInputProcessor(image, image_size)\n input_processor.normalize_image()\n input_processor.set_scale_factors_to_output_size()\n image = input_processor.resize_and_crop_image()\n image_scale = input_processor.image_scale_to_original\n return image, image_scale\n\n\[email protected]_graph\ndef batch_image_files_decode(image_files):\n raw_images = tf.TensorArray(tf.uint8, size=0, dynamic_size=True)\n for i in tf.range(tf.shape(image_files)[0]):\n image = tf.io.decode_image(image_files[i])\n image.set_shape([None, None, None])\n raw_images = raw_images.write(i, image)\n return raw_images.stack()\n\n\ndef batch_image_preprocess(raw_images,\n image_size: Union[int, Tuple[int, int]],\n batch_size: int = None):\n \"\"\"Preprocess batched images for inference.\n\n Args:\n raw_images: a list of images, each image can be a tensor or a numpy arary.\n image_size: single integer of image size for square image or tuple of two\n integers, in the format of (image_height, image_width).\n batch_size: if None, use map_fn to deal with dynamic batch size.\n\n Returns:\n (image, scale): a tuple of processed images and scales.\n \"\"\"\n if not batch_size:\n # map_fn is a little bit slower due to some extra overhead.\n map_fn = functools.partial(image_preprocess, image_size=image_size)\n images, scales = tf.map_fn(\n map_fn, raw_images, dtype=(tf.float32, tf.float32), back_prop=False)\n return (images, scales)\n\n # If batch size is known, use a simple loop.\n scales, images = [], []\n for i in range(batch_size):\n image, scale = image_preprocess(raw_images[i], image_size)\n scales.append(scale)\n images.append(image)\n images = tf.stack(images)\n scales = tf.stack(scales)\n return (images, scales)\n\n\ndef build_inputs(image_path_pattern: Text, image_size: Union[int, Tuple[int,\n int]]):\n \"\"\"Read and preprocess input images.\n\n Args:\n image_path_pattern: a path to indicate a single or multiple files.\n image_size: single integer of image size for square image or tuple of two\n integers, in the format of (image_height, image_width).\n\n Returns:\n (raw_images, images, scales): raw images, processed images, and scales.\n\n Raises:\n ValueError if image_path_pattern doesn't match any file.\n \"\"\"\n raw_images, images, scales = [], [], []\n for f in tf.io.gfile.glob(image_path_pattern):\n image = Image.open(f)\n raw_images.append(image)\n image, scale = image_preprocess(image, image_size)\n images.append(image)\n scales.append(scale)\n if not images:\n raise ValueError(\n 'Cannot find any images for pattern {}'.format(image_path_pattern))\n return raw_images, tf.stack(images), tf.stack(scales)\n\n\ndef build_model(model_name: Text, inputs: tf.Tensor, **kwargs):\n \"\"\"Build model for a given model name.\n\n Args:\n model_name: the name of the model.\n inputs: an image tensor or a numpy array.\n **kwargs: extra parameters for model builder.\n\n Returns:\n (cls_outputs, box_outputs): the outputs for class and box predictions.\n Each is a dictionary with key as feature level and value as predictions.\n \"\"\"\n model_arch = det_model_fn.get_model_arch(model_name)\n cls_outputs, box_outputs = utils.build_model_with_precision(\n kwargs.get('precision', None), model_arch, inputs, model_name, **kwargs)\n if kwargs.get('precision', None):\n # Post-processing has multiple places with hard-coded float32.\n # TODO(tanmingxing): Remove them once post-process can adpat to dtypes.\n cls_outputs = {k: tf.cast(v, tf.float32) for k, v in cls_outputs.items()}\n box_outputs = {k: tf.cast(v, tf.float32) for k, v in box_outputs.items()}\n return cls_outputs, box_outputs\n\n\ndef restore_ckpt(sess, ckpt_path, ema_decay=0.9998, export_ckpt=None):\n \"\"\"Restore variables from a given checkpoint.\n\n Args:\n sess: a tf session for restoring or exporting models.\n ckpt_path: the path of the checkpoint. Can be a file path or a folder path.\n ema_decay: ema decay rate. If None or zero or negative value, disable ema.\n export_ckpt: whether to export the restored model.\n \"\"\"\n sess.run(tf.global_variables_initializer())\n if tf.io.gfile.isdir(ckpt_path):\n ckpt_path = tf.train.latest_checkpoint(ckpt_path)\n if ema_decay > 0:\n ema = tf.train.ExponentialMovingAverage(decay=ema_decay)\n ema_vars = utils.get_ema_vars()\n var_dict = ema.variables_to_restore(ema_vars)\n ema_assign_op = ema.apply(ema_vars)\n else:\n var_dict = utils.get_ema_vars()\n ema_assign_op = None\n tf.train.get_or_create_global_step()\n sess.run(tf.global_variables_initializer())\n saver = tf.train.Saver(var_dict, max_to_keep=1)\n saver.restore(sess, ckpt_path)\n\n if export_ckpt:\n print('export model to {}'.format(export_ckpt))\n if ema_assign_op is not None:\n sess.run(ema_assign_op)\n saver = tf.train.Saver(max_to_keep=1, save_relative_paths=True)\n saver.save(sess, export_ckpt)\n\n\ndef det_post_process_combined(params, cls_outputs, box_outputs, scales,\n min_score_thresh, max_boxes_to_draw):\n \"\"\"A combined version of det_post_process with dynamic batch size support.\"\"\"\n batch_size = tf.shape(list(cls_outputs.values())[0])[0]\n cls_outputs_all = []\n box_outputs_all = []\n # Concatenates class and box of all levels into one tensor.\n for level in range(params['min_level'], params['max_level'] + 1):\n if params['data_format'] == 'channels_first':\n cls_outputs[level] = tf.transpose(cls_outputs[level], [0, 2, 3, 1])\n box_outputs[level] = tf.transpose(box_outputs[level], [0, 2, 3, 1])\n\n cls_outputs_all.append(tf.reshape(\n cls_outputs[level],\n [batch_size, -1, params['num_classes']]))\n box_outputs_all.append(tf.reshape(\n box_outputs[level], [batch_size, -1, 4]))\n cls_outputs_all = tf.concat(cls_outputs_all, 1)\n box_outputs_all = tf.concat(box_outputs_all, 1)\n\n # Create anchor_label for picking top-k predictions.\n eval_anchors = anchors.Anchors(params['min_level'], params['max_level'],\n params['num_scales'], params['aspect_ratios'],\n params['anchor_scale'], params['image_size'])\n anchor_boxes = eval_anchors.boxes\n scores = tf.math.sigmoid(cls_outputs_all)\n # apply bounding box regression to anchors\n boxes = anchors.decode_box_outputs_tf(box_outputs_all, anchor_boxes)\n boxes = tf.expand_dims(boxes, axis=2)\n scales = tf.expand_dims(scales, axis=-1)\n nmsed_boxes, nmsed_scores, nmsed_classes, valid_detections = (\n tf.image.combined_non_max_suppression(\n boxes,\n scores,\n max_boxes_to_draw,\n max_boxes_to_draw,\n score_threshold=min_score_thresh,\n clip_boxes=False))\n del valid_detections # to be used in futue.\n\n image_ids = tf.cast(\n tf.tile(\n tf.expand_dims(tf.range(batch_size), axis=1), [1, max_boxes_to_draw]),\n dtype=tf.float32)\n y = nmsed_boxes[..., 0] * scales\n x = nmsed_boxes[..., 1] * scales\n height = nmsed_boxes[..., 2] * scales - y\n width = nmsed_boxes[..., 3] * scales - x\n detection_list = [\n # Format: (image_ids, y, x, height, width, score, class)\n image_ids, y, x, height, width, nmsed_scores,\n tf.cast(nmsed_classes + 1, tf.float32)\n ]\n detections = tf.stack(detection_list, axis=2, name='detections')\n return detections\n\n\ndef det_post_process(params: Dict[Any, Any], cls_outputs: Dict[int, tf.Tensor],\n box_outputs: Dict[int, tf.Tensor], scales: List[float],\n min_score_thresh, max_boxes_to_draw):\n \"\"\"Post preprocessing the box/class predictions.\n\n Args:\n params: a parameter dictionary that includes `min_level`, `max_level`,\n `batch_size`, and `num_classes`.\n cls_outputs: an OrderDict with keys representing levels and values\n representing logits in [batch_size, height, width, num_anchors].\n box_outputs: an OrderDict with keys representing levels and values\n representing box regression targets in [batch_size, height, width,\n num_anchors * 4].\n scales: a list of float values indicating image scale.\n min_score_thresh: A float representing the threshold for deciding when to\n remove boxes based on score.\n max_boxes_to_draw: Max number of boxes to draw.\n\n Returns:\n detections_batch: a batch of detection results. Each detection is a tensor\n with each row representing [image_id, x, y, width, height, score, class].\n \"\"\"\n if not params['batch_size']:\n # Use combined version for dynamic batch size.\n return det_post_process_combined(params, cls_outputs, box_outputs, scales,\n min_score_thresh, max_boxes_to_draw)\n\n # TODO(tanmingxing): refactor the code to make it more explicity.\n outputs = {\n 'cls_outputs_all': [None],\n 'box_outputs_all': [None],\n 'indices_all': [None],\n 'classes_all': [None]\n }\n det_model_fn.add_metric_fn_inputs(params, cls_outputs, box_outputs, outputs,\n -1)\n\n # Create anchor_label for picking top-k predictions.\n eval_anchors = anchors.Anchors(params['min_level'], params['max_level'],\n params['num_scales'], params['aspect_ratios'],\n params['anchor_scale'], params['image_size'])\n anchor_labeler = anchors.AnchorLabeler(eval_anchors, params['num_classes'])\n\n # Add all detections for each input image.\n detections_batch = []\n for index in range(params['batch_size']):\n cls_outputs_per_sample = outputs['cls_outputs_all'][index]\n box_outputs_per_sample = outputs['box_outputs_all'][index]\n indices_per_sample = outputs['indices_all'][index]\n classes_per_sample = outputs['classes_all'][index]\n detections = anchor_labeler.generate_detections(\n cls_outputs_per_sample,\n box_outputs_per_sample,\n indices_per_sample,\n classes_per_sample,\n image_id=[index],\n image_scale=[scales[index]],\n min_score_thresh=min_score_thresh,\n max_boxes_to_draw=max_boxes_to_draw,\n disable_pyfun=params.get('disable_pyfun'))\n if params['batch_size'] > 1:\n # pad to fixed length if batch size > 1.\n padding_size = max_boxes_to_draw - tf.shape(detections)[0]\n detections = tf.pad(detections, [[0, padding_size], [0, 0]])\n detections_batch.append(detections)\n return tf.stack(detections_batch, name='detections')\n\n\ndef visualize_image(image,\n boxes,\n classes,\n scores,\n id_mapping,\n min_score_thresh=anchors.MIN_SCORE_THRESH,\n max_boxes_to_draw=anchors.MAX_DETECTIONS_PER_IMAGE,\n line_thickness=2,\n **kwargs):\n \"\"\"Visualizes a given image.\n\n Args:\n image: a image with shape [H, W, C].\n boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].\n classes: a class prediction with shape [N].\n scores: A list of float value with shape [N].\n id_mapping: a dictionary from class id to name.\n min_score_thresh: minimal score for showing. If claass probability is below\n this threshold, then the object will not show up.\n max_boxes_to_draw: maximum bounding box to draw.\n line_thickness: how thick is the bounding box line.\n **kwargs: extra parameters.\n\n Returns:\n output_image: an output image with annotated boxes and classes.\n \"\"\"\n category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}\n img = np.array(image)\n vis_utils.visualize_boxes_and_labels_on_image_array(\n img,\n boxes,\n classes,\n scores,\n category_index,\n min_score_thresh=min_score_thresh,\n max_boxes_to_draw=max_boxes_to_draw,\n line_thickness=line_thickness,\n **kwargs)\n return img\n\n\ndef parse_label_id_mapping(\n label_id_mapping: Union[Text, Dict[int, Text]] = None) -> Dict[int, Text]:\n \"\"\"Parse label id mapping from a string or a yaml file.\n\n The label_id_mapping is a dict that maps class id to its name, such as:\n\n {\n 1: \"person\",\n 2: \"dog\"\n }\n\n Args:\n label_id_mapping:\n\n Returns:\n A dictionary with key as integer id and value as a string of name.\n \"\"\"\n if label_id_mapping is None:\n return coco_id_mapping\n\n if isinstance(label_id_mapping, dict):\n label_id_dict = label_id_mapping\n elif isinstance(label_id_mapping, str):\n with tf.io.gfile.GFile(label_id_mapping) as f:\n label_id_dict = yaml.load(f, Loader=yaml.FullLoader)\n else:\n raise TypeError('label_id_mapping must be a dict or a yaml filename, '\n 'containing a mapping from class ids to class names.')\n\n return label_id_dict\n\n\ndef visualize_image_prediction(image,\n prediction,\n disable_pyfun=True,\n label_id_mapping=None,\n **kwargs):\n \"\"\"Viusalize detections on a given image.\n\n Args:\n image: Image content in shape of [height, width, 3].\n prediction: a list of vector, with each vector has the format of [image_id,\n y, x, height, width, score, class].\n disable_pyfun: disable pyfunc for faster post processing.\n label_id_mapping: a map from label id to name.\n **kwargs: extra parameters for vistualization, such as min_score_thresh,\n max_boxes_to_draw, and line_thickness.\n\n Returns:\n a list of annotated images.\n \"\"\"\n boxes = prediction[:, 1:5]\n classes = prediction[:, 6].astype(int)\n scores = prediction[:, 5]\n\n if not disable_pyfun:\n # convert [x, y, width, height] to [y, x, height, width]\n boxes[:, [0, 1, 2, 3]] = boxes[:, [1, 0, 3, 2]]\n\n label_id_mapping = label_id_mapping or coco_id_mapping\n boxes[:, 2:4] += boxes[:, 0:2]\n return visualize_image(image, boxes, classes, scores, label_id_mapping,\n **kwargs)\n\n\nclass ServingDriver(object):\n \"\"\"A driver for serving single or batch images.\n\n This driver supports serving with image files or arrays, with configurable\n batch size.\n\n Example 1. Serving streaming image contents:\n\n driver = inference.ServingDriver(\n 'efficientdet-d0', '/tmp/efficientdet-d0', batch_size=1)\n driver.build()\n for m in image_iterator():\n predictions = driver.serve_files([m])\n driver.visualize(m, predictions[0])\n # m is the new image with annotated boxes.\n\n Example 2. Serving batch image contents:\n\n imgs = []\n for f in ['/tmp/1.jpg', '/tmp/2.jpg']:\n imgs.append(np.array(Image.open(f)))\n\n driver = inference.ServingDriver(\n 'efficientdet-d0', '/tmp/efficientdet-d0', batch_size=len(imgs))\n driver.build()\n predictions = driver.serve_images(imgs)\n for i in range(len(imgs)):\n driver.visualize(imgs[i], predictions[i])\n\n Example 3: another way is to use SavedModel:\n\n # step1: export a model.\n driver = inference.ServingDriver('efficientdet-d0', '/tmp/efficientdet-d0')\n driver.build()\n driver.export('/tmp/saved_model_path')\n\n # step2: Serve a model.\n with tf.Session() as sess:\n tf.saved_model.load(sess, ['serve'], self.saved_model_dir)\n raw_images = []\n for f in tf.io.gfile.glob('/tmp/images/*.jpg'):\n raw_images.append(np.array(PIL.Image.open(f)))\n detections = sess.run('detections:0', {'image_arrays:0': raw_images})\n driver = inference.ServingDriver(\n 'efficientdet-d0', '/tmp/efficientdet-d0')\n driver.visualize(raw_images[0], detections[0])\n PIL.Image.fromarray(raw_images[0]).save(output_image_path)\n \"\"\"\n\n def __init__(self,\n model_name: Text,\n ckpt_path: Text,\n batch_size: int = 1,\n use_xla: bool = False,\n min_score_thresh: float = None,\n max_boxes_to_draw: float = None,\n line_thickness: int = None,\n model_params: Dict[Text, Any] = None):\n \"\"\"Initialize the inference driver.\n\n Args:\n model_name: target model name, such as efficientdet-d0.\n ckpt_path: checkpoint path, such as /tmp/efficientdet-d0/.\n batch_size: batch size for inference.\n use_xla: Whether run with xla optimization.\n min_score_thresh: minimal score threshold for filtering predictions.\n max_boxes_to_draw: the maximum number of boxes per image.\n line_thickness: the line thickness for drawing boxes.\n model_params: model parameters for overriding the config.\n \"\"\"\n self.model_name = model_name\n self.ckpt_path = ckpt_path\n self.batch_size = batch_size\n\n self.params = hparams_config.get_detection_config(model_name).as_dict()\n\n if model_params:\n self.params.update(model_params)\n self.params.update(dict(is_training_bn=False))\n self.label_id_mapping = parse_label_id_mapping(\n self.params.get('label_id_mapping', None))\n\n self.signitures = None\n self.sess = None\n self.disable_pyfun = True\n self.use_xla = use_xla\n\n self.min_score_thresh = min_score_thresh or anchors.MIN_SCORE_THRESH\n self.max_boxes_to_draw = (\n max_boxes_to_draw or anchors.MAX_DETECTIONS_PER_IMAGE)\n self.line_thickness = line_thickness\n\n def __del__(self):\n if self.sess:\n self.sess.close()\n\n def _build_session(self):\n sess_config = tf.ConfigProto()\n if self.use_xla:\n sess_config.graph_options.optimizer_options.global_jit_level = (\n tf.OptimizerOptions.ON_2)\n return tf.Session(config=sess_config)\n\n def build(self, params_override=None):\n \"\"\"Build model and restore checkpoints.\"\"\"\n params = copy.deepcopy(self.params)\n if params_override:\n params.update(params_override)\n\n if not self.sess:\n self.sess = self._build_session()\n with self.sess.graph.as_default():\n image_files = tf.placeholder(tf.string, name='image_files', shape=[None])\n raw_images = batch_image_files_decode(image_files)\n raw_images = tf.identity(raw_images, name='image_arrays')\n images, scales = batch_image_preprocess(raw_images, params['image_size'],\n self.batch_size)\n if params['data_format'] == 'channels_first':\n images = tf.transpose(images, [0, 3, 1, 2])\n class_outputs, box_outputs = build_model(self.model_name, images,\n **params)\n params.update(\n dict(batch_size=self.batch_size, disable_pyfun=self.disable_pyfun))\n detections = det_post_process(params, class_outputs, box_outputs, scales,\n self.min_score_thresh,\n self.max_boxes_to_draw)\n\n restore_ckpt(\n self.sess,\n self.ckpt_path,\n ema_decay=self.params['moving_average_decay'],\n export_ckpt=None)\n\n self.signitures = {\n 'image_files': image_files,\n 'image_arrays': raw_images,\n 'prediction': detections,\n }\n return self.signitures\n\n def visualize(self, image, prediction, **kwargs):\n \"\"\"Visualize prediction on image.\"\"\"\n return visualize_image_prediction(\n image,\n prediction,\n disable_pyfun=self.disable_pyfun,\n label_id_mapping=self.label_id_mapping,\n **kwargs)\n\n def serve_files(self, image_files: List[Text]):\n \"\"\"Serve a list of input image files.\n\n Args:\n image_files: a list of image files with shape [1] and type string.\n\n Returns:\n A list of detections.\n \"\"\"\n if not self.sess:\n self.build()\n predictions = self.sess.run(\n self.signitures['prediction'],\n feed_dict={self.signitures['image_files']: image_files})\n return predictions\n\n def benchmark(self, image_arrays, trace_filename=None):\n \"\"\"Benchmark inference latency/throughput.\n\n Args:\n image_arrays: a list of images in numpy array format.\n trace_filename: If None, specify the filename for saving trace.\n \"\"\"\n if not self.sess:\n self.build()\n\n # init session\n self.sess.run(\n self.signitures['prediction'],\n feed_dict={self.signitures['image_arrays']: image_arrays})\n\n start = time.perf_counter()\n for _ in range(10):\n self.sess.run(\n self.signitures['prediction'],\n feed_dict={self.signitures['image_arrays']: image_arrays})\n end = time.perf_counter()\n inference_time = (end - start) / 10\n\n print('Per batch inference time: ', inference_time)\n print('FPS: ', self.batch_size / inference_time)\n\n if trace_filename:\n run_options = tf.RunOptions()\n run_options.trace_level = tf.RunOptions.FULL_TRACE\n run_metadata = tf.RunMetadata()\n self.sess.run(\n self.signitures['prediction'],\n feed_dict={self.signitures['image_arrays']: image_arrays},\n options=run_options,\n run_metadata=run_metadata)\n with tf.io.gfile.GFile(trace_filename, 'w') as trace_file:\n from tensorflow.python.client import timeline # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top\n trace = timeline.Timeline(step_stats=run_metadata.step_stats)\n trace_file.write(trace.generate_chrome_trace_format(show_memory=True))\n\n def serve_images(self, image_arrays):\n \"\"\"Serve a list of image arrays.\n\n Args:\n image_arrays: A list of image content with each image has shape [height,\n width, 3] and uint8 type.\n\n Returns:\n A list of detections.\n \"\"\"\n if not self.sess:\n self.build()\n predictions = self.sess.run(\n self.signitures['prediction'],\n feed_dict={self.signitures['image_arrays']: image_arrays})\n return predictions\n\n def load(self, saved_model_dir_or_frozen_graph: Text):\n \"\"\"Load the model using saved model or a frozen graph.\"\"\"\n if not self.sess:\n self.sess = self._build_session()\n self.signitures = {\n 'image_files': 'image_files:0',\n 'image_arrays': 'image_arrays:0',\n 'prediction': 'detections:0',\n }\n\n # Load saved model if it is a folder.\n if tf.io.gfile.isdir(saved_model_dir_or_frozen_graph):\n return tf.saved_model.load(self.sess, ['serve'],\n saved_model_dir_or_frozen_graph)\n\n # Load a frozen graph.\n graph_def = tf.GraphDef()\n with tf.gfile.GFile(saved_model_dir_or_frozen_graph, 'rb') as f:\n graph_def.ParseFromString(f.read())\n return tf.import_graph_def(graph_def, name='')\n\n def freeze(self):\n \"\"\"Freeze the graph.\"\"\"\n output_names = [self.signitures['prediction'].op.name]\n graphdef = tf.graph_util.convert_variables_to_constants(\n self.sess, self.sess.graph_def, output_names)\n return graphdef\n\n def export(self, output_dir):\n \"\"\"Export a saved model.\"\"\"\n signitures = self.signitures\n signature_def_map = {\n 'serving_default':\n tf.saved_model.predict_signature_def(\n {signitures['image_arrays'].name: signitures['image_arrays']},\n {signitures['prediction'].name: signitures['prediction']}),\n 'serving_base64':\n tf.saved_model.predict_signature_def(\n {signitures['image_files'].name: signitures['image_files']},\n {signitures['prediction'].name: signitures['prediction']}),\n }\n b = tf.saved_model.Builder(output_dir)\n b.add_meta_graph_and_variables(\n self.sess,\n tags=['serve'],\n signature_def_map=signature_def_map,\n assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),\n clear_devices=True)\n b.save()\n logging.info('Model saved at %s', output_dir)\n\n # also save freeze pb file.\n graphdef = self.freeze()\n pb_path = os.path.join(output_dir, self.model_name + '_frozen.pb')\n tf.io.gfile.GFile(pb_path, 'wb').write(graphdef.SerializeToString())\n logging.info('Free graph saved at %s', pb_path)\n\n\nclass InferenceDriver(object):\n \"\"\"A driver for doing batch inference.\n\n Example usage:\n\n driver = inference.InferenceDriver('efficientdet-d0', '/tmp/efficientdet-d0')\n driver.inference('/tmp/*.jpg', '/tmp/outputdir')\n\n \"\"\"\n\n def __init__(self,\n model_name: Text,\n ckpt_path: Text,\n model_params: Dict[Text, Any] = None):\n \"\"\"Initialize the inference driver.\n\n Args:\n model_name: target model name, such as efficientdet-d0.\n ckpt_path: checkpoint path, such as /tmp/efficientdet-d0/.\n model_params: model parameters for overriding the config.\n \"\"\"\n self.model_name = model_name\n self.ckpt_path = ckpt_path\n self.params = hparams_config.get_detection_config(model_name).as_dict()\n if model_params:\n self.params.update(model_params)\n self.params.update(dict(is_training_bn=False))\n self.label_id_mapping = parse_label_id_mapping(\n self.params.get('label_id_mapping', None))\n\n self.disable_pyfun = True\n\n def inference(self, image_path_pattern: Text, output_dir: Text, **kwargs):\n \"\"\"Read and preprocess input images.\n\n Args:\n image_path_pattern: Image file pattern such as /tmp/img*.jpg\n output_dir: the directory for output images. Output images will be named\n as 0.jpg, 1.jpg, ....\n **kwargs: extra parameters for for vistualization, such as\n min_score_thresh, max_boxes_to_draw, and line_thickness.\n\n Returns:\n Annotated image.\n \"\"\"\n params = copy.deepcopy(self.params)\n with tf.Session() as sess:\n # Buid inputs and preprocessing.\n raw_images, images, scales = build_inputs(image_path_pattern,\n params['image_size'])\n if params['data_format'] == 'channels_first':\n images = tf.transpose(images, [0, 3, 1, 2])\n # Build model.\n class_outputs, box_outputs = build_model(self.model_name, images,\n **self.params)\n restore_ckpt(\n sess,\n self.ckpt_path,\n ema_decay=self.params['moving_average_decay'],\n export_ckpt=None)\n\n # for postprocessing.\n params.update(\n dict(batch_size=len(raw_images), disable_pyfun=self.disable_pyfun))\n\n # Build postprocessing.\n detections_batch = det_post_process(\n params,\n class_outputs,\n box_outputs,\n scales,\n min_score_thresh=kwargs.get('min_score_thresh',\n anchors.MIN_SCORE_THRESH),\n max_boxes_to_draw=kwargs.get('max_boxes_to_draw',\n anchors.MAX_DETECTIONS_PER_IMAGE))\n predictions = sess.run(detections_batch)\n # Visualize results.\n for i, prediction in enumerate(predictions):\n img = visualize_image_prediction(\n raw_images[i],\n prediction,\n disable_pyfun=self.disable_pyfun,\n label_id_mapping=self.label_id_mapping,\n **kwargs)\n output_image_path = os.path.join(output_dir, str(i) + '.jpg')\n Image.fromarray(img).save(output_image_path)\n logging.info('writing file to %s', output_image_path)\n\n return predictions\n" ]
[ [ "tensorflow.compat.v1.RunOptions", "tensorflow.compat.v1.image.combined_non_max_suppression", "tensorflow.python.client.timeline.Timeline", "tensorflow.compat.v1.expand_dims", "tensorflow.compat.v1.shape", "tensorflow.compat.v1.graph_util.convert_variables_to_constants", "tensorflow.compat.v1.TensorArray", "tensorflow.compat.v1.placeholder", "tensorflow.compat.v1.train.Saver", "tensorflow.compat.v1.math.sigmoid", "tensorflow.compat.v1.gfile.GFile", "tensorflow.compat.v1.saved_model.load", "tensorflow.compat.v1.cast", "tensorflow.compat.v1.GraphDef", "tensorflow.compat.v1.stack", "tensorflow.compat.v1.train.ExponentialMovingAverage", "tensorflow.compat.v1.RunMetadata", "tensorflow.compat.v1.io.gfile.glob", "tensorflow.compat.v1.reshape", "tensorflow.compat.v1.pad", "tensorflow.compat.v1.Session", "tensorflow.compat.v1.transpose", "tensorflow.compat.v1.io.decode_image", "tensorflow.compat.v1.concat", "tensorflow.compat.v1.map_fn", "tensorflow.compat.v1.global_variables_initializer", "tensorflow.compat.v1.saved_model.Builder", "tensorflow.compat.v1.range", "tensorflow.compat.v1.ConfigProto", "tensorflow.compat.v1.io.gfile.isdir", "tensorflow.compat.v1.train.latest_checkpoint", "tensorflow.compat.v1.identity", "tensorflow.compat.v1.import_graph_def", "tensorflow.compat.v1.saved_model.predict_signature_def", "tensorflow.compat.v1.io.gfile.GFile", "numpy.array", "tensorflow.compat.v1.train.get_or_create_global_step", "tensorflow.compat.v1.get_collection" ] ]
marcelodiaz558/BasicSR
[ "1d5138ed567e966965fd1540838d27e6f5082b70" ]
[ "basicsr/ops/upfirdn2d/upfirdn2d.py" ]
[ "# modify from https://github.com/rosinality/stylegan2-pytorch/blob/master/op/upfirdn2d.py # noqa:E501\n\nimport os\nimport torch\nfrom torch.autograd import Function\nfrom torch.nn import functional as F\n\nBASICSR_JIT = os.getenv('BASICSR_JIT')\nif BASICSR_JIT == 'True':\n from torch.utils.cpp_extension import load\n module_path = os.path.dirname(__file__)\n upfirdn2d_ext = load(\n 'upfirdn2d',\n sources=[\n os.path.join(module_path, 'src', 'upfirdn2d.cpp'),\n os.path.join(module_path, 'src', 'upfirdn2d_kernel.cu'),\n ],\n )\nelse:\n try:\n from . import upfirdn2d_ext\n except ImportError:\n pass\n # avoid annoying print output\n # print(f'Cannot import deform_conv_ext. Error: {error}. You may need to: \\n '\n # '1. compile with BASICSR_EXT=True. or\\n '\n # '2. set BASICSR_JIT=True during running')\n\n\nclass UpFirDn2dBackward(Function):\n\n @staticmethod\n def forward(ctx, grad_output, kernel, grad_kernel, up, down, pad, g_pad, in_size, out_size):\n\n up_x, up_y = up\n down_x, down_y = down\n g_pad_x0, g_pad_x1, g_pad_y0, g_pad_y1 = g_pad\n\n grad_output = grad_output.reshape(-1, out_size[0], out_size[1], 1)\n\n grad_input = upfirdn2d_ext.upfirdn2d(\n grad_output,\n grad_kernel,\n down_x,\n down_y,\n up_x,\n up_y,\n g_pad_x0,\n g_pad_x1,\n g_pad_y0,\n g_pad_y1,\n )\n grad_input = grad_input.view(in_size[0], in_size[1], in_size[2], in_size[3])\n\n ctx.save_for_backward(kernel)\n\n pad_x0, pad_x1, pad_y0, pad_y1 = pad\n\n ctx.up_x = up_x\n ctx.up_y = up_y\n ctx.down_x = down_x\n ctx.down_y = down_y\n ctx.pad_x0 = pad_x0\n ctx.pad_x1 = pad_x1\n ctx.pad_y0 = pad_y0\n ctx.pad_y1 = pad_y1\n ctx.in_size = in_size\n ctx.out_size = out_size\n\n return grad_input\n\n @staticmethod\n def backward(ctx, gradgrad_input):\n kernel, = ctx.saved_tensors\n\n gradgrad_input = gradgrad_input.reshape(-1, ctx.in_size[2], ctx.in_size[3], 1)\n\n gradgrad_out = upfirdn2d_ext.upfirdn2d(\n gradgrad_input,\n kernel,\n ctx.up_x,\n ctx.up_y,\n ctx.down_x,\n ctx.down_y,\n ctx.pad_x0,\n ctx.pad_x1,\n ctx.pad_y0,\n ctx.pad_y1,\n )\n # gradgrad_out = gradgrad_out.view(ctx.in_size[0], ctx.out_size[0],\n # ctx.out_size[1], ctx.in_size[3])\n gradgrad_out = gradgrad_out.view(ctx.in_size[0], ctx.in_size[1], ctx.out_size[0], ctx.out_size[1])\n\n return gradgrad_out, None, None, None, None, None, None, None, None\n\n\nclass UpFirDn2d(Function):\n\n @staticmethod\n def forward(ctx, input, kernel, up, down, pad):\n up_x, up_y = up\n down_x, down_y = down\n pad_x0, pad_x1, pad_y0, pad_y1 = pad\n\n kernel_h, kernel_w = kernel.shape\n batch, channel, in_h, in_w = input.shape\n ctx.in_size = input.shape\n\n input = input.reshape(-1, in_h, in_w, 1)\n\n ctx.save_for_backward(kernel, torch.flip(kernel, [0, 1]))\n\n out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1\n out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1\n ctx.out_size = (out_h, out_w)\n\n ctx.up = (up_x, up_y)\n ctx.down = (down_x, down_y)\n ctx.pad = (pad_x0, pad_x1, pad_y0, pad_y1)\n\n g_pad_x0 = kernel_w - pad_x0 - 1\n g_pad_y0 = kernel_h - pad_y0 - 1\n g_pad_x1 = in_w * up_x - out_w * down_x + pad_x0 - up_x + 1\n g_pad_y1 = in_h * up_y - out_h * down_y + pad_y0 - up_y + 1\n\n ctx.g_pad = (g_pad_x0, g_pad_x1, g_pad_y0, g_pad_y1)\n\n out = upfirdn2d_ext.upfirdn2d(input, kernel, up_x, up_y, down_x, down_y, pad_x0, pad_x1, pad_y0, pad_y1)\n # out = out.view(major, out_h, out_w, minor)\n out = out.view(-1, channel, out_h, out_w)\n\n return out\n\n @staticmethod\n def backward(ctx, grad_output):\n kernel, grad_kernel = ctx.saved_tensors\n\n grad_input = UpFirDn2dBackward.apply(\n grad_output,\n kernel,\n grad_kernel,\n ctx.up,\n ctx.down,\n ctx.pad,\n ctx.g_pad,\n ctx.in_size,\n ctx.out_size,\n )\n\n return grad_input, None, None, None, None\n\n\ndef upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):\n if input.device.type == 'cpu':\n out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1], pad[0], pad[1])\n else:\n out = UpFirDn2d.apply(input, kernel, (up, up), (down, down), (pad[0], pad[1], pad[0], pad[1]))\n\n return out\n\n\ndef upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0, pad_x1, pad_y0, pad_y1):\n _, channel, in_h, in_w = input.shape\n input = input.reshape(-1, in_h, in_w, 1)\n\n _, in_h, in_w, minor = input.shape\n kernel_h, kernel_w = kernel.shape\n\n out = input.view(-1, in_h, 1, in_w, 1, minor)\n out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])\n out = out.view(-1, in_h * up_y, in_w * up_x, minor)\n\n out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0), max(pad_y1, 0)])\n out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :, ]\n\n out = out.permute(0, 3, 1, 2)\n out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x + pad_x0 + pad_x1])\n w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)\n out = F.conv2d(out, w)\n out = out.reshape(\n -1,\n minor,\n in_h * up_y + pad_y0 + pad_y1 - kernel_h + 1,\n in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1,\n )\n out = out.permute(0, 2, 3, 1)\n out = out[:, ::down_y, ::down_x, :]\n\n out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1\n out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1\n\n return out.view(-1, channel, out_h, out_w)\n" ]
[ [ "torch.nn.functional.conv2d", "torch.nn.functional.pad", "torch.flip" ] ]
tdiethe/MXFusion
[ "fbdba79ca85cb5a9760722ffd932b9ec4c401745" ]
[ "testing/distributions/gp/cond_gp_test.py" ]
[ "import pytest\nimport mxnet as mx\nimport numpy as np\nfrom mxfusion.models import Model\nfrom mxfusion.components.variables.runtime_variable import is_sampled_array, get_num_samples\nfrom mxfusion.components.distributions import ConditionalGaussianProcess\nfrom mxfusion.components.distributions.gp.kernels import RBF\nfrom mxfusion.components.variables import Variable\nfrom mxfusion.util.testutils import prepare_mxnet_array\nfrom mxfusion.util.testutils import MockMXNetRandomGenerator\nfrom scipy.stats import multivariate_normal\nimport matplotlib\nmatplotlib.use('Agg')\nimport GPy\n\n\[email protected](\"set_seed\")\nclass TestConditionalGaussianProcessDistribution(object):\n\n @pytest.mark.parametrize(\"dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv, rv_isSamples, num_samples\", [\n (np.float64, np.random.rand(5,2), False, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, np.random.rand(3,5,1), True, 3),\n (np.float64, np.random.rand(3,5,2), True, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, np.random.rand(5,1), False, 3),\n (np.float64, np.random.rand(3,5,2), True, np.random.rand(8,2), False, np.random.rand(3,8,1), True, np.random.rand(3,2)+0.1, True, np.random.rand(3,1)+0.1, True, np.random.rand(3,5,1), True, 3),\n (np.float64, np.random.rand(5,2), False, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, np.random.rand(5,1), False, 1),\n ])\n def test_log_pdf(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples,\n rv, rv_isSamples, num_samples):\n from scipy.linalg.lapack import dtrtrs\n X_mx = prepare_mxnet_array(X, X_isSamples, dtype)\n X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype)\n Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype)\n rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype)\n rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype)\n rv_mx = prepare_mxnet_array(rv, rv_isSamples, dtype)\n rv_shape = rv.shape[1:] if rv_isSamples else rv.shape\n\n rbf = RBF(2, True, 1., 1., 'rbf', None, dtype)\n X_var = Variable(shape=(5,2))\n X_cond_var = Variable(shape=(8,2))\n Y_cond_var = Variable(shape=(8,1))\n gp = ConditionalGaussianProcess.define_variable(X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype).factor\n\n variables = {gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx, gp.random_variable.uuid: rv_mx}\n log_pdf_rt = gp.log_pdf(F=mx.nd, variables=variables).asnumpy()\n\n log_pdf_np = []\n for i in range(num_samples):\n X_i = X[i] if X_isSamples else X\n X_cond_i = X_cond[i] if X_cond_isSamples else X_cond\n Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond\n lengthscale_i = rbf_lengthscale[i] if rbf_lengthscale_isSamples else rbf_lengthscale\n variance_i = rbf_variance[i] if rbf_variance_isSamples else rbf_variance\n rv_i = rv[i] if rv_isSamples else rv\n rbf_np = GPy.kern.RBF(input_dim=2, ARD=True)\n rbf_np.lengthscale = lengthscale_i\n rbf_np.variance = variance_i\n K_np = rbf_np.K(X_i)\n Kc_np = rbf_np.K(X_cond_i, X_i)\n Kcc_np = rbf_np.K(X_cond_i)\n\n L = np.linalg.cholesky(Kcc_np)\n LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0]\n LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0]\n\n mu = LinvKxt.T.dot(LInvY)\n cov = K_np - LinvKxt.T.dot(LinvKxt)\n log_pdf_np.append(multivariate_normal.logpdf(rv_i[:,0], mean=mu[:,0], cov=cov))\n log_pdf_np = np.array(log_pdf_np)\n isSamples_any = any([X_isSamples, rbf_lengthscale_isSamples, rbf_variance_isSamples, rv_isSamples])\n assert np.issubdtype(log_pdf_rt.dtype, dtype)\n assert is_sampled_array(mx.nd, log_pdf_rt) == isSamples_any\n if isSamples_any:\n assert get_num_samples(mx.nd, log_pdf_rt) == num_samples\n assert np.allclose(log_pdf_np, log_pdf_rt)\n\n @pytest.mark.parametrize(\"dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv_shape, num_samples\", [\n (np.float64, np.random.rand(5,2), False, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, (5,1), 3),\n (np.float64, np.random.rand(3,5,2), True, np.random.rand(3,8,2), True, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, (5,1), 3),\n (np.float64, np.random.rand(3,5,2), True, np.random.rand(3,8,2), True, np.random.rand(3,8,1), True, np.random.rand(3,2)+0.1, True, np.random.rand(3,1)+0.1, True, (5,1), 3),\n (np.float64, np.random.rand(5,2), False, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, (5,1), 1),\n ])\n def test_draw_samples(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples,\n rv_shape, num_samples):\n from scipy.linalg.lapack import dtrtrs\n X_mx = prepare_mxnet_array(X, X_isSamples, dtype)\n X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype)\n Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype)\n rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype)\n rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype)\n\n rand = np.random.randn(num_samples, *rv_shape)\n rand_gen = MockMXNetRandomGenerator(mx.nd.array(rand.flatten(), dtype=dtype))\n\n rbf = RBF(2, True, 1., 1., 'rbf', None, dtype)\n X_var = Variable(shape=(5,2))\n X_cond_var = Variable(shape=(8,2))\n Y_cond_var = Variable(shape=(8,1))\n gp = ConditionalGaussianProcess.define_variable(X=X_var, X_cond=X_cond_var, Y_cond=Y_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype, rand_gen=rand_gen).factor\n\n variables = {gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx}\n samples_rt = gp.draw_samples(F=mx.nd, variables=variables, num_samples=num_samples).asnumpy()\n\n samples_np = []\n for i in range(num_samples):\n X_i = X[i] if X_isSamples else X\n X_cond_i = X_cond[i] if X_cond_isSamples else X_cond\n Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond\n lengthscale_i = rbf_lengthscale[i] if rbf_lengthscale_isSamples else rbf_lengthscale\n variance_i = rbf_variance[i] if rbf_variance_isSamples else rbf_variance\n rand_i = rand[i]\n rbf_np = GPy.kern.RBF(input_dim=2, ARD=True)\n rbf_np.lengthscale = lengthscale_i\n rbf_np.variance = variance_i\n K_np = rbf_np.K(X_i)\n Kc_np = rbf_np.K(X_cond_i, X_i)\n Kcc_np = rbf_np.K(X_cond_i)\n\n L = np.linalg.cholesky(Kcc_np)\n LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0]\n LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0]\n\n mu = LinvKxt.T.dot(LInvY)\n cov = K_np - LinvKxt.T.dot(LinvKxt)\n L_cov_np = np.linalg.cholesky(cov)\n sample_np = mu + L_cov_np.dot(rand_i)\n samples_np.append(sample_np)\n samples_np = np.array(samples_np)\n assert np.issubdtype(samples_rt.dtype, dtype)\n assert get_num_samples(mx.nd, samples_rt) == num_samples\n print(samples_np, samples_rt)\n assert np.allclose(samples_np, samples_rt)\n\n @pytest.mark.parametrize(\"dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples, rv, rv_isSamples, num_samples\", [\n (np.float64, np.random.rand(5,2), False, np.random.rand(8,2), False, np.random.rand(8,1), False, np.random.rand(2)+0.1, False, np.random.rand(1)+0.1, False, np.random.rand(3,5,1), True, 3),\n ])\n def test_clone_cond_gp(self, dtype, X, X_isSamples, X_cond, X_cond_isSamples, Y_cond, Y_cond_isSamples, rbf_lengthscale, rbf_lengthscale_isSamples, rbf_variance, rbf_variance_isSamples,\n rv, rv_isSamples, num_samples):\n from scipy.linalg.lapack import dtrtrs\n X_mx = prepare_mxnet_array(X, X_isSamples, dtype)\n X_cond_mx = prepare_mxnet_array(X_cond, X_cond_isSamples, dtype)\n Y_cond_mx = prepare_mxnet_array(Y_cond, Y_cond_isSamples, dtype)\n rbf_lengthscale_mx = prepare_mxnet_array(rbf_lengthscale, rbf_lengthscale_isSamples, dtype)\n rbf_variance_mx = prepare_mxnet_array(rbf_variance, rbf_variance_isSamples, dtype)\n rv_mx = prepare_mxnet_array(rv, rv_isSamples, dtype)\n rv_shape = rv.shape[1:] if rv_isSamples else rv.shape\n\n rbf = RBF(2, True, 1., 1., 'rbf', None, dtype)\n m = Model()\n m.X_var = Variable(shape=(5,2))\n m.X_cond_var = Variable(shape=(8,2))\n m.Y_cond_var = Variable(shape=(8,1))\n m.Y = ConditionalGaussianProcess.define_variable(X=m.X_var, X_cond=m.X_cond_var, Y_cond=m.Y_cond_var, kernel=rbf, shape=rv_shape, dtype=dtype)\n\n gp = m.clone()[0].Y.factor\n\n variables = {gp.X.uuid: X_mx, gp.X_cond.uuid: X_cond_mx, gp.Y_cond.uuid: Y_cond_mx, gp.rbf_lengthscale.uuid: rbf_lengthscale_mx, gp.rbf_variance.uuid: rbf_variance_mx, gp.random_variable.uuid: rv_mx}\n log_pdf_rt = gp.log_pdf(F=mx.nd, variables=variables).asnumpy()\n\n log_pdf_np = []\n for i in range(num_samples):\n X_i = X[i] if X_isSamples else X\n X_cond_i = X_cond[i] if X_cond_isSamples else X_cond\n Y_cond_i = Y_cond[i] if Y_cond_isSamples else Y_cond\n lengthscale_i = rbf_lengthscale[i] if rbf_lengthscale_isSamples else rbf_lengthscale\n variance_i = rbf_variance[i] if rbf_variance_isSamples else rbf_variance\n rv_i = rv[i] if rv_isSamples else rv\n rbf_np = GPy.kern.RBF(input_dim=2, ARD=True)\n rbf_np.lengthscale = lengthscale_i\n rbf_np.variance = variance_i\n K_np = rbf_np.K(X_i)\n Kc_np = rbf_np.K(X_cond_i, X_i)\n Kcc_np = rbf_np.K(X_cond_i)\n\n L = np.linalg.cholesky(Kcc_np)\n LInvY = dtrtrs(L, Y_cond_i, lower=1, trans=0)[0]\n LinvKxt = dtrtrs(L, Kc_np, lower=1, trans=0)[0]\n\n mu = LinvKxt.T.dot(LInvY)\n cov = K_np - LinvKxt.T.dot(LinvKxt)\n log_pdf_np.append(multivariate_normal.logpdf(rv_i[:,0], mean=mu[:,0], cov=cov))\n log_pdf_np = np.array(log_pdf_np)\n isSamples_any = any([X_isSamples, rbf_lengthscale_isSamples, rbf_variance_isSamples, rv_isSamples])\n assert np.issubdtype(log_pdf_rt.dtype, dtype)\n assert is_sampled_array(mx.nd, log_pdf_rt) == isSamples_any\n if isSamples_any:\n assert get_num_samples(mx.nd, log_pdf_rt) == num_samples\n assert np.allclose(log_pdf_np, log_pdf_rt)\n" ]
[ [ "numpy.allclose", "scipy.linalg.lapack.dtrtrs", "numpy.issubdtype", "numpy.random.randn", "scipy.stats.multivariate_normal.logpdf", "numpy.linalg.cholesky", "numpy.random.rand", "matplotlib.use", "numpy.array" ] ]
irfankhan10/pytket-extensions
[ "8ab33c1dcff91dfc50d471fbc160277e82d2492b" ]
[ "modules/pytket-aqt/tests/convert_test.py" ]
[ "# Copyright 2020-2021 Cambridge Quantum Computing\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Tuple, List\nimport json\nimport os\nimport numpy as np\nimport pytest\nfrom pytket.circuit import Circuit, OpType # type: ignore\nfrom pytket.extensions.aqt.backends.aqt import _translate_aqt, AQTBackend, _aqt_rebase\n\nskip_remote_tests: bool = os.getenv(\"PYTKET_RUN_REMOTE_TESTS\") is None\nREASON = \"PYTKET_RUN_REMOTE_TESTS not set (requires configuration of AQT access token)\"\n\n\ndef tk_to_aqt(circ: Circuit) -> Tuple[List[List], str]:\n \"\"\"Convert a circuit to AQT list representation\"\"\"\n c = circ.copy()\n AQTBackend(device_name=\"sim/noise-model-1\").default_compilation_pass().apply(c)\n return _translate_aqt(c)\n\n\[email protected](skip_remote_tests, reason=REASON)\ndef test_convert() -> None:\n circ = Circuit(4, 4)\n circ.H(0).CX(0, 1)\n circ.add_gate(OpType.noop, [1])\n circ.CRz(0.5, 1, 2)\n circ.add_barrier([2])\n circ.ZZPhase(0.3, 2, 3).CX(3, 0).Tdg(1)\n circ.Measure(0, 0)\n circ.Measure(1, 2)\n circ.Measure(2, 3)\n circ.Measure(3, 1)\n\n circ_aqt = tk_to_aqt(circ)\n assert json.loads(circ_aqt[1]) == [0, 3, 1, 2]\n assert all(gate[0] in [\"X\", \"Y\", \"MS\"] for gate in circ_aqt[0])\n\n\ndef test_rebase_CX() -> None:\n circ = Circuit(2)\n circ.CX(0, 1)\n orig_circ = circ.copy()\n\n _aqt_rebase().apply(circ)\n\n u1 = orig_circ.get_unitary()\n u2 = circ.get_unitary()\n\n assert np.allclose(u1, u2)\n\n\ndef test_rebase_singleq() -> None:\n circ = Circuit(1)\n # some arbitrary unitary\n circ.add_gate(OpType.U3, [0.01231, 0.848, 38.200], [0])\n orig_circ = circ.copy()\n\n _aqt_rebase().apply(circ)\n\n u1 = orig_circ.get_unitary()\n u2 = circ.get_unitary()\n\n assert np.allclose(u1, u2)\n\n\ndef test_rebase_large() -> None:\n circ = Circuit(3)\n # some arbitrary unitary\n circ.Rx(0.21, 0).Rz(0.12, 1).Rz(8.2, 2).X(2).CX(0, 1).CX(1, 2).Rz(0.44, 1).Rx(\n 0.43, 0\n )\n orig_circ = circ.copy()\n\n _aqt_rebase().apply(circ)\n\n u1 = orig_circ.get_unitary()\n u2 = circ.get_unitary()\n\n assert np.allclose(u1, u2)\n" ]
[ [ "numpy.allclose" ] ]
Ipuch/Humanoid2D
[ "3afd3926b9fb2ddc39be9bef99b89f864f41dcb7", "3afd3926b9fb2ddc39be9bef99b89f864f41dcb7" ]
[ "examples/walk_integration.py", "examples/walk10Dof.py" ]
[ "import biorbd\nimport numpy as np\n\nfrom bioptim import OdeSolver, CostType, RigidBodyDynamics\nfrom bioptim import Solver, DefectType\n\nfrom humanoid_2d import Humanoid2D, Integration, add_custom_plots, HumanoidOcp, HumanoidOcpMultiPhase\n\n\ndef torque_driven_dynamics(model: biorbd.Model, states: np.array, controls: np.array, params: np.array):\n q = states[: model.nbQ()]\n qdot = states[model.nbQ() :]\n tau = controls\n qddot = model.ForwardDynamicsConstraintsDirect(q, qdot, tau).to_array()\n return np.hstack((qdot, qddot))\n\n\ndef main():\n n_shooting = 30\n ode_solver = OdeSolver.RK4()\n # ode_solver = OdeSolver.COLLOCATION()\n time = 0.3\n n_threads = 8\n # for human in Humanoid2D:\n human = Humanoid2D.HUMANOID_3DOF\n model_path = human\n print(human)\n # --- Solve the program --- #\n humanoid = HumanoidOcpMultiPhase(\n biorbd_model_path=model_path.value,\n phase_time=time,\n n_shooting=n_shooting,\n ode_solver=ode_solver,\n rigidbody_dynamics=RigidBodyDynamics.ODE,\n n_threads=n_threads,\n nb_phases=1,\n )\n\n add_custom_plots(humanoid.ocp)\n humanoid.ocp.add_plot_penalty(CostType.ALL)\n # humanoid.ocp.print()\n\n solv = Solver.IPOPT(show_online_optim=False, show_options=dict(show_bounds=True))\n solv.set_maximum_iterations(1000)\n solv.set_linear_solver(\"ma57\")\n solv.set_print_level(5)\n sol = humanoid.ocp.solve(solv)\n\n # --- Show results --- #\n print(sol.status)\n sol.print_cost()\n\n from bioptim import Shooting, SolutionIntegrator\n\n sol.integrate(\n shooting_type=Shooting.SINGLE_CONTINUOUS,\n keep_intermediate_points=False,\n merge_phases=False,\n continuous=True,\n integrator=SolutionIntegrator.SCIPY_DOP853,\n )\n print(sol.states[\"q\"])\n\n integration = Integration(\n ocp=humanoid.ocp, solution=sol, state_keys=[\"q\", \"qdot\"], control_keys=[\"tau\"], function=torque_driven_dynamics\n )\n\n out = integration.integrate(\n shooting_type=Shooting.SINGLE_CONTINUOUS,\n keep_intermediate_points=False,\n merge_phases=False,\n continuous=True,\n integrator=SolutionIntegrator.SCIPY_DOP853,\n )\n print(out.states[\"q\"])\n\n print(sol.states[\"q\"] - out.states[\"q\"])\n\n import matplotlib.pyplot as plt\n\n plt.figure(1)\n plt.plot(sol.states[\"q\"][0, :])\n plt.plot(out.states[\"q\"][0, :])\n plt.show()\n # plot in red\n\n # ça plante pas à vérifier ;)\n\n\nif __name__ == \"__main__\":\n main()\n", "import numpy as np\nimport biorbd_casadi as biorbd\n\nfrom bioptim import (\n OptimalControlProgram,\n DynamicsFcn,\n DynamicsList,\n Bounds,\n QAndQDotBounds,\n InitialGuess,\n ObjectiveFcn,\n ObjectiveList,\n ConstraintList,\n ConstraintFcn,\n InterpolationType,\n Node,\n BoundsList,\n OdeSolver,\n Solver,\n CostType,\n PhaseTransitionList,\n PhaseTransitionFcn,\n)\n\n\ndef prepare_ocp(\n biorbd_model_path: str,\n final_time: float,\n n_shooting: int,\n ode_solver: OdeSolver = OdeSolver.RK4(),\n use_sx: bool = False,\n n_threads: int = 1,\n implicit_dynamics: bool = False,\n) -> OptimalControlProgram:\n \"\"\"\n The initialization of an ocp\n\n Parameters\n ----------\n biorbd_model_path: str\n The path to the biorbd model\n final_time: float\n The time in second required to perform the task\n n_shooting: int\n The number of shooting points to define int the direct multiple shooting program\n ode_solver: OdeSolver = OdeSolver.RK4()\n Which type of OdeSolver to use\n use_sx: bool\n If the SX variable should be used instead of MX (can be extensive on RAM)\n n_threads: int\n The number of threads to use in the paralleling (1 = no parallel computing)\n implicit_dynamics: bool\n implicit\n Returns\n -------\n The OptimalControlProgram ready to be solved\n \"\"\"\n\n model = biorbd.Model(biorbd_model_path)\n n_q = model.nbQ()\n n_qdot = model.nbQdot()\n n_tau = model.nbGeneralizedTorque()\n tau_min, tau_max, tau_init = -400, 400, 0\n\n # --- Dynamics --- #\n dynamics = DynamicsList()\n dynamics.add(DynamicsFcn.TORQUE_DRIVEN, with_contact=True, phase=0)\n\n # --- Objective function --- #\n objective_functions = ObjectiveList()\n objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_CONTROL, key=\"tau\", phase=0)\n\n # torso stability\n objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_QDDOT, phase=0, index=[0, 1, 2], weight=0.01)\n # head stability\n objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_QDDOT, derivative=True, phase=0, index=3, weight=0.01)\n objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_STATE, key=\"qdot\", phase=0, index=3, weight=0.01)\n\n # keep velocity CoM around 1.5 m/s\n objective_functions.add(ObjectiveFcn.Mayer.MINIMIZE_COM_VELOCITY, index=1, target=1.5, node=Node.START, weight=1000)\n objective_functions.add(ObjectiveFcn.Mayer.MINIMIZE_COM_VELOCITY, index=1, target=1.5, node=Node.END, weight=1000)\n\n # instead of phase transition\n objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_COM_VELOCITY, index=2, weight=0.1)\n\n # --- Constraints --- #\n constraints = ConstraintList()\n # Contact force in Z are positive\n constraints.add(\n ConstraintFcn.TRACK_CONTACT_FORCES, min_bound=0, max_bound=np.inf, node=Node.ALL, contact_index=1, phase=0\n ) # FP0 > 0 en Z\n\n # contact node at zero position and zero speed\n constraints.add(ConstraintFcn.TRACK_MARKERS, node=Node.START, marker_index=\"RFoot\", phase=0)\n constraints.add(ConstraintFcn.TRACK_MARKERS_VELOCITY, node=Node.START, marker_index=\"RFoot\", phase=0)\n\n # first and last step constraints\n constraints.add(\n ConstraintFcn.TRACK_MARKERS, target=np.array([0, -0.4, 0]), node=Node.START, marker_index=\"LFoot\", phase=0\n )\n # Ensure lift of foot\n constraints.add(\n ConstraintFcn.TRACK_MARKERS,\n index=2,\n min_bound=0.05,\n max_bound=np.inf,\n node=Node.MID,\n marker_index=\"LFoot\",\n phase=0,\n )\n constraints.add(\n ConstraintFcn.TRACK_MARKERS, target=np.array([0, 0.4, 0]), node=Node.END, marker_index=\"LFoot\", phase=0\n )\n\n phase_transitions = PhaseTransitionList()\n phase_transitions.add(PhaseTransitionFcn.CYCLIC, index=[0, 1, 2, 3], weight=1000) # key=\"q\"\n # phase_transitions.add(custom_phase_transition, phase_pre_idx=2, coef=0.5)\n\n x_bounds = BoundsList()\n x_bounds.add(bounds=QAndQDotBounds(model))\n\n x_bounds[0][n_q + 3, 0] = 0 # head velocity zero at the beginning\n x_bounds[0][n_q + 3, -1] = 0 # head velocity zero at the end\n x_bounds[0].max[2, :] = 0 # torso bended forward\n x_bounds[0].min[n_q - 2 : n_q, 0] = -np.pi / 8 # driving knees\n\n # Supervised shoulders\n i = 1 # 1 if head\n x_bounds[0][5 + i, 0] = -np.pi / 6\n x_bounds[0][6 + i, 0] = np.pi / 6\n x_bounds[0][5 + i, -1] = np.pi / 6\n x_bounds[0][6 + i, -1] = -np.pi / 6\n\n x_bounds[0][5 + i + n_q, 0] = 0\n x_bounds[0][5 + i + n_q, -1] = 0\n x_bounds[0][6 + i + n_q, 0] = 0\n x_bounds[0][6 + i + n_q, -1] = 0\n\n # Unsupervised arms not working trying another time with cyclic constraints\n # x_bounds[0].max[5, 0] = -1e-5 # position is negative at start\n # x_bounds[0].min[6, 0] = 1e-5 # position is positive at start\n #\n # x_bounds[0].min[5, -1] = 1e-5 # position is positive at the end\n # x_bounds[0].max[6, -1] = -1e-5 # position is negative at the end\n #\n # x_bounds[0][n_q + 5, [0, -1]] = 0 # velocity of shoulders zero at begining and end\n # x_bounds[0][n_q + 6, [0, -1]] = 0 # velocity of shoulders zero at begining and end\n # x_bounds[0].max[n_q + 6, 1] = -1e-5 # velocity of left shoulder negative\n # x_bounds[0].min[n_q + 6, 1] = -5 # velocity of left shoulder negative\n # x_bounds[0].min[n_q + 5, 1] = 1e-5 # velocity of right shoulder positive\n # x_bounds[0].max[n_q + 5, 1] = 5 # velocity of right shoulder positive\n\n u_bounds = BoundsList()\n u_bounds.add([tau_min] * n_tau, [tau_max] * n_tau)\n # root is not actuated\n u_bounds[0][:3, :] = 0\n\n # --- Initial guess --- #\n q0 = [0] * n_q\n # Torso over the floor and bent\n q0[1] = 0.8\n q0[2] = -3.14 / 6\n qdot0 = [0] * n_qdot\n X0 = []\n X0.extend(q0)\n X0.extend(qdot0)\n x_init = InitialGuess(X0, interpolation=InterpolationType.CONSTANT)\n u_init = InitialGuess([tau_init] * n_tau)\n\n return OptimalControlProgram(\n biorbd_model=model,\n dynamics=dynamics,\n n_shooting=n_shooting,\n ode_solver=ode_solver,\n phase_time=final_time,\n x_init=x_init,\n u_init=u_init,\n x_bounds=x_bounds,\n u_bounds=u_bounds,\n objective_functions=objective_functions,\n constraints=constraints,\n phase_transitions=phase_transitions,\n use_sx=use_sx,\n n_threads=n_threads,\n )\n\n\ndef main():\n model_path = \"models/Humanoid10Dof.bioMod\"\n n_shooting = 10\n ode_solver = OdeSolver.RK4(n_integration_steps=5)\n # ode_solver = OdeSolver.COLLOCATION()\n time = 0.3\n n_threads = 8\n # --- Solve the program --- #\n\n ocp = prepare_ocp(\n biorbd_model_path=model_path,\n final_time=time,\n n_shooting=n_shooting,\n ode_solver=ode_solver,\n implicit_dynamics=False,\n n_threads=n_threads,\n )\n # ocp.print(to_graph=True)\n # ocp.add_plot_penalty(CostType.ALL)\n\n # Plot CoM pos and velocity\n for i, nlp in enumerate(ocp.nlp):\n ocp.add_plot(\n \"CoM\", lambda t, x, u, p: plot_com(x, nlp), phase=i, legend=[\"CoMy\", \"Comz\", \"CoM_doty\", \"CoM_dotz\"]\n )\n\n solv = Solver.IPOPT(show_online_optim=True, show_options=dict(show_bounds=True))\n sol = ocp.solve(solv)\n\n # --- Show results --- #\n sol.print()\n sol.animate()\n sol.graphs(show_bounds=True)\n\n\ndef plot_com(x, nlp):\n com_func = biorbd.to_casadi_func(\"CoMPlot\", nlp.model.CoM, nlp.states[\"q\"].mx, expand=False)\n com_dot_func = biorbd.to_casadi_func(\n \"Compute_CoM\", nlp.model.CoMdot, nlp.states[\"q\"].mx, nlp.states[\"qdot\"].mx, expand=False\n )\n q = nlp.states[\"q\"].mapping.to_second.map(x[nlp.states[\"q\"].index, :])\n qdot = nlp.states[\"qdot\"].mapping.to_second.map(x[nlp.states[\"qdot\"].index, :])\n\n return np.concatenate((np.array(com_func(q)[1:, :]), np.array(com_dot_func(q, qdot)[1:, :])))\n\n\nif __name__ == \"__main__\":\n main()\n" ]
[ [ "matplotlib.pyplot.figure", "numpy.hstack", "matplotlib.pyplot.show", "matplotlib.pyplot.plot" ], [ "numpy.array" ] ]