How to use strat method in hypothesis

Best Python code snippet using hypothesis

cpac_pipeline.py

Source:cpac_pipeline.py Github

copy

Full Screen

1import os2import sys3import copy4import argparse5import nipype.pipeline.engine as pe6import nipype.interfaces.fsl as fsl7import nipype.interfaces.utility as util8import nipype.interfaces.io as nio9from nipype.pipeline.utils import format_dot10from multiprocessing import Process11import CPAC12from CPAC.anat_preproc.anat_preproc import create_anat_preproc13from CPAC.func_preproc.func_preproc import create_func_preproc14from CPAC.seg_preproc.seg_preproc import create_seg_preproc15from CPAC.registration import create_nonlinear_register, create_bbregister_func_to_mni16from CPAC.nuisance import create_nuisance, bandpass_voxels17from CPAC.median_angle import create_median_angle_correction18from CPAC.generate_motion_statistics import motion_power_statistics19from CPAC.generate_motion_statistics import fristons_twenty_four20from CPAC.scrubbing import create_scrubbing_preproc21from CPAC.timeseries import create_surface_registration, get_voxel_timeseries,\22 get_roi_timeseries, get_vertices_timeseries23from CPAC.network_centrality import create_resting_state_graphs, get_zscore24from CPAC.utils.datasource import *25from CPAC.utils import Configuration26from CPAC.utils.utils import extract_one_d, set_gauss, \27 prepare_symbolic_links, \28 get_scan_params, get_tr29from CPAC.vmhc.vmhc import create_vmhc30from CPAC.reho.reho import create_reho31from CPAC.alff.alff import create_alff32from CPAC.sca.sca import create_sca33import zlib34import linecache35from string import Template36class strategy:37 def __init__(self):38 self.resource_pool = {}39 self.leaf_node = None40 self.leaf_out_file = None41 self.name = []42 def append_name(self, name):43 self.name.append(name)44 def get_name(self):45 return self.name46 def set_leaf_properties(self, node, out_file):47 self.leaf_node = node48 self.leaf_out_file = out_file49 def get_leaf_properties(self):50 return self.leaf_node, self.leaf_out_file51 def get_resource_pool(self):52 return self.resource_pool53 def get_node_from_resource_pool(self, resource_key):54 try:55 if resource_key in self.resource_pool:56 return self.resource_pool[resource_key]57 except:58 print 'no node for output: ', resource_key59 raise60 def update_resource_pool(self, resources):61 for key, value in resources.items():62 if key in self.resource_pool:63 print 'Warning key %s already exists in resource pool, replacing with %s ' % (key, value)64 self.resource_pool[key] = value65 66def prep_workflow(sub_dict, c, strategies):67 68 print '********************',c.standardResolutionBrain69 70 if sub_dict['unique_id']:71 subject_id = sub_dict['subject_id'] +"_"+ sub_dict['unique_id']72 else:73 subject_id = sub_dict['subject_id']74 75 76 wfname = 'resting_preproc_' + str(subject_id)77 workflow = pe.Workflow(name=wfname)78 workflow.base_dir = c.workingDirectory79 workflow.config['execution'] = {'hash_method': 'timestamp', 'crashdump_dir': os.path.abspath(c.crashLogDirectory)}80 mflow = None81 pflow = None82 strat_list = []83 workflow_bit_id = {}84 workflow_counter = 085 """86 Initialize Anatomical Input Data Flow87 """88 new_strat_list = []89 num_strat = 090 strat_initial = None91 for gather_anat in c.runAnatomicalDataGathering:92 strat_initial = strategy()93 if gather_anat == 1:94 flow = create_anat_datasource()95 flow.inputs.inputnode.subject = subject_id96 flow.inputs.inputnode.anat = sub_dict['anat']97 anat_flow = flow.clone('anat_gather_%d' % num_strat)98 strat_initial.set_leaf_properties(anat_flow, 'outputspec.anat')99 num_strat += 1100 strat_list.append(strat_initial)101 print strat_list102 """103 Inserting Anatomical Preprocessing workflow104 """105 new_strat_list = []106 num_strat = 0107 if 1 in c.runAnatomicalPreprocessing:108 workflow_bit_id['anat_preproc'] = workflow_counter109 for strat in strat_list:110 # create a new node, Remember to change its name! 111 anat_preproc = create_anat_preproc().clone('anat_preproc_%d' % num_strat)112 try:113 # connect the new node to the previous leaf114 node, out_file = strat.get_leaf_properties()115 workflow.connect(node, out_file, anat_preproc, 'inputspec.anat')116 except:117 print 'Invalid Connection: Anat Preprocessing No valid Previous for strat : ', num_strat, ' resource_pool: ', strat.get_resource_pool()118 num_strat += 1119 continue120 if 0 in c.runAnatomicalPreprocessing:121 # we are forking so create a new node122 tmp = strategy()123 tmp.resource_pool = dict(strat.resource_pool)124 tmp.leaf_node = (strat.leaf_node)125 tmp.leaf_out_file = str(strat.leaf_out_file)126 tmp.name = list(strat.name)127 strat = tmp128 new_strat_list.append(strat)129 strat.append_name('anat_preproc')130 strat.set_leaf_properties(anat_preproc, 'outputspec.brain')131 # add stuff to resource pool if we need it 132 strat.update_resource_pool({'anatomical_brain':(anat_preproc, 'outputspec.brain')})133 strat.update_resource_pool({'anatomical_reorient':(anat_preproc, 'outputspec.reorient')})134 num_strat += 1135 strat_list += new_strat_list136 """137 Inserting Registration Preprocessing138 Workflow139 """140 new_strat_list = []141 num_strat = 0142 workflow_counter += 1143 if 1 in c.runRegistrationPreprocessing:144 workflow_bit_id['anat_mni_register'] = workflow_counter145 for strat in strat_list:146 reg_anat_mni = create_nonlinear_register('anat_mni_register_%d' % num_strat)147 try:148 node, out_file = strat.get_leaf_properties()149 workflow.connect(node, out_file,150 reg_anat_mni, 'inputspec.input_brain')151 node, out_file = strat.get_node_from_resource_pool('anatomical_reorient')152 workflow.connect(node, out_file,153 reg_anat_mni, 'inputspec.input_skull')154 reg_anat_mni.inputs.inputspec.reference_brain = c.standardResolutionBrainAnat155 reg_anat_mni.inputs.inputspec.reference_skull = c.standardAnat156 except:157 print 'Invalid Connection: Anatomical Registration:', num_strat, ' resource_pool: ', strat.get_resource_pool()158 raise159 if 0 in c.runRegistrationPreprocessing:160 tmp = strategy()161 tmp.resource_pool = dict(strat.resource_pool)162 tmp.leaf_node = (strat.leaf_node)163 tmp.leaf_out_file = str(strat.leaf_out_file)164 tmp.name = list(strat.name)165 strat = tmp166 new_strat_list.append(strat)167 strat.append_name('anat_mni_register')168 strat.set_leaf_properties(reg_anat_mni, 'outputspec.output_brain')169 strat.update_resource_pool({'anatomical_to_mni_linear_xfm':(reg_anat_mni, 'outputspec.linear_xfm'),170 'anatomical_to_mni_nonlinear_xfm':(reg_anat_mni, 'outputspec.nonlinear_xfm'),171 'mni_to_anatomical_linear_xfm':(reg_anat_mni, 'outputspec.invlinear_xfm'),172 'mni_normalized_anatomical':(reg_anat_mni, 'outputspec.output_brain')})173 num_strat += 1174 strat_list += new_strat_list175 """176 Inserting Segmentation Preprocessing177 Workflow178 """179 new_strat_list = []180 num_strat = 0181 workflow_counter += 1182 if 1 in c.runSegmentationPreprocessing:183 workflow_bit_id['seg_preproc'] = workflow_counter184 for strat in strat_list:185 seg_preproc = create_seg_preproc('seg_preproc_%d'%num_strat)186 187 try:188 node, out_file = strat.get_node_from_resource_pool('anatomical_brain')189 workflow.connect(node, out_file,190 seg_preproc, 'inputspec.brain')191 node, out_file = strat.get_node_from_resource_pool('mni_to_anatomical_linear_xfm')192 workflow.connect(node, out_file, 193 seg_preproc, 'inputspec.standard2highres_mat')194 seg_preproc.inputs.inputspec.PRIOR_CSF = c.PRIOR_CSF195 seg_preproc.inputs.inputspec.PRIOR_GRAY = c.PRIOR_GRAY196 seg_preproc.inputs.inputspec.PRIOR_WHITE = c.PRIOR_WHITE197 seg_preproc.inputs.csf_threshold.csf_threshold = \198 c.cerebralSpinalFluidThreshold199 seg_preproc.inputs.wm_threshold.wm_threshold = \200 c.whiteMatterThreshold201 seg_preproc.inputs.gm_threshold.gm_threshold = \202 c.grayMatterThreshold203 seg_preproc.get_node('csf_threshold').iterables = ('csf_threshold',204 c.cerebralSpinalFluidThreshold)205 seg_preproc.get_node('wm_threshold').iterables = ('wm_threshold',206 c.whiteMatterThreshold)207 seg_preproc.get_node('gm_threshold').iterables = ('gm_threshold',208 c.grayMatterThreshold)209 except:210 print 'Invalid Connection: Segmentation Preprocessing:', num_strat, ' resource_pool: ', strat.get_resource_pool()211 raise212 if 0 in c.runSegmentationPreprocessing:213 tmp = strategy()214 tmp.resource_pool = dict(strat.resource_pool)215 tmp.leaf_node = (strat.leaf_node)216 tmp.leaf_out_file = str(strat.leaf_out_file)217 tmp.name = list(strat.name)218 strat = tmp219 new_strat_list.append(strat)220 221 strat.append_name('seg_preproc')222 strat.update_resource_pool({'anatomical_gm_mask' : (seg_preproc,'outputspec.gm_mask'),223 'anatomical_csf_mask': (seg_preproc, 'outputspec.csf_mask'),224 'anatomical_wm_mask' : (seg_preproc, 'outputspec.wm_mask'),225 'seg_probability_maps': (seg_preproc,'outputspec.probability_maps'),226 'seg_mixeltype': (seg_preproc, 'outputspec.mixeltype'),227 'seg_partial_volume_map': (seg_preproc, 'outputspec.partial_volume_map'),228 'seg_partial_volume_files': (seg_preproc, 'outputspec.partial_volume_files')})229 num_strat += 1230 strat_list += new_strat_list231 """232 Inserting Functional Input Data workflow233 """234 new_strat_list = []235 num_strat = 0236 if 1 in c.runFunctionalDataGathering:237 for strat in strat_list:238 # create a new node, Remember to change its name! 239 #Flow = create_func_datasource(sub_dict['rest'])240 #Flow.inputs.inputnode.subject = subject_id241 funcFlow = create_func_datasource(sub_dict['rest'], 'func_gather_%d' % num_strat)242 funcFlow.inputs.inputnode.subject = subject_id243 244 if 0 in c.runFunctionalDataGathering:245 # we are forking so create a new node246 tmp = strategy()247 tmp.resource_pool = dict(strat.resource_pool)248 tmp.leaf_node = (strat.leaf_node)249 tmp.leaf_out_file = str(strat.leaf_out_file)250 tmp.name = list(strat.name)251 strat = tmp252 new_strat_list.append(strat)253 strat.set_leaf_properties(funcFlow, 'outputspec.rest')254 num_strat += 1255 strat_list += new_strat_list256 """257 Inserting Functional Image Preprocessing258 Workflow259 """260 new_strat_list = []261 num_strat = 0262 workflow_counter += 1263 if 1 in c.runFunctionalPreprocessing:264 workflow_bit_id['func_preproc'] = workflow_counter265 for strat in strat_list: 266 267 slice_timing = sub_dict.get('scan_parameters') 268 #a node which checks if scan _parameters are present for each scan269 scan_params = pe.Node(util.Function(input_names=['subject',270 'scan',271 'subject_map',272 'start_indx',273 'stop_indx'],274 output_names=['tr',275 'tpattern',276 'ref_slice',277 'start_indx',278 'stop_indx'],279 function = get_scan_params ),280 name = 'scan_params_%d' % num_strat)281 282 convert_tr = pe.Node(util.Function(input_names = ['tr'],283 output_names= ['tr'],284 function = get_tr),285 name = 'convert_tr_%d' % num_strat)286 287 #if scan parameters are available slice timing correction is 288 #turned on 289 if slice_timing:290 291 func_preproc = create_func_preproc(slice_timing_correction=True, wf_name = 'func_preproc_%d' % num_strat)292 293 #getting the scan parameters294 workflow.connect(funcFlow, 'outputspec.subject',295 scan_params, 'subject')296 workflow.connect(funcFlow, 'outputspec.scan',297 scan_params, 'scan')298 scan_params.inputs.subject_map = sub_dict299 scan_params.inputs.start_indx = c.startIdx300 scan_params.inputs.stop_indx = c.stopIdx301 302 #passing the slice timing correction parameters303 workflow.connect(scan_params, 'tr',304 func_preproc, 'scan_params.tr')305 workflow.connect(scan_params, 'ref_slice',306 func_preproc, 'scan_params.ref_slice')307 workflow.connect(scan_params, 'tpattern',308 func_preproc, 'scan_params.acquisition')309 310 workflow.connect(scan_params, 'start_indx',311 func_preproc, 'inputspec.start_idx')312 workflow.connect(scan_params, 'stop_indx',313 func_preproc, 'inputspec.stop_idx')314 315 workflow.connect(scan_params, 'tr',316 convert_tr, 'tr')317 else: 318 func_preproc = create_func_preproc(slice_timing_correction=False, wf_name ='func_preproc_%d' % num_strat)319 func_preproc.inputs.inputspec.start_idx = c.startIdx320 func_preproc.inputs.inputspec.stop_idx = c.stopIdx321 322 convert_tr.inputs.tr = c.TR323 324 node = None325 out_file = None326 try:327 node, out_file = strat.get_leaf_properties()328 workflow.connect(node, out_file, func_preproc, 'inputspec.rest')329 except:330 print 'Invalid Connection: Functional Preprocessing No valid Previous for strat : ', num_strat, ' resource_pool: ', strat.get_resource_pool()331 num_strat += 1332 continue333 if 0 in c.runFunctionalPreprocessing:334 # we are forking so create a new node335 tmp = strategy()336 tmp.resource_pool = dict(strat.resource_pool)337 tmp.leaf_node = (strat.leaf_node)338 tmp.leaf_out_file = str(strat.leaf_out_file)339 tmp.name = list(strat.name)340 strat = tmp341 new_strat_list.append(strat)342 343 strat.append_name('func_preproc')344 strat.set_leaf_properties(func_preproc, 'outputspec.preprocessed')345 # add stuff to resource pool if we need it 346 if slice_timing:347 strat.update_resource_pool({'slice_timing_corrected': (func_preproc, 'outputspec.slice_time_corrected')})348 strat.update_resource_pool({'mean_functional':(func_preproc, 'outputspec.example_func')})349 strat.update_resource_pool({'functional_preprocessed_mask':(func_preproc, 'outputspec.preprocessed_mask')})350 strat.update_resource_pool({'movement_parameters':(func_preproc, 'outputspec.movement_parameters')})351 strat.update_resource_pool({'max_displacement':(func_preproc, 'outputspec.max_displacement')})352 strat.update_resource_pool({'preprocessed':(func_preproc, 'outputspec.preprocessed')})353 strat.update_resource_pool({'functional_brain_mask':(func_preproc, 'outputspec.mask')})354 strat.update_resource_pool({'motion_correct':( func_preproc, 'outputspec.motion_correct')})355 356 num_strat += 1357 strat_list += new_strat_list358 """359 Inserting Friston's 24 parameter Workflow360 Incase this workflow runs , it overwrites the movement_parameters file361 So the file contains 24 parameters for motion and that gets wired to all the workflows362 that depend on. The effect should be seen when regressing out nuisance signals and motion363 is used as one of the regressors364 """365 new_strat_list = []366 num_strat = 0367 workflow_counter += 1368 if 1 in c.runFristonModel:369 workflow_bit_id['fristons_parameter_model'] = workflow_counter370 for strat in strat_list:371 fristons_model = fristons_twenty_four(wf_name='fristons_parameter_model_%d' % num_strat)372 try:373 node, out_file = strat.get_node_from_resource_pool('movement_parameters')374 workflow.connect(node, out_file,375 fristons_model, 'inputspec.movement_file')376 except:377 print 'Invalid Connection: fristons_parameter_model ', num_strat, ' resource_pool: ', strat.get_resource_pool()378 raise379 if 0 in c.runFristonModel:380 tmp = strategy()381 tmp.resource_pool = dict(strat.resource_pool)382 tmp.leaf_node = (strat.leaf_node)383 tmp.leaf_out_file = str(strat.leaf_out_file)384 tmp.name = list(strat.name)385 strat = tmp386 new_strat_list.append(strat)387 strat.append_name('fristons_parameter_model')388 strat.update_resource_pool({'movement_parameters':(fristons_model, 'outputspec.movement_file')})389 num_strat += 1390 strat_list += new_strat_list391 """392 Inserting Anatomical to Functional Registration393 """394 new_strat_list = []395 num_strat = 0396 workflow_counter += 1397 if 1 in c.runAnatomicalToFunctionalRegistration:398 workflow_bit_id['anat_to_func_register'] = workflow_counter399 for strat in strat_list:400 anat_to_func_reg = pe.Node(interface=fsl.FLIRT(),401 name='anat_to_func_register_%d' % num_strat)402 anat_to_func_reg.inputs.cost = 'corratio'403 anat_to_func_reg.inputs.dof = 6404 inv_anat_to_func = pe.Node(interface=fsl.ConvertXFM(),405 name='inv_anat_to_func_register_%d' % num_strat)406 inv_anat_to_func.inputs.invert_xfm = True407 408 func_gm = pe.Node(interface=fsl.ApplyXfm(),409 name='func_gm_%d' % num_strat)410 func_gm.inputs.apply_xfm = True411 func_gm.inputs.interp = 'nearestneighbour'412 func_csf = pe.Node(interface=fsl.ApplyXfm(),413 name='func_csf_%d' % num_strat)414 func_csf.inputs.apply_xfm = True415 func_csf.inputs.interp = 'nearestneighbour'416 func_wm = pe.Node(interface=fsl.ApplyXfm(),417 name='func_wm_%d' % num_strat)418 func_wm.inputs.apply_xfm = True419 func_wm.inputs.interp = 'nearestneighbour'420 try:421 node, out_file = strat.get_node_from_resource_pool('anatomical_brain')422 workflow.connect(node, out_file,423 anat_to_func_reg, 'in_file')424 workflow.connect(anat_to_func_reg, 'out_matrix_file',425 inv_anat_to_func, 'in_file')426 node, out_file = strat.get_node_from_resource_pool('mean_functional')427 workflow.connect(node, out_file,428 anat_to_func_reg, 'reference')429 workflow.connect(node, out_file,430 func_gm, 'reference')431 workflow.connect(node, out_file,432 func_csf, 'reference')433 workflow.connect(node, out_file,434 func_wm, 'reference')435 node, out_file = strat.get_node_from_resource_pool('anatomical_gm_mask')436 workflow.connect(node, out_file,437 func_gm, 'in_file')438 workflow.connect(anat_to_func_reg, 'out_matrix_file',439 func_gm, 'in_matrix_file')440 node, out_file = strat.get_node_from_resource_pool('anatomical_csf_mask')441 workflow.connect(node, out_file,442 func_csf, 'in_file')443 workflow.connect(anat_to_func_reg, 'out_matrix_file',444 func_csf, 'in_matrix_file')445 node, out_file = strat.get_node_from_resource_pool('anatomical_wm_mask')446 workflow.connect(node, out_file,447 func_wm, 'in_file')448 workflow.connect(anat_to_func_reg, 'out_matrix_file',449 func_wm, 'in_matrix_file')450 except:451 print 'Invalid Connection: Anatomical to Functional Registration:', num_strat, ' resource_pool: ', strat.get_resource_pool()452 raise453 if 0 in c.runAnatomicalToFunctionalRegistration:454 tmp = strategy()455 tmp.resource_pool = dict(strat.resource_pool)456 tmp.leaf_node = (strat.leaf_node)457 tmp.leaf_out_file = str(strat.leaf_out_file)458 tmp.name = list(strat.name)459 strat = tmp460 new_strat_list.append(strat)461 462 strat.append_name('anat_to_func_register')463 strat.update_resource_pool({'anatomical_to_functional_xfm':(anat_to_func_reg, 'out_matrix_file'),464 'inverse_anatomical_to_functional_xfm':(inv_anat_to_func, 'out_file'),465 'functional_gm_mask':(func_gm, 'out_file'),466 'functional_wm_mask':(func_wm, 'out_file'),467 'functional_csf_mask':(func_csf, 'out_file')})468 num_strat += 1469 strat_list += new_strat_list470 471 """472 Inserting Register Functional to MNI Workflow473 """474 new_strat_list = []475 num_strat = 0476 workflow_counter += 1477 if 1 in c.runRegisterFuncToMNI:478 workflow_bit_id['func_to_mni'] = workflow_counter479 for strat in strat_list:480 func_to_mni = create_bbregister_func_to_mni('func_to_mni_%d' % num_strat)481 func_to_mni.inputs.inputspec.mni = c.standardResolutionBrain482 func_to_mni.inputs.inputspec.interp = 'trilinear'483 func_to_mni.inputs.inputspec.bbr_schedule = c.boundaryBasedRegistrationSchedule484 try:485 def pick_wm(seg_prob_list):486 seg_prob_list.sort()487 return seg_prob_list[-1]488 489 node, out_file = strat.get_node_from_resource_pool('mean_functional')490 workflow.connect(node, out_file,491 func_to_mni, 'inputspec.func')492 node, out_file = strat.get_node_from_resource_pool('seg_probability_maps')493 workflow.connect(node, (out_file, pick_wm),494 func_to_mni, 'inputspec.anat_wm_segmentation')495 496 node, out_file = strat.get_node_from_resource_pool('anatomical_reorient')497 workflow.connect(node, out_file,498 func_to_mni, 'inputspec.anat_skull')499 500 node, out_file = strat.get_node_from_resource_pool('anatomical_brain')501 workflow.connect(node, out_file,502 func_to_mni, 'inputspec.anat')503 504 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_linear_xfm')505 workflow.connect(node, out_file,506 func_to_mni, 'inputspec.anat_to_mni_linear_xfm')507 508 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')509 workflow.connect(node, out_file,510 func_to_mni, 'inputspec.anat_to_mni_nonlinear_xfm')511 except:512 print 'Invalid Connection: Register Functional to MNI:', num_strat, ' resource_pool: ', strat.get_resource_pool()513 raise514 515 if 0 in c.runRegisterFuncToMNI:516 tmp = strategy()517 tmp.resource_pool = dict(strat.resource_pool)518 tmp.leaf_node = (strat.leaf_node)519 tmp.out_file = str(strat.leaf_out_file)520 tmp.name = list(strat.name)521 strat = tmp522 new_strat_list.append(strat)523 strat.append_name('func_to_mni')524 #strat.set_leaf_properties(func_mni_warp, 'out_file')525 strat.update_resource_pool({'mean_functional_in_mni':(func_to_mni, 'outputspec.mni_func'),526 'mean_functional_in_anat':(func_to_mni, 'outputspec.anat_func'),527 'anatomical_wm_edge':(func_to_mni, 'outputspec.anat_wm_edge'),528 'functional_to_anat_linear_xfm':(func_to_mni, 'outputspec.func_to_anat_linear_xfm'),529 'functional_to_mni_linear_xfm':(func_to_mni, 'outputspec.func_to_mni_linear_xfm'),530 'mni_to_functional_linear_xfm':(func_to_mni, 'outputspec.mni_to_func_linear_xfm')})531 num_strat += 1532 strat_list += new_strat_list533 """534 Inserting Generate Motion Statistics Workflow535 """536 new_strat_list = []537 num_strat = 0538 workflow_counter += 1539 if 1 in c.runGenerateMotionStatistics:540 workflow_bit_id['gen_motion_stats'] = workflow_counter541 for strat in strat_list:542 543 gen_motion_stats = motion_power_statistics('gen_motion_stats_%d'% num_strat)544 gen_motion_stats.inputs.scrubbing_input.threshold = c.scrubbingThreshold545 gen_motion_stats.inputs.scrubbing_input.remove_frames_before = c.numRemovePrecedingFrames546 gen_motion_stats.inputs.scrubbing_input.remove_frames_after = c.numRemoveSubsequentFrames547 gen_motion_stats.get_node('scrubbing_input').iterables = ('threshold', c.scrubbingThreshold)548 549 try:550 ##**special case where the workflow is not getting outputs from resource pool551 #but is connected to functional datasource552 workflow.connect(funcFlow, 'outputspec.subject',553 gen_motion_stats, 'inputspec.subject_id')554 555 workflow.connect(funcFlow, 'outputspec.scan',556 gen_motion_stats, 'inputspec.scan_id')557 558 node,out_file = strat.get_node_from_resource_pool('motion_correct')559 workflow.connect(node, out_file,560 gen_motion_stats, 'inputspec.motion_correct')561 562 node, out_file = strat.get_node_from_resource_pool('movement_parameters')563 workflow.connect(node, out_file,564 gen_motion_stats, 'inputspec.movement_parameters')565 566 node, out_file = strat.get_node_from_resource_pool('max_displacement')567 workflow.connect(node, out_file,568 gen_motion_stats, 'inputspec.max_displacement')569 570 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')571 workflow.connect(node, out_file,572 gen_motion_stats, 'inputspec.mask')573 574 except:575 print 'Invalid Connection: Generate Motion Statistics:', num_strat, ' resource_pool: ', strat.get_resource_pool()576 raise577 if 0 in c.runGenerateMotionStatistics:578 tmp = strategy()579 tmp.resource_pool = dict(strat.resource_pool)580 tmp.leaf_node = (strat.leaf_node)581 tmp.leaf_out_file = str(strat.leaf_out_file)582 tmp.name = list(strat.name)583 strat = tmp584 new_strat_list.append(strat)585 586 strat.append_name('gen_motion_stats')587 strat.update_resource_pool({'frame_wise_displacement':(gen_motion_stats, 'outputspec.FD_1D'),588 'scrubbing_frames_excluded':(gen_motion_stats, 'outputspec.frames_ex_1D'),589 'scrubbing_frames_included':(gen_motion_stats, 'outputspec.frames_in_1D'),590 'power_params':(gen_motion_stats,'outputspec.power_params'),591 'motion_params':(gen_motion_stats, 'outputspec.motion_params')})592 num_strat += 1593 strat_list += new_strat_list594 595 """596 Inserting Nuisance Workflow597 """598 new_strat_list = []599 num_strat = 0600 workflow_counter += 1601 if 1 in c.runNuisance:602 workflow_bit_id['nuisance'] = workflow_counter603 for strat in strat_list:604 nuisance = create_nuisance('nuisance_%d' % num_strat)605 606 nuisance.get_node('residuals').iterables = ([('selector', c.Corrections),607 ('compcor_ncomponents', c.nComponents)])608 nuisance.inputs.inputspec.harvard_oxford_mask = c.harvardOxfordMask609 610 try:611 node, out_file = strat.get_leaf_properties()612 workflow.connect(node, out_file,613 nuisance, 'inputspec.subject')614 node, out_file = strat.get_node_from_resource_pool('anatomical_gm_mask')615 workflow.connect(node, out_file,616 nuisance, 'inputspec.gm_mask')617 node, out_file = strat.get_node_from_resource_pool('anatomical_wm_mask')618 workflow.connect(node, out_file,619 nuisance, 'inputspec.wm_mask')620 node, out_file = strat.get_node_from_resource_pool('anatomical_csf_mask')621 workflow.connect(node, out_file,622 nuisance, 'inputspec.csf_mask')623 node, out_file = strat.get_node_from_resource_pool('movement_parameters')624 workflow.connect(node, out_file,625 nuisance, 'inputspec.motion_components')626 627 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')628 workflow.connect(node, out_file,629 nuisance, 'inputspec.func_to_anat_linear_xfm')630 631 node, out_file = strat.get_node_from_resource_pool('mni_to_anatomical_linear_xfm')632 workflow.connect(node, out_file,633 nuisance, 'inputspec.mni_to_anat_linear_xfm')634 635 636 except:637 print 'Invalid Connection: Nuisance:', num_strat, ' resource_pool: ', strat.get_resource_pool()638 raise639 if 0 in c.runNuisance:640 tmp = strategy()641 tmp.resource_pool = dict(strat.resource_pool)642 tmp.leaf_node = (strat.leaf_node)643 tmp.leaf_out_file = str(strat.leaf_out_file)644 tmp.name = list(strat.name)645 strat = tmp646 new_strat_list.append(strat)647 strat.append_name('nuisance')648 strat.set_leaf_properties(nuisance, 'outputspec.subject')649 strat.update_resource_pool({'functional_nuisance_residuals':(nuisance, 'outputspec.subject')})650 num_strat += 1651 strat_list += new_strat_list652 """653 Inserting Median Angle Correction Workflow654 """655 new_strat_list = []656 num_strat = 0657 workflow_counter += 1658 if 1 in c.runMedianAngleCorrection:659 workflow_bit_id['median_angle_corr'] = workflow_counter660 for strat in strat_list:661 median_angle_corr = create_median_angle_correction('median_angle_corr_%d' % num_strat)662 median_angle_corr.get_node('median_angle_correct').iterables = ('target_angle_deg', c.targetAngleDeg)663 try:664 node, out_file = strat.get_leaf_properties()665 workflow.connect(node, out_file,666 median_angle_corr, 'inputspec.subject')667 except:668 print 'Invalid Connection: Median Angle Correction:', num_strat, ' resource_pool: ', strat.get_resource_pool()669 raise670 if 0 in c.runMedianAngleCorrection:671 tmp = strategy()672 tmp.resource_pool = dict(strat.resource_pool)673 tmp.leaf_node = (strat.leaf_node)674 tmp.leaf_out_file = str(strat.leaf_out_file)675 tmp.name = list(strat.name)676 strat = tmp677 new_strat_list.append(strat)678 strat.append_name('median_angle_corr')679 strat.set_leaf_properties(median_angle_corr, 'outputspec.subject')680 strat.update_resource_pool({'functional_median_angle_corrected':(median_angle_corr, 'outputspec.subject')})681 num_strat += 1682 strat_list += new_strat_list683 """684 Inserting ALFF/fALFF685 Workflow686 """687 new_strat_list = []688 num_strat = 0689 if 1 in c.runALFF:690 for strat in strat_list:691 alff = create_alff('alff_%d' % num_strat)692 alff.inputs.hp_input.hp = c.highPassFreqALFF693 alff.inputs.lp_input.lp = c.lowPassFreqALFF694 alff.get_node('hp_input').iterables = ('hp',695 c.highPassFreqALFF)696 alff.get_node('lp_input').iterables = ('lp',697 c.lowPassFreqALFF)698 699 try:700 node, out_file = strat.get_leaf_properties()701 workflow.connect(node, out_file,702 alff, 'inputspec.rest_res')703 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')704 workflow.connect(node, out_file,705 alff, 'inputspec.rest_mask')706 except:707 print 'Invalid Connection: ALFF:', num_strat, ' resource_pool: ', strat.get_resource_pool()708 raise709 strat.append_name('alff_falff')710 strat.update_resource_pool({'alff_img':(alff, 'outputspec.alff_img')})711 strat.update_resource_pool({'falff_img':(alff, 'outputspec.falff_img')})712 strat.update_resource_pool({'alff_Z_img':(alff, 'outputspec.alff_Z_img')})713 strat.update_resource_pool({'falff_Z_img':(alff, 'outputspec.falff_Z_img')})714 num_strat += 1715 strat_list += new_strat_list716 """717 Inserting Frequency Filtering Node718 """719 new_strat_list = []720 num_strat = 0721 workflow_counter += 1722 if 1 in c.runFrequencyFiltering:723 workflow_bit_id['frequency_filter'] = workflow_counter724 for strat in strat_list:725 frequency_filter = pe.Node(util.Function(input_names=['realigned_file',726 'bandpass_freqs',727 'sample_period'],728 output_names=['bandpassed_file'],729 function=bandpass_voxels),730 name='frequency_filter_%d' % num_strat)731 frequency_filter.iterables = ('bandpass_freqs', c.nuisanceBandpassFreq)732 try:733 node, out_file = strat.get_leaf_properties()734 workflow.connect(node, out_file,735 frequency_filter, 'realigned_file')736 except:737 print 'Invalid Connection: Frequency Filtering:', num_strat, ' resource_pool: ', strat.get_resource_pool()738 raise739 if 0 in c.runFrequencyFiltering:740 tmp = strategy()741 tmp.resource_pool = dict(strat.resource_pool)742 tmp.leaf_node = (strat.leaf_node)743 tmp.leaf_out_file = str(strat.leaf_out_file)744 tmp.name = list(strat.name)745 strat = tmp746 new_strat_list.append(strat)747 strat.append_name('frequency_filter')748 strat.set_leaf_properties(frequency_filter, 'bandpassed_file')749 strat.update_resource_pool({'functional_freq_filtered':(frequency_filter, 'bandpassed_file')})750 num_strat += 1751 strat_list += new_strat_list752 """753 Inserting Scrubbing Workflow754 """755 new_strat_list = []756 num_strat = 0757 workflow_counter += 1758 if 1 in c.runScrubbing:759 workflow_bit_id['scrubbing'] = workflow_counter760 for strat in strat_list:761 762 scrubbing = create_scrubbing_preproc('scrubbing_%d'%num_strat)763 764 try:765 766 node, out_file = strat.get_leaf_properties()767 workflow.connect(node, out_file,768 scrubbing, 'inputspec.preprocessed')769 770 node, out_file = strat.get_node_from_resource_pool('scrubbing_frames_included')771 workflow.connect(node, out_file,772 scrubbing, 'inputspec.frames_in_1D')773 774 node, out_file = strat.get_node_from_resource_pool('movement_parameters')775 workflow.connect(node, out_file,776 scrubbing, 'inputspec.movement_parameters')777 778 except:779 print 'Invalid Connection: Scrubbing Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()780 raise781 if 0 in c.runScrubbing:782 tmp = strategy()783 tmp.resource_pool = dict(strat.resource_pool)784 tmp.leaf_node = (strat.leaf_node)785 tmp.leaf_out_file = str(strat.leaf_out_file)786 tmp.name = list(strat.name)787 strat = tmp788 new_strat_list.append(strat)789 strat.append_name('scrubbing')790 strat.set_leaf_properties(scrubbing, 'outputspec.preprocessed')791 strat.update_resource_pool({'scrubbing_movement_parameters' : (scrubbing, 'outputspec.scrubbed_movement_parameters'),792 'scrubbed_preprocessed': (scrubbing, 'outputspec.preprocessed')})793 num_strat += 1794 strat_list += new_strat_list795 """796 Register Functional timeseries to MNI space797 """798 new_strat_list = []799 num_strat = 0800 for strat in strat_list:801 func_mni_warp = pe.Node(interface=fsl.ApplyWarp(),802 name='func_mni_warp_%d' % num_strat)803 func_mni_warp.inputs.ref_file = c.standardResolutionBrain804 functional_brain_mask_to_standard = pe.Node(interface=fsl.ApplyWarp(),805 name='functional_brain_mask_to_standard1_%d' % num_strat)806 functional_brain_mask_to_standard.inputs.interp = 'nn'807 functional_brain_mask_to_standard.inputs.ref_file = c.standard808 try:809 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')810 workflow.connect(node, out_file,811 func_mni_warp, 'field_file')812 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')813 workflow.connect(node, out_file,814 func_mni_warp, 'premat') 815 node, out_file = strat.get_leaf_properties()816 workflow.connect(node, out_file,817 func_mni_warp, 'in_file')818 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')819 workflow.connect(node, out_file,820 functional_brain_mask_to_standard, 'in_file')821 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')822 workflow.connect(node, out_file,823 functional_brain_mask_to_standard, 'premat')824 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')825 workflow.connect(node, out_file,826 functional_brain_mask_to_standard, 'field_file')827 except:828 print 'Invalid Connection: Register Functional timeseries to MNI space:', num_strat, ' resource_pool: ', strat.get_resource_pool()829 raise830 strat.update_resource_pool({'functional_mni':(func_mni_warp, 'out_file'),831 'functional_brain_mask_to_standard':(functional_brain_mask_to_standard, 'out_file')})832 num_strat += 1833 strat_list += new_strat_list834 """835 Inserting VMHC836 Workflow837 """838 new_strat_list = []839 num_strat = 0840 if 1 in c.runVMHC:841 for strat in strat_list:842 preproc = create_vmhc()843 preproc.inputs.inputspec.brain_symmetric = \844 c.brainSymmetric845 preproc.inputs.inputspec.symm_standard = \846 c.symmStandard847 preproc.inputs.inputspec.twomm_brain_mask_dil = \848 c.twommBrainMaskDiluted849 preproc.inputs.inputspec.config_file_twomm = \850 c.configFileTwomm851 preproc.inputs.inputspec.standard = \852 c.standard853 preproc.inputs.fwhm_input.fwhm = c.fwhm854 preproc.get_node('fwhm_input').iterables = ('fwhm',855 c.fwhm)856 vmhc = preproc.clone('vmhc_%d' % num_strat)857 try:858 node, out_file = strat.get_leaf_properties()859 workflow.connect(node, out_file,860 vmhc, 'inputspec.rest_res')861 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')862 workflow.connect(node, out_file,863 vmhc, 'inputspec.example_func2highres_mat')864 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')865 workflow.connect(node, out_file,866 vmhc, 'inputspec.rest_mask')867 node, out_file = strat.get_node_from_resource_pool('anatomical_brain')868 workflow.connect(node, out_file,869 vmhc, 'inputspec.brain')870 node, out_file = strat.get_node_from_resource_pool('anatomical_reorient')871 workflow.connect(node, out_file,872 vmhc, 'inputspec.reorient')873 except:874 print 'Invalid Connection: VMHC:', num_strat, ' resource_pool: ', strat.get_resource_pool()875 raise876 strat.update_resource_pool({'vmhc_raw_score':(vmhc, 'outputspec.VMHC_FWHM_img')})877 strat.update_resource_pool({'vmhc_z_score':(vmhc, 'outputspec.VMHC_Z_FWHM_img')})878 strat.update_resource_pool({'vmhc_z_score_stat_map':(vmhc, 'outputspec.VMHC_Z_stat_FWHM_img')})879 strat.append_name('vmhc')880 num_strat += 1881 strat_list += new_strat_list882 """883 Inserting REHO884 Workflow885 """886 new_strat_list = []887 num_strat = 0888 if 1 in c.runReHo:889 for strat in strat_list:890 preproc = create_reho()891 preproc.inputs.inputspec.cluster_size = c.clusterSize892 reho = preproc.clone('reho_%d' % num_strat)893 try:894 node, out_file = strat.get_leaf_properties()895 workflow.connect(node, out_file,896 reho, 'inputspec.rest_res_filt')897 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')898 workflow.connect(node, out_file,899 reho, 'inputspec.rest_mask')900 except:901 print 'Invalid Connection: REHO:', num_strat, ' resource_pool: ', strat.get_resource_pool()902 raise903 strat.update_resource_pool({'raw_reho_map':(reho, 'outputspec.raw_reho_map')})904 strat.update_resource_pool({'reho_Z_img':(reho, 'outputspec.z_score')})905 strat.append_name('reho')906 num_strat += 1907 strat_list += new_strat_list908 """909 Transforming ALFF Z scores and fAlff Z scores to MNI910 """911 new_strat_list = []912 num_strat = 0913 if 1 in c.runRegisterFuncToMNI and (1 in c.runALFF):914 for strat in strat_list:915 alff_Z_to_standard = pe.Node(interface=fsl.ApplyWarp(),916 name='alff_Z_to_standard_%d' % num_strat)917 alff_Z_to_standard.inputs.ref_file = c.standard918 falff_Z_to_standard = alff_Z_to_standard.clone('falff_Z_to_standard_%d' % num_strat)919 falff_Z_to_standard.inputs.ref_file = c.standard920 try:921 node, out_file = strat.get_node_from_resource_pool('alff_Z_img')922 workflow.connect(node, out_file,923 alff_Z_to_standard, 'in_file')924 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')925 workflow.connect(node, out_file,926 alff_Z_to_standard, 'premat')927 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')928 workflow.connect(node, out_file,929 alff_Z_to_standard, 'field_file')930 node, out_file = strat.get_node_from_resource_pool('falff_Z_img')931 workflow.connect(node, out_file,932 falff_Z_to_standard, 'in_file')933 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')934 workflow.connect(node, out_file,935 falff_Z_to_standard, 'premat')936 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')937 workflow.connect(node, out_file,938 falff_Z_to_standard, 'field_file')939 except:940 print 'Invalid Connection: Register Functional to MNI:', num_strat, ' resource_pool: ', strat.get_resource_pool()941 raise942 strat.update_resource_pool({'alff_Z_to_standard':(alff_Z_to_standard, 'out_file')})943 strat.update_resource_pool({'falff_Z_to_standard':(falff_Z_to_standard, 'out_file')})944 strat.append_name('alff_falff_to_standard')945 num_strat += 1946 strat_list += new_strat_list947 inputnode_fwhm = None948 if len(c.fwhm) > 0:949 inputnode_fwhm = pe.Node(util.IdentityInterface(fields=['fwhm']),950 name='fwhm_input')951 inputnode_fwhm.iterables = ("fwhm", c.fwhm)952 """ 953 Smoothing ALFF fALFF Z scores and or possibly Z scores in MNI 954 """955 new_strat_list = []956 num_strat = 0957 if (1 in c.runALFF) and len(c.fwhm) > 0:958 for strat in strat_list:959 alff_Z_to_standard_smooth = None960 falff_Z_to_standard_smooth = None961 alff_Z_smooth = pe.Node(interface=fsl.MultiImageMaths(),962 name='alff_Z_smooth_%d' % num_strat)963 falff_Z_smooth = alff_Z_smooth.clone('falff_Z_smooth_%d' % num_strat)964 try:965 node, out_file = strat.get_node_from_resource_pool('alff_Z_img')966 workflow.connect(node, out_file,967 alff_Z_smooth, 'in_file')968 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),969 alff_Z_smooth, 'op_string')970 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')971 workflow.connect(node, out_file,972 alff_Z_smooth, 'operand_files')973 node, out_file = strat.get_node_from_resource_pool('falff_Z_img')974 workflow.connect(node, out_file,975 falff_Z_smooth, 'in_file')976 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),977 falff_Z_smooth, 'op_string')978 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')979 workflow.connect(node, out_file,980 falff_Z_smooth, 'operand_files')981 except:982 print 'Invalid Connection: ALFF smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()983 raise984 strat.append_name('alff_falff_Z_smooth')985 strat.update_resource_pool({'alff_Z_smooth':(alff_Z_smooth, 'out_file')})986 strat.update_resource_pool({'falff_Z_smooth':(falff_Z_smooth, 'out_file')})987 if c.runRegisterFuncToMNI:988 alff_Z_to_standard_smooth = alff_Z_smooth.clone('alff_Z_to_standard_smooth_%d' % num_strat)989 falff_Z_to_standard_smooth = alff_Z_smooth.clone('falff_Z_to_standard_smooth_%d' % num_strat)990 try:991 node, out_file = strat.get_node_from_resource_pool('alff_Z_to_standard')992 workflow.connect(node, out_file,993 alff_Z_to_standard_smooth, 'in_file')994 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),995 alff_Z_to_standard_smooth, 'op_string')996 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask_to_standard')997 workflow.connect(node, out_file,998 alff_Z_to_standard_smooth, 'operand_files')999 node, out_file = strat.get_node_from_resource_pool('falff_Z_to_standard')1000 workflow.connect(node, out_file,1001 falff_Z_to_standard_smooth, 'in_file')1002 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1003 falff_Z_to_standard_smooth, 'op_string')1004 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask_to_standard')1005 workflow.connect(node, out_file,1006 falff_Z_to_standard_smooth, 'operand_files')1007 except:1008 print 'Invalid Connection: ALFF smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1009 raise1010 strat.append_name('alff_falff_Z_to_standard_smooth')1011 strat.update_resource_pool({'alff_Z_to_standard_smooth':(alff_Z_to_standard_smooth, 'out_file')})1012 strat.update_resource_pool({'falff_Z_to_standard_smooth':(falff_Z_to_standard_smooth, 'out_file')})1013 num_strat += 11014 strat_list += new_strat_list1015 """1016 Transforming ReHo Z scores to MNI1017 """1018 new_strat_list = []1019 num_strat = 01020 if 1 in c.runRegisterFuncToMNI and (1 in c.runReHo):1021 for strat in strat_list:1022 reho_Z_to_standard = pe.Node(interface=fsl.ApplyWarp(),1023 name='reho_Z_to_standard_%d' % num_strat)1024 reho_Z_to_standard.inputs.ref_file = c.standard1025 try:1026 node, out_file = strat.get_node_from_resource_pool('reho_Z_img')1027 workflow.connect(node, out_file,1028 reho_Z_to_standard, 'in_file')1029 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')1030 workflow.connect(node, out_file,1031 reho_Z_to_standard, 'premat')1032 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')1033 workflow.connect(node, out_file,1034 reho_Z_to_standard, 'field_file')1035 except:1036 print 'Invalid Connection: Register Functional to MNI:', num_strat, ' resource_pool: ', strat.get_resource_pool()1037 raise1038 strat.update_resource_pool({'reho_Z_to_standard':(reho_Z_to_standard, 'out_file')})1039 strat.append_name('reho_Z_transform')1040 num_strat += 11041 strat_list += new_strat_list1042 """1043 1044 Smoothing ReHo Z scores and or possibly Z scores in MNI 1045 """1046 new_strat_list = []1047 num_strat = 01048 if (1 in c.runReHo) and len(c.fwhm) > 0:1049 for strat in strat_list:1050 reho_Z_to_standard_smooth = None1051 reho_Z_smooth = pe.Node(interface=fsl.MultiImageMaths(),1052 name='reho_Z_smooth_%d' % num_strat)1053 try:1054 node, out_file = strat.get_node_from_resource_pool('reho_Z_img')1055 workflow.connect(node, out_file,1056 reho_Z_smooth, 'in_file')1057 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1058 reho_Z_smooth, 'op_string')1059 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')1060 workflow.connect(node, out_file,1061 reho_Z_smooth, 'operand_files')1062 except:1063 print 'Invalid Connection: reho_Z smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1064 raise1065 strat.append_name('reho_Z_smooth')1066 strat.update_resource_pool({'reho_Z_smooth':(reho_Z_smooth, 'out_file')})1067 if 1 in c.runRegisterFuncToMNI:1068 reho_Z_to_standard_smooth = reho_Z_smooth.clone('reho_Z_to_standard_smooth_%d' % num_strat)1069 try:1070 node, out_file = strat.get_node_from_resource_pool('reho_Z_to_standard')1071 workflow.connect(node, out_file,1072 reho_Z_to_standard_smooth, 'in_file')1073 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1074 reho_Z_to_standard_smooth, 'op_string')1075 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask_to_standard')1076 workflow.connect(functional_brain_mask_to_standard, 'out_file',1077 reho_Z_to_standard_smooth, 'operand_files')1078 except:1079 print 'Invalid Connection: reho_Z_to_standard smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1080 raise1081 strat.append_name('reho_Z_to_standard_smooth')1082 strat.update_resource_pool({'reho_Z_to_standard_smooth':(reho_Z_to_standard_smooth, 'out_file')})1083 num_strat += 11084 strat_list += new_strat_list1085 """1086 Voxel Based Time Series 1087 """1088 new_strat_list = []1089 num_strat = 01090 if 1 in c.runVoxelTimeseries:1091 for strat in strat_list:1092 resample_functional_to_mask = pe.Node(interface=fsl.FLIRT(),1093 name='resample_functional_to_mask_%d' % num_strat)1094 resample_functional_to_mask.inputs.interp = 'nearestneighbour'1095 resample_functional_to_mask.inputs.apply_xfm = True1096 resample_functional_to_mask.inputs.in_matrix_file = c.identityMatrix1097 mask_dataflow = create_mask_dataflow(c.maskSpecificationFile, 'mask_dataflow_%d' % num_strat)1098 voxel_timeseries = get_voxel_timeseries('voxel_timeseries_%d' % num_strat)1099 voxel_timeseries.inputs.inputspec.output_type = c.voxelTSOutputs1100 try:1101 node, out_file = strat.get_node_from_resource_pool('functional_mni')1102 #resample the input functional file to mask 1103 workflow.connect(node, out_file,1104 resample_functional_to_mask, 'in_file' )1105 workflow.connect(mask_dataflow, 'select_mask.out_file',1106 resample_functional_to_mask, 'reference')1107 #connect it to the voxel_timeseries1108 workflow.connect(mask_dataflow, 'select_mask.out_file',1109 voxel_timeseries, 'input_mask.mask')1110 workflow.connect(resample_functional_to_mask, 'out_file',1111 voxel_timeseries, 'inputspec.rest')1112 except:1113 print 'Invalid Connection: Voxel TimeSeries Analysis Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()1114 raise1115 if 0 in c.runVoxelTimeseries:1116 tmp = strategy()1117 tmp.resource_pool = dict(strat.resource_pool)1118 tmp.leaf_node = (strat.leaf_node)1119 tmp.leaf_out_file = str(strat.leaf_out_file)1120 tmp.name = list(strat.name)1121 strat = tmp1122 new_strat_list.append(strat)1123 strat.append_name('voxel_timeseries')1124 strat.update_resource_pool({'voxel_timeseries': (voxel_timeseries, 'outputspec.mask_outputs')})1125 num_strat += 11126 strat_list += new_strat_list1127 """1128 ROI Based Time Series1129 """1130 new_strat_list = []1131 num_strat = 01132 if 1 in c.runROITimeseries:1133 for strat in strat_list:1134 resample_functional_to_roi = pe.Node(interface=fsl.FLIRT(), 1135 name='resample_functional_to_roi_%d'%num_strat)1136 resample_functional_to_roi.inputs.interp = 'nearestneighbour'1137 resample_functional_to_roi.inputs.apply_xfm = True1138 resample_functional_to_roi.inputs.in_matrix_file = c.identityMatrix1139 roi_dataflow = create_roi_dataflow(c.roiSpecificationFile, 'roi_dataflow_%d'%num_strat)1140 roi_timeseries = get_roi_timeseries('roi_timeseries_%d'%num_strat) 1141 roi_timeseries.inputs.inputspec.output_type = c.roiTSOutputs1142 try:1143 node, out_file = strat.get_node_from_resource_pool('functional_mni')1144 #resample the input functional file to roi1145 workflow.connect(node, out_file,1146 resample_functional_to_roi,'in_file' )1147 workflow.connect(roi_dataflow, 'select_roi.out_file',1148 resample_functional_to_roi, 'reference')1149 #connect it to the roi_timeseries1150 workflow.connect(roi_dataflow, 'select_roi.out_file',1151 roi_timeseries, 'input_roi.roi')1152 workflow.connect(resample_functional_to_roi, 'out_file',1153 roi_timeseries, 'inputspec.rest')1154 except:1155 print 'Invalid Connection: ROI TimeSeries Analysis Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()1156 raise1157 if 0 in c.runROITimeseries:1158 tmp = strategy()1159 tmp.resource_pool = dict(strat.resource_pool)1160 tmp.leaf_node = (strat.leaf_node)1161 tmp.leaf_out_file = str(strat.leaf_out_file)1162 tmp.name = list(strat.name)1163 strat = tmp1164 new_strat_list.append(strat)1165 strat.append_name('roi_timeseries')1166 strat.update_resource_pool({'roi_timeseries' : (roi_timeseries, 'outputspec.roi_outputs')})1167 num_strat += 11168 strat_list += new_strat_list1169 """1170 Inserting SCA1171 Workflow for ROI INPUT1172 """1173 new_strat_list = []1174 num_strat = 01175 if 1 in c.runSCA and (1 in c.runROITimeseries):1176 for strat in strat_list:1177 sca_roi = create_sca('sca_roi_%d' % num_strat)1178 try:1179 node, out_file = strat.get_leaf_properties()1180 workflow.connect(node, out_file,1181 sca_roi, 'inputspec.functional_file')1182 node, out_file = strat.get_node_from_resource_pool('roi_timeseries')1183 workflow.connect(node, (out_file, extract_one_d),1184 sca_roi, 'inputspec.timeseries_one_d')1185 except:1186 print 'Invalid Connection: SCA ROI:', num_strat, ' resource_pool: ', strat.get_resource_pool()1187 raise1188 strat.update_resource_pool({'sca_roi_correlations':(sca_roi, 'outputspec.correlation_file')})1189 strat.update_resource_pool({'sca_roi_Z':(sca_roi, 'outputspec.Z_score')})1190 strat.append_name('sca_rois')1191 num_strat += 11192 strat_list += new_strat_list1193 """1194 Inserting SCA1195 Workflow for Voxel INPUT1196 """1197 new_strat_list = []1198 num_strat = 01199 if 1 in c.runSCA and (1 in c.runVoxelTimeseries):1200 for strat in strat_list:1201 sca_seed = create_sca('sca_seed_%d' % num_strat)1202 try:1203 node, out_file = strat.get_leaf_properties()1204 workflow.connect(node, out_file,1205 sca_seed, 'inputspec.functional_file')1206 node, out_file = strat.get_node_from_resource_pool('voxel_timeseries')1207 workflow.connect(node, (out_file, extract_one_d),1208 sca_seed, 'inputspec.timeseries_one_d')1209 except:1210 print 'Invalid Connection: SCA:', num_strat, ' resource_pool: ', strat.get_resource_pool()1211 raise1212 strat.update_resource_pool({'sca_seed_correlations':(sca_seed, 'outputspec.correlation_file')})1213 strat.update_resource_pool({'sca_seed_Z':(sca_seed, 'outputspec.Z_score')})1214 strat.append_name('sca_seeds')1215 num_strat += 11216 strat_list += new_strat_list1217 """1218 Transforming SCA Voxel Z scores to MNI1219 """1220 new_strat_list = []1221 num_strat = 01222 if 1 in c.runRegisterFuncToMNI and (1 in c.runSCA) and (1 in c.runVoxelTimeseries):1223 for strat in strat_list:1224 sca_seed_Z_to_standard = pe.MapNode(interface=fsl.ApplyWarp(),1225 name='sca_seed_Z_to_standard_%d' % num_strat, iterfield=['in_file'])1226 sca_seed_Z_to_standard.inputs.ref_file = c.standard1227 try:1228 node, out_file = strat.get_node_from_resource_pool('sca_seed_Z')1229 workflow.connect(node, out_file,1230 sca_seed_Z_to_standard, 'in_file')1231 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')1232 workflow.connect(node, out_file,1233 sca_seed_Z_to_standard, 'premat')1234 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')1235 workflow.connect(node, out_file,1236 sca_seed_Z_to_standard, 'field_file')1237 except:1238 print 'Invalid Connection: Register Functional to MNI:', num_strat, ' resource_pool: ', strat.get_resource_pool()1239 raise1240 strat.update_resource_pool({'sca_seed_Z_to_standard':(sca_seed_Z_to_standard, 'out_file')})1241 strat.append_name('sca_seed_based_to_mni')1242 num_strat += 11243 strat_list += new_strat_list1244 """1245 Transforming SCA ROI Z scores to MNI1246 """1247 new_strat_list = []1248 num_strat = 01249 if 1 in c.runRegisterFuncToMNI and (1 in c.runSCA) and (1 in c.runROITimeseries):1250 for strat in strat_list:1251 sca_roi_Z_to_standard = pe.MapNode(interface=fsl.ApplyWarp(),1252 name='sca_roi_Z_to_standard_%d' % num_strat, iterfield=['in_file'])1253 sca_roi_Z_to_standard.inputs.ref_file = c.standard1254 try:1255 node, out_file = strat.get_node_from_resource_pool('sca_roi_Z')1256 workflow.connect(node, out_file,1257 sca_roi_Z_to_standard, 'in_file')1258 node, out_file = strat.get_node_from_resource_pool('functional_to_anat_linear_xfm')1259 workflow.connect(node, out_file,1260 sca_roi_Z_to_standard, 'premat')1261 node, out_file = strat.get_node_from_resource_pool('anatomical_to_mni_nonlinear_xfm')1262 workflow.connect(node, out_file,1263 sca_roi_Z_to_standard, 'field_file')1264 except:1265 print 'Invalid Connection: Register Functional to MNI:', num_strat, ' resource_pool: ', strat.get_resource_pool()1266 raise1267 strat.update_resource_pool({'sca_roi_Z_to_standard':(sca_roi_Z_to_standard, 'out_file')})1268 strat.append_name('sca_roi_based_to_mni')1269 num_strat += 11270 strat_list += new_strat_list1271 """1272 1273 Smoothing SCA seed based Z scores and or possibly Z scores in MNI 1274 """1275 new_strat_list = []1276 num_strat = 01277 if (1 in c.runSCA) and (1 in c.runVoxelTimeseries) and len(c.fwhm) > 0:1278 for strat in strat_list:1279 sca_seed_Z_to_standard_smooth = None1280 sca_seed_Z_smooth = pe.MapNode(interface=fsl.MultiImageMaths(),1281 name='sca_seed_Z_smooth_%d' % num_strat, iterfield=['in_file'])1282 try:1283 node, out_file = strat.get_node_from_resource_pool('sca_seed_Z')1284 workflow.connect(node, out_file,1285 sca_seed_Z_smooth, 'in_file')1286 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1287 sca_seed_Z_smooth, 'op_string')1288 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')1289 workflow.connect(node, out_file,1290 sca_seed_Z_smooth, 'operand_files')1291 except:1292 print 'Invalid Connection: sca_Z smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1293 raise1294 strat.append_name('sca_seed_Z_smooth')1295 strat.update_resource_pool({'sca_seed_Z_smooth':(sca_seed_Z_smooth, 'out_file')})1296 if 1 in c.runRegisterFuncToMNI:1297 sca_seed_Z_to_standard_smooth = sca_seed_Z_smooth.clone('sca_seed_Z_to_standard_smooth_%d' % num_strat)1298 try:1299 node, out_file = strat.get_node_from_resource_pool('sca_seed_Z_to_standard')1300 workflow.connect(node, out_file,1301 sca_seed_Z_to_standard_smooth, 'in_file')1302 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1303 sca_seed_Z_to_standard_smooth, 'op_string')1304 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask_to_standard')1305 workflow.connect(node, out_file,1306 sca_seed_Z_to_standard_smooth, 'operand_files')1307 except:1308 print 'Invalid Connection: sca_seed_Z_to_standard smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1309 raise1310 strat.append_name('sca_seed_Z_to_standard_smooth')1311 strat.update_resource_pool({'sca_seed_Z_to_standard_smooth':(sca_seed_Z_to_standard_smooth, 'out_file')})1312 num_strat += 11313 strat_list += new_strat_list1314 """1315 1316 Smoothing SCA roi based Z scores and or possibly Z scores in MNI 1317 """1318 if (1 in c.runSCA) and (1 in c.runROITimeseries) and len(c.fwhm) > 0:1319 for strat in strat_list:1320 sca_roi_Z_to_standard_smooth = None1321 sca_roi_Z_smooth = pe.MapNode(interface=fsl.MultiImageMaths(),1322 name='sca_roi_Z_smooth_%d' % num_strat, iterfield=['in_file'])1323 try:1324 node, out_file = strat.get_node_from_resource_pool('sca_roi_Z')1325 workflow.connect(node, out_file,1326 sca_roi_Z_smooth, 'in_file')1327 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1328 sca_roi_Z_smooth, 'op_string')1329 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask')1330 workflow.connect(node, out_file,1331 sca_roi_Z_smooth, 'operand_files')1332 except:1333 print 'Invalid Connection: sca_Z smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1334 raise1335 strat.append_name('sca_roi_Z_to_smooth')1336 strat.update_resource_pool({'sca_roi_Z_smooth':(sca_roi_Z_smooth, 'out_file')})1337 if 1 in c.runRegisterFuncToMNI:1338 sca_roi_Z_to_standard_smooth = sca_roi_Z_smooth.clone('sca_roi_Z_to_standard_smooth_%d' % num_strat)1339 try:1340 node, out_file = strat.get_node_from_resource_pool('sca_roi_Z_to_standard')1341 workflow.connect(node, out_file,1342 sca_roi_Z_to_standard_smooth, 'in_file')1343 workflow.connect(inputnode_fwhm, ('fwhm', set_gauss),1344 sca_roi_Z_to_standard_smooth, 'op_string')1345 node, out_file = strat.get_node_from_resource_pool('functional_brain_mask_to_standard')1346 workflow.connect(node, out_file,1347 sca_roi_Z_to_standard_smooth, 'operand_files')1348 except:1349 print 'Invalid Connection: sca_roi_Z_to_standard smooth:', num_strat, ' resource_pool: ', strat.get_resource_pool()1350 raise1351 strat.append_name('sca_roi_Z_to_standard_smooth')1352 strat.update_resource_pool({'sca_roi_Z_to_standard_smooth':(sca_roi_Z_to_standard_smooth, 'out_file')})1353 num_strat += 11354 strat_list += new_strat_list1355 """1356 Inserting Surface Registration1357 """1358 new_strat_list = []1359 num_strat = 01360 1361 workflow_counter += 11362 if 1 in c.runSurfaceRegistraion:1363 workflow_bit_id['surface_registration'] = workflow_counter1364 for strat in strat_list:1365 1366 surface_reg = create_surface_registration('surface_reg_%d'%num_strat)1367 surface_reg.inputs.inputspec.recon_subjects = c.reconSubjectsDirectory1368 surface_reg.inputs.inputspec.subject_id = subject_id1369 1370 try:1371 1372 node, out_file = strat.get_leaf_properties()1373 workflow.connect(node, out_file,1374 surface_reg,'inputspec.rest' )1375 1376 node, out_file = strat.get_node_from_resource_pool('anatomical_brain')1377 workflow.connect(node, out_file,1378 surface_reg, 'inputspec.brain')1379 1380 except:1381 print 'Invalid Connection: Surface Registration Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()1382 raise1383 if 0 in c.runSurfaceRegistraion:1384 tmp = strategy()1385 tmp.resource_pool = dict(strat.resource_pool)1386 tmp.leaf_node = (strat.leaf_node)1387 tmp.leaf_out_file = str(strat.leaf_out_file)1388 tmp.name = list(strat.name)1389 strat = tmp1390 new_strat_list.append(strat)1391 1392 strat.append_name('surface_registration')1393 1394 strat.update_resource_pool({'bbregister_registration' : (surface_reg, 'outputspec.out_reg_file'),1395 'left_hemisphere_surface' : (surface_reg, 'outputspec.lh_surface_file'),1396 'right_hemisphere_surface' : (surface_reg, 'outputspec.rh_surface_file')})1397 1398 num_strat += 11399 strat_list += new_strat_list 1400 1401 """1402 Inserting vertices based timeseries1403 """1404 new_strat_list = []1405 num_strat = 01406 1407 if 1 in c.runVerticesTimeSeries:1408 for strat in strat_list:1409 1410 vertices_timeseries = get_vertices_timeseries('vertices_timeseries_%d'%num_strat)1411 1412 try:1413 1414 node, out_file = strat.get_node_from_resource_pool('left_hemisphere_surface')1415 workflow.connect(node, out_file,1416 vertices_timeseries, 'inputspec.lh_surface_file')1417 1418 node, out_file = strat.get_node_from_resource_pool('right_hemisphere_surface')1419 workflow.connect(node, out_file,1420 vertices_timeseries, 'inputspec.rh_surface_file') 1421 1422 except:1423 print 'Invalid Connection: Vertices Time Series Extraction Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()1424 raise1425 if 0 in c.runVerticesTimeSeries:1426 tmp = strategy()1427 tmp.resource_pool = dict(strat.resource_pool)1428 tmp.leaf_node = (strat.leaf_node)1429 tmp.leaf_out_file = str(strat.leaf_out_file)1430 tmp.name = list(strat.name)1431 strat = tmp1432 new_strat_list.append(strat)1433 1434 strat.append_name('surface_registration')1435 1436 strat.update_resource_pool({'vertices_timeseries' : (vertices_timeseries, 'outputspec.surface_outputs')})1437 1438 num_strat += 11439 strat_list += new_strat_list 1440 1441 """1442 Inserting Network centrality1443 """1444 new_strat_list = []1445 num_strat = 01446 1447 if 1 in c.runNetworkCentrality:1448 for strat in strat_list:1449 1450 1451 1452 resample_functional_to_template = pe.Node(interface=fsl.FLIRT(), 1453 name='resample_functional_to_template_%d'%num_strat)1454 resample_functional_to_template.inputs.interp = 'nearestneighbour'1455 resample_functional_to_template.inputs.apply_xfm = True1456 resample_functional_to_template.inputs.in_matrix_file = c.identityMatrix1457 1458 template_dataflow = create_mask_dataflow(c.templateSpecificationFile, 'template_dataflow_%d'%num_strat)1459 1460 network_centrality = create_resting_state_graphs(c.memoryAllocatedForDegreeCentrality, 'network_centrality_%d'%num_strat)1461 network_centrality.inputs.inputspec.threshold_option = c.correlationThresholdOption1462 network_centrality.inputs.inputspec.threshold = c.correlationThreshold1463 network_centrality.inputs.centrality_options.weight_options = c.centralityWeightOptions1464 network_centrality.inputs.centrality_options.method_options = c.centralityMethodOptions1465 1466 1467 1468 try:1469 1470 node, out_file = strat.get_node_from_resource_pool('functional_mni')1471 1472 #resample the input functional file to template(roi/mask) 1473 workflow.connect(node, out_file,1474 resample_functional_to_template, 'in_file' )1475 workflow.connect(template_dataflow, 'outputspec.out_file',1476 resample_functional_to_template, 'reference')1477 1478 workflow.connect(resample_functional_to_template, 'out_file',1479 network_centrality, 'inputspec.subject')1480 workflow.connect(template_dataflow, 'outputspec.out_file',1481 network_centrality, 'inputspec.template')1482 strat.append_name('network_centrality')1483 1484 strat.update_resource_pool({'centrality_outputs' : (network_centrality, 'outputspec.centrality_outputs')})1485 1486 1487 #if smoothing is required1488 if len(c.fwhm) > 0 :1489 1490 z_score = get_zscore('centrality_zscore_%d'%num_strat)1491 1492 smoothing = pe.MapNode(interface=fsl.MultiImageMaths(),1493 name='smooth_centrality_%d'% num_strat, 1494 iterfield=['in_file'])1495 1496 #calculate zscores1497 workflow.connect(template_dataflow, 'outputspec.out_file',1498 z_score, 'inputspec.mask_file')1499 workflow.connect(network_centrality, 'outputspec.centrality_outputs',1500 z_score, 'inputspec.input_file')1501 1502 #connecting zscores to smoothing1503 workflow.connect(template_dataflow, 'outputspec.out_file',1504 smoothing, 'operand_files')1505 workflow.connect(z_score, 'outputspec.z_score_img',1506 smoothing, 'in_file')1507 workflow.connect(inputnode_fwhm,('fwhm', set_gauss),1508 smoothing, 'op_string')1509 1510 strat.update_resource_pool({'centrality_outputs_smoothed' : (smoothing, 'out_file'),1511 'centrality_outputs_zscore' : (z_score, 'outputspec.z_score_img')})1512 1513 1514 1515 except:1516 print 'Invalid Connection: Network Centrality Workflow:', num_strat, ' resource_pool: ', strat.get_resource_pool()1517 raise1518 if 0 in c.runNetworkCentrality:1519 tmp = strategy()1520 tmp.resource_pool = dict(strat.resource_pool)1521 tmp.leaf_node = (strat.leaf_node)1522 tmp.leaf_out_file = str(strat.leaf_out_file)1523 tmp.name = list(strat.name)1524 strat = tmp1525 new_strat_list.append(strat)1526 num_strat += 11527 strat_list += new_strat_list 1528 ###################### end of workflow ###########1529 try:1530 workflow.write_graph(graph2use='orig')1531 except:1532 pass1533 """1534 Datasink1535 """1536 import networkx as nx1537 num_strat = 01538 sink_idx = 01539 for strat in strat_list:1540 rp = strat.get_resource_pool()1541 # build helper dictionary to assist with a clean strategy label for symlinks1542 strategy_tag_helper_symlinks = {}1543 if 'scrubbing' in strat.get_name():1544 strategy_tag_helper_symlinks['_threshold'] = 11545 else:1546 strategy_tag_helper_symlinks['_threshold'] = 01547 if 'seg_preproc' in strat.get_name():1548 strategy_tag_helper_symlinks['_csf_threshold'] = 11549 strategy_tag_helper_symlinks['_wm_threshold'] = 11550 strategy_tag_helper_symlinks['_gm_threshold'] = 11551 else:1552 strategy_tag_helper_symlinks['_csf_threshold'] = 01553 strategy_tag_helper_symlinks['_wm_threshold'] = 01554 strategy_tag_helper_symlinks['_gm_threshold'] = 01555 if 'median_angle_corr' in strat.get_name():1556 strategy_tag_helper_symlinks['_target_angle_deg'] = 11557 else:1558 strategy_tag_helper_symlinks['_target_angle_deg'] = 01559 if 'nuisance' in strat.get_name():1560 strategy_tag_helper_symlinks['nuisance'] = 11561 else:1562 strategy_tag_helper_symlinks['nuisance'] = 01563 strat_tag = ""1564 hash_val = 01565 for name in strat.get_name():1566 if not ('alff' in name.lower()) and not ('vmhc' in name.lower()) \1567 and not ('reho' in name.lower()) and not ('sca' in name.lower()) \1568 and not ('network_centrality' in name.lower()) and not ('timeseries' in name.lower()):1569 strat_tag += name + '_'1570 print name, ' ~~~ ', 2 ** workflow_bit_id[name]1571 hash_val += 2 ** workflow_bit_id[name]1572 pipeline_id = ''1573 pipeline_id = linecache.getline(os.path.realpath(os.path.join(CPAC.__path__[0], 'utils', 'pipeline_names.py')), hash_val)1574 pipeline_id = pipeline_id.rstrip('\r\n')1575 if pipeline_id == '':1576 print 'hash value ', hash_val, ' is greater than the number of words'1577 print 'resorting to crc32 value as pipeline_id'1578 pipeline_id = zlib.crc32(strat_tag)1579 print strat_tag, ' ~~~~~ ', hash_val, ' ~~~~~~ ', pipeline_id1580 for key in rp.keys():1581 ds = pe.Node(nio.DataSink(), name='sinker_%d' % sink_idx)1582 ds.inputs.base_directory = c.sinkDirectory1583 ds.inputs.container = os.path.join('pipeline_%s' % pipeline_id, subject_id)1584 ds.inputs.regexp_substitutions = [(r"/_sca_roi(.)*[/]", '/'),1585 (r"/_smooth_centrality_(\d)+[/]", '/'),1586 (r"/_z_score(\d)+[/]", "/")]1587 node, out_file = rp[key]1588 workflow.connect(node, out_file,1589 ds, key)1590 if 1 in c.runSymbolicLinks:1591 link_node = pe.Node(interface=util.Function(input_names=['in_file', 'strategies',1592 'subject_id', 'pipeline_id', 'helper'],1593 output_names=[],1594 function=prepare_symbolic_links),1595 name='link_%d' % sink_idx, iterfield=['in_file'])1596 link_node.inputs.strategies = strategies1597 link_node.inputs.subject_id = subject_id1598 link_node.inputs.pipeline_id = 'pipeline_%s' % (pipeline_id)1599 link_node.inputs.helper = dict(strategy_tag_helper_symlinks)1600 workflow.connect(ds, 'out_file', link_node, 'in_file')1601 sink_idx += 11602 d_name = os.path.join(c.sinkDirectory, ds.inputs.container)1603 if not os.path.exists(d_name):1604 os.makedirs(d_name)1605 try:1606 G = nx.DiGraph()1607 strat_name = strat.get_name()1608 G.add_edges_from([(strat_name[s], strat_name[s+1]) for s in range(len(strat_name)-1)])1609 dotfilename = os.path.join(d_name, 'strategy.dot')1610 nx.write_dot(G, dotfilename)1611 format_dot(dotfilename, 'png')1612 except:1613 print "Cannot Create the strategy and pipeline graph, dot or/and pygraphviz is not installed"1614 pass1615 print d_name, '*'1616 num_strat += 11617 workflow.run(plugin='MultiProc',1618 plugin_args={'n_procs': c.numCoresPerSubject})1619# workflow.run(updatehash=True)1620 sub_w_path = os.path.join(c.workingDirectory, wfname)1621 1622 if c.removeWorkingDir:1623 try:1624 if os.path.exists(sub_w_path):1625 import shutil1626 print "removing dir -> ", sub_w_path1627 shutil.rmtree(sub_w_path)1628 except:1629 print "Couldn't remove subjects %s working directory"%(wfname)1630 pass1631 1632 print "End of subject workflow ", wfname1633 1634 return workflow1635def run(config, subject_list_file, indx, strategies):1636 import commands1637 commands.getoutput('source ~/.bashrc')1638 import os1639 import sys1640 import argparse1641 import pickle1642 import yaml1643 1644 1645 c = Configuration(yaml.load(open(os.path.realpath(config), 'r'))) 1646 1647 try:1648 sublist = yaml.load(open(os.path.realpath(subject_list_file), 'r'))1649 except:1650 raise Exception ("Subject list is not in proper YAML format. Please check your file")1651 sub_dict = sublist[int(indx) - 1]...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1import pyrebase, json2import websocket, time, requests3import talib4import numpy as np5from threading import Thread6websockets = {7 "SOCKETBTC1M" : "wss://stream.binance.com:9443/ws/btcusdt@kline_1m",8 "SOCKETBTC5M" : "wss://stream.binance.com:9443/ws/btcusdt@kline_5m",9 "SOCKETBTC1H" : "wss://stream.binance.com:9443/ws/btcusdt@kline_1h",10 "SOCKETBTC4H" : "wss://stream.binance.com:9443/ws/btcusdt@kline_4h",11 "SOCKETBTC1D" : "wss://stream.binance.com:9443/ws/btcusdt@kline_1d",12 "SOCKETETH1M" : "wss://stream.binance.com:9443/ws/ethusdt@kline_1m",13 "SOCKETETH5M" : "wss://stream.binance.com:9443/ws/ethusdt@kline_5m",14 "SOCKETETH1H" : "wss://stream.binance.com:9443/ws/ethusdt@kline_1h",15 "SOCKETETH4H" : "wss://stream.binance.com:9443/ws/ethusdt@kline_4h",16 "SOCKETETH1D" : "wss://stream.binance.com:9443/ws/ethusdt@kline_1d",17 "SOCKETBNB1M" : "wss://stream.binance.com:9443/ws/bnbusdt@kline_1m",18 "SOCKETBNB5M" : "wss://stream.binance.com:9443/ws/bnbusdt@kline_5m",19 "SOCKETBNB1H" : "wss://stream.binance.com:9443/ws/bnbusdt@kline_1h",20 "SOCKETBNB4H" : "wss://stream.binance.com:9443/ws/bnbusdt@kline_4h",21 "SOCKETBNB1D" : "wss://stream.binance.com:9443/ws/bnbusdt@kline_1d",22 "SOCKETZIL1M" : "wss://stream.binance.com:9443/ws/zilusdt@kline_1m",23 "SOCKETZIL5M" : "wss://stream.binance.com:9443/ws/zilusdt@kline_5m",24 "SOCKETZIL1H" : "wss://stream.binance.com:9443/ws/zilusdt@kline_1h",25 "SOCKETZIL4H" : "wss://stream.binance.com:9443/ws/zilusdt@kline_4h",26 "SOCKETZIL1D" : "wss://stream.binance.com:9443/ws/zilusdt@kline_1d"27}28SMAs = {29 'BTC1m' : 0,30 'BTC5m' : 0,31 'BTC1h' : 0,32 'BTC4h' : 0,33 'BTC1d' : 0,34 'ETH1m' : 0,35 'ETH5m' : 0,36 'ETH1h' : 0,37 'ETH4h' : 0,38 'ETH1d' : 0,39 'BNB1m' : 0,40 'BNB5m' : 0,41 'BNB1h' : 0,42 'BNB4h' : 0,43 'BNB1d' : 0,44 'ZIL1m' : 0,45 'ZIL5m' : 0,46 'ZIL1h' : 0,47 'ZIL4h' : 0,48 'ZIL1d' : 0,49}50RSIs = {51 'BTC1m' : 0,52 'BTC5m' : 0,53 'BTC1h' : 0,54 'BTC4h' : 0,55 'BTC1d' : 0,56 'ETH1m' : 0,57 'ETH5m' : 0,58 'ETH1h' : 0,59 'ETH4h' : 0,60 'ETH1d' : 0,61 'BNB1m' : 0,62 'BNB5m' : 0,63 'BNB1h' : 0,64 'BNB4h' : 0,65 'BNB1d' : 0,66 'ZIL1m' : 0,67 'ZIL5m' : 0,68 'ZIL1h' : 0,69 'ZIL4h' : 0,70 'ZIL1d' : 0,71}72EMAs = {73 'BTC1m' : 0,74 'BTC5m' : 0,75 'BTC1h' : 0,76 'BTC4h' : 0,77 'BTC1d' : 0,78 'ETH1m' : 0,79 'ETH5m' : 0,80 'ETH1h' : 0,81 'ETH4h' : 0,82 'ETH1d' : 0,83 'BNB1m' : 0,84 'BNB5m' : 0,85 'BNB1h' : 0,86 'BNB4h' : 0,87 'BNB1d' : 0,88 'ZIL1m' : 0,89 'ZIL5m' : 0,90 'ZIL1h' : 0,91 'ZIL4h' : 0,92 'ZIL1d' : 0,93}94MACDs = {95 'BTC1m' : 0,96 'BTC5m' : 0,97 'BTC1h' : 0,98 'BTC4h' : 0,99 'BTC1d' : 0,100 'ETH1m' : 0,101 'ETH5m' : 0,102 'ETH1h' : 0,103 'ETH4h' : 0,104 'ETH1d' : 0,105 'BNB1m' : 0,106 'BNB5m' : 0,107 'BNB1h' : 0,108 'BNB4h' : 0,109 'BNB1d' : 0,110 'ZIL1m' : 0,111 'ZIL5m' : 0,112 'ZIL1h' : 0,113 'ZIL4h' : 0,114 'ZIL1d' : 0,115}116closes = {117 'BTC1m' : [],118 'BTC5m' : [],119 'BTC1h' : [],120 'BTC4h' : [],121 'BTC1d' : [],122 'ETH1m' : [],123 'ETH5m' : [],124 'ETH1h' : [],125 'ETH4h' : [],126 'ETH1d' : [],127 'BNB1m' : [],128 'BNB5m' : [],129 'BNB1h' : [],130 'BNB4h' : [],131 'BNB1d' : [],132 'ZIL1m' : [],133 'ZIL5m' : [],134 'ZIL1h' : [],135 'ZIL4h' : [],136 'ZIL1d' : [],137}138allStrats = {}139stratsBTC = {}140stratsETH = {}141stratsBNB = {}142stratsZIL = {}143timeperiodSmall = 12144timeperiod = 26145rsiperiod = 21146strats = None147firebase = None148def rotateList(l, n):149 return l[n:] + l[:n]150def returnPrice(arr):151 return float(arr[4])152def getData():153 global closes154 tickers = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT', 'ZILUSDT']155 timeframes = ['1m', '5m', '1h', '4h', '1d']156 for ticker in tickers:157 for timeframe in timeframes:158 res = requests.get('https://api.binance.com/api/v3/klines?symbol=' + ticker+ '&interval=' + timeframe+ '&limit=26')159 response = json.loads(res.text)160 data = list(map(returnPrice, response))161 closes[ticker[:3]+timeframe] = data162def initiateStrats():163 global allStrats, strats, firebase164 f = open('api.json')165 api = json.load(f)166 fireBaseConfig = api167 firebase = pyrebase.initialize_app(fireBaseConfig)168 db = firebase.database()169 strats = db.child("strats").get()170 for user in strats.each():171 usersstrats = firebase.database().child("strats").child(user.key()).get()172 for strat in usersstrats.each():173 if strat.val()['active'] == True:174 allStrats[strat.key()] = strat.val()175 splitStrats()176def splitStrats():177 global allStrats, stratsBTC, stratsETH, stratsBNB, stratsZIL178 for strat in allStrats:179 if allStrats[strat]['ticker'] == 'BTCUSDT':180 stratsBTC[strat] = allStrats[strat]181 elif allStrats[strat]['ticker'] == 'ETHUSDT':182 stratsETH[strat] = allStrats[strat] 183 elif allStrats[strat]['ticker'] == 'BNBUSDT':184 stratsBNB[strat] = allStrats[strat]185 elif allStrats[strat]['ticker'] == 'ZILUSDT':186 stratsZIL[strat] = allStrats[strat]187 return188def buyOrder(strat, price):189 user = findUser(strat)190 coins = firebase.database().child("coins").child(user).get().val()191 ticker = allStrats[strat]['ticker'][:3]192 amount = allStrats[strat]['amount']193 if float(coins) > float(amount):194 195 current = firebase.database().child("crypto").child(user).child(ticker).get().val()196 updated = float(current) + price/float(amount)197 firebase.database().child("crypto").child(user).update({ticker : updated})198 current = firebase.database().child("coins").child(user).get().val()199 updated = float(current) - float(amount)200 firebase.database().child("coins").update({user : updated})201 dbOrder(user, amount, allStrats[strat]['name'], allStrats[strat]['ticker'], time.time(), 'BUY', price) 202 else:203 dbOrder(user, amount, allStrats[strat]['name'], allStrats[strat]['ticker'], time.time(), 'ERROR', price ) 204def sellOrder(strat, price):205 user = findUser(strat)206 coins = firebase.database().child("coins").child(user).get().val()207 ticker = allStrats[strat]['ticker'][:3]208 amount = allStrats[strat]['amount']209 cryptoBal = firebase.database().child("crypto").child(user).child(ticker).get().val()210 211 if (float(cryptoBal) > float(amount)/price):212 updated = float(cryptoBal) - float(amount)/price213 firebase.database().child("crypto").child(user).update({ticker : updated})214 current = firebase.database().child("coins").child(user).get().val()215 updated = float(current) + float(amount)216 firebase.database().child("coins").update({user : updated})217 218 dbOrder(user, amount, allStrats[strat]['name'], allStrats[strat]['ticker'], time.time(), 'SELL', price ) 219 else:220 dbOrder(user, amount, allStrats[strat]['name'], allStrats[strat]['ticker'], time.time(), 'ERROR', price )221def dbOrder(user, amount, strat, ticker, time, type, price):222 order = {223 "amount": amount,224 "strat": strat,225 "ticker": ticker,226 "time": time,227 "type": type,228 "price": price229 }230 firebase.database().child("orders").child(user).push(order)231def findUser(stratToFind):232 for user in strats.each():233 usersstrats = firebase.database().child("strats").child(user.key()).get()234 for strat in usersstrats.each():235 if strat.key() == stratToFind:236 return user.key()237def on_open(ws):238 print("opened connection")239 240def on_close(ws):241 print("closed connection")242def on_message(ws, message):243 global allStrats244 global closes245 global SMAs246 global EMAs247 global MACDs248 global RSIs249 global timeperiod250 global timeperiodSmall251 json_message = json.loads(message)252 candle = json_message['k']253 is_candle_closed = candle['x']254 close = candle['c']255 volume = candle['v']256 high = candle['h']257 low = candle['l']258 if is_candle_closed:259 print("candlestick closed at {}".format(close))260 if 'btc' in ws.url:261 if '1m' in ws.url:262 if len(closes['BTC1m']) < timeperiod:263 closes['BTC1m'].append(float(close))264 else:265 closes['BTC1m'] = rotateList(closes['BTC1m'],1)266 closes['BTC1m'][-1] = float(close)267 SMAs['BTC1m'] = sum(closes['BTC1m']) / float(len(closes['BTC1m']))268 EMAs["BTC1m"] = (talib.EMA(np.array(closes['BTC1m']), timeperiod = timeperiod))[-1]269 MACDs['BTC1m'] = (talib.EMA(np.array(closes['BTC1m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BTC1m']), timeperiod = timeperiod))[-1]270 RSIs['BTC1m'] = (talib.RSI(np.array(closes['BTC1m']), timeperiod=rsiperiod))[-1]271 272 for strat in stratsBTC:273 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':274 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BTC1m']:275 buyOrder(strat, float(close))276 if allStrats[strat]['sellConditions']['indicator'] == "SMA":277 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BTC1m']:278 sellOrder(strat, float(close))279 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':280 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BTC1m']:281 buyOrder(strat, float(close))282 if allStrats[strat]['sellConditions']['indicator'] == "EMA":283 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BTC1m']:284 sellOrder(strat, float(close))285 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':286 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BTC1m']:287 buyOrder(strat, float(close))288 if allStrats[strat]['sellConditions']['indicator'] == "MACD":289 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BTC1m']:290 sellOrder(strat, float(close))291 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 292 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BTC1m']:293 sellOrder(strat, float(close))294 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':295 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BTC1m']:296 buyOrder(strat, float(close))297 elif '5m' in ws.url:298 if len(closes['BTC5m']) < timeperiod:299 closes['BTC5m'].append(float(close))300 else:301 closes['BTC5m'] = rotateList(closes['BTC5m'],1)302 closes['BTC5m'][-1] = float(close)303 SMAs['BTC5m'] = sum(closes['BTC5m']) / float(len(closes['BTC5m']))304 EMAs["BTC5m"] = (talib.EMA(np.array(closes['BTC5m']), timeperiod = timeperiod))[-1]305 MACDs['BTC5m'] = (talib.EMA(np.array(closes['BTC5m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BTC5m']), timeperiod = timeperiod))[-1]306 RSIs['BTC5m'] = (talib.RSI(np.array(closes['BTC5m']), timeperiod=rsiperiod))[-1]307 for strat in stratsBTC:308 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':309 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BTC5m']:310 buyOrder(strat, float(close))311 if allStrats[strat]['sellConditions']['indicator'] == "SMA":312 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BTC5m']:313 sellOrder(strat, float(close))314 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':315 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BTC5m']:316 buyOrder(strat, float(close))317 if allStrats[strat]['sellConditions']['indicator'] == "EMA":318 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BTC5m']:319 sellOrder(strat, float(close))320 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':321 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BTC5m']:322 buyOrder(strat, float(close))323 if allStrats[strat]['sellConditions']['indicator'] == "MACD":324 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BTC5m']:325 sellOrder(strat, float(close))326 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':327 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BTC5m']:328 buyOrder(strat, float(close))329 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 330 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BTC5m']:331 sellOrder(strat, float(close))332 elif '1h' in ws.url:333 if len(closes['BTC1h']) < timeperiod:334 closes['BTC1h'].append(float(close))335 else:336 closes['BTC1h'] = rotateList(closes['BTC1h'],1)337 closes['BTC1h'][-1] = float(close)338 SMAs['BTC1h'] = sum(closes['BTC1h']) / float(len(closes['BTC1h']))339 EMAs["BTC1h"] = (talib.EMA(np.array(closes['BTC1h']), timeperiod = timeperiod))[-1]340 MACDs['BTC1h'] = (talib.EMA(np.array(closes['BTC1h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BTC1h']), timeperiod = timeperiod))[-1]341 RSIs['BTC1h'] = (talib.RSI(np.array(closes['BTC1h']), timeperiod=rsiperiod))[-1]342 for strat in stratsBTC:343 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':344 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BTC1h']:345 buyOrder(strat, float(close))346 if allStrats[strat]['sellConditions']['indicator'] == "SMA":347 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BTC1h']:348 sellOrder(strat, float(close))349 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':350 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BTC1h']:351 buyOrder(strat, float(close))352 if allStrats[strat]['sellConditions']['indicator'] == "EMA":353 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BTC1h']:354 sellOrder(strat, float(close))355 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':356 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BTC1h']:357 buyOrder(strat, float(close))358 if allStrats[strat]['sellConditions']['indicator'] == "MACD":359 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BTC1h']:360 sellOrder(strat, float(close))361 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':362 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BTC1h']:363 buyOrder(strat, float(close))364 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 365 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BTC1h']:366 sellOrder(strat, float(close))367 RSIs['BTC1h'] = talib.RSI(closes['BTC1h'], timeperiod=timeperiod)368 elif '4h' in ws.url:369 if len(closes['BTC4h']) < timeperiod:370 closes['BTC4h'].append(float(close))371 else:372 closes['BTC4h'] = rotateList(closes['BTC4h'],1)373 closes['BTC4h'][-1] = float(close)374 SMAs['BTC4h'] = sum(closes['BTC4h']) / float(len(closes['BTC4h']))375 EMAs["BTC4h"] = (talib.EMA(np.array(closes['BTC4h']), timeperiod = timeperiod))[-1]376 MACDs['BTC4h'] = (talib.EMA(np.array(closes['BTC4h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BTC4h']), timeperiod = timeperiod))[-1]377 RSIs['BTC4h'] = (talib.RSI(np.array(closes['BTC4h']), timeperiod=rsiperiod))[-1]378 for strat in stratsBTC:379 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':380 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BTC4h']:381 buyOrder(strat, float(close))382 if allStrats[strat]['sellConditions']['indicator'] == "SMA":383 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BTC4h']:384 sellOrder(strat, float(close))385 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':386 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BTC4h']:387 buyOrder(strat, float(close))388 if allStrats[strat]['sellConditions']['indicator'] == "EMA":389 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BTC4h']:390 sellOrder(strat, float(close))391 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':392 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BTC4h']:393 buyOrder(strat, float(close))394 if allStrats[strat]['sellConditions']['indicator'] == "MACD":395 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BTC4h']:396 sellOrder(strat, float(close))397 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':398 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BTC4h']:399 buyOrder(strat, float(close))400 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 401 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BTC4h']:402 sellOrder(strat, float(close))403 elif '1d' in ws.url:404 if len(closes['BTC1d']) < timeperiod:405 closes['BTC1d'].append(float(close))406 else:407 closes['BTC1d'] = rotateList(closes['BTC1d'],1)408 closes['BTC1d'][-1] = float(close)409 SMAs['BTC1d'] = sum(closes['BTC1d']) / float(len(closes['BTC1d']))410 EMAs["BTC1d"] = (talib.EMA(np.array(closes['BTC1d']), timeperiod = timeperiod))[-1]411 MACDs['BTC1d'] = (talib.EMA(np.array(closes['BTC1d']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BTC1d']), timeperiod = timeperiod))[-1]412 RSIs['BTC1d'] = (talib.RSI(np.array(closes['BTC1d']), timeperiod=rsiperiod))[-1]413 for strat in stratsBTC:414 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':415 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BTC1d']:416 buyOrder(strat, float(close))417 if allStrats[strat]['sellConditions']['indicator'] == "SMA":418 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BTC1d']:419 sellOrder(strat, float(close))420 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':421 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BTC1d']:422 buyOrder(strat, float(close))423 if allStrats[strat]['sellConditions']['indicator'] == "EMA":424 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BTC1d']:425 sellOrder(strat, float(close))426 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':427 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BTC1d']:428 buyOrder(strat, float(close))429 if allStrats[strat]['sellConditions']['indicator'] == "MACD":430 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BTC1d']:431 sellOrder(strat, float(close))432 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':433 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BTC1d']:434 buyOrder(strat, float(close))435 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 436 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BTC1d']:437 sellOrder(strat, float(close))438 elif 'eth' in ws.url:439 if '1m' in ws.url:440 if len(closes['ETH1m']) < timeperiod:441 closes['ETH1m'].append(float(close))442 else:443 closes['ETH1m'] = rotateList(closes['ETH1m'],1)444 closes['ETH1m'][-1] = float(close)445 SMAs['ETH1m'] = sum(closes['ETH1m']) / float(len(closes['ETH1m']))446 EMAs["ETH1m"] = (talib.EMA(np.array(closes['ETH1m']), timeperiod = timeperiod))[-1]447 MACDs['ETH1m'] = (talib.EMA(np.array(closes['ETH1m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ETH1m']), timeperiod = timeperiod))[-1]448 RSIs['ETH1m'] = (talib.RSI(np.array(closes['ETH1m']), timeperiod=rsiperiod))[-1]449 for strat in stratsETH:450 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':451 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ETH1m']:452 buyOrder(strat, float(close))453 if allStrats[strat]['sellConditions']['indicator'] == "SMA":454 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ETH1m']:455 sellOrder(strat, float(close))456 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':457 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ETH1m']:458 buyOrder(strat, float(close))459 if allStrats[strat]['sellConditions']['indicator'] == "EMA":460 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ETH1m']:461 sellOrder(strat, float(close))462 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':463 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ETH1m']:464 buyOrder(strat, float(close))465 if allStrats[strat]['sellConditions']['indicator'] == "MACD":466 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ETH1m']:467 sellOrder(strat, float(close))468 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':469 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ETH1m']:470 buyOrder(strat, float(close))471 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 472 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ETH1m']:473 sellOrder(strat, float(close))474 elif '5m' in ws.url:475 if len(closes['ETH5m']) < timeperiod:476 closes['ETH5m'].append(float(close))477 else:478 closes['ETH5m'] = rotateList(closes['ETH5m'],1)479 closes['ETH5m'][-1] = close480 SMAs['ETH5m'] = sum(closes['ETH5m']) / float(len(closes['ETH5m']))481 EMAs["ETH5m"] = (talib.EMA(np.array(closes['ETH5m']), timeperiod = timeperiod))[-1]482 MACDs['ETH5m'] = (talib.EMA(np.array(closes['ETH5m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ETH5m']), timeperiod = timeperiod))[-1]483 RSIs['ETH5m'] = (talib.RSI(np.array(closes['ETH5m']), timeperiod=rsiperiod))[-1]484 for strat in stratsETH:485 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':486 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ETH5m']:487 buyOrder(strat, float(close))488 if allStrats[strat]['sellConditions']['indicator'] == "SMA":489 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ETH5m']:490 sellOrder(strat, float(close))491 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':492 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ETH5m']:493 buyOrder(strat, float(close))494 if allStrats[strat]['sellConditions']['indicator'] == "EMA":495 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ETH5m']:496 sellOrder(strat, float(close))497 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':498 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ETH5m']:499 buyOrder(strat, float(close))500 if allStrats[strat]['sellConditions']['indicator'] == "MACD":501 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ETH5m']:502 sellOrder(strat, float(close))503 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':504 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ETH5m']:505 buyOrder(strat, float(close))506 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 507 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ETH5m']:508 sellOrder(strat, float(close))509 elif '1h' in ws.url:510 if len(closes['ETH1h']) < timeperiod:511 closes['ETH1h'].append(float(close))512 else:513 closes['ETH1h'] = rotateList(closes['ETH1h'],1)514 closes['ETH1h'][-1] = close515 SMAs['ETH1h'] = sum(closes['ETH1h']) / float(len(closes['ETH1h']))516 EMAs["ETH1h"] = (talib.EMA(np.array(closes['ETH1h']), timeperiod = timeperiod))[-1]517 MACDs['ETH1h'] = (talib.EMA(np.array(closes['ETH1h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ETH1h']), timeperiod = timeperiod))[-1]518 RSIs['ETH1h'] = (talib.RSI(np.array(closes['ETH1h']), timeperiod=rsiperiod))[-1]519 for strat in stratsETH:520 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':521 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ETH1h']:522 buyOrder(strat, float(close))523 if allStrats[strat]['sellConditions']['indicator'] == "SMA":524 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ETH1h']:525 sellOrder(strat, float(close))526 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':527 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ETH1h']:528 buyOrder(strat, float(close))529 if allStrats[strat]['sellConditions']['indicator'] == "EMA":530 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ETH1h']:531 sellOrder(strat, float(close))532 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':533 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ETH1h']:534 buyOrder(strat, float(close))535 if allStrats[strat]['sellConditions']['indicator'] == "MACD":536 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ETH1h']:537 sellOrder(strat, float(close))538 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':539 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ETH1h']:540 buyOrder(strat, float(close))541 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 542 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ETH1h']:543 sellOrder(strat, float(close))544 elif '4h' in ws.url:545 if len(closes['ETH4h']) < timeperiod:546 closes['ETH4h'].append(float(close))547 else:548 closes['ETH4h'] = rotateList(closes['ETH4h'],1)549 closes['ETH4h'][-1] = float(close)550 SMAs['ETH4h'] = sum(closes['ETH4h']) / float(len(closes['ETH4h']))551 EMAs["ETH4h"] = (talib.EMA(np.array(closes['ETH4h']), timeperiod = timeperiod))[-1]552 MACDs['ETH4h'] = (talib.EMA(np.array(closes['ETH4h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ETH4h']), timeperiod = timeperiod))[-1]553 RSIs['ETH4h'] = (talib.RSI(np.array(closes['ETH4h']), timeperiod=rsiperiod))[-1]554 for strat in stratsETH:555 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':556 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ETH4h']:557 buyOrder(strat, float(close))558 if allStrats[strat]['sellConditions']['indicator'] == "SMA":559 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ETH4h']:560 sellOrder(strat, float(close))561 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':562 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ETH4h']:563 buyOrder(strat, float(close))564 if allStrats[strat]['sellConditions']['indicator'] == "EMA":565 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ETH4h']:566 sellOrder(strat, float(close))567 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':568 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ETH4h']:569 buyOrder(strat, float(close))570 if allStrats[strat]['sellConditions']['indicator'] == "MACD":571 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ETH4h']:572 sellOrder(strat, float(close))573 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':574 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ETH4h']:575 buyOrder(strat, float(close))576 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 577 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ETH4h']:578 sellOrder(strat, float(close))579 elif '1d' in ws.url:580 if len(closes['ETH1d']) < timeperiod:581 closes['ETH1d'].append(float(close))582 else:583 closes['ETH1d'] = rotateList(closes['ETH1d'],1)584 closes['ETH1d'][-1] = float(close)585 SMAs['ETH1d'] = sum(closes['ETH1d']) / float(len(closes['ETH1d']))586 EMAs["ETH1d"] = (talib.EMA(np.array(closes['ETH1d']), timeperiod = timeperiod))[-1]587 MACDs['ETH1d'] = (talib.EMA(np.array(closes['ETH1d']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ETH1d']), timeperiod = timeperiod))[-1]588 RSIs['ETH1d'] = (talib.RSI(np.array(closes['ETH1d']), timeperiod=rsiperiod))[-1]589 for strat in stratsETH:590 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':591 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ETH1d']:592 buyOrder(strat, float(close))593 if allStrats[strat]['sellConditions']['indicator'] == "SMA":594 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ETH1d']:595 sellOrder(strat, float(close))596 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':597 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ETH1d']:598 buyOrder(strat, float(close))599 if allStrats[strat]['sellConditions']['indicator'] == "EMA":600 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ETH1d']:601 sellOrder(strat, float(close))602 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':603 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ETH1d']:604 buyOrder(strat, float(close))605 if allStrats[strat]['sellConditions']['indicator'] == "MACD":606 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ETH1d']:607 sellOrder(strat, float(close))608 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':609 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ETH1d']:610 buyOrder(strat, float(close))611 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 612 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ETH1d']:613 sellOrder(strat, float(close))614 elif 'bnb' in ws.url:615 if '1m' in ws.url:616 if len(closes['BNB1m']) < timeperiod:617 closes['BNB1m'].append(float(close))618 else:619 closes['BNB1m'] = rotateList(closes['BNB1m'],1)620 closes['BNB1m'][-1] = float(close)621 SMAs['BNB1m'] = sum(closes['BNB1m']) / float(len(closes['BNB1m']))622 EMAs["BNB1m"] = (talib.EMA(np.array(closes['BNB1m']), timeperiod = timeperiod))[-1]623 MACDs['BNB1m'] = (talib.EMA(np.array(closes['BNB1m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BNB1m']), timeperiod = timeperiod))[-1]624 RSIs['BNB1m'] = (talib.RSI(np.array(closes['BNB1m']), timeperiod=rsiperiod))[-1]625 for strat in stratsBNB:626 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':627 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BNB1m']:628 buyOrder(strat, float(close))629 if allStrats[strat]['sellConditions']['indicator'] == "SMA":630 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BNB1m']:631 sellOrder(strat, float(close))632 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':633 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BNB1m']:634 buyOrder(strat, float(close))635 if allStrats[strat]['sellConditions']['indicator'] == "EMA":636 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BNB1m']:637 sellOrder(strat, float(close))638 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':639 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BNB1m']:640 buyOrder(strat, float(close))641 if allStrats[strat]['sellConditions']['indicator'] == "MACD":642 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BNB1m']:643 sellOrder(strat, float(close))644 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':645 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BNB1m']:646 buyOrder(strat, float(close))647 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 648 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BNB1m']:649 sellOrder(strat, float(close))650 elif '5m' in ws.url:651 if len(closes['BNB5m']) < timeperiod:652 closes['BNB5m'].append(float(close))653 else:654 closes['BNB5m'] = rotateList(closes['BNB5m'],1)655 closes['BNB5m'][-1] = float(close)656 SMAs['BNB5m'] = sum(closes['BNB5m']) / float(len(closes['BNB5m']))657 EMAs["BNB5m"] = (talib.EMA(np.array(closes['BNB5m']), timeperiod = timeperiod))[-1]658 MACDs['BNB5m'] = (talib.EMA(np.array(closes['BNB5m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BNB5m']), timeperiod = timeperiod))[-1]659 RSIs['BNB5m'] = (talib.RSI(np.array(closes['BNB5m']), timeperiod=rsiperiod))[-1]660 for strat in stratsBNB:661 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':662 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BNB5m']:663 buyOrder(strat, float(close))664 if allStrats[strat]['sellConditions']['indicator'] == "SMA":665 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BNB5m']:666 sellOrder(strat, float(close))667 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':668 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BNB5m']:669 buyOrder(strat, float(close))670 if allStrats[strat]['sellConditions']['indicator'] == "EMA":671 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BNB5m']:672 sellOrder(strat, float(close))673 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':674 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BNB5m']:675 buyOrder(strat, float(close))676 if allStrats[strat]['sellConditions']['indicator'] == "MACD":677 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BNB5m']:678 sellOrder(strat, float(close))679 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':680 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BNB5m']:681 buyOrder(strat, float(close))682 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 683 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BNB5m']:684 sellOrder(strat, float(close))685 elif '1h' in ws.url:686 if len(closes['BNB1h']) < timeperiod:687 closes['BNB1h'].append(float(close))688 else:689 closes['BNB1h'] = rotateList(closes['BNB1h'],1)690 closes['BNB1h'][-1] = float(close)691 SMAs['BNB1h'] = sum(closes['BNB1h']) / float(len(closes['BNB1h']))692 EMAs["BNB1h"] = (talib.EMA(np.array(closes['BNB1h']), timeperiod = timeperiod))[-1]693 MACDs['BNB1h'] = (talib.EMA(np.array(closes['BNB1h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BNB1h']), timeperiod = timeperiod))[-1]694 RSIs['BNB1h'] = (talib.RSI(np.array(closes['BNB1h']), timeperiod=rsiperiod))[-1]695 for strat in stratsBNB:696 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':697 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BNB1h']:698 buyOrder(strat, float(close))699 if allStrats[strat]['sellConditions']['indicator'] == "SMA":700 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BNB1h']:701 sellOrder(strat, float(close))702 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':703 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BNB1h']:704 buyOrder(strat, float(close))705 if allStrats[strat]['sellConditions']['indicator'] == "EMA":706 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BNB1h']:707 sellOrder(strat, float(close))708 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':709 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BNB1h']:710 buyOrder(strat, float(close))711 if allStrats[strat]['sellConditions']['indicator'] == "MACD":712 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BNB1h']:713 sellOrder(strat, float(close))714 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':715 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BNB1h']:716 buyOrder(strat, float(close))717 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 718 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BNB1h']:719 sellOrder(strat, float(close))720 elif '4h' in ws.url:721 if len(closes['BNB4h']) < timeperiod:722 closes['BNB4h'].append(float(close))723 else:724 closes['BNB4h'] = rotateList(closes['BNB4h'],1)725 closes['BNB4h'][-1] = float(close)726 SMAs['BNB4h'] = sum(closes['BNB4h']) / float(len(closes['BNB4h']))727 EMAs["BNB4h"] = (talib.EMA(np.array(closes['BNB4h']), timeperiod = timeperiod))[-1]728 MACDs['BNB4h'] = (talib.EMA(np.array(closes['BNB4h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BNB4h']), timeperiod = timeperiod))[-1]729 RSIs['BNB4h'] = (talib.RSI(np.array(closes['BNB4h']), timeperiod=rsiperiod))[-1]730 for strat in stratsBNB:731 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':732 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BNB4h']:733 buyOrder(strat, float(close))734 if allStrats[strat]['sellConditions']['indicator'] == "SMA":735 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BNB4h']:736 sellOrder(strat, float(close))737 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':738 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BNB4h']:739 buyOrder(strat, float(close))740 if allStrats[strat]['sellConditions']['indicator'] == "EMA":741 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BNB4h']:742 sellOrder(strat, float(close))743 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':744 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BNB4h']:745 buyOrder(strat, float(close))746 if allStrats[strat]['sellConditions']['indicator'] == "MACD":747 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BNB4h']:748 sellOrder(strat, float(close))749 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':750 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BNB4h']:751 buyOrder(strat, float(close))752 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 753 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BNB4h']:754 sellOrder(strat, float(close))755 elif '1d' in ws.url:756 if len(closes['BNB1d']) < timeperiod:757 closes['BNB1d'].append(float(close))758 else:759 closes['BNB1d'] = rotateList(closes['BNB1d'],1)760 closes['BNB1d'][-1] = float(close)761 SMAs['BNB1d'] = sum(closes['BNB1d']) / float(len(closes['BNB1d']))762 EMAs["BNB1d"] = (talib.EMA(np.array(closes['BNB1d']), timeperiod = timeperiod))[-1]763 MACDs['BNB1d'] = (talib.EMA(np.array(closes['BNB1d']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['BNB1d']), timeperiod = timeperiod))[-1]764 RSIs['BNB1d'] = (talib.RSI(np.array(closes['BNB1d']), timeperiod=rsiperiod))[-1]765 for strat in stratsBNB:766 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':767 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['BNB1d']:768 buyOrder(strat, float(close))769 if allStrats[strat]['sellConditions']['indicator'] == "SMA":770 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['BNB1d']:771 sellOrder(strat, float(close))772 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':773 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['BNB1d']:774 buyOrder(strat, float(close))775 if allStrats[strat]['sellConditions']['indicator'] == "EMA":776 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['BNB1d']:777 sellOrder(strat, float(close))778 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':779 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['BNB1d']:780 buyOrder(strat, float(close))781 if allStrats[strat]['sellConditions']['indicator'] == "MACD":782 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['BNB1d']:783 sellOrder(strat, float(close))784 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':785 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['BNB1d']:786 buyOrder(strat, float(close))787 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 788 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['BNB1d']:789 sellOrder(strat, float(close))790 elif 'zil' in ws.url:791 if '1m' in ws.url:792 if len(closes['ZIL1m']) < timeperiod:793 closes['ZIL1m'].append(float(close))794 else:795 closes['ZIL1m'] = rotateList(closes['ZIL1m'],1)796 closes['ZIL1m'][-1] = float(close)797 SMAs['ZIL1m'] = sum(closes['ZIL1m']) / float(len(closes['ZIL1m']))798 EMAs["ZIL1m"] = (talib.EMA(np.array(closes['ZIL1m']), timeperiod = timeperiod))[-1]799 MACDs['ZIL1m'] = (talib.EMA(np.array(closes['ZIL1m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ZIL1m']), timeperiod = timeperiod))[-1]800 RSIs['ZIL1m'] = (talib.RSI(np.array(closes['ZIL1m']), timeperiod=rsiperiod))[-1]801 for strat in stratsZIL:802 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':803 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ZIL1m']:804 buyOrder(strat, float(close))805 if allStrats[strat]['sellConditions']['indicator'] == "SMA":806 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ZIL1m']:807 sellOrder(strat, float(close))808 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':809 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ZIL1m']:810 buyOrder(strat, float(close))811 if allStrats[strat]['sellConditions']['indicator'] == "EMA":812 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ZIL1m']:813 sellOrder(strat, float(close))814 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':815 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ZIL1m']:816 buyOrder(strat, float(close))817 if allStrats[strat]['sellConditions']['indicator'] == "MACD":818 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ZIL1m']:819 sellOrder(strat, float(close))820 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':821 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ZIL1m']:822 buyOrder(strat, float(close))823 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 824 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ZIL1m']:825 sellOrder(strat, float(close))826 elif '5m' in ws.url:827 if len(closes['ZIL5m']) < timeperiod:828 closes['ZIL5m'].append(float(close))829 else:830 closes['ZIL5m'] = rotateList(closes['ZIL5m'],1)831 closes['ZIL5m'][-1] = float(close)832 SMAs['ZIL5m'] = sum(closes['ZIL5m']) / float(len(closes['ZIL5m']))833 EMAs["ZIL5m"] = (talib.EMA(np.array(closes['ZIL5m']), timeperiod = timeperiod))[-1]834 MACDs['ZIL5m'] = (talib.EMA(np.array(closes['ZIL5m']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ZIL5m']), timeperiod = timeperiod))[-1]835 RSIs['ZIL5m'] = (talib.RSI(np.array(closes['ZIL5m']), timeperiod=rsiperiod))[-1]836 for strat in stratsZIL:837 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':838 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ZIL5m']:839 buyOrder(strat, float(close))840 if allStrats[strat]['sellConditions']['indicator'] == "SMA":841 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ZIL5m']:842 sellOrder(strat, float(close))843 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':844 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ZIL5m']:845 buyOrder(strat, float(close))846 if allStrats[strat]['sellConditions']['indicator'] == "EMA":847 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ZIL5m']:848 sellOrder(strat, float(close))849 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':850 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ZIL5m']:851 buyOrder(strat, float(close))852 if allStrats[strat]['sellConditions']['indicator'] == "MACD":853 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ZIL5m']:854 sellOrder(strat, float(close))855 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':856 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ZIL5m']:857 buyOrder(strat, float(close))858 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 859 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ZIL5m']:860 sellOrder(strat, float(close))861 elif '1h' in ws.url:862 if len(closes['ZIL1h']) < timeperiod:863 closes['ZIL1h'].append(float(close))864 else:865 closes['ZIL1h'] = rotateList(closes['ZIL1h'],1)866 closes['ZIL1h'][-1] = float(close)867 SMAs['ZIL1h'] = sum(closes['ZIL1h']) / float(len(closes['ZIL1h']))868 EMAs["ZIL1h"] = (talib.EMA(np.array(closes['ZIL1h']), timeperiod = timeperiod))[-1]869 MACDs['ZIL1h'] = (talib.EMA(np.array(closes['ZIL1h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ZIL1h']), timeperiod = timeperiod))[-1]870 RSIs['ZIL1h'] = (talib.RSI(np.array(closes['ZIL1h']), timeperiod=rsiperiod))[-1]871 for strat in stratsZIL:872 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':873 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ZIL1h']:874 buyOrder(strat, float(close))875 if allStrats[strat]['sellConditions']['indicator'] == "SMA":876 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ZIL1h']:877 sellOrder(strat, float(close))878 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':879 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ZIL1h']:880 buyOrder(strat, float(close))881 if allStrats[strat]['sellConditions']['indicator'] == "EMA":882 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ZIL1h']:883 sellOrder(strat, float(close))884 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':885 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ZIL1h']:886 buyOrder(strat, float(close))887 if allStrats[strat]['sellConditions']['indicator'] == "MACD":888 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ZIL1h']:889 sellOrder(strat, float(close))890 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':891 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ZIL1h']:892 buyOrder(strat, float(close))893 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 894 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ZIL1h']:895 sellOrder(strat, float(close))896 elif '4h' in ws.url:897 if len(closes['ZIL4h']) < timeperiod:898 closes['ZIL4h'].append(float(close))899 else:900 closes['ZIL4h'] = rotateList(closes['ZIL4h'],1)901 closes['ZIL4h'][-1] = float(close)902 SMAs['ZIL4h'] = sum(closes['ZIL4h']) / float(len(closes['ZIL4h']))903 EMAs["ZIL4h"] = (talib.EMA(np.array(closes['ZIL4h']), timeperiod = timeperiod))[-1]904 MACDs['ZIL4h'] = (talib.EMA(np.array(closes['ZIL4h']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ZIL4h']), timeperiod = timeperiod))[-1]905 RSIs['ZIL4h'] = (talib.RSI(np.array(closes['ZIL4h']), timeperiod=rsiperiod))[-1]906 for strat in stratsZIL:907 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':908 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ZIL4h']:909 buyOrder(strat, float(close))910 if allStrats[strat]['sellConditions']['indicator'] == "SMA":911 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ZIL4h']:912 sellOrder(strat, float(close))913 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':914 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ZIL4h']:915 buyOrder(strat, float(close))916 if allStrats[strat]['sellConditions']['indicator'] == "EMA":917 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ZIL4h']:918 sellOrder(strat, float(close))919 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':920 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ZIL4h']:921 buyOrder(strat, float(close))922 if allStrats[strat]['sellConditions']['indicator'] == "MACD":923 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ZIL4h']:924 sellOrder(strat, float(close))925 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':926 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ZIL4h']:927 buyOrder(strat, float(close))928 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 929 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ZIL4h']:930 sellOrder(strat, float(close))931 elif '1d' in ws.url:932 if len(closes['ZIL1d']) < timeperiod:933 closes['ZIL1d'].append(float(close))934 else:935 closes['ZIL1d'] = rotateList(closes['ZIL1d'],1)936 closes['ZIL1d'][-1] = float(close)937 SMAs['ZIL1d'] = sum(closes['ZIL1d']) / float(len(closes['ZIL1d']))938 EMAs["ZIL1d"] = (talib.EMA(np.array(closes['ZIL1d']), timeperiod = timeperiod))[-1]939 MACDs['ZIL1d'] = (talib.EMA(np.array(closes['ZIL1d']), timeperiod = timeperiodSmall))[-1] - (talib.EMA(np.array(closes['ZIL1d']), timeperiod = timeperiod))[-1]940 RSIs['ZIL1d'] = (talib.RSI(np.array(closes['ZIL1d']), timeperiod=rsiperiod))[-1]941 for strat in stratsZIL:942 if allStrats[strat]['buyConditions']['indicator'] == 'SMA':943 if float(allStrats[strat]['buyConditions']['targetValue']) >= SMAs['ZIL1d']:944 buyOrder(strat, float(close))945 if allStrats[strat]['sellConditions']['indicator'] == "SMA":946 if float(allStrats[strat]['sellConditions']['targetValue']) <= SMAs['ZIL1d']:947 sellOrder(strat, float(close))948 if allStrats[strat]['buyConditions']['indicator'] == 'EMA':949 if float(allStrats[strat]['buyConditions']['targetValue']) >= EMAs['ZIL1d']:950 buyOrder(strat, float(close))951 if allStrats[strat]['sellConditions']['indicator'] == "EMA":952 if float(allStrats[strat]['sellConditions']['targetValue']) <= EMAs['ZIL1d']:953 sellOrder(strat, float(close))954 if allStrats[strat]['buyConditions']['indicator'] == 'MACD':955 if float(allStrats[strat]['buyConditions']['targetValue']) >= MACDs['ZIL1d']:956 buyOrder(strat, float(close))957 if allStrats[strat]['sellConditions']['indicator'] == "MACD":958 if float(allStrats[strat]['sellConditions']['targetValue']) <= MACDs['ZIL1d']:959 sellOrder(strat, float(close))960 if allStrats[strat]['buyConditions']['indicator'] == 'RSI':961 if float(allStrats[strat]['buyConditions']['targetValue']) >= RSIs['ZIL1d']:962 buyOrder(strat, float(close))963 if allStrats[strat]['sellConditions']['indicator'] == "RSI": 964 if float(allStrats[strat]['sellConditions']['targetValue']) <= RSIs['ZIL1d']:965 sellOrder(strat, float(close))966 print(len(closes['BTC1m']), closes['BTC1m'], SMAs['BTC1m'],EMAs['BTC1m'],MACDs['BTC1m'],RSIs['BTC1m'])967threads = []968sockets = [] 969def main():970 initiateStrats()971 global threads, sockets972 for socket in websockets:973 ws = websocket.WebSocketApp(websockets[socket], on_open = on_open, on_message = on_message, on_close = on_close)974 sockets.append(ws)975 wst = Thread(target=ws.run_forever)976 wst.daemon = True977 wst.start()978 threads.append(wst)979 time.sleep(60)980 for ws in sockets:981 ws.close()982 for thread in threads:983 thread.join()984 threads = []985 sockets = []986 main()987if __name__ == "__main__":988 getData()...

