def test_info(self): ''' Test the info module. ''' for patch in self._name_list: test_patch = Patch(os.path.join(self._dir, patch)) test_patch.scan() for index, cluster in enumerate(test_patch): # Testing basic information loading test_start = abs(cluster.start - self._patch_list[patch][index]['start']) if test_start > 0.0001: print('Module Info "start" test failed') print('Expected: {}, Obtained: {}'.format(self._patch_list[patch][index]['start'], cluster.start)) test_open_period = sum((cluster.open_period - self._patch_list[patch][index]['open_period'])**2) if test_open_period > 0.0001: print('Module Info "open_period" test failed') print('Total difference: {}'.format(test_open_period)) test_shut_period = sum((cluster.shut_period - self._patch_list[patch][index]['shut_period'])**2) if test_shut_period > 0.0001: print('Module Info "shut_period" test failed') print('Total difference: {}'.format(test_shut_period)) test_open_amp = sum((cluster.open_amp - self._patch_list[patch][index]['open_amp'])**2) if test_open_amp > 0.0001: print('Module Info "open_amp" test failed') print('Total difference: {}'.format(test_open_amp)) test_shut_amp = sum((cluster.shut_amp - self._patch_list[patch][index]['shut_amp'])**2) if test_shut_amp > 0.0001: print('Module Info "shut_amp" test failed') print('Total difference: {}'.format(test_shut_amp))
def scan_folder(self, clear = True): ''' Scan and collect all the clusters in the folder. ''' if clear: self._cluster_list = [] for folder in self._folder_list: patch_list = [csv for csv in os.listdir(folder) if csv[-4:] == '.csv'] for patch in patch_list: current_path = os.path.join(folder, patch) current_patch = Patch(current_path) current_patch.scan() for cluster in current_patch: self._cluster_list.append(cluster) return self._cluster_list
def test_PlotComputation_using_PlotMPL(self): ''' Testing the PlotComputation using the PlotMPL module which uses Matplotlib as backend. ''' for patch in self._name_list: test_patch = Patch(os.path.join(self._dir, patch)) test_patch.scan() for index, cluster in enumerate(test_patch): cluster.compute_mode() cluster.compute_mode_detail() test_plot = PlotSingle(self._dir) test_plot.load_cluster(cluster) test_plot.plot_original() test_plot.plot_popen_on_original() test_plot.plot_open_close() test_plot.plot_cost_difference()
def scan_orded_folder(self, func = _add_cluster, clear = True, export = False): ''' Scan the folder and apply the func to every clusters in the folder. If func is not provided, a whole list of the clusters in the folder will be generated. ''' if clear: self._cluster_list = [] for folder in self._folder_list: current_path = folder current_folder_list = list_folder(current_path) for receptor in current_folder_list: current_path_R = os.path.join(current_path, receptor) current_folder_list_R = list_folder(current_path_R) for mutation in current_folder_list_R: current_path_M = os.path.join(current_path_R, mutation) current_folder_list_M = list_folder(current_path_M) for composition in current_folder_list_M: current_path_C = os.path.join(current_path_M, composition) current_folder_list_C = list_folder(current_path_C) for agonist in current_folder_list_C: current_path_A = os.path.join(current_path_C, agonist) current_folder_list_A = list_folder(current_path_A) for concentration in current_folder_list_A: path = os.path.join(current_path_A, concentration) patch_list = [csv for csv in os.listdir(path) if csv[-4:] == '.csv'] for patch in patch_list: filepath = os.path.join(path, patch) current_patch = Patch(filepath) current_patch.scan() for cluster in current_patch: func(self, cluster, receptor = receptor, mutation = mutation, composition = composition, agonist = agonist, concentration = concentration) if export: return self._cluster_list
def test_batch_analysis(self): ''' test the batch analysis. ''' cluster_list = [] for patch in self._name_list: test_patch = Patch(os.path.join(self._dir, patch)) test_patch.scan() for cluster in test_patch: cluster.compute_mode() cluster.compute_mode_detail() cluster_list.append(cluster) # Test BatchAnalysis class batch_analysis = BatchAnalysis(list(cluster_list)) test_dict = batch_analysis.compute_cluster_summary() for patchname in self._name_list: for cluster_no in range(1, self.cluster_num+1): test_dict = batch_analysis.compute_cluster_summary( patchname = patchname, cluster_no = cluster_no) temp_amp = np.mean(self._patch_list[patchname][cluster_no-1]['open_amp'] - self._patch_list[patchname][cluster_no-1]['shut_amp']) if abs(test_dict['mean_amp'] - temp_amp) > 0.0001: print('Amplitude error') temp_duration = (sum(self._patch_list[patchname][cluster_no-1]['open_period']) + sum(self._patch_list[patchname][cluster_no-1]['shut_period'])) if abs(test_dict['duration'] - temp_duration) > 0.0001: print('Duration error') temp_popen = sum(self._patch_list[patchname][cluster_no-1]['open_period']) / temp_duration if abs(test_dict['popen'] - temp_popen) > 0.0001: print('Popen error') # Test StretchSummary class batch_analysis.compute_stretch_summary()
def test_scn_write(): intervals = np.hstack((np.arange(1,501),np.arange(1,501))) amplitudes = np.ones(1000)*-1 amplitudes[::2] = 0 amplitudes = amplitudes.astype('int') flags = np.zeros(1000) flags = flags.astype('int') dcio.scn_write(intervals, amplitudes, flags, filename='./test.SCN') new_patch = Patch('./test.SCN') new_patch.read_scn(tres=0, tcrit=500) new_patch.write_scn() new_patch = Patch('./modified_test.SCN') new_patch.read_scn(tres=0, tcrit=np.inf) period = new_patch[1].period print(sum(intervals[3:-1]-period))
def test_batch_query(self): ''' test the batch_query module. ''' cluster_list = [] for patch in self._name_list: test_patch = Patch(os.path.join(self._dir, patch)) test_patch.scan() for cluster in test_patch: cluster_list.append(cluster) # Test search orded folder scan choice_dict = OrderedDict([('receptor', ['NMDA', 'AMPA']), ('mutation', ['S219P', 'S200T']), ('composition', ['a1','ab']), ('agonist', ['glycine', 'taurine']), ('concentration', ['100', '0.1'])]) organisation = {keys:{key: [] for key in choice_dict[keys]} for keys in choice_dict} for index in range(len(self._name_list)): temp_filepath = self._dir for key in choice_dict: choice = random.choice(choice_dict[key]) temp_filepath = os.path.join(temp_filepath, choice) organisation[key][choice].extend([index]*self.cluster_num) try: os.makedirs(temp_filepath) except FileExistsError: pass shutil.copy2(os.path.join(self._dir, self._name_list[index]), os.path.join(temp_filepath, self._name_list[index])) test_batch = Batch(folder_list = self._dir) if len(test_batch.scan_folder()) != len(cluster_list): print('scan_folder error') test_cluster_list = test_batch.scan_orded_folder(clear = True,export = True) copy_organisation = copy.deepcopy(organisation) for cluster in test_cluster_list: index = self._name_list.index(cluster.patchname) for key in organisation: if not index in organisation[key][getattr(cluster, key)]: print('scan_orded_folder error') else: copy_organisation[key][getattr(cluster, key)].remove(index) for i in copy_organisation: for j in copy_organisation[i]: if copy_organisation[i][j]: print('scan_orded_folder error') # Test query copy_organisation = copy.deepcopy(organisation) for i in organisation: for j in organisation[i]: if organisation[i][j]: query_list = test_batch.query(**{i: j}) for cluster in query_list: index = self._name_list.index(cluster.patchname) try: copy_organisation[i][j].remove(index) except ValueError: print('query error') for i in copy_organisation: for j in copy_organisation[i]: if copy_organisation[i][j]: print('query error')
cluster_list = [] data = np.loadtxt('./ekdist_paramteters.csv', delimiter=',', dtype={ 'names': ('filename', 'concentration', 'res', 'tcrit'), 'formats': ('S8', 'S3', 'f4', 'f4')}) concentrations = [1,3,10,100] for concentration in concentrations: for patch in data: if float(patch['concentration']) == concentration: filename = "./ekdist_traces/{}.scn".format(patch['filename'].decode('utf8')) # # filename = "./raw trace/{}/{}.scn".format(patch['concentration'].decode('utf8'), # patch['filename'].decode('utf8')) patch = Patch(filename) patch.read_scn(tres=patch['res']*1e-6, tcrit=patch['tcrit']*1e-6) cluster_list.extend(patch.get_cluster_list()) batch_analysis = BatchAnalysis(cluster_list) summary = batch_analysis.compute_cluster_summary() summary['start'] /= 1000 summary['end'] /= 1000 np.savetxt('temp.csv', summary, delimiter=',', fmt = '%s,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e') col_name = str(summary.dtype.names) col_name = col_name[1:-1] + '\n' f = open('temp.csv','r') string = f.read()
'15072411', '15080408' ] tres = [ 20, 30, 20 ] tcrit = [ 100, 70, 100 ] popen =[] for index, name in enumerate(name_list): patch = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/3/{}.SCN'.format(name)) patch.read_scn(tres=tres[index], tcrit=tcrit[index]) maxopen = 1 maxshut = float('inf') def rule(cluster, maxopen, maxshut): if (cluster.mean_open > maxopen) or (cluster.mean_shut > maxshut): return False else: return True for i in range(3): patch = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/3/{}.SCN'.format(name_list[i])) patch.read_scn(tres=tres[i], tcrit=tcrit[i]) patch.filter_cluster(rule, maxopen, maxshut)
# -*- coding: utf-8 -*- """ Created on Fri May 20 11:26:23 2016 @author: zhiyiwu """ from info import Cluster,Patch from dcpyps import dataset from PlotAnalysis import PlotSingle from batch_analysis import BatchAnalysis import matplotlib.pyplot as plt patch_F = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/1/15071710.SCN') patch_F.read_scn(tres=30, tcrit=100) plt.plot(patch_F.mean_open, patch_F.mean_shut,'o', alpha=0.5, label = 'F') patch_G = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/1/15080500.SCN') patch_G.read_scn(tres=20, tcrit=300) plt.plot(patch_G.mean_open, patch_G.mean_shut,'o', alpha=0.5, label = 'G') patch_H = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/1/15080705.SCN') patch_H.read_scn(tres=20, tcrit=300) plt.plot(patch_H.mean_open, patch_H.mean_shut,'o', alpha=0.5, label = 'H') plt.legend()
from info import Cluster,Patch from dcpyps import dataset from PlotAnalysis import PlotSingle from batch_analysis import BatchAnalysis import matplotlib.pyplot as plt import numpy as np maxopen = 1 maxshut = float('inf') def rule(cluster, maxopen, maxshut): if (cluster.mean_open > maxopen) or (cluster.mean_shut > maxshut): return False else: return True patch_G = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/1/15080500.SCN') patch_G.read_scn(tres=20, tcrit=300) patch_H = Patch('/Users/zhiyiwu/Documents/pharmfit/raw trace/1/15080705.SCN') patch_H.read_scn(tres=20, tcrit=300) patch_G.filter_cluster(rule, maxopen, maxshut) patch_G.write_scn() patch_H.filter_cluster(rule, maxopen, maxshut) patch_H.write_scn()
def change(name, tres, tcrit, minPopen = 0, maxPopen = 1.1): new_patch = Patch('./set/{}.SCN'.format(name)) new_patch.read_scn(tres=tres, tcrit=tcrit) new_patch.filter_cluster(rule, minPopen, maxPopen) new_patch.write_scn()
# -*- coding: utf-8 -*- """ Created on Tue Jun 7 09:32:25 2016 @author: zhiyiwu """ import numpy as np import matplotlib.pyplot as plt from info import Patch from binary_search import BFS patch = Patch('/Users/zhiyiwu/Documents/pharmfit/12061415.csv') patch.scan() cluster = patch[1] opening = cluster.open_period log_opening = np.log10(opening) plt.hist(log_opening) sep_open = sorted(BFS(log_opening[:, np.newaxis])) print(sep_open) shutting = cluster.shut_period plt.hist(np.log10(shutting)) log_shutting = np.log10(shutting) plt.hist(log_shutting) sep_shut = sorted(BFS(log_shutting[:, np.newaxis])) print(sep_shut) period = cluster.period