Skip to content

Commit

Permalink
ecg gen: attribute name updates
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanshi-s committed May 23, 2024
1 parent cab8f88 commit 8532a3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
18 changes: 9 additions & 9 deletions codes/ecg-image-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ The basic mode of the tool creates ECG images without distortions. The mode of o
- `--num_columns` : Number of columns of the ECG leads. The default(-1) will plot a single column for 2 lead data and 4 columns for the 12 or any other number of lead data. Default: -1; type: int
- `--full_mode`: Sets the lead to add at the bottom of the paper ECG as a long strip obtained from the WFDB record's `.hea` header file, if the lead II is not available plots the first lead from the header file; default: `'II'`; type: str
- `--mask_unplotted_samples`: Mask the samples not plotted in the images in the generated WFDB signal file; default: False. For example: for the 3x4 format, the code plots 2.5 seconds of each lead on the image and saves the complete signal in the WFDB file. If the flag is set, the code will mask the part of the signal not plotted in the image (In this case, t > 2.5seconds) with Nan values in the modified WFDB file.
- `--num_images`: Number of ECG images to be generated; default: all files in the input directory; type: int
- `--deterministic_lead`: Remove lead names from all generated images, default=False.
- `--random_resolution`: Generate random resolutions of images, if True resolution is randomly picked from the range [50, `-r`] else every image is generated at the `-r` resolution; default: False
- `--max_num_images`: Number of ECG images to be generated, if max_num_images is less than the number of files in the input directory it will generate maximum number of images and the order is dependent on the OS library; default: all files in the input directory; type: int
- `--remove_lead_names`: Remove lead names from all generated images, default=False.
- `--random_resolution`: Generate random resolutions of images, if True resolution is randomly picked from the range [50, `r`] else every image is generated at the `-r` resolution; default: False
- `--random_padding`: Generate random padding widths on images, if True pad inches is randomly picked from the range [0, `--pad_inches`], else every image is padded with `--pad_inches`; default: False
- `--random_dc`: Add ECG calibration pulse to a random number of generated images. The parameter is the probability of the images having the calibration pulse; type: Float, default: 0 (no calibration pulse). Set to 1 to add the pulse to all images. Make sure that `--random_dc` $\in$ [0, 1].
- `--calibration_pulse`: Add ECG calibration pulse to a random number of generated images. The parameter is the probability of the images having the calibration pulse; type: Float, default: 0 (no calibration pulse). Set to 1 to add the pulse to all images. Make sure that `--calibration_pulse` $\in$ [0, 1].
- `--random_grid_present`: Probability of the generated images having the ECG paper grid; type: Float, default: 1 (adds the grid to all images). Make sure that `--random_grid_present` $\in$ [0, 1]. When 0, the images do not have the background grid.
- `--random_add_header`: Probability of adding printed text to a random set of images; type: Float, default: 0 (no text added). Make sure that `--random_add_header` $\in$ [0, 1]. If `--print_header` is True, code prints text on all the images regardless of the `--random_add_header` attribute.
- `--random_print_header`: Probability of adding printed text to a random set of images; type: Float, default: 0 (no text added). Make sure that `--random_print_header` $\in$ [0, 1]. If `--print_header` is True, code prints text on all the images regardless of the `--random_print_header` attribute.
- `--random_bw`: Make random set of images black and white controlled by this parameter; type: Float, default: 0 (generates colored ECG). Make sure that `--random_bw` $\in$ [0, 1].
- `--standard_grid_color`: Color of the grid lines, 1: brown, 2: pink, 3: blue, 4: green, 5(Default): red .Make sure that `standard_grid_color` $\in$ [1, 5], type: int
- `--random_grid_color`: Generates random colors for the gridlines, If `'--random_bw` > 0, then the color of gridlines for the non black and white ECG images is picked randomly. Default: False.
- `--store_config`: Store config information for each image in a json file, type = int, Default: 0. If `--store_config` is set to 1, the config file has high level attributes, refer template1.json. If `--store_config` is set to 2, config file will have more detailed information, refer template2.json.
- `--store_text_bounding_box`: Store bounding box coordinates for the lead names in the json file under the attribute `text_bounding_box` in the config JSON file generated for every record (conditional); Add `--store_config` parameter to the command you are running. Note: Add `--store_config` parameter to the command you are running to save the config file with bounding box information.
- `--bbox`: Store bounding box coordinates for every individual ECG lead signal in the json file under the attribute `lead_bounding_box` in the config JSON file generated for every record (conditional). Note: Add `--store_config` parameter to the command you are running to save the config file with bounding box information.
- `--lead_name_bbox`: Store bounding box coordinates for the lead names in the json file under the attribute `text_bounding_box` in the config JSON file generated for every record (conditional); Add `--store_config` parameter to the command you are running. Note: Add `--store_config` parameter to the command you are running to save the config file with bounding box information.
- `--lead_bbox`: Store bounding box coordinates for every individual ECG lead signal in the json file under the attribute `lead_bounding_box` in the config JSON file generated for every record (conditional). Note: Add `--store_config` parameter to the command you are running to save the config file with bounding box information.
**Example:**
```bash
python gen_ecg_images_from_data_batch.py -i <path_to_input_directory> -o <path_to_output_directory> -se 10 --store_text_bounding_box --bbox --random_add_header 0.8 --random_dc 0.5 --store_config 1
python gen_ecg_images_from_data_batch.py -i <path_to_input_directory> -o <path_to_output_directory> -se 10 --lead_name_bbox --lead_bbox --random_add_header 0.8 --calibration_pulse 0.5 --store_config 1
```
## Adding distortions to the synethic images
Expand Down Expand Up @@ -187,7 +187,7 @@ The basic mode of the tool creates ECG images without distortions. The mode of o
**Example:**
```
python gen_ecg_images_from_data_batch.py -i <input_dir> -o <output_dir> --random_add_header 0.8 -se 20 --store_text_bounding_box --resolution 300 --random_padding --pad_inches 1 --bbox --augment -rot 5 -noise 40 --deterministic_rot --deterministic_noise --hw_text -n 4 --x_offset 30 --y_offset 20 --wrinkles -ca 45
python gen_ecg_images_from_data_batch.py -i <input_dir> -o <output_dir> --random_add_header 0.8 -se 20 --lead_name_bbox --resolution 300 --random_padding --pad_inches 1 --lead_bbox --augment -rot 5 -noise 40 --deterministic_rot --deterministic_noise --hw_text -n 4 --x_offset 30 --y_offset 20 --wrinkles -ca 45
```
| | |
Expand Down
26 changes: 13 additions & 13 deletions codes/ecg-image-generator/gen_ecg_image_from_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def get_parser():
parser.add_argument('--random_padding',action="store_true",default=False)
parser.add_argument('--random_grid_color',action="store_true",default=False)
parser.add_argument('--standard_grid_color', type=int, default=5)
parser.add_argument('--random_dc',type=float,default=0)
parser.add_argument('--calibration_pulse',type=float,default=0)
parser.add_argument('--random_grid_present',type=float,default=1)
parser.add_argument('--random_add_header',type=float,default=0)
parser.add_argument('--random_print_header',type=float,default=0)
parser.add_argument('--random_bw',type=float,default=0)
parser.add_argument('--deterministic_lead',action="store_false",default=True)
parser.add_argument('--store_text_bounding_box',action="store_true",default=False)
parser.add_argument('--remove_lead_names',action="store_false",default=True)
parser.add_argument('--lead_name_bbox',action="store_true",default=False)
parser.add_argument('--store_config', type=int,default=0)

parser.add_argument('--deterministic_offset',action="store_true",default=False)
Expand All @@ -75,7 +75,7 @@ def get_parser():
parser.add_argument('--hw_text',action='store_true',default=False)
parser.add_argument('--wrinkles',action='store_true',default=False)
parser.add_argument('--augment',action='store_true',default=False)
parser.add_argument('--bbox',action='store_true',default=False)
parser.add_argument('--lead_bbox',action='store_true',default=False)

return parser

Expand Down Expand Up @@ -104,15 +104,15 @@ def run_single_file(args):
padding = random.choice(range(0,args.pad_inches+1)) if (args.random_padding) else args.pad_inches

papersize = ''
lead = args.deterministic_lead
lead = args.remove_lead_names

bernoulli_dc = bernoulli(args.random_dc)
bernoulli_dc = bernoulli(args.calibration_pulse)
bernoulli_bw = bernoulli(args.random_bw)
bernoulli_grid = bernoulli(args.random_grid_present)
if args.print_header:
bernoulli_add_print = bernoulli(1)
else:
bernoulli_add_print = bernoulli(args.random_add_header)
bernoulli_add_print = bernoulli(args.random_print_header)

font = os.path.join('Fonts',random.choice(os.listdir("Fonts")))

Expand All @@ -126,7 +126,7 @@ def run_single_file(args):

