I have a problem while modifying an NumPy array in depended module, that was previously defined in parrent module. I have checked, and it's modifying only localy in the function calc()
How can I modify an NumPy array that was defined in other module, inside a function?
main_module.py
import numpy as np
from pprint import pprint
test_array = np.array([1, 2, 3])
pprint(test_array)
process.py
from main_module import *
def calc():
global test_array
test_array = np.append(test_array, [4])
pprint(test_array)
calc()
pprint(test_array)
In python globals are global to the module, not to the whole program. The standard way to do something like this in an object oriented language is to attach the relevant array to some object for example:
main_module:
import numpy as np
from pprint import pprint
class GlobalArrayHolder(object):
def __init__(self):
self.test_array = np.array([1, 2, 3])
arrayholder = GlobalArrayHolder()
pprint(arrayholder.test_array)
process:
import numpy as np
from pprint import pprint
from main_module import arrayholder
def calc(arrayholder):
arrayholder.test_array = np.append(arrayholder.test_array, [4])
pprint(arrayholder.test_array)
calc(arrayholder)
pprint(arrayholder.test_array)
If you don't want to define your own class for this you can use a simple built in class like a dict. For example:
main_module:
import numpy as np
from pprint import pprint
arrayholder = {'test_array':np.array([1, 2, 3])}
pprint(arrayholder['test_array'])
process:
import numpy as np
from pprint import pprint
from main_module import arrayholder
def calc(arrayholder):
arrayholder['test_array'] = np.append(arrayholder['test_array'], [4])
pprint(arrayholder['test_array'])
calc(arrayholder)
pprint(arrayholder['test_array'])
Related
enter image description here
enter image description here
import SpatialDE
import numpy as np
import anndata
import scanpy as sc
import cv2
from sklearn.metrics import silhouette_score
from sklearn import metrics
import scanpy as sc
import pandas as pd
from decimal import Decimal
from decimal import Decimal,ROUND_HALF_UP
mousebrain_after_normalization_path='/home/user/imputation_test_data/MouseBrain_D1_normalization.h5ad'
adata_before = anndata.read_h5ad(mousebrain_after_normalization_path)
counts_before = pd.DataFrame(adata_before.X, columns=adata_before.var_names, index=adata_before.obs_names)
coord_before = pd.DataFrame(adata_before.obsm['spatial'], columns=['x', 'y'], index=adata_before.obs_names)
result_before = SpatialDE.run(coord_before, counts_before)
adata_before.obsm['spatialDE_result'] = result_before
I tried to use SpatialDE to evaluate my anndata on the computer server, I have transferred anndata.X to counts_before(which is a dataframe) and adata.obsm['spatial'] to be coord_before. and using SpatialDE.run to finish them. But no result.
I am using clr to import c# dll in python
one of the functions return ushort[,] ,
which is considered as System.UInt16[,] in python
How can in convert System.UInt16[,] to numpy uint16 matrix?
I can do the conversion only by looping on the matrix, reading each element and assigning its value to the respective position in another numpy matrix, but this solution is very slow.
Is there a faster conversion method which can utilize numpy vectorization ?
Here's a sample for my loop
import clr
import os
import numpy as np
dll_name = os.path.join(os.path.abspath(os.path.dirname(__file__)), ("mydll") + ".dll")
clr.AddReference(dll_name)
from mynamespace import myclass
myobject = myclass()
numpy_matrix = np.empty([80,260],dtype = np.uint16)
SystemInt16_matrix = myobject.Getdata()
for i in range(20):
for j in range(32):
numpy_matrix[i,j]=SystemInt16_matrix[i,j]
I could find the solution, instead of the loop I should use np.fromiter & reshape
import clr
import os
import numpy as np
dll_name = os.path.join(os.path.abspath(os.path.dirname(__file__)), ("mydll") + ".dll")
clr.AddReference(dll_name)
from mynamespace import myclass
myobject = myclass()
SystemInt16_matrix = myobject.Getdata()
numpy_matrix = np.fromiter(SystemInt16_matrix, np.int16).reshape((20, 32))
I have a function that I created and I want the function to be applied to these different values using a for loop or something.
How do I create a for loop that takes each value but stores them in different arrays?
I have this so far:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import netCDF4 as s
import numpy.ma as ma
fwf_tot = fwf_ice + ds.runoff_tundra*ds.LSMGr #data input i am using
# function i want to apply to the data
def ob_annual(ob_monthly, id_number):
ann_sum = ob_monthly.where(ds.ocean_basins == id_number).resample(TIME='1AS').sum().sum(dim=('X','Y'))
return ann_sum
This is where my problem is to create the for loop to save for these different values. I think this for loop is just saving the function applied to the last value (87) and not the others. How might I fix this? I expected there to be an output of 7 arrays with each a size of 59.
obs = np.array([26,28,29,30,76,84,87])
total_obs = []
for i in obs:
total_obs = ob_annual(fwf_tot_grnl, i)
print(total_obs.shape)
(59)
You replace your list total_obs at each iteration. You must append each value into it:
for i in obs:
total_obs.append(ob_annual(fwf_tot_grnl, i))
or use a comprehension list
total_obs = [ob_annual(fwf_tot_grnl, i) for i in obs]
I'm using scipy signaltonoise function below is the code but it returns an error. I searched regarding this in github too but couldn't find it. Can you please help.
import numpy as np
import cv2
import math
import os
import csv
from scipy import stats
from PIL import Image
from skimage.color import rgb2gray
from multiprocessing import Pool
from skimage.feature import local_binary_pattern # Local Binary Pattern function
from scipy.stats import itemfreq # To calculate a normalized histogram
import scipy.stats as sp
from skimage.feature import hog
from scipy.ndimage.measurements import label
from scipy import signal as sg
def calc_snr(img):
snr = stats.signaltonoise(img, axis=None)
return snr
snr = calc_snr(img)
scipy.stats.signaltonoise() was deprecated in scipy 0.16.0 and removed in 1.0.0. If you need to use the function without downgrading scipy, you can see the original code from the function before it was removed on github here, and reproduced below:
import numpy as np
def signaltonoise(a, axis=0, ddof=0):
a = np.asanyarray(a)
m = a.mean(axis)
sd = a.std(axis=axis, ddof=ddof)
return np.where(sd == 0, 0, m/sd)
I am setting random seed for both random and numpy.random at the beginning of my main file:
import random
import numpy as np
np.random.seed(42)
random.seed(42)
import torch
Nevertheless, when I create a Net() object with randomly initialized parameters, it gives a completely different result every time:
net=neuralnet.Net()
print ("initialized params: ", net.fc1.weight)
Note that neuralnet.Net() is in a different file, and is a class that extends torch.nn.Module. it is torch.nn.Module that is randomly initializing net.fc1.weight, not my own code.
How is it possible that when I create a Net() object with randomly initialized parameters, it gives a completely different result every time?
try:
import torch
torch.manual_seed(0)
For further information:
https://pytorch.org/docs/stable/notes/randomness.html
Have you looked at: https://github.com/pytorch/pytorch/issues/7068?
There are some recommendations on how to reproduce the results.
Example:
import sys
import random
import datetime as dt
import numpy as np
import torch
torch.manual_seed(42)
torch.cuda.manual_seed(42)
np.random.seed(42)
random.seed(42)
torch.backends.cudnn.deterministic = True
features = torch.randn(2, 5)
# Print stuff.
fnp = features.view(-1).numpy()
print("Time: {}".format(dt.datetime.now()))
for el in fnp:
print("{:.20f}".format(el))
print("Python: {}".format(sys.version))
print("Numpy: {}".format(np.__version__))
print("Pytorch: {}".format(torch.__version__))