def fill_unknown_traits(traits_to_update, new_traits): """Returns traits_to_update, with all None values replaced with the corresponding index in new_traits traits_to_update -- a numpy array, with None values representing missing data new_traits -- a numpy array, with None values representing missing data returns an array that is traits_to_update, with all None values replaced with the corresponding values in new_traits. This is useful when some, but not all traits of an organism are known. For example, PCR of specific genes may have fixed some traits, but you would like to predict the rest. Note -- in the special case where traits_to_update is the value None, new_traits will be returned. This is done so that we don't have to guess or specify beforehand how many traits there will be when filling in tip nodes """ if traits_to_update is None: return array(new_traits) masked_traits_to_update = masked_object(traits_to_update,None) result = where(masked_traits_to_update.mask == True,\ new_traits,masked_traits_to_update) return array(result)
def calc_mean_and_std(ROICorrMaps, n_subjects, ROIAtlasmask, ddof=1, applyFisher=False): ''' Function for calculating the mean and standard deviation of the data. At a time, only one of the nii file is loaded and the elements keep on adding as we enumerate over the subjects. ''' mask = nib.load(ROIAtlasmask).get_data() mask = ma.masked_object(mask, 0).mask if (n_subjects != 0): f = nib.load(ROICorrMaps[0]) dimensions = f.get_header().get_data_shape() print(dimensions) else: exit mask = np.repeat(mask[:, :, :, np.newaxis], dimensions[3], axis=3) # print(ROICorrMaps) Sample_mean_Array = np.zeros(dimensions) Sample_std_Array = np.zeros(dimensions) Sample_mean_Array = ma.masked_array(Sample_mean_Array, mask=mask, fill_value=0) Sample_std_Array = ma.masked_array(Sample_std_Array, mask=mask, fill_value=0) for count, subject in enumerate(ROICorrMaps): Corr_data = nib.load(subject).get_data() Corr_data = ma.masked_array(Corr_data, mask=mask, fill_value=0) if applyFisher: Corr_data = np.arctanh(Corr_data) Sample_mean_Array += Corr_data Sample_std_Array += np.square(Corr_data) print('Done subject ', count + 1) Sample_mean_Array /= n_subjects # import pdb; pdb.set_trace() Sample_std_Array = np.sqrt( (Sample_std_Array - n_subjects * np.square(Sample_mean_Array)) / (n_subjects - ddof)) return Sample_mean_Array, Sample_std_Array
import numpy as np import numpy.ma as ma food = np.array(['green_eggs', 'ham'], dtype=object) # don't eat spoiled food eat = ma.masked_object(food, 'green_eggs') print eat # plain ol` ham is boring fresh_food = np.array(['cheese', 'ham', 'pineapple'], dtype=object) eat = ma.masked_object(fresh_food, 'green_eggs') print eat eat
] # - Use Pandas to read columns according to colnames data = pandas.read_csv('Video_Game_Sales_as_of_Jan_2017.csv', names=colnames) # - Converting columns into NumPy Arrays name = np.asarray(data.Name.tolist()) platform = np.asarray(data.Platform.tolist()) year_of_release = np.asarray(data.Year_Of_Release.tolist()) genre = np.asarray(data.Genre.tolist()) publisher = np.asarray(data.Publisher.tolist()) # - Masking Critic Scores with review count less than 20 critic_score = np.asarray(data.Critic_Score.tolist()) critic_count = ma.masked_invalid(data.Critic_Count.tolist()) critic_count = ma.masked_less(critic_count, 20) critic_score = ma.masked_object(critic_score, critic_count) # - Masking User Scores with review count less than 20 user_score = np.asarray(data.User_Score.tolist()) * 10 user_count = ma.masked_invalid(data.User_Count.tolist()) user_count = ma.masked_less(user_count, 20) user_score = ma.masked_object(user_score, user_count) rating = np.asarray(data.Rating.tolist()) # ============================= Global Sales Code ============================= # - Masking Global Sales 3 STD away from medium global_sales = np.asarray(data.Global_Sales.tolist()) # millions global_sales_mean = sp.mean(global_sales) global_sales_std = sp.std(global_sales) global_sales = ma.masked_outside(global_sales,
2. masked_invalid(a[, copy]): Mask an array where invalid values occur (NaNs or infs) 3. masked_less(x, value[, copy]) Mask an array where less than a given value. 4. masked_less_equal(x, value[, copy]) Mask an array where less than or equal to a given value. 5. masked_not_equal(x, value[, copy]) Mask an array where not equal to a given value. 6. masked_outside(x, v1, v2[, copy]) Mask an array outside a given interval, excludng v1 and v2 ''' # 7. numpy.ma.masked_object print("--------- Example 5: use of masked_object() ---------") food = np.array(['green_eggs', 'ham'], dtype=object) ## it is required to specify type as object to perform masked_object eat = ma.masked_object(food, "green_eggs") print("result of masked_object is : ", eat) ''' 8. numpy.ma.masked_values(x, value, rtol=1e-05, atol=1e-08, copy=True, shrink=True) Mask using floating point equality. Return a MaskedArray, masked where the data in array x are approximately equal to value. 9. masked_where(condition, a[, copy]) Mask an array where a condition is met. ''' x = ma.array([1, 2, 3]) x[0] = ma.masked print("*" * 50) print("--------- Example 6: masking an entry ---------") print("masked x is ", x) x.mask = True