configs = read_config_file(os.path.join(os.getcwd(), args.config_file))

out_array = get_paper_ecg(input_file=filename,header_file=header, configs=configs, mask_unplotted_samples=args.mask_unplotted_samples, start_index=args.start_index, store_configs=args.store_config, store_text_bbox=args.store_text_bounding_box, output_directory=args.output_directory,resolution=resolution,papersize=papersize,add_lead_names=lead,add_dc_pulse=bernoulli_dc,add_bw=bernoulli_bw,show_grid=bernoulli_grid,add_print=bernoulli_add_print,pad_inches=padding,font_type=font,standard_colours=standard_colours,full_mode=args.full_mode,bbox = args.bbox, columns = args.num_columns, seed=args.seed)
out_array = get_paper_ecg(input_file=filename,header_file=header, configs=configs, mask_unplotted_samples=args.mask_unplotted_samples, start_index=args.start_index, store_configs=args.store_config, store_text_bbox=args.lead_name_bbox, output_directory=args.output_directory,resolution=resolution,papersize=papersize,add_lead_names=lead,add_dc_pulse=bernoulli_dc,add_bw=bernoulli_bw,show_grid=bernoulli_grid,add_print=bernoulli_add_print,pad_inches=padding,font_type=font,standard_colours=standard_colours,full_mode=args.full_mode,bbox = args.lead_bbox, columns = args.num_columns, seed=args.seed)

for out in out_array:
if args.store_config:
Expand All @@ -150,7 +150,7 @@ def run_single_file(args):
x_offset = args.x_offset if (args.deterministic_offset) else random.choice(range(1,args.x_offset+1))
y_offset = args.y_offset if (args.deterministic_offset) else random.choice(range(1,args.y_offset+1))

out = get_handwritten(link=args.link,num_words=num_words,input_file=out,output_dir=args.output_directory,x_offset=x_offset,y_offset=y_offset,handwriting_size_factor=args.handwriting_size_factor,bbox = args.bbox)
out = get_handwritten(link=args.link,num_words=num_words,input_file=out,output_dir=args.output_directory,x_offset=x_offset,y_offset=y_offset,handwriting_size_factor=args.handwriting_size_factor,bbox = args.lead_bbox)
else:
num_words = 0
x_offset = 0
Expand All @@ -168,7 +168,7 @@ def run_single_file(args):
crease_angle = args.crease_angle if (args.deterministic_angle) else random.choice(range(0,args.crease_angle+1))
num_creases_vertically = args.num_creases_vertically if (args.deterministic_vertical) else random.choice(range(1,args.num_creases_vertically+1))
num_creases_horizontally = args.num_creases_horizontally if (args.deterministic_horizontal) else random.choice(range(1,args.num_creases_horizontally+1))
out = get_creased(out,output_directory=args.output_directory,ifWrinkles=ifWrinkles,ifCreases=ifCreases,crease_angle=crease_angle,num_creases_vertically=num_creases_vertically,num_creases_horizontally=num_creases_horizontally,bbox = args.bbox)
out = get_creased(out,output_directory=args.output_directory,ifWrinkles=ifWrinkles,ifCreases=ifCreases,crease_angle=crease_angle,num_creases_vertically=num_creases_vertically,num_creases_horizontally=num_creases_horizontally,bbox = args.lead_bbox)
else:
crease_angle = 0
num_creases_horizontally = 0
Expand All @@ -183,7 +183,7 @@ def run_single_file(args):
if(augment):
noise = args.noise if (args.deterministic_noise) else random.choice(range(1,args.noise+1))

if(not args.bbox):
if(not args.lead_bbox):
do_crop = random.choice((True,False))
if(do_crop):
crop = args.crop
Expand All @@ -198,7 +198,7 @@ def run_single_file(args):
else:
temp = random.choice(range(10000,20000))
rotate = args.rotate
out = get_augment(out,output_directory=args.output_directory,rotate=args.rotate,noise=noise,crop=crop,temperature=temp,bbox = args.bbox, store_text_bounding_box = args.store_text_bounding_box, json_dict = json_dict)
out = get_augment(out,output_directory=args.output_directory,rotate=args.rotate,noise=noise,crop=crop,temperature=temp,bbox = args.lead_bbox, store_text_bounding_box = args.lead_name_bbox, json_dict = json_dict)

else:
crop = 0
Expand Down
14 changes: 7 additions & 7 deletions codes/ecg-image-generator/gen_ecg_images_from_data_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_parser():
parser.add_argument('-o', '--output_directory', type=str, required=True)
parser.add_argument('-se', '--seed', type=int, required=False, default = -1)
parser.add_argument('--num_leads',type=str,default='twelve')
parser.add_argument('--num_images',type=int,default = -1)
parser.add_argument('--max_num_images',type=int,default = -1)
parser.add_argument('--config_file', type=str, default='config.yaml')

parser.add_argument('-r','--resolution',type=int,required=False,default = 200)
Expand Down Expand Up @@ -47,12 +47,12 @@ def get_parser():
parser.add_argument('--random_padding',action="store_true",default=False)
parser.add_argument('--random_grid_color',action="store_true",default=False)
parser.add_argument('--standard_grid_color', type=int, default=5)
parser.add_argument('--random_dc',type=float,default=0)
parser.add_argument('--calibration_pulse',type=float,default=0)
parser.add_argument('--random_grid_present',type=float,default=1)
parser.add_argument('--random_add_header',type=float,default=0)
parser.add_argument('--random_print_header',type=float,default=0)
parser.add_argument('--random_bw',type=float,default=0)
parser.add_argument('--deterministic_lead',action="store_false",default=True)
parser.add_argument('--store_text_bounding_box',action="store_true",default=False)
parser.add_argument('--remove_lead_names',action="store_false",default=True)
parser.add_argument('--lead_name_bbox',action="store_true",default=False)
parser.add_argument('--store_config', type=int, default=0)

parser.add_argument('--deterministic_offset',action="store_true",default=False)
Expand All @@ -72,7 +72,7 @@ def get_parser():
parser.add_argument('--hw_text',action='store_true',default=False)
parser.add_argument('--wrinkles',action='store_true',default=False)
parser.add_argument('--augment',action='store_true',default=False)
parser.add_argument('--bbox',action='store_true',default=False)
parser.add_argument('--lead_bbox',action='store_true',default=False)

return parser

Expand Down Expand Up @@ -106,7 +106,7 @@ def run(args):
args.output_directory = os.path.join(original_output_dir, '/'.join(folder_struct_list))
i += run_single_file(args)

if(args.num_images != -1 and i >= args.num_images):
if(args.max_num_images != -1 and i >= args.max_num_images):
break

if __name__=='__main__':
Expand Down

0 comments on commit 8532a3d

Please sign in to comment.