Full Screen

Full Screen

reader.py

Source:reader.py Github

copy

Full Screen

...16 # load yaml17 with open(init_file, 'r') as file:18 init_content = yaml.load(file, Loader=yaml.FullLoader)19 return init_content20def create_strat(tokens, strat):21 '''Create new strat that can be appended to config.'''22 if 'strat' not in strat.keys():23 return 'unspecified strategy'24 new_strat = {}25 new_strat['strat'] = strat['strat']26 if tokens:27 new_strat['tokens'] = tokens28 else:29 new_strat['tokens'] = strat['tokens']30 for field, value in DEFAULT_FIELDS.items():31 if field not in strat.keys():32 new_strat[field] = value33 else:34 value = strat[field]35 if field == 'name':36 value = value.lower().replace(' ', '')37 new_strat[field] = value38 return new_strat39def read_file(init_file):40 '''Read and parse init file and return config. Search for missing arguments/errors.'''41 init_file = Path(init_file)42 config = SimpleNamespace()43 init_content = load_yaml(init_file)44 if isinstance(init_content, str):45 config.error = init_content46 return config47 # check strategies48 if 'strategies' not in init_content.keys():49 config.error = 'missing strategies'50 return config51 # check that config file has tokens or each strat has tokens52 tokens = ''53 if 'tokens' not in init_content.keys():54 for strat in init_content['strategies']:55 if 'tokens' not in strat.keys():56 config.error = 'missing tokens'57 return config58 else:59 tokens = init_content['tokens']60 # load all fields into config61 if 'id' in init_content.keys():62 config.id = init_content['id']63 else:64 config.id = None65 config.strategies = []66 for strat in init_content['strategies']:67 new_strat = create_strat(tokens, strat)68 if isinstance(new_strat, str):69 config.error = new_strat70 return config71 config.strategies.append(new_strat)72 config.error = None73 return config74def read_arguments(args):75 '''Read arguments and return config.'''76 config = SimpleNamespace()77 config.id = args.id78 strat = {79 'strat': args.strat,80 'tokens': {81 'binance_api_key': args.tokens.split('#')[0],...